Swap test
In-game article clicks load inline without leaving the challenge.

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 | ϕ ⟩ {\displaystyle |\phi \rangle } and | ψ ⟩ {\displaystyle |\psi \rangle } and outputs a Bernoulli random variable that is 1 with probability 1 2 − 1 2 | ⟨ ψ | ϕ ⟩ | 2 {\displaystyle \textstyle {\frac {1}{2}}-{\frac {1}{2}}{|\langle \psi |\phi \rangle |}^{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 {\displaystyle {|\langle \psi |\phi \rangle |}^{2}}, to ε {\displaystyle \varepsilon } additive error by taking the average over O ( 1 ε 2 ) {\displaystyle O(\textstyle {\frac {1}{\varepsilon ^{2}}})} runs of the swap test. This requires O ( 1 ε 2 ) {\displaystyle O(\textstyle {\frac {1}{\varepsilon ^{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: | ϕ ⟩ {\displaystyle |\phi \rangle } and | ψ ⟩ {\displaystyle |\psi \rangle }. The state of the system at the beginning of the protocol is | 0 , ϕ , ψ ⟩ {\displaystyle |0,\phi ,\psi \rangle }. After the Hadamard gate, the state of the system is 1 2 ( | 0 , ϕ , ψ ⟩ + | 1 , ϕ , ψ ⟩ ) {\displaystyle {\frac {1}{\sqrt {2}}}(|0,\phi ,\psi \rangle +|1,\phi ,\psi \rangle )}. The controlled SWAP gate transforms the state into 1 2 ( | 0 , ϕ , ψ ⟩ + | 1 , ψ , ϕ ⟩ ) {\displaystyle {\frac {1}{\sqrt {2}}}(|0,\phi ,\psi \rangle +|1,\psi ,\phi \rangle )}. The second Hadamard gate results in 1 2 ( | 0 , ϕ , ψ ⟩ + | 1 , ϕ , ψ ⟩ + | 0 , ψ , ϕ ⟩ − | 1 , ψ , ϕ ⟩ ) = 1 2 | 0 ⟩ ( | ϕ , ψ ⟩ + | ψ , ϕ ⟩ ) + 1 2 | 1 ⟩ ( | ϕ , ψ ⟩ − | ψ , ϕ ⟩ ) {\displaystyle {\frac {1}{2}}(|0,\phi ,\psi \rangle +|1,\phi ,\psi \rangle +|0,\psi ,\phi \rangle -|1,\psi ,\phi \rangle )={\frac {1}{2}}|0\rangle (|\phi ,\psi \rangle +|\psi ,\phi \rangle )+{\frac {1}{2}}|1\rangle (|\phi ,\psi \rangle -|\psi ,\phi \rangle )}
The measurement gate on the first qubit ensures that it's 0 with a probability of
P ( First qubit = 0 ) = 1 2 ( ⟨ ϕ | ⟨ ψ | + ⟨ ψ | ⟨ ϕ | ) 1 2 ( | ϕ ⟩ | ψ ⟩ + | ψ ⟩ | ϕ ⟩ ) = 1 2 + 1 2 | ⟨ ψ | ϕ ⟩ | 2 {\displaystyle P({\text{First qubit}}=0)={\frac {1}{2}}{\Big (}\langle \phi |\langle \psi |+\langle \psi |\langle \phi |{\Big )}{\frac {1}{2}}{\Big (}|\phi \rangle |\psi \rangle +|\psi \rangle |\phi \rangle {\Big )}={\frac {1}{2}}+{\frac {1}{2}}{|\langle \psi |\phi \rangle |}^{2}}
when measured. If ψ {\displaystyle \psi } and ϕ {\displaystyle \phi } are orthogonal ( | ⟨ ψ | ϕ ⟩ | 2 = 0 ) {\displaystyle ({|\langle \psi |\phi \rangle |}^{2}=0)}, then the probability that 0 is measured is 1 2 {\displaystyle {\frac {1}{2}}}. If the states are equal ( | ⟨ ψ | ϕ ⟩ | 2 = 1 ) {\displaystyle ({|\langle \psi |\phi \rangle |}^{2}=1)}, then the probability that 0 is measured is 1.
In general, for P {\displaystyle P} trials of the swap test using P {\displaystyle P} copies of | ϕ ⟩ {\displaystyle |\phi \rangle } and P {\displaystyle P} copies of | ψ ⟩ {\displaystyle |\psi \rangle }, the fraction of measurements that are zero is 1 − 1 P ∑ i = 1 P M i {\displaystyle 1-\textstyle {\frac {1}{P}}\textstyle \sum _{i=1}^{P}M_{i}}, so by taking P → ∞ {\displaystyle P\rightarrow \infty }, one can get arbitrary precision of this value.
Below is the pseudocode for estimating the value of | ⟨ ψ | ϕ ⟩ | 2 {\displaystyle |\langle \psi |\phi \rangle |^{2}} using P copies of | ψ ⟩ {\displaystyle |\psi \rangle } and | ϕ ⟩ {\displaystyle |\phi \rangle }: