Computing Library › Quantum Foundations
Quantum Foundations

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

Kronos motion — 14 mev materials test

The result

The probability of measuring the ancilla as 0 is P(0) = (1 + ||^2)/2. If the two states are identical, ||^2 = 1 and the ancilla is always 0. If they are orthogonal, the overlap is 0 and the ancilla is 0 only half the time. Estimating P(0) over many runs yields the overlap.

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.