Loading...
Loading...
Measuring overlap between quantum states
The Swap Test is a fundamental quantum primitive that measures the overlap, or fidelity, between two quantum states without requiring full state tomography. By interfering a control qubit with the swapped states of two registers, the test extracts the squared inner product |\langle \phi | \psi \rangle|^2 through simple projective measurements on the ancilla. This protocol underpins numerous quantum machine learning algorithms, state comparison tasks, and quantum fingerprinting schemes.
The swap test operates on three registers: a single ancilla qubit initialized in |0\rangle, and two n-qubit registers prepared in states |\psi\rangle and |\phi\rangle respectively. The protocol begins by applying a Hadamard gate to the ancilla, placing it in the |+\rangle state. A controlled-SWAP operation is then performed with the ancilla as control, conditionally exchanging the contents of the two n-qubit registers. Finally, a second Hadamard gate is applied to the ancilla before measurement in the computational basis.
The measurement statistics of the ancilla encode the overlap between the two states. When the states are identical, the controlled-SWAP leaves the total state unchanged and the two Hadamard gates cancel, always yielding measurement outcome |0\rangle. When the states are orthogonal, the controlled-SWAP introduces a phase shift that results in equal probabilities for |0\rangle and |1\rangle outcomes. For arbitrary states, the probability of measuring |0\rangle is directly related to the squared overlap |\langle \psi | \phi \rangle|^2.
The swap test requires only one ancilla qubit and n controlled-SWAP gates, making it remarkably resource-efficient compared to full quantum state tomography. However, it is a destructive test in the sense that the joint state of the two registers is altered by the measurement. In applications where the states must be preserved, the test must be performed on copies or the states must be re-prepared after each trial.
Post-Hadamard State
Measurement Probability
Overlap Extraction
In quantum machine learning, the swap test serves as the workhorse for computing kernel functions that measure similarity between quantum-encoded data points. Quantum support vector machines and quantum neural networks frequently employ the swap test to evaluate overlap-based loss functions without explicitly learning the state amplitudes. This is particularly valuable when data is encoded into exponentially large Hilbert spaces through feature maps, as the swap test bypasses the need for classical access to the quantum state vector.
Quantum fingerprinting represents another elegant application, where exponentially long classical strings are encoded into logarithmically sized quantum states. The swap test enables a quantum communication protocol that verifies string equality with exponentially less communication than any classical protocol. This demonstrates a provable quantum advantage in the simultaneous message passing model of communication complexity.
Despite its versatility, the swap test faces practical challenges on NISQ hardware. Controlled-SWAP gates decompose into multiple CNOT gates, and the required coherence across all three registers is substantial. Error mitigation strategies, such as zero-noise extrapolation and symmetry verification, are often necessary to obtain reliable overlap estimates from noisy devices. Furthermore, the test assumes pure state inputs; mixed states require the more resource-intensive destructive swap test or full-state tomography.
Quantum Kernel
Fidelity Bound
Runnable implementations you can copy and experiment with.
Construct a swap test circuit to measure the overlap between two two-qubit states using Qiskit.
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
n = 2
anc = QuantumRegister(1, "anc")
psi_reg = QuantumRegister(n, "psi")
phi_reg = QuantumRegister(n, "phi")
creg = ClassicalRegister(1, "c")
circuit = QuantumCircuit(anc, psi_reg, phi_reg, creg)
# Prepare |phi> with rotations
circuit.rx(0.3, phi_reg[0])
circuit.ry(0.5, phi_reg[1])
# Swap test
circuit.h(anc[0])
for i in range(n):
circuit.cswap(anc[0], psi_reg[i], phi_reg[i])
circuit.h(anc[0])
circuit.measure(anc[0], creg[0])
print(circuit.draw())Estimate state overlap using PennyLane's controlled-SWAP gates and ancilla measurement probabilities.
import pennylane as qml
import numpy as np
n = 2
dev = qml.device("default.qubit", wires=2 * n + 1)
@qml.qnode(dev)
def swap_test():
# Prepare |psi> = |0>^n (default initialization)
# Prepare |phi>
qml.RX(0.3, wires=1)
qml.RY(0.5, wires=2)
# Ancilla in |+>
qml.Hadamard(0)
# Controlled-SWAPs
qml.ctrl(qml.SWAP, control=0)(wires=[1, 3])
qml.ctrl(qml.SWAP, control=0)(wires=[2, 4])
# Final Hadamard
qml.Hadamard(0)
return qml.probs(wires=0)
probs = swap_test()
overlap_squared = 2 * probs[0] - 1
print(f"|<psi|phi>|^2 = {overlap_squared}")