# In a Swap Test why is the control qubit influenced and aren't the target qubits altered?

I am in a study group learning about quantum computing using O'Reilly - Programming Quantum Computing. I'm a developer, not a physicist. :) I feel like my question is rudimentary but I can't seem to find an answer here or anywhere else.

I'm confused on the Swap test; why it works and why doesn't it affect the qubits being swapped. Not the physics behind why the measurements diverge or approach |1>, but rather why the condition qubit of the cswap operation is altered at all.

I understand the cswap operation swaps two qubits based on a condition qubit. You might say the condition qubit has an influence on the swapped qubits.

However, a Swap Test shows that the condition qubit itself will be changed based on how closely the target qubits are equal. How do the swapped qubits have any influence back on the condition qubit? And furthermore, during the swap test aren't we potentially swapping the qubits? Should we be swapping them back so that our test doesn't actually change the state of the target qubits?

• This isn't an issue just for the swap test, but throughout the study of quantum algorithms. It's a mechanism called "phase kickback". With that specific term, you should find loads of material on this site! May 11, 2021 at 8:16

From a programmer's perspective, you have actually asked a pretty nuanced question. You seem to have all the building blocks so I'll try and share how I think about this.

Quantum gates are sometimes named similary to classical computing operations. This is usually done when the quantum gate acts like the classical operation $$\text{ if it is applied only to }\lvert 0 \rangle$$ or $$\lvert 1 \rangle$$. This naming convention seems to have sprouted from the desire to show that a quantum computer can replicate a classical computer's calculations. Along those lines; the x-gate applied to only to $$\lvert 0 \rangle$$ or $$\lvert 1 \rangle$$ produces the truth table of the classical NOT. The controlled-x (cx), which is sometimes referred to as the CNOT-gate produces the same truth table for the target qubit as the classical NAND operation when restricted to $$\lvert 0 \rangle$$ or $$\lvert 1 \rangle$$. For those with a progamming background these labels carry meaning and can cause confusion.

So it is important to internalize when our classical analogy is safe and when it has broken down and needs to be abandoned. From my personal perspective abandoning classical computing means I switch out of "programmer mode" and into "math/matrix mode" (i.e. “Toto, I've a feeling we're not in Kansas anymore.”).

Now, as to what is going on in the swap test specifically:

1. The first step of the test was to take $$q_0$$ out of the $$\lvert 0 \rangle$$ state into the $$\lvert + \rangle$$ state with the hadamard gate. Because our control gate is not exactly $$\lvert 0 \rangle$$ or $$\lvert 1 \rangle$$ we are exposing our algorithm to the quantum effects (phase-kickback in this case). Step 1 takes us firmly out of the classical computing and our analogies to classical conditional-controls no longer apply. But we get to exploit the power of the quantum effects.
2. The Swap Test is named based on its gates (hadamard, cswap, hadamard, measure), not its algorithmic outcome. From a programmer's perspective the swap test is closer to a classical function which performs the dot product between $$q_1$$ and $$q_2$$ and stores the results in $$q_0$$. The changes to $$q_0$$ are truly a "feature" not a "bug" in the algorithm.

Why not swap back $$q_1$$ and $$q_2$$.

1. From a classical computing perspective we already have the desired outcome of our algorithm, so any more operations just adds complexity and noise.
2. From a quantum computing perspective all three qubits are entangled and any operation on one of them has the potential to impact the measurements we would get on the others.

Lastly: But in quantum computing it seems that most any interaction with a qubit potentially has an effect. Is this generally correct?
If the qubits are entangled, then yes.

When I start with two qubits $$q_0$$ and $$q_1$$, both are $$\lvert 0 \rangle$$ by default. I can treat them as two qubits and operations on them are, in classical parlance, un-coupled from each other. Once I entangle them, I no longer have two qubits, but I have one "bigger qubit". So anything I do, or did in the past, on either $$q_0$$ or $$q_1$$ has the potential to impact measurements I might make on any other entangled qubits.

• Thanks for the thorough and "developer based" response! This really helped my thinking. Jan 6, 2023 at 16:11

Deciding which qubit is a source and which is a target and changes depends on a basis. In a basis-independent sense, the qubits get entangled, and it affects both sides. In a swap test you apply Hadamard gate to the control qubit which can be interpreted as a change of basis, and subsequent CSWAP gate changes the control qubit

• So, my hang-up may be from my traditional programming background. In "classic computers" an operation that has a condition, like an "if (someVar someCondition) then..." the evaluation of that condition should never alter someVar. But in quantum computing it seems that most any interaction with a qubit potentially has an effect. Is this generally correct? May 12, 2021 at 18:19