The swap test (overlap / fidelity estimator) #
Category: 3-Local (QM-validity).
The swap test opens the ancilla-interferometry algorithm family (beside the
Hadamard-oracle family Deutsch–Jozsa / Simon / Bernstein–Vazirani). One ancilla qubit
interferes two system copies ψ, φ through a controlled swap:
H_anc ∘ cSWAP ∘ H_anc on |0⟩ ⊗ ψ ⊗ φ.
Tracking the ancilla:
H_anc:|0⟩⊗ψ⊗φ ↦ (|0⟩+|1⟩)/√2 ⊗ ψ⊗φ;cSWAP:↦ (|0⟩⊗ψ⊗φ + |1⟩⊗φ⊗ψ)/√2;H_anc:↦ (1/2)[ |0⟩⊗(ψ⊗φ + φ⊗ψ) + |1⟩⊗(ψ⊗φ − φ⊗ψ) ].
The ancilla-0 marginal (swapTestProb0) is the headline:
P(0) = (1/4)‖ψ⊗φ + φ⊗ψ‖² = (1 + |⟨ψ,φ⟩|²)/2 (swap_test_prob),
using the key tensor identity ⟨ψ⊗φ, φ⊗ψ⟩ = ⟨ψ,φ⟩·⟨φ,ψ⟩ = |⟨ψ,φ⟩|². Hence
P(0) = 1 when ψ = φ (swap_test_equal) and P(0) = 1/2 when ⟨ψ,φ⟩ = 0
(swap_test_orthogonal): the test reads off |⟨ψ,φ⟩|², the squared overlap / fidelity.
Model. Combined space EuclideanSpace ℂ (Fin 2 × ι × ι) for an arbitrary Fintype ι
(the system index — any finite dimension). The two system states ψ φ : EuclideanSpace ℂ ι
enter as the product amplitude (a,i,j) ↦ [a=0]·ψ i·φ j (swapInit). The two gates
(hadAnc on the ancilla factor via hadEntry, cSwap swapping the two ι factors on
ancilla 1) are amplitude functions; the two-Hadamard collapse (hadEntry orthogonality)
gives the explicit ancilla-0 amplitude (1/2)(ψ i φ j + φ i ψ j) (swapTest_apply).
Honest scope. This is the exact single-shot output-probability identity — the
inner-product-geometry (QM-validity) statement. The estimation (repeating the test to
resolve |⟨ψ,φ⟩|² to a target precision) is the statistical wrapper, noted but not
formalised here. No tensor caveats beyond the explicit Fin 2 × ι × ι model.
(For ι = Empty the unit hypotheses ‖ψ‖ = 1 are unsatisfiable, so the headlines hold
vacuously there; non-vacuous content needs an inhabited system ι.)
Definitions #
The swap-test circuit H_anc ∘ cSWAP ∘ H_anc on |0⟩ ⊗ ψ ⊗ φ.
Equations
Instances For
The ancilla-0 marginal probability P(0) = ∑_{i,j} ‖swapTest(0,i,j)‖².
Equations
- CSD.Empirical.QM.SwapTest.swapTestProb0 ψ φ = ∑ i : ι, ∑ j : ι, ‖CSD.Empirical.QM.SwapTest.swapTest ψ φ (0, i, j)‖ ^ 2
Instances For
Amplitude lemmas #
H(0,b) = (√2)⁻¹ (the all-zero ancilla row).
After the first Hadamard the ancilla is in a uniform superposition over the
unchanged product: hadAnc (swapInit ψ φ) (a,i,j) = (√2)⁻¹·ψ i·φ j for every a.
After the controlled swap the two pointer branches carry the two orderings:
ancilla 1 swaps i,j.
The ancilla-0 amplitude after the full circuit. The two-Hadamard collapse on the
ancilla (hadEntry orthogonality) leaves the symmetric combination:
swapTest ψ φ (0,i,j) = (1/2)(ψ i φ j + φ i ψ j).
Inner-product infrastructure #
The Euclidean inner product in coordinates: ⟨x,y⟩ = ∑ i, conj (x i) · y i.
z · conj z = ‖z‖² (the Complex.ofReal form, used to keep the cast atom canonical).
The headline #
Swap test (output-probability identity). For unit states ψ, φ, the ancilla-0
marginal probability is (1 + |⟨ψ,φ⟩|²)/2. The cross terms factor through the tensor
identity ⟨ψ⊗φ, φ⊗ψ⟩ = ⟨ψ,φ⟩·⟨φ,ψ⟩ = ‖⟨ψ,φ⟩‖² (conjugate-linearity in the first
argument fixes the order).
Equal states: P(0) = 1 (overlap 1).