I have a simple implementation of Grover's algorithm. As depicted in the results below, the oracle function marks the state |111>. How can I change the Oracle function to mark |010>?
import cirq
from cirq import H, Z, X
qq = cirq.LineQubit.range(3)
circuit = cirq.Circuit()
circuit.append(H.on_each(*qq))
def oracle():
yield Z(qq[2]).controlled_by(*(qq[0:2]))
def grover_diffusion():
yield H.on_each(*qq)
yield X.on_each(*qq)
yield Z(qq[2]).controlled_by(*(qq[0:2]))
yield X.on_each(*qq)
yield H.on_each(*qq)
for i in range(2):
circuit.append(oracle())
circuit.append(grover_diffusion())
circuit.append(cirq.measure(*qq, key='result'))
# determine the statistics of the measurements
s = cirq.Simulator()
trials_number = 1000
samples = s.run(circuit, repetitions=trials_number)
def bitstring(bits):
return "".join(str(int(b)) for b in bits)
counts = samples.histogram(key = "result", fold_func = bitstring)
print("Measurement output: ", counts)
# Output is something like
# Measurement output: Counter({'111': 953, '001': 10, '000': 9, '110': 9, '010': 9, '101': 4, '100': 4, '011': 2})
```