19
19
References:
20
20
- https://en.wikipedia.org/wiki/Deutsch-Jozsa_algorithm
21
21
- https://qiskit.org/textbook/ch-states/atoms-computation.html#4.2-Remembering-how-to-add-
22
- """ # noqa
22
+ """ # noqa
23
23
24
24
import numpy as np
25
25
import qiskit as q
@@ -33,28 +33,28 @@ def dj_oracle(case: str, n: int) -> q.QuantumCircuit:
33
33
"""
34
34
# This circuit has n+1 qubits: the size of the input,
35
35
# plus one output qubit
36
- oracle_qc = q .QuantumCircuit (n + 1 )
37
-
36
+ oracle_qc = q .QuantumCircuit (n + 1 )
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 ** n )
43
43
# Next, format 'b' as a binary string of length 'n', padded with zeros:
44
- b_str = format (b , '0' + str (n )+ 'b' )
45
- # Next, we place the first X-gates. Each digit in our binary string
44
+ b_str = format (b , "0" + str (n ) + "b" )
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
48
for qubit in range (len (b_str )):
49
- if b_str [qubit ] == '1' :
49
+ if b_str [qubit ] == "1" :
50
50
oracle_qc .x (qubit )
51
- # Do the controlled-NOT gates for each qubit, using the output qubit
51
+ # Do the controlled-NOT gates for each qubit, using the output qubit
52
52
# as the target:
53
53
for qubit in range (n ):
54
54
oracle_qc .cx (qubit , n )
55
55
# Next, place the final X-gates
56
56
for qubit in range (len (b_str )):
57
- if b_str [qubit ] == '1' :
57
+ if b_str [qubit ] == "1" :
58
58
oracle_qc .x (qubit )
59
59
60
60
# Case in which oracle is constant
@@ -64,40 +64,40 @@ def dj_oracle(case: str, n: int) -> q.QuantumCircuit:
64
64
output = np .random .randint (2 )
65
65
if output == 1 :
66
66
oracle_qc .x (n )
67
-
67
+
68
68
oracle_gate = oracle_qc .to_gate ()
69
- oracle_gate .name = "Oracle" # To show when we display the circuit
69
+ oracle_gate .name = "Oracle" # To show when we display the circuit
70
70
return oracle_gate
71
71
72
72
73
73
def dj_algorithm (oracle : q .QuantumCircuit , n : int ) -> q .QuantumCircuit :
74
74
"""
75
75
Returns the complete Deustch-Jozsa Quantum Circuit,
76
- adding Input & Uutput registers and Hadamard & Measurement Gates,
76
+ adding Input & Uutput 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 (n + 1 , n )
80
80
# Set up the output qubit:
81
81
dj_circuit .x (n )
82
82
dj_circuit .h (n )
83
83
# And set up the input register:
84
84
for qubit in range (n ):
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 (n + 1 ))
88
88
# Finally, perform the H-gates again and measure:
89
89
for qubit in range (n ):
90
90
dj_circuit .h (qubit )
91
-
91
+
92
92
for i in range (n ):
93
93
dj_circuit .measure (i , i )
94
-
94
+
95
95
return dj_circuit
96
96
97
97
98
98
def deutsch_jozsa (case : str , n : int ) -> q .result .counts .Counts :
99
99
"""
100
- Main function that builds the circuit using other helper functions,
100
+ Main function that builds the circuit using other helper functions,
101
101
runs the experiment 1000 times & returns the resultant qubit counts
102
102
>>> deutsch_jozsa("constant", 3)
103
103
{'000': 1000}
0 commit comments