# Alternatives to the swap test using a smaller number of qubits

I need to compute the inner product between two generic quantum states. To this aim, one can use the swap test as explained in https://en.wikipedia.org/wiki/Swap_test .

In my case, both of the quantum states are given by two parametrized quantum circuits having the same structure but different parameters.

Since the swap test requires a relatively big register (one ancilla, a set of $$n$$ qubits for $$\psi$$ and a set of $$n$$ qubits for $$\phi$$), I am wondering if there are alternatives where I do not have to double the quantum register size for $$\psi$$ and $$\phi$$, so use $$n+1$$ qubits instead of $$2n+1$$. Or, maybe there is no way to do that.

Let us say that you want to evaluate $$|\langle\psi|\varphi\rangle|^2$$. Let us denote $$U$$ and $$V$$ the unitaires associated to your parametrized quantum circuits, producing respectively $$|\psi\rangle$$ and $$|\varphi\rangle$$. Using these notations, you actually want to evaluate $$|\langle0|U^\dagger V|0\rangle|^2$$. Note that this is equal to the probability of getting $$|0\rangle$$ when measuring the $$U^\dagger V|0\rangle$$ state in the computational basis.

I'm assuming here that you are able to apply $$U^\dagger$$, which is true if you have access to $$U$$'s circuit description.

Thus, what you can do is:

1. Start from the $$|0\rangle$$ state.
2. Apply $$V$$.
3. Apply $$U^\dagger$$.
4. Measure in the computational basis.

You're interested in the frequency at which you've measured $$|0\rangle$$ state, which will provide a good estimate for the quantity you're interested in. You can furthermore upper-bound the absolute error of this estimation using a simple confidence interval.

I'm not sure this is the most efficient way to do it though, but it uses the least number of qubits you could ask for.

• Thanks, this is exactly what I needed. I have in fact the quantum circuit associated with $U$, but how do I build the quantum circuit associated with $U^\dagger$? Dec 11, 2023 at 10:51
• @francler - you just run it backwards, taking the adjoint of each gate. If you know that $U=U_3 U_2 U_1$ then you know that $U^\dagger=U_1^\dagger U_2^\dagger U_3^\dagger$. Dec 11, 2023 at 14:43

The SWAP test is great when you don't have access to $$U$$ or $$V$$ and have no other way to prepare $$|\psi\rangle$$ or $$|\phi\rangle$$, and uses $$2n+1$$ qubits - while Tristan's test uses only $$n$$ qubits, in addition to any ancillary registers used for $$U$$ or $$V$$. Tristan's approach assumes we can deduce $$U^\dagger$$ from $$U$$, which, as indicated, is almost as simple as running $$U$$ backwards.

Alternatively and morally equivalently you could use a Hadamard test on $$n+1$$ qubits (+other ancillary qubits) as follows:

1. Prepare a single-qubit control register as $$|0\rangle$$;
2. Apply a Hadamard gate to $$|0\rangle$$;
3. Apply $$U$$ to $$|00\cdots 0\rangle$$, controlled off of the control qubit being $$|0\rangle$$;
4. Apply $$V$$ to the same register, controlled off of the control qubit being $$|1\rangle$$;
This test runs both $$U$$ and $$V$$ forward, but uses at least one more qubit than Tristan's ($$n+1$$) while also having to assume that we have a way to control $$U$$ and $$V$$.
(We almost always assume that if we know how to execute $$U$$ then we can execute $$U^\dagger$$, and/or that we can execute controlled versions of $$U$$ - these assumptions may be risky though, especially the second one and especially on real-world devices!)