@@ -15,28 +15,28 @@ def dj_oracle(case: str, n: int):
15
15
# We need to make a QuantumCircuit object to return
16
16
# This circuit has n+1 qubits: the size of the input,
17
17
# plus one output qubit
18
- oracle_qc = q .QuantumCircuit (n + 1 )
19
-
18
+ oracle_qc = q .QuantumCircuit (n + 1 )
19
+
20
20
# First, let's deal with the case in which oracle is balanced
21
21
if case == "balanced" :
22
22
# First generate a random number that tells us which CNOTs to
23
23
# wrap in X-gates:
24
- b = np .random .randint (1 ,2 ** n )
24
+ b = np .random .randint (1 , 2 ** n )
25
25
# Next, format 'b' as a binary string of length 'n', padded with zeros:
26
- b_str = format (b , '0' + str (n )+ 'b' )
27
- # Next, we place the first X-gates. Each digit in our binary string
26
+ b_str = format (b , "0" + str (n ) + "b" )
27
+ # Next, we place the first X-gates. Each digit in our binary string
28
28
# correspopnds to a qubit, if the digit is 0, we do nothing, if it's 1
29
29
# we apply an X-gate to that qubit:
30
30
for qubit in range (len (b_str )):
31
- if b_str [qubit ] == '1' :
31
+ if b_str [qubit ] == "1" :
32
32
oracle_qc .x (qubit )
33
- # Do the controlled-NOT gates for each qubit, using the output qubit
33
+ # Do the controlled-NOT gates for each qubit, using the output qubit
34
34
# as the target:
35
35
for qubit in range (n ):
36
36
oracle_qc .cx (qubit , n )
37
37
# Next, place the final X-gates
38
38
for qubit in range (len (b_str )):
39
- if b_str [qubit ] == '1' :
39
+ if b_str [qubit ] == "1" :
40
40
oracle_qc .x (qubit )
41
41
42
42
# Case in which oracle is constant
@@ -46,29 +46,29 @@ def dj_oracle(case: str, n: int):
46
46
output = np .random .randint (2 )
47
47
if output == 1 :
48
48
oracle_qc .x (n )
49
-
49
+
50
50
oracle_gate = oracle_qc .to_gate ()
51
- oracle_gate .name = "Oracle" # To show when we display the circuit
51
+ oracle_gate .name = "Oracle" # To show when we display the circuit
52
52
return oracle_gate
53
53
54
54
55
55
def dj_algorithm (oracle , n : int ):
56
- dj_circuit = q .QuantumCircuit (n + 1 , n )
56
+ dj_circuit = q .QuantumCircuit (n + 1 , n )
57
57
# Set up the output qubit:
58
58
dj_circuit .x (n )
59
59
dj_circuit .h (n )
60
60
# And set up the input register:
61
61
for qubit in range (n ):
62
62
dj_circuit .h (qubit )
63
63
# Let's append the oracle gate to our circuit:
64
- dj_circuit .append (oracle , range (n + 1 ))
64
+ dj_circuit .append (oracle , range (n + 1 ))
65
65
# Finally, perform the H-gates again and measure:
66
66
for qubit in range (n ):
67
67
dj_circuit .h (qubit )
68
-
68
+
69
69
for i in range (n ):
70
70
dj_circuit .measure (i , i )
71
-
71
+
72
72
return dj_circuit
73
73
74
74
0 commit comments