Skip to content

Commit 8dd8d58

Browse files
authored
Refactor BV example to allow just constructing circuit (#291)
Review: @Strilanc
1 parent fda82b5 commit 8dd8d58

File tree

1 file changed

+33
-22
lines changed

1 file changed

+33
-22
lines changed

cirq/examples/bernstein_vazirani.py

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
import collections
1616
import random
17+
from typing import Sequence
18+
1719
from absl import app
1820
from absl import flags
1921

@@ -29,41 +31,50 @@ def bitstring(bits):
2931
return ''.join(str(int(b)) for b in bits)
3032

3133

32-
def bv(n_qubits: int,
33-
a: int,
34-
shots: int = NUM_SHOTS
35-
) -> collections.Counter:
36-
"""Creates and executes the circuit for Bernstein-Vazirani algorithm.
34+
def bv_circuit(qubits: Sequence[cirq.QubitId], a: int) -> cirq.Circuit:
35+
"""Creates a circuit for the Bernstein-Vazirani algorithm.
3736
3837
Args:
39-
n_qubits: integer < 30, number of qubits in the simulated circuit.
40-
a: integer < 2**n_qubits, representing the unknown bit string.
41-
circuit_name: string to identify the circuit
42-
device: type of the device used
43-
shots: number of times the circuit has been executed.
38+
qubits: list of qubits in the circuit.
39+
a: integer < 2**len(qubits), representing the unknown bit string.
4440
4541
Returns:
4642
Result object, containing measurement data after the circuit has run.
4743
"""
48-
# 1. Define a sequence of qubits.
49-
qubits = [cirq.google.XmonQubit(0, x) for x in range(n_qubits)]
50-
# 2. Create a circuit (qubits start in the |0> state).
51-
circuit = cirq.circuits.Circuit()
52-
# 3. Apply Hadamard gates to the inputs.
44+
# 1. Create a circuit (qubits start in the |0> state).
45+
circuit = cirq.Circuit()
46+
# 2. Apply Hadamard gates to the inputs.
5347
H_layer = [cirq.H(qubit) for qubit in qubits]
5448
circuit.append(H_layer)
55-
# 4. Apply the inner-product oracle
56-
O_layer = [cirq.Z(qubits[i]) for i in range(n_qubits) if a & (1 << i)]
49+
# 3. Apply the inner-product oracle
50+
O_layer = [cirq.Z(qubit) for i, qubit in enumerate(qubits) if a & (1 << i)]
5751
circuit.append(O_layer)
58-
# 5. Apply Hadamard gates to the outputs
52+
# 4. Apply Hadamard gates to the outputs
5953
circuit.append(H_layer)
60-
# 6. Apply measurement layer
54+
# 5. Apply measurement layer
6155
circuit.append(cirq.ops.MeasurementGate('result').on(qubit)
62-
for i, qubit in enumerate(qubits))
56+
for qubit in qubits)
57+
return circuit
58+
59+
60+
def bv(n_qubits: int, a: int, shots: int = NUM_SHOTS) -> collections.Counter:
61+
"""Creates and executes the circuit for Bernstein-Vazirani algorithm.
62+
63+
Args:
64+
n_qubits: integer < 30, number of qubits in the simulated circuit.
65+
a: integer < 2**n_qubits, representing the unknown bit string.
66+
shots: number of times the circuit has been executed.
67+
68+
Returns:
69+
Result object, containing measurement data after the circuit has run.
70+
"""
71+
# Generate the circuit.
72+
qubits = [cirq.google.XmonQubit(0, x) for x in range(n_qubits)]
73+
circuit = bv_circuit(qubits, a)
6374

64-
# 7. Debug step
6575
print(circuit)
66-
# 8. Run and collect results
76+
77+
# Simulate and return results.
6778
simulator = cirq.google.Simulator()
6879
result = simulator.run(circuit, repetitions=NUM_SHOTS)
6980
result_bits = result.measurements['result'] # 2D array of (rep, qubit)

0 commit comments

Comments
 (0)