State Comparison
The Swap Test: quick reference
Uses one ancilla and a controlled-SWAP to measure how similar two quantum states are.
- Topic
- State Comparison
- Level
- Intermediate
- Circuit size
- 3 Qubits
- Key result
- Compares two unknown states without learning either
Concepts covered: Swap Test, State Fidelity, Controlled-SWAP, Interference
How the Swap Test works, step by step
- Prepare the Two States. Load the two states to compare onto Q1 and Q2. Here they are 0 and plus, which overlap by half.
- Superpose the Ancilla and Begin the Swap. A Hadamard on the helper (Q0), and the first CNOT of the controlled swap.
- Controlled Middle CNOT (Toffoli). The Toffoli performs the middle step of the swap only when the helper is 1.
- Finish the Swap and Interfere. The closing CNOT completes the swap, and a Hadamard on the helper makes the swapped and unswapped branches interfere.
- Read the Similarity. The helper reads 0 three quarters of the time, so the overlap of the two states is one half.
Frequently asked questions
What is the Swap Test?
Uses one ancilla and a controlled-SWAP to measure how similar two quantum states are. It is a intermediate-level topic in state comparison.
How many qubits does the Swap Test circuit use?
The QuanBit tutorial uses a 3 qubits circuit. Key result: Compares two unknown states without learning either.
Can I simulate Swap Test online for free?
Yes. The interactive Swap Test tutorial at https://www.quanbit.tech/learn/swap-test runs free in the browser with nothing to install. You can step through the circuit gate by gate, watch the probabilities, statevector and Bloch spheres update, build the circuit yourself and take a short quiz.