Skip to content

Commit ce74e77

Browse files
github-actionsgithub-actions
github-actions
authored and
github-actions
committed
fixup! Format Python code with psf/black push
1 parent e9c3d82 commit ce74e77

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

quantum/deutsch_jozsa.py

+17-17
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
References:
2020
- https://en.wikipedia.org/wiki/Deutsch-Jozsa_algorithm
2121
- https://qiskit.org/textbook/ch-states/atoms-computation.html#4.2-Remembering-how-to-add-
22-
""" # noqa
22+
""" # noqa
2323

2424
import numpy as np
2525
import qiskit as q
@@ -33,28 +33,28 @@ def dj_oracle(case: str, n: int) -> q.QuantumCircuit:
3333
"""
3434
# This circuit has n+1 qubits: the size of the input,
3535
# plus one output qubit
36-
oracle_qc = q.QuantumCircuit(n+1)
37-
36+
oracle_qc = q.QuantumCircuit(n + 1)
37+
3838
# First, let's deal with the case in which oracle is balanced
3939
if case == "balanced":
4040
# First generate a random number that tells us which CNOTs to
4141
# wrap in X-gates:
42-
b = np.random.randint(1,2**n)
42+
b = np.random.randint(1, 2 ** n)
4343
# 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
4646
# correspopnds to a qubit, if the digit is 0, we do nothing, if it's 1
4747
# we apply an X-gate to that qubit:
4848
for qubit in range(len(b_str)):
49-
if b_str[qubit] == '1':
49+
if b_str[qubit] == "1":
5050
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
5252
# as the target:
5353
for qubit in range(n):
5454
oracle_qc.cx(qubit, n)
5555
# Next, place the final X-gates
5656
for qubit in range(len(b_str)):
57-
if b_str[qubit] == '1':
57+
if b_str[qubit] == "1":
5858
oracle_qc.x(qubit)
5959

6060
# Case in which oracle is constant
@@ -64,40 +64,40 @@ def dj_oracle(case: str, n: int) -> q.QuantumCircuit:
6464
output = np.random.randint(2)
6565
if output == 1:
6666
oracle_qc.x(n)
67-
67+
6868
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
7070
return oracle_gate
7171

7272

7373
def dj_algorithm(oracle: q.QuantumCircuit, n: int) -> q.QuantumCircuit:
7474
"""
7575
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,
7777
to the Oracle Circuit passed in arguments
7878
"""
79-
dj_circuit = q.QuantumCircuit(n+1, n)
79+
dj_circuit = q.QuantumCircuit(n + 1, n)
8080
# Set up the output qubit:
8181
dj_circuit.x(n)
8282
dj_circuit.h(n)
8383
# And set up the input register:
8484
for qubit in range(n):
8585
dj_circuit.h(qubit)
8686
# 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))
8888
# Finally, perform the H-gates again and measure:
8989
for qubit in range(n):
9090
dj_circuit.h(qubit)
91-
91+
9292
for i in range(n):
9393
dj_circuit.measure(i, i)
94-
94+
9595
return dj_circuit
9696

9797

9898
def deutsch_jozsa(case: str, n: int) -> q.result.counts.Counts:
9999
"""
100-
Main function that builds the circuit using other helper functions,
100+
Main function that builds the circuit using other helper functions,
101101
runs the experiment 1000 times & returns the resultant qubit counts
102102
>>> deutsch_jozsa("constant", 3)
103103
{'000': 1000}

0 commit comments

Comments
 (0)