# Can quantum algorithms include conditional jumps/change an instruction pointer?

From what I've seen (in talks for a general physics audience, but I'm not in the field of quantum computing), all or most quantum algorithms are fixed sequences of instructions applied to registers made of qbits. These instructions are built from quantum logic gates (e.g. Hadamard, Pauli, CNOT), just as classical computer instructions like "ADD" are built from half-adders and full-adders, and ultimately from classical logic gates like AND, OR, NOT, or just NAND.

Conditional jump instructions, such as JZ ("jump if zero") and JNE ("jump if not equal") in x86, are important in classical computing, without which I don't think it would be possible to implement the familiar if, else, for, and while of high-level languages. If the "instruction pointer" (e.g. the EIP register in x86) in a quantum computer has a classical value, then I would conclude that data-dependent jumps are not possible, since querying the value of a qbit would destroy its coherence. But am I wrong about the architecture of current or future quantum computers—is the instruction pointer itself made of qbits? That is, can the position in the instruction sequence be part of a quantum superposition? Can one quantum arithmetic-logic unit be on different instruction steps at the same time?

I have seen statements that quantum computers can run classical algorithms. Some classical algorithms require data-dependent jumps. Or is it the case that quantum algorithms can jump if the states are $$|0\rangle$$ and $$|1\rangle$$ (i.e. already on the measurement basis) but not a mixed $$a|0\rangle + b|1\rangle$$, just as cloning is possible for some states, but not arbitrary states?

I haven't found obvious counter-examples; e.g. Wikipedia's description of Grover's algorithm appears to lack data-dependent jumps. (It says "repeat N times," but N is assigned externally. A compiler could unroll that loop.) On the other hand, the LanQ quantum imperative programming language includes a while statement, which suggests that it can do data-dependent jumps, if the expr in while (expr) can have a superimposed value.

If I'm thinking about this correctly and quantum algorithms can't make data-dependent jumps, it seems similar to SIMD programming, in which we would effectively perform if statements by masking part of the data, rather than by jumping over instructions, and certain kinds of loops aren't possible. Am I thinking about it correctly?

That's an interesting question!

## Running any classical algorithm

First, let us say a word about the fact that quantum computers can run classical algorithms. As you've correctly intuited, what is generally meant by this statement is that we can simply perform computations on the basis states, since it is possible to implement any classical gate using the quantum ones. This would thus also be possible to implement a JZ gate quite easily: the state being a basis one, there's no problem measuring it.

Concerning the LanQ language, it seems that the control instruction (be they if or while) are classical ones, depending on classical information (for instance, we can see in some examples that they act depending on the result of a measurement).

## A Quantum conditional jump

So now, let us talk about implementing a quantum JZ instruction. Fundamentally, what happens in a classical executable when such an instruction is met is that the processor will read off the value of the register and jump to some part of the executable (either the next line or a defined point in the executable), from which it will continue to act.

What's a bit peculiar in this case in quantum computing is that we're generally keener to speak about circuits rather than lists of instructions. But if we were to imagine what would a quantum JZ instruction look like, ti would mean that the processor applies some gate $$U_0$$ if the state is in state $$|0\rangle$$ and $$U_{\neq0}$$ otherwise, in superposition.

This is actually perfectly doable using controlled gates. Suppose we have a state $$|\psi\rangle$$ and we want to do such a thing. Let us take an additional ancilla qubit in state $$|0\rangle$$. For clarity, let us decompose $$|\psi\rangle$$ into $$\alpha\left|0\right\rangle+\beta\left|\neq0\right\rangle$$. Our state is thus: $$\alpha|0\rangle|0\rangle+\beta|\neq0\rangle|0\rangle.$$ We first apply $$X$$ gates on the first register, followed by an $$X$$-gate on the ancilla qubit controlled off the first register. We then apply once again $$X$$ gates on the first register. What that does is essentially testing whether the first register is in state $$|0\rangle$$ in superposition, yielding the state: $$\alpha|0\rangle|1\rangle+\beta|\neq0\rangle|0\rangle.$$ We now apply $$U_0$$ controlled off the ancilla qubit on the first register: $$\alpha U_0|0\rangle|1\rangle+\beta|\neq0\rangle|0\rangle.$$ We then apply an $$X$$ gate on the ancilla qubit and apply $$U_{\neq0}$$ on the first register controlled off the ancilla qubit, finally yielding: $$\alpha U_0|0\rangle|0\rangle+\beta U_{\neq0}|\neq0\rangle|1\rangle.$$ Thus, we've successfully simlated a JZ instruction in superposition.

## Practicality and usefulness

The goal of the previous section was to show that this is theoretically possible. Now, there are caveats to this implementation.

First, for any conditional branch in the program, we would have to add an ancilla qubit. Basically, the ancilla register will represent the path the algorithm took along the way. Note that if we don't act on it along the way, there's little we can do to select the path we want at the end, which may or may not be desirable (but it likely isn't).

Furthermore, controlling a gate from its circuit description is (unless I'm mistaken) done by transforming every gate into controlled ones (we can actually do a bit better for some gates that will cancel themselves, but that's the gist of it). Controlled operations are quite costly on current quantum computers: not only they are noisy and can lead to errors, but decomposing a controlled gate can lead to quite deep circuits. So I don't see this paradigm be used anytime soon.

## A word at the end

That said, conditional instructions are used all the time in Quantum Computing, in the form of CNOT and Toffoli gates. It's just rare AFAIK that we build large controlled gates like this. It is also possible to measure part of the register and act accordingly classically. At the end of the day, it all depends on what you actually want to do with your computation.