Skip to content

Commit 1a33ec4

Browse files
committed
Address Further Review Comments
1 parent 4e2a95b commit 1a33ec4

File tree

1 file changed

+27
-30
lines changed

1 file changed

+27
-30
lines changed

quantum/deutsch_jozsa.py

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,77 +25,77 @@
2525
import qiskit as q
2626

2727

28-
def dj_oracle(case: str, n: int) -> q.QuantumCircuit:
28+
def dj_oracle(case: str, num_qubits: int) -> q.QuantumCircuit:
2929
"""
3030
Returns a Quantum Circuit for the Oracle function.
3131
The circuit returned can represent balanced or constant function,
3232
according to the arguments passed
3333
"""
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,
3535
# plus one output qubit
36-
oracle_qc = q.QuantumCircuit(n+1)
36+
oracle_qc = q.QuantumCircuit(num_qubits+1)
3737

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**num_qubits)
4343
# 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")
4545
# 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:
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)
5151
# Do the controlled-NOT gates for each qubit, using the output qubit
5252
# 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)
5555
# 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)
5959

6060
# Case in which oracle is constant
6161
if case == "constant":
6262
# First decide what the fixed output of the oracle will be
6363
# (either always 0 or always 1)
6464
output = np.random.randint(2)
6565
if output == 1:
66-
oracle_qc.x(n)
66+
oracle_qc.x(num_qubits)
6767

6868
oracle_gate = oracle_qc.to_gate()
6969
oracle_gate.name = "Oracle" # To show when we display the circuit
7070
return oracle_gate
7171

7272

73-
def dj_algorithm(oracle: q.QuantumCircuit, n: int) -> q.QuantumCircuit:
73+
def dj_algorithm(oracle: q.QuantumCircuit, num_qubits: int) -> q.QuantumCircuit:
7474
"""
7575
Returns the complete Deustch-Jozsa Quantum Circuit,
7676
adding Input & Output 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(num_qubits+1, num_qubits)
8080
# 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)
8383
# And set up the input register:
84-
for qubit in range(n):
84+
for qubit in range(num_qubits):
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(num_qubits+1))
8888
# Finally, perform the H-gates again and measure:
89-
for qubit in range(n):
89+
for qubit in range(num_qubits):
9090
dj_circuit.h(qubit)
9191

92-
for i in range(n):
92+
for i in range(num_qubits):
9393
dj_circuit.measure(i, i)
9494

9595
return dj_circuit
9696

9797

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:
9999
"""
100100
Main function that builds the circuit using other helper functions,
101101
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:
107107
# Use Aer's qasm_simulator
108108
simulator = q.Aer.get_backend("qasm_simulator")
109109

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)
112112

113113
# Execute the circuit on the qasm simulator
114114
job = q.execute(dj_circuit, simulator, shots=1000)
@@ -118,8 +118,5 @@ def deutsch_jozsa(case: str, n: int) -> q.result.counts.Counts:
118118

119119

120120
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

Comments
 (0)