ArticleslgStudy

physics

Swap test

Swap test is a physics topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Swap test rather than just read about it. In short: The swap test is a procedure in quantum computation that is used to check how much two quantum states differ, appearing first in the work of Barenco et al. and later rediscovered by Harry Buhrman, Richard Cleve, John Watrous, and Ronald de Wolf. It appears commonly in quantum machine learning, and is a circuit used for proofs-of-concept in implementations of quantum computers.

Swap test — main illustration
Swap test — illustration

Key takeaways

  • Swap test belongs to physics; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Swap test to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Swap test from memory before moving on to harder problems.

Reference excerpt

The swap test is a procedure in quantum computation that is used to check how much two quantum states differ, appearing first in the work of Barenco et al. and later rediscovered by Harry Buhrman, Richard Cleve, John Watrous, and Ronald de Wolf. It appears commonly in quantum machine learning, and is a circuit used for proofs-of-concept in implementations of quantum computers. Formally, the swap test takes two input states |ϕ⟩ and |ψ⟩ and outputs a Bernoulli random variable that is 1 with probability 12−12|⟨ψ|ϕ⟩|2 (where the expressions here use bra–ket notation). This allows one to, for example, estimate the squared inner product between the two states, |⟨ψ|ϕ⟩|2, to ε additive error by taking the average over O(1ε2) runs of the swap test. This requires O(1ε2) copies of the input states. The squared inner product roughly measures "overlap" between the two states, and can be used in linear-algebraic applications, including clustering quantum states.

Explanation of the circuit Consider two states: |ϕ⟩ and |ψ⟩. The state of the system at the beginning of the protocol is |0,ϕ,ψ⟩. After the Hadamard gate, the state of the system is 12(|0,ϕ,ψ⟩+|1,ϕ,ψ⟩). The controlled SWAP gate transforms the state into 12(|0,ϕ,ψ⟩+|1,ψ,ϕ⟩). The second Hadamard gate results in 12(|0,ϕ,ψ⟩+|1,ϕ,ψ⟩+|0,ψ,ϕ⟩−|1,ψ,ϕ⟩)=12|0⟩(|ϕ,ψ⟩+|ψ,ϕ⟩)+12|1⟩(|ϕ,ψ⟩−|ψ,ϕ⟩) The measurement gate on the first qubit ensures that it's 0 with a probability of P(First qubit=0)=12(⟨ϕ|⟨ψ|+⟨ψ|⟨ϕ|)12(|ϕ⟩|ψ⟩+|ψ⟩|ϕ⟩)=12+12|⟨ψ|ϕ⟩|2 when measured. If ψ and ϕ are orthogonal (|⟨ψ|ϕ⟩|2=0), then the probability that 0 is measured is 12. If the states are equal (|⟨ψ|ϕ⟩|2=1), then the probability that 0 is measured is 1. In general, for P trials of the swap test using P copies of |ϕ⟩ and P copies of |ψ⟩, the fraction of measurements that are zero is 1−1P∑i=1PMi, so by taking P→∞, one can get arbitrary precision of this value. Below is the pseudocode for estimating the value of |⟨ψ|ϕ⟩|2 using P copies of |ψ⟩ and |ϕ⟩:

Inputs P copies each of the n qubits quantum states |ψ⟩ and |ϕ⟩ Output An estimate of |⟨ψ|ϕ⟩|2

for j ranging from 1 to P: initialize an ancilla qubit A in state |0⟩ apply a Hadamard gate to the ancilla qubit A for i ranging from 1 to n: apply CSWAP to ψi and ϕi (the ith qubit of the jth copy of |ψ⟩ and |ϕ⟩), with A as the control qubit apply a Hadamard gate to the ancilla qubit A measure A in the Z basis and record the measurement Mj as either a 0 or 1 compute s=1−2P∑i=1PMi. return s as our estimate of |⟨ψ|ϕ⟩|2

See also Hadamard test

References

Illustrations

Swap test: Circuit implementing the swap test between two states |ϕ⟩ and |ψ⟩
Circuit implementing the swap test between two states |ϕ⟩ and |ψ⟩

Worked examples

Example 1 — a first encounter with Swap test

Start with the simplest possible case. Write down what Swap test claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In physics, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Swap test before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Swap test ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Swap test

In research
Swap test appears in physics research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Swap test in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Swap test is common in secondary-school and first-year university syllabi. It links to neighbouring topics Quantum algorithms, so understanding it makes those chapters shorter.
In everyday life
Look for Swap test outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Swap test” →

How to study Swap test in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Swap test means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Swap test out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Swap test in simple terms?

The swap test is a procedure in quantum computation that is used to check how much two quantum states differ, appearing first in the work of Barenco et al. and later rediscovered by Harry Buhrman, Richard Cleve, John Watrous, and Ronald de Wolf. It appears commonly in quantum machine learning, and…

Why does Swap test matter?

Because it connects several physics ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Swap test?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Swap test.

Tags

  • Quantum algorithms

Keep exploring