Computing Library › Quantum Algorithms
Quantum Algorithms
The SWAP Test
The SWAP test estimates how similar two quantum states are — the overlap |⟨ψ|φ⟩|² — using a controlled-SWAP and a single measured qubit.
- Output
- |⟨ψ|φ⟩|² from measurement statistics
- Gate
- controlled-SWAP (Fredkin)
- Use
- quantum kernels, fingerprinting
What it does
An ancilla in superposition controls a SWAP between the two states; a Hadamard and measurement on the ancilla yield a 0-probability that encodes the overlap. It never reveals the states — only their similarity.
Circuit sketch
Where it's used
Quantum kernel methods and classifiers, state comparison, and entanglement diagnostics.
In code (Qiskit)
python
qc=QuantumCircuit(3,1)
qc.h(0); qc.cswap(0,1,2); qc.h(0); qc.measure(0,0)