A circuit can compare two quantum registers without first learning descriptions of their states. The SWAP test returns a random bit whose statistics encode an overlap. Even when the two registers contain the same state, its meaning depends on whether that state is pure or mixed.
The circuit for two pure inputs
Let and have the same finite-dimensional Hilbert space.
Definition 1.6.1
(Swap operator and its projectors)
The swap operator on is defined by . It is Hermitian and , so and are orthogonal projectors onto the symmetric and antisymmetric subspaces, with .
Definition 1.6.2
(SWAP test)
Let and be normalized, and start an ancilla in . Apply a Hadamard to , then a controlled SWAP of that swaps exactly when , then another to , and finally measure in the computational basis.
Writing for , the state immediately before measurement is
Theorem 1.6.3
(SWAP-test statistics for pure inputs)
With input , the ancilla outcomes have probabilities
Proof
(Tracing the ancilla through the circuit)
The first Hadamard gives . The controlled swap gives , and the second Hadamard gives the displayed state. The two ancilla outcomes correspond to orthogonal branches. Expanding the squared norm of the branch,
The cross term is , which is real. This gives , and .
Proof X-ray
Symmetry is what the ancilla measures
The proof expands a norm, but the structure behind it is the swap operator. Since , the two branches are exactly and , because . Then and the circuit measures symmetric versus antisymmetric components, rather than directly outputting a classical description of either state. Where the hypotheses enter: normalization of fixes , and the identity is the only place the overlap appears. Equal states are symmetric and always give outcome ; the antisymmetric part is nonzero only if the states differ.
Equal pure states up to global phase give ; orthogonal inputs give , not zero. Measurement also generally changes the joint state of conditional on the outcome. A single output does not reveal the exact overlap.
Common mistake
(Outcome 0 does not certify equality)
Because for every pair, an outcome happens at least half the time even for orthogonal states. A single therefore proves nothing; only outcome is conclusive, since it can occur only when the states differ. Evidence for equality builds up only statistically, from many runs.
Worked example 1.6.4
(A numerical comparison of zero and plus)
For and , the squared overlap is , so and . On outcome , the unnormalized branch is and the normalized two-register state is . Thus the output bit is random even though the input states are fixed and known in this example.
Mixed inputs and a limitation
Suppose instead that the two registers are independently prepared in density matrices and , so their joint input is .
Theorem 1.6.5
(SWAP test on independent mixed inputs)
For the product input ,
For an arbitrary joint state the formula is .
Proof
(Linearity and the swap trace)
The ancilla measurement is linear in the input, and for pure product inputs it equals ; by linearity for every joint state , hence . For the product input, use an orthonormal basis and compute
Taking , gives the stated value.
For pure states it reduces to the squared inner product above. The product-input assumption matters: for an arbitrary correlated joint state the probability need not equal the expression in its two marginals.
Worked example 1.6.6
(Two copies of a mixed state)
Let be independent. Then , so . This exceeds , the value for the maximally mixed pair of the next counterexample, because is purer than .
Counterexample mode
Identical mixed states need not pass with certainty
If each independent input is , then and , despite . The quantity is a Hilbert–Schmidt overlap; it is not an equality indicator for mixed states. If the registers instead share the correlated Bell state , each marginal is still but the joint state is symmetric, so .
Estimating the overlap
Repeat the circuit on independently and identically prepared product inputs, with independent runs. Write . If is the fraction of zero outcomes, then is an unbiased estimator of . Since , Chebyshev's inequality gives, for ,
This bound may exceed one and is then uninformative. Finite samples can yield a negative estimate even though the true overlap of density matrices is nonnegative. Hardware errors or dependent repetitions need a different analysis.
Worked example 1.6.7
(How many runs are enough)
To estimate within with failure probability at most , we need , that is . At this the bound guarantees with probability at least . With only the bound equals , which is uninformative.
Exercises
Quick check
(What is for orthogonal pure inputs?)
Set the overlap to zero.
Solution · Answer
.
Quick check
(For two independent copies of , find .)
Use .
Solution · Answer
, so .
Quick check
(For equal pure inputs, can outcome occur in the ideal circuit?)
Which component of the input does outcome detect?
Solution · Answer
No; the antisymmetric component is zero.
Quick check
(Why is replacing a correlated joint input by the product of its marginals unsafe?)
Compare the Bell-state example with .
Solution · Answer
The Bell-state example has the same marginals as but different SWAP statistics: versus .