Skip to content
Quantum ComputingAN INTERACTIVE OPEN TEXTBOOK
charithj.com.au

The swap test

Estimate the squared overlap of two pure quantum states with an interference experiment.

Core18 min lessonDr. Charith Jayasekara↓ Chapter PDF
By the end, you’ll be able to
  • Relate ancilla statistics to pure-state overlap.
  • Work an overlap calculation from the three-wire circuit.
  • Explain how the interpretation changes for mixed states.

Before you begin: Tensor products, controlled gates, inner products and repeated measurements.

Suppose two programs prepare states ∣ψ⟩|\psi\rangle and ∣φ⟩|\varphi\rangle. We want to know how similar the preparations are without first reconstructing every amplitude. The swap test estimates their squared overlap,

F=∣⟨ψ∣φ⟩∣2.F=|\langle\psi|\varphi\rangle|^2.

For normalised pure states, FF lies between zero and one. It equals one when the states differ only by an overall phase, and zero for orthogonal states. Each test consumes fresh preparations, so the ability to repeatedly prepare both states is part of the input model.

Use one ancilla and two equal-size target registers. Prepare the ancilla in ∣0⟩|0\rangle, apply HH, apply a controlled-SWAP between the targets, apply another HH to the ancilla and measure it. The controlled-SWAP exchanges the target registers only in the ancilla’s 1 branch.

Before the last HH, the joint state is

∣0⟩∣ψ⟩∣φ⟩+∣1⟩∣φ⟩∣ψ⟩2.\frac{|0\rangle|\psi\rangle|\varphi\rangle +|1\rangle|\varphi\rangle|\psi\rangle}{\sqrt2}.

The two routes interfere when the final HH recombines them. Their overlap is

⟨ψ∣φ⟩⟨φ∣ψ⟩=∣⟨ψ∣φ⟩∣2.\langle\psi|\varphi\rangle\langle\varphi|\psi\rangle =|\langle\psi|\varphi\rangle|^2.

Consequently,

P(0)=1+F2,P(1)=1−F2.P(0)=\frac{1+F}{2},\qquad P(1)=\frac{1-F}{2}.

The probability of zero ranges from one half to one. Even two orthogonal states produce zero half of the time. One zero outcome therefore gives weak evidence by itself; repeated trials supply the estimate.

Swap test: interference reveals squared overlapThree-register circuit with H on ancilla, controlled SWAP of two target registers, H and ancilla measurement. Plot shows ancilla-zero probability decreasing from one to one half as the angle between the example pure states grows from zero to pi over two. Swap test: interference reveals squared overlap ancilla |0⟩ |ψ⟩ |φ⟩ H H |ψ⟩ = |0⟩, |φ⟩ = cos θ |0⟩ + sin θ |1⟩ 0.5 0.75 1 0 π/6 π/4 π/3 π/2 P(0) = (1 + |⟨ψ|φ⟩|²)/2. Orthogonal inputs still give P(0) = ½.
Controlled-SWAP circuit and exact ancilla-zero probabilities for a rotating target state.

The controlled-SWAP appears as a control dot joined to two crossed symbols. The plot uses ∣ψ⟩=∣0⟩|\psi\rangle=|0\rangle and ∣φ⟩=cos⁡θ∣0⟩+sin⁡θ∣1⟩|\varphi\rangle=\cos\theta|0\rangle+\sin\theta|1\rangle. Its minimum probability is one half, making the baseline visible.

Checkpoint. Orthogonal inputs give P(0)=1/2P(0)=1/2. Does that mean they overlap halfway? No. The zero-probability formula has a one-half baseline; subtract it before interpreting the overlap.

Take

∣ψ⟩=∣0⟩,∣φ⟩=12∣0⟩+32∣1⟩.|\psi\rangle=|0\rangle,\qquad |\varphi\rangle=\frac12|0\rangle+\frac{\sqrt3}{2}|1\rangle.

Their inner product is 1/21/2, its squared magnitude is 1/41/4, and the ancilla’s zero probability is 5/8=0.6255/8=0.625. If a run of 800 tests gives 500 zeros, then

F^=2500800−1=0.25.\hat F=2\frac{500}{800}-1=0.25.

This is an illustrative record consistent with the ideal value. Its approximate standard error is 20.625(0.375)/800≈0.0342\sqrt{0.625(0.375)/800}\approx0.034. Sampling error concerns uncertainty about the observed circuit’s statistics; noise in preparation, SWAP operations or readout can change those statistics.

Freely vary θ\theta in the plotted family and inspect both FF and P(0)P(0). Then compare ∣+⟩|+\rangle with ∣−⟩|-\rangle. Both states give 50/50 in the Z basis, but their overlap is zero, so the ideal swap test gives ancilla zero half the time. A single fixed-basis histogram can therefore miss a large state difference.

Next compare ∣ψ⟩|\psi\rangle with −∣ψ⟩-|\psi\rangle. The test gives zero with certainty because an overall phase describes the same physical pure state. Compare that with changing only one component’s sign: the resulting relative phase can change the overlap and the measured result.

For separate mixed-state inputs ρ\rho and σ\sigma, the same experiment measures

2P(0)−1=Tr⁡(ρσ).2P(0)-1=\operatorname{Tr}(\rho\sigma).

This quantity is the Hilbert–Schmidt overlap. It is generally different from the usual mixed-state fidelity. For example, two identical maximally mixed qubits have ρ=σ=I/2\rho=\sigma=I/2 and Tr⁡(ρσ)=1/2\operatorname{Tr}(\rho\sigma)=1/2, so the test gives P(0)=3/4P(0)=3/4, even though the input density matrices are identical. That example clarifies why the pure-state assumption matters.

  1. For inputs ∣0⟩|0\rangle and ∣+⟩|+\rangle, find FF and P(0)P(0).

  2. With orthogonal pure inputs, what is the chance of ten independent zero outcomes?

  3. What resources grow with register size?

Worked answers
  1. F=1/2F=1/2 and P(0)=3/4P(0)=3/4.

  2. (1/2)10=1/1024(1/2)^{10}=1/1024. A string of zeros supports a similarity claim statistically; finite data do not prove equality.

  3. both prepared registers and the controlled register swap, which expands into multiple physical gates and may require routing.

PAUSE & THINK

A swap test has ideal P(0)=0.9 for pure inputs. What is their squared overlap?

Choose your answer

The swap test compares preparations by interfering the unchanged and exchanged register orders. Always state the input-state assumption and translate the ancilla probability into the intended overlap.

Buhrman, Cleve, Watrous and de Wolf, quantum fingerprinting and the comparison test; Aharonov, Jones and Landau, ancilla interference framework. The examples, mixed-state calculation and plot data above are original.

ONE IDEA FURTHER

Ready for the next step?

Your progress stays in this browser.

View progress & resume a chapter →