The SWAP Test
The SWAP test uses one ancilla qubit and a controlled-SWAP to estimate the overlap between two unknown quantum states.
Comparing states without reading them
The SWAP test is a circuit that estimates how similar two quantum states are — the magnitude of their inner product — without needing to know either state. It respects the constraint that you cannot simply read out an unknown state, and it is a common subroutine in quantum machine learning and verification.
The circuit
- Prepare an ancilla qubit in |0> and put it in superposition with a Hadamard
- Apply a controlled-SWAP: the ancilla controls whether the two input states are swapped
- Apply a second Hadamard to the ancilla
- Measure the ancilla in the computational basis
The result
The probability of measuring the ancilla as 0 is P(0) = (1 + |
Why it works
The controlled-SWAP entangles the ancilla with the symmetry of the two-state pair. Identical states are symmetric under swapping and interfere constructively toward ancilla-0; orthogonal states have no such symmetry. The final Hadamard converts this interference into a measurable probability — a clean example of using interference to extract a global property.
Uses and limits
The SWAP test measures only the magnitude of the overlap, not its phase, and only statistically, so precision improves as one over the square root of the number of shots. Despite these limits it is widely used: for state comparison, fingerprinting, kernel evaluation in quantum machine learning, and as a diagnostic to verify that a prepared state matches a target without full tomography.