Introduction
The quantum SWAP test is a basic quantum subroutine used to compare two quantum states. Given two input states, it checks if they are equal, and it estimates how close they are by measuring an auxiliary control qubit after a controlled-SWAP operation.
In quantum protocols, the SWAP test can be used as a local procedure inside a node whenever the protocol needs to compare two quantum registers, estimate an inner product, test equality of quantum states, estimate purity, or evaluate state overlap. It is especially useful when the states are not known classically and full quantum state tomography would be too expensive.
Outline
For pure states $|\psi\rangle$ and $|\phi\rangle$, the SWAP test estimates the squared overlap $|\langle \psi|\phi\rangle|^2$. For mixed states $\rho$ and $\sigma$, it estimates the Hilbert-Schmidt inner product $\mathrm{Tr}(\rho\sigma)$.
The subroutine can be described abstractly as follows.
- Input. A node receives two quantum registers, containing states $\rho$ and $\sigma$, together with an auxiliary qubit initialized to $|0\rangle$.
- First Hadamard. The node applies a Hadamard gate to the auxiliary qubit.
- Controlled-SWAP. Controlled on the auxiliary qubit, the node applies a SWAP operation between the two input registers.
- Second Hadamard. The node applies another Hadamard gate to the auxiliary qubit.
- Measurement. The auxiliary qubit is measured in the computational basis.
- Output. The output is a classical bit. Repeating the subroutine many times gives an estimate of the overlap between the two input states.
Assumptions
No particular assumptions on the inputs or subroutine.
The test usually requires fresh copies of the input states for repeated runs. Estimating the overlap to high precision therefore requires multiple copies of the states.
Also the SWAP test has a one-sided error, meaning that if the two states are equal it always returns equality (outcome 0), while as if they are completely orthogonal, it gives a random outcome with probability 1/2.
Notation
- Let the two input states be $|\psi\rangle$ and $|\phi\rangle$ in the pure-state case.
- Let $\rho$ and $\sigma$ denote the two input density operators in the mixed-state case.
- Let $A$ denote the auxiliary qubit.
- Let $\mathrm{SWAP}$ denote the unitary that exchanges the two input registers:
$$\mathrm{SWAP}|\psi\rangle|\phi\rangle = |\phi\rangle|\psi\rangle . $$ - The controlled-SWAP operation is denoted by
$$ \mathrm{CSWAP}. $$ - The outcome of measuring the auxiliary qubit is denoted by $b\in{0,1}$.
Properties
- State-overlap estimation. For pure states, the SWAP test estimates $|\langle \psi|\phi\rangle|^2$.
- Mixed-state inner product estimation. For mixed states, the SWAP test estimates $\mathrm{Tr}(\rho\sigma)$.
- Non-tomographic. The subroutine does not reconstruct the full input states.
- Probabilistic output. A single run gives one classical bit. Many repetitions are needed to estimate the overlap accurately.
- Equality testing. If two pure states are identical, the auxiliary qubit is always measured as $0$. If they are orthogonal, the auxiliary qubit is measured as $0$ with probability $1/2$.
Technical description
For pure input states $|\psi\rangle$ and $|\phi\rangle$, the initial state is
$$
|0\rangle|\psi\rangle|\phi\rangle .
$$
After applying a Hadamard gate to the auxiliary qubit, the state becomes
$$
\frac{1}{\sqrt{2}}\left(|0\rangle|\psi\rangle|\phi\rangle + |1\rangle|\psi\rangle|\phi\rangle\right).
$$
The controlled-SWAP operation gives
$$
\frac{1}{\sqrt{2}}\left(|0\rangle|\psi\rangle|\phi\rangle + |1\rangle|\phi\rangle|\psi\rangle\right).
$$
After applying the second Hadamard gate to the auxiliary qubit, the state becomes
$$
\frac{1}{2}|0\rangle\left(|\psi\rangle|\phi\rangle + |\phi\rangle|\psi\rangle\right)
+
\frac{1}{2}|1\rangle\left(|\psi\rangle|\phi\rangle โ |\phi\rangle|\psi\rangle\right).
$$
Therefore, the probability of measuring the auxiliary qubit in state $0$ is
$$ \Pr[b=0] = \frac{1}{2}\left(1 + |\langle \psi|\phi\rangle|^2\right). $$
Similarly, the probability of measuring outcome $1$ is
$$ \Pr[b=1] = \frac{1}{2}\left(1 โ |\langle \psi|\phi\rangle|^2\right). $$
Thus the squared overlap can be recovered from the probability of outcome $0$ as
$$ |\langle \psi|\phi\rangle|^2 = 2\Pr[b=0]-1. $$
Equivalently, it can be recovered from the probability of outcome $1$ as
$$ |\langle \psi|\phi\rangle|^2 = 1-2\Pr[b=1].$$
For mixed input states $\rho$ and $\sigma$, the same circuit gives
$$ \Pr[b=0] = \frac{1}{2}\left(1+\mathrm{Tr}(\rho\sigma)\right), $$
and
$$ \Pr[b=1] = \frac{1}{2}\left(1-\mathrm{Tr}(\rho\sigma)\right). $$
Therefore, repeated runs estimate
$$ \mathrm{Tr}(\rho\sigma) = 2\Pr[b=0]-1. $$
In the special case $\rho=\sigma$, the SWAP test estimates the purity of the state: $\mathrm{Tr}(\rho^2). $
To estimate the relevant overlap to additive error $\varepsilon$, the subroutine is repeated on fresh copies of the input states and the empirical frequency of outcome $0$ is used as an estimator.
Further information
The SWAP test has useful generalisations when one has asymmetric copy access to the two states being compared. One important example is the generalised SWAP test, or GSWAP test [5], which compares a single copy of one state with several copies of another reference state in one joint subroutine.
In a typical formulation, the GSWAP test takes as input one copy of a state $\rho$, $M$ copies of a reference pure state $|\psi\rangle$, and an ancilla register. It then performs a generalised controlled permutation test and measures the ancilla system to decide whether the input state matches the reference state. For $M=1$, the construction reduces to the ordinary SWAP test.
The acceptance probability can be written as
$$
\frac{1}{M+1}
+
\frac{M}{M+1}\langle \psi|\rho|\psi\rangle .
$$
Equivalently, writing the fidelity with the reference state as
$$
F^2 = \langle \psi|\rho|\psi\rangle ,
$$
one obtains
$$ \frac{1}{M+1}
+
\frac{M}{M+1}F^2 .
$$
Thus, as $M$ increases, the GSWAP test approaches an ideal projective test onto the reference state. This makes it useful in settings where a verifier has several stored copies of a reference state but receives only a single copy of a candidate state to be tested.
This generalised test appears in work on programmable projective measurements and has been used in quantum identification and quantum PUF protocols, where a node may need to compare a received quantum response against several stored copies of a valid reference response.
References
- H. Buhrman, R. Cleve, J. Watrous, and R. de Wolf, โQuantum Fingerprinting,โ Physical Review Letters, vol. 87, no. 16, 2001.
- A. Barenco, A. Berthiaume, D. Deutsch, A. Ekert, R. Jozsa, and C. Macchiavello, โStabilization of Quantum Computations by Symmetrization,โ SIAM Journal on Computing, vol. 26, no. 5, 1997.
- M. A. Nielsen and I. L. Chuang, Quantum Computation and Quantum Information, Cambridge University Press, 2000.
- R. de Wolf, Quantum Computing: Lecture Notes, 2021.
- N. Kumar, U. Chabaud, E. Kashefi, D. Markham, and E. Diamanti, โOptimal quantum-programmable projective measurements with coherent states,โ Physical Review Research, vol. 3, 043035, 2021..


Leave a Reply