25
25
import qiskit as q
26
26
27
27
28
- def dj_oracle (case : str , n : int ) -> q .QuantumCircuit :
28
+ def dj_oracle (case : str , num_qubits : int ) -> q .QuantumCircuit :
29
29
"""
30
30
Returns a Quantum Circuit for the Oracle function.
31
31
The circuit returned can represent balanced or constant function,
32
32
according to the arguments passed
33
33
"""
34
- # This circuit has n +1 qubits: the size of the input,
34
+ # This circuit has num_qubits +1 qubits: the size of the input,
35
35
# plus one output qubit
36
- oracle_qc = q .QuantumCircuit (n + 1 )
36
+ oracle_qc = q .QuantumCircuit (num_qubits + 1 )
37
37
38
38
# First, let's deal with the case in which oracle is balanced
39
39
if case == "balanced" :
40
40
# First generate a random number that tells us which CNOTs to
41
41
# wrap in X-gates:
42
- b = np .random .randint (1 ,2 ** n )
42
+ b = np .random .randint (1 ,2 ** num_qubits )
43
43
# Next, format 'b' as a binary string of length 'n', padded with zeros:
44
- b_str = format (b , '0' + str ( n ) + 'b' )
44
+ b_str = format (b , f"0 { num_qubits } b" )
45
45
# Next, we place the first X-gates. Each digit in our binary string
46
46
# correspopnds to a qubit, if the digit is 0, we do nothing, if it's 1
47
47
# we apply an X-gate to that qubit:
48
- for qubit in range ( len ( b_str ) ):
49
- if b_str [ qubit ] == '1' :
50
- oracle_qc .x (qubit )
48
+ for index , bit in enumerate ( b_str ):
49
+ if bit == '1' :
50
+ oracle_qc .x (index )
51
51
# Do the controlled-NOT gates for each qubit, using the output qubit
52
52
# as the target:
53
- for qubit in range (n ):
54
- oracle_qc .cx (qubit , n )
53
+ for index in range (num_qubits ):
54
+ oracle_qc .cx (index , num_qubits )
55
55
# Next, place the final X-gates
56
- for qubit in range ( len ( b_str ) ):
57
- if b_str [ qubit ] == '1' :
58
- oracle_qc .x (qubit )
56
+ for index , bit in enumerate ( b_str ):
57
+ if bit == '1' :
58
+ oracle_qc .x (index )
59
59
60
60
# Case in which oracle is constant
61
61
if case == "constant" :
62
62
# First decide what the fixed output of the oracle will be
63
63
# (either always 0 or always 1)
64
64
output = np .random .randint (2 )
65
65
if output == 1 :
66
- oracle_qc .x (n )
66
+ oracle_qc .x (num_qubits )
67
67
68
68
oracle_gate = oracle_qc .to_gate ()
69
69
oracle_gate .name = "Oracle" # To show when we display the circuit
70
70
return oracle_gate
71
71
72
72
73
- def dj_algorithm (oracle : q .QuantumCircuit , n : int ) -> q .QuantumCircuit :
73
+ def dj_algorithm (oracle : q .QuantumCircuit , num_qubits : int ) -> q .QuantumCircuit :
74
74
"""
75
75
Returns the complete Deustch-Jozsa Quantum Circuit,
76
76
adding Input & Output registers and Hadamard & Measurement Gates,
77
77
to the Oracle Circuit passed in arguments
78
78
"""
79
- dj_circuit = q .QuantumCircuit (n + 1 , n )
79
+ dj_circuit = q .QuantumCircuit (num_qubits + 1 , num_qubits )
80
80
# Set up the output qubit:
81
- dj_circuit .x (n )
82
- dj_circuit .h (n )
81
+ dj_circuit .x (num_qubits )
82
+ dj_circuit .h (num_qubits )
83
83
# And set up the input register:
84
- for qubit in range (n ):
84
+ for qubit in range (num_qubits ):
85
85
dj_circuit .h (qubit )
86
86
# Let's append the oracle gate to our circuit:
87
- dj_circuit .append (oracle , range (n + 1 ))
87
+ dj_circuit .append (oracle , range (num_qubits + 1 ))
88
88
# Finally, perform the H-gates again and measure:
89
- for qubit in range (n ):
89
+ for qubit in range (num_qubits ):
90
90
dj_circuit .h (qubit )
91
91
92
- for i in range (n ):
92
+ for i in range (num_qubits ):
93
93
dj_circuit .measure (i , i )
94
94
95
95
return dj_circuit
96
96
97
97
98
- def deutsch_jozsa (case : str , n : int ) -> q .result .counts .Counts :
98
+ def deutsch_jozsa (case : str , num_qubits : int ) -> q .result .counts .Counts :
99
99
"""
100
100
Main function that builds the circuit using other helper functions,
101
101
runs the experiment 1000 times & returns the resultant qubit counts
@@ -107,8 +107,8 @@ def deutsch_jozsa(case: str, n: int) -> q.result.counts.Counts:
107
107
# Use Aer's qasm_simulator
108
108
simulator = q .Aer .get_backend ("qasm_simulator" )
109
109
110
- oracle_gate = dj_oracle (case , n )
111
- dj_circuit = dj_algorithm (oracle_gate , n )
110
+ oracle_gate = dj_oracle (case , num_qubits )
111
+ dj_circuit = dj_algorithm (oracle_gate , num_qubits )
112
112
113
113
# Execute the circuit on the qasm simulator
114
114
job = q .execute (dj_circuit , simulator , shots = 1000 )
@@ -118,8 +118,5 @@ def deutsch_jozsa(case: str, n: int) -> q.result.counts.Counts:
118
118
119
119
120
120
if __name__ == "__main__" :
121
- counts = deutsch_jozsa ("constant" , 3 )
122
- print (f"Deutsch Jozsa - Constant Oracle: { counts } " )
123
-
124
- counts = deutsch_jozsa ("balanced" , 3 )
125
- print (f"Deutsch Jozsa - Balanced Oracle: { counts } " )
121
+ print (f"Deutsch Jozsa - Constant Oracle: { deutsch_jozsa ('constant' , 3 )} " )
122
+ print (f"Deutsch Jozsa - Balanced Oracle: { deutsch_jozsa ('balanced' , 3 )} " )
0 commit comments