1
$\begingroup$

I was wondering if it's theoretically possible to have a while loop that runs a bit like this:

  1. You initialize a set of qbits with the H-gate.
  2. You perform a set of operations on them.
  3. You check the state of those qbits and decide to either go back to step 2 or to continue with step 4.
  4. You measure the outcome.

What I understand is that you can do this if you work with upper bounds (How to implement a while loop in a quantum computer?) where you simply use conditional operations a number of times. If I'm not mistaken this also requires an additional qbit for every loop iteration you make. So if you want to loop 10 times, you need 10 qbits to store the whether the condition was made or not.

However, what I am looking for is the possibility to have it run indefinitely until all undesired states of the superposition are filtered out. Basically creating something that sounds a lot like a non-deterministic computer.

I was thinking maybe it could work something like this: Imagine you start with a qbyte in a uniform superposition and you desire it to be in a superposition where [1111111> is a near 99% outcome. Every iteration you see a single 0 in your qbyte, you perform Hadamard operations on all qbits. Now instead of using just a quantum computer, I would suggest putting both a quantum computer and a cat in a box. The cat operates the quantum computer and at the end of an iteration the cat measures the qbyte. If the qbyte is all 1's, the cat will stop and wait for some nice human to open the box. If a single qbyte is a 0, then the cat will Hadamard all the qbits again and repeat the process until they're all 1's.

Does any of this make sense? I was thinking since the cat itself is unobserved, it can be in a quantum superposition while measuring the qbytes. I am not very knowledgeable on the field of quantum mechanics and am also a bit novice to quantum computing, so a lot of the stuff I just said probably doesn't make much sense. So, feel free to point out all my misinformed/misguided/misinterpreted errors in my argument.

$\endgroup$

1 Answer 1

1
$\begingroup$

One example is that you run a circuit in a while loop until a measurement result satisfies a condition.

from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister, transpile
from qiskit_aer import AerSimulator, Aer
from qiskit.visualization import plot_histogram
from qiskit_ibm_runtime import QiskitRuntimeService
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
from qiskit_ibm_runtime import Session, SamplerV2 as Sampler
from qiskit.circuit.library import ZGate, XGate, RZGate
service = QiskitRuntimeService()

# create a quantum circuit
num_of_qubits = 8 # 1byte
is_stop = False

data_r = QuantumRegister(num_of_qubits, "data")
out_r = QuantumRegister(1, "out")
cr = ClassicalRegister(1, "meas")
qc = QuantumCircuit(data_r, out_r, cr)
qc.h(data_r)
qc.append(XGate().control(num_of_qubits), list(data_r)+[out_r])
qc.measure(out_r, cr)
qc.draw("mpl")

enter image description here

aer = AerSimulator()
pm = generate_preset_pass_manager(backend=aer, optimization_level=1)
isa_qc = pm.run(qc)
result = None

num_of_loops = 0
while(not is_stop):
    num_of_loops += 1
    with Session(backend=aer) as session:
        sampler = Sampler(session=session)
        result = sampler.run([isa_qc], shots=1).result()
        pub_result = result[0]
        counts = pub_result.data.meas.get_counts()
        if '1' in counts:
            is_stop = True
            print("all qubits are 1, # of loops {}".format(num_of_loops))
all qubits are 1, # of loops 90

As far as I know, there's no such mechanism that it loops inside a quantum circuit itself. In my view, a loop would typically be used when the number of executions is unknown(or could be large), while we would use a quantum computer since we know that a calculation would be finished within a small number of times. Hence, not having a loop inside it makes sense, at least, to me.

$\endgroup$
2
  • $\begingroup$ But this is just a classical while loop built around a quantum process, right? It's just measuring the qubits every loop. I was more looking for a whole new kind of loop operation that operates within the quantum world $\endgroup$ Commented Aug 10 at 17:51
  • $\begingroup$ I've added a comment at the end of my answer. $\endgroup$ Commented Aug 11 at 2:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.