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.[1] and later rediscovered by Harry Buhrman, Richard Cleve, John Watrous, and Ronald de Wolf.
[2] It appears commonly in quantum machine learning, and is a circuit used for proofs-of-concept in implementations of quantum computers.
[3][4] Formally, the swap test takes two input states
and outputs a Bernoulli random variable that is 1 with probability
(where the expressions here use bra–ket notation).
This allows one to, for example, estimate the squared inner product between the two states,
additive error by taking the average over
runs of the swap test.
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.
The state of the system at the beginning of the protocol is
0 , ϕ , ψ ⟩
After the Hadamard gate, the state of the system is
0 , ϕ , ψ ⟩ +
1 , ϕ , ψ ⟩ )
The controlled SWAP gate transforms the state into
0 , ϕ , ψ ⟩ +
1 , ψ , ϕ ⟩ )
The second Hadamard gate results in
0 , ϕ , ψ ⟩ +
1 , ϕ , ψ ⟩ +
0 , ψ , ϕ ⟩ −
The measurement gate on the first qubit ensures that it's 0 with a probability of
, then the probability that 0 is measured is
If the states are equal
, then the probability that 0 is measured is 1.
trials of the swap test using
, the fraction of measurements that are zero is
, one can get arbitrary precision of this value.
Below is the pseudocode for estimating the value of