Quantum Computing Asked by JIYUAN WANG on September 18, 2020
import cirq
def set_io_qubits(qubit_count):
input_qubits = [cirq.GridQubit(i, 0) for i in range(qubit_count)]
return (input_qubits)
def debug_oracle(input_qubits):
yield (cirq.H(input_qubits[0]))
def make_grover_circuit(input_qubits, oracle):
c = cirq.Circuit()
c.append(oracle)
c.append(oracle)
c.append(oracle)
c.append(cirq.measure(*input_qubits, key='result'))
return c
def main():
input_qubits = set_io_qubits(2)
oracle = debug_oracle(input_qubits)
circuit = make_grover_circuit(input_qubits, oracle)
print('Circuit:')
print(circuit)
if __name__ == '__main__':
main()
I add c.append(oracle)
three times in make_grover_circuit
. However, when I print the circuit, it seems there is only one oracle
. Is it a bug? Or just I can’t .append
my oracle more than once? Here is my result of the program.
/Library/Frameworks/Python.framework/Versions/3.7/bin/python3 /Users/a123/Desktop/Cirq/test.py
Circuit:
(0, 0): ───H───M('result')───
│
(1, 0): ───────M─────────────
Process finished with exit code 0
The issue is that you are using the result of a generator method multiple times:
def gen():
yield 1
g = gen()
print(list(g)) # prints [1]
print(list(g)) # prints []
This is a common mistake to make in Python. The solution is to store a list of the items instead of the generator:
def gen():
yield 1
gen_items_safe_to_reuse = list(gen())
Answered by Craig Gidney on September 18, 2020
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP