Concept

SWAP Test

Estimates |⟨ψ|φ⟩|² with a controlled-SWAP and one ancilla. Does not measure the data states in Z.

Qubits 3 Cost O(1) Default Same states |0⟩ vs |0⟩

q0 is the probe, q1 is |ψ⟩, q2 is |φ⟩. H on the ancilla, CSWAP (Fredkin) controlled on q0 targeting q1 and q2, then H again. The ancilla is |0⟩ with probability (1 + |⟨ψ|φ⟩|²)/2.

Default comparison is |0⟩ vs |0⟩, so the overlap is 1 and the ancilla is always |0⟩. Orthogonal states give 50/50. |0⟩ vs |+⟩ gives 75% |0⟩ on the ancilla.

How it works

  1. 1
    Prepare

    Default: both data wires stay |0⟩. Orthogonal: X 2. Similar: H 2.

  2. 2
    Probe

    H 0; CSWAP [0,1,2]; H 0.

  3. 3
    Read ancilla

    Measure q0. Do not interpret q1/q2 marginals as the overlap.

Default Qubi (static)
H 0
CSWAP [0,1,2]
H 0

Function form: SwapTest("same")

Circuit
q0q1q2HH
Expected result

State. |000⟩

P(ancilla q0 = 0) = 1. Data wires remain |00⟩.

BasisAmplitudeProbability
|000⟩ 1
100%

Notes

  • Identical states: P(ancilla = 0) = 1.
  • Orthogonal: P(ancilla = 0) = 1/2.
  • |0⟩ vs |+⟩: P(ancilla = 0) = 3/4.

Change parameters

The write-up above is for the default circuit. Use this control to generate other variants and load them in the simulator.