# How to best implement this algorithm?

I am trying to design an algorithm to find a solution to a certain problem. I have N qubits which encode the problem (let's call them the problem qubits) and one check_qubit (which is flipped to |1> when the solution to the problem is found). My algorithm should look as follows:

1. Act with function1() (this function simply adds certain gates to my circuit)
2. Act with an operator which does the following:
• If check_qubit is in state |0>: repeat steps 1-2 --> if after a certain amount of tries/repeats, the check_qubit is still not in state |1> this means that the no solution has been found to the problem
• If check_qubit is in state |1>: terminate the algorithm and print out (on of) the state(s) with check_qubit in state |1> --> this means that the problem has been solved

Note that at step 2 I would have a large superposition of states, and am only interested in those where the check_qubit is in state |1> (as this would indicate a solution to my problem has been found).

The 3rd step is where I am stuck. I am not sure how to best implement this. There are a couple of options:

• I could add a measurement to my problem_qubits which is controlled by my check_qubit, but I am not sure if this is a thing in Cirq, and I am not sure how I would then terminate the loop.

• I could define an operator which absorbs all the states in my superposition which have the check_qubit in state |0>, which is controlled by the check_qubit, but again I am not sure how I would implement this.

I am not sure which is the best option, and how to implement it in Cirq. So far, I have mainly been using the cirq.Simulator().simulate option, but an implementation with cirq.Simulator().run would be fine as well.

Some example code is:

problem_qubits = cirq.GridQubit.rect(1, n, top=0)
check_qubit = cirq.NamedQubit('check')

circuit = cirq.Circuit()
for T in range(1, 100):       # max amount of tries/repeats is 100
circuit.append(function1())
circuit.append(...) # this is the step I am stuck at
results = cirq.Simulator().simulate(circuit) $$$$
`