Skip to main content

Plain-Language Intuition

Mode: Simple / School
You are given two qubits in unknown states. Are they the same? You cannot just measure and compare, because one measurement reveals almost nothing about a state. The swap test answers the question with a single helper qubit: 1. Put the helper in superposition. 2. Swap the two states, but only in the branch where the helper is 1. 3. Apply a Hadamard to the helper and measure it. If the two states are identical, swapping them changes nothing, the two branches recombine perfectly, and the helper always reads 0. The more the states differ, the more often it reads 1. For completely different states it reads 0 and 1 equally often. Repeating the test gives a similarity score, used in quantum machine learning and for comparing quantum "fingerprints" of large files.

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

  1. Prepare the Two States. Load the two states to compare onto Q1 and Q2. Here they are 0 and plus, which overlap by half.
  2. Superpose the Ancilla and Begin the Swap. A Hadamard on the helper (Q0), and the first CNOT of the controlled swap.
  3. Controlled Middle CNOT (Toffoli). The Toffoli performs the middle step of the swap only when the helper is 1.
  4. Finish the Swap and Interfere. The closing CNOT completes the swap, and a Hadamard on the helper makes the swapped and unswapped branches interfere.
  5. 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.