-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeutsch_jozsa.py
48 lines (36 loc) · 1.11 KB
/
deutsch_jozsa.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from pyquil import Program, get_qc
from pyquil.gates import H, X, CNOT, MEASURE
def oracle(qubits):
# Example of a balanced oracle
CNOT(qubits[0], qubits[-1])
def deutsch_jozsa(n_qubits):
p = Program()
# Create a quantum computer
qc = get_qc('qvm-name')
# Create qubits
register = list(range(n_qubits))
ancilla = n_qubits
# Prepare initial state
p += [H(i) for i in register]
p += X(ancilla)
p += H(ancilla)
# Apply the oracle
oracle(register + [ancilla])
# Apply Hadamard gates to the register qubits
p += [H(i) for i in register]
# Measure the register qubits
ro = p.declare('ro', 'BIT', n_qubits)
for i in register:
p += MEASURE(i, ro[i])
# Compile the program
executable = qc.compile(p)
# Run the program and get results
result = qc.run(executable)
measured_bits = result.readout_data.get('ro')
# Check if all results are 0
all_zero = all(bit == 0 for bit in measured_bits[0])
return all_zero
# Example usage
n_qubits = 2
is_constant = deutsch_jozsa(n_qubits)
print(f"Constant function: {is_constant}")