Fidelity Quantum Kernel — SWAP Test
What You'll Learn:
- How the SWAP test uses quantum interference to measure state overlap without computing inner products
- Why P(ancilla=0) = (1 + |⟨φ(x)|φ(y)⟩|²) / 2 — the core identity behind fidelity estimation
- When SWAP test kernels are preferable to inversion test or compute-uncompute approaches
- How to verify kernel matrices are valid (PSD, symmetric, unit diagonal)
Level: Advanced | Time: 25 minutes | Qubits: 2n+1 | Framework: PennyLane
Prerequisites
- Quantum Kernel — kernel trick, feature maps, kernel matrices
- Bell State — entanglement, measurement
The Idea
A quantum kernel measures how "similar" two data points are after encoding them into quantum states. The most direct way to measure similarity is fidelity — the squared overlap |⟨φ(x)|φ(y)⟩|² between the encoded states. If the states are identical, fidelity = 1. If orthogonal, fidelity = 0.
The challenge: how do you measure the overlap between two quantum states? You can't just "look" at a quantum state — measurement collapses it. The SWAP test solves this using an elegant interference trick:
- Encode x and y into separate quantum registers
- Use an ancilla qubit to conditionally swap the two registers
- The ancilla's measurement probability encodes the overlap
Think of it like comparing two photographs by overlaying them: if they match perfectly, you see a clear image (high overlap). If they're different, you see interference (low overlap). The SWAP test does this at the quantum level.
How It Works
The SWAP Test Circuit
CODE┌───┐ ┌───┐ ancilla: ┤ H ├───●────●──────────┤ H ├── Measure └───┘ │ │ └───┘ │ │ reg1 q₁: ─φ(x)──×────┼────────────────── │ │ reg1 q₂: ─φ(x)──┼────×────────────────── │ │ reg2 q₃: ─φ(y)──×────┼────────────────── │ │ reg2 q₄: ─φ(y)──┼────×──────────────────
The controlled-SWAP (CSWAP/Fredkin gate) swaps register 1 and register 2 only when the ancilla is |1⟩. After the second Hadamard, the ancilla encodes the overlap.
Step by Step
- Encode data: Register 1 holds |φ(x)⟩, register 2 holds |φ(y)⟩
- Hadamard on ancilla: Creates |+⟩ = (|0⟩ + |1⟩)/√2
- Controlled-SWAP: When ancilla=|1⟩, swap the registers
- Hadamard on ancilla: Interferes the "swapped" and "not-swapped" branches
- Measure ancilla: P(0) = (1 + |⟨φ(x)|φ(y)⟩|²) / 2
The Core Identity
After the circuit, the ancilla state is:
|ψ_ancilla⟩ = ((1 + F)/2)|0⟩ + ((1 - F)/2)|1⟩
where F = |⟨φ(x)|φ(y)⟩|². Therefore:
Fidelity = 2·P(ancilla=0) - 1
Data Encoding
Each feature x_i is encoded via angle encoding with phase:
q_i: ── RY(x_i · π) ── RZ(x_i · π/2) ──
A CNOT between adjacent qubits adds entanglement, allowing the kernel to capture non-linear feature correlations.
The Math
SWAP Test Derivation
Start with the joint state after encoding and Hadamard:
CODE|Ψ⟩ = |+⟩ ⊗ |φ(x)⟩ ⊗ |φ(y)⟩ = 1/√2 (|0⟩|φ(x)⟩|φ(y)⟩ + |1⟩|φ(x)⟩|φ(y)⟩)
After controlled-SWAP:
|Ψ'⟩ = 1/√2 (|0⟩|φ(x)⟩|φ(y)⟩ + |1⟩|φ(y)⟩|φ(x)⟩)
After second Hadamard on ancilla:
CODE|Ψ''⟩ = 1/2 |0⟩(|φ(x)⟩|φ(y)⟩ + |φ(y)⟩|φ(x)⟩) + 1/2 |1⟩(|φ(x)⟩|φ(y)⟩ - |φ(y)⟩|φ(x)⟩)
Probability of measuring ancilla = 0:
CODEP(0) = 1/4 · ||φ(x)⟩|φ(y)⟩ + |φ(y)⟩|φ(x)⟩||² = 1/2 (1 + |⟨φ(x)|φ(y)⟩|²)
Kernel Properties
A valid kernel matrix must be:
- Symmetric: K(x,y) = K(y,x) — guaranteed by fidelity definition
- Positive semi-definite: all eigenvalues ≥ 0 — guaranteed by construction
- Unit diagonal: K(x,x) = 1 — fidelity of a state with itself
- Bounded: 0 ≤ K(x,y) ≤ 1
Expected Output
| Property | Expected |
|---|---|
| K(x,x) | 1.0 (self-fidelity) |
| K(x,y) | [0, 1] (bounded) |
| Symmetry | K = K^T |
| PSD | All eigenvalues ≥ 0 |
| Qubits (n=2) | 5 (2+2+1) |
Running the Circuit
PYTHONfrom circuit import run_circuit, verify_kernel # Compute kernel matrix result = run_circuit(n_samples=10, n_data_qubits=2) print(f"Total qubits: {result['total_qubits']}") print(f"Diagonal mean: {result['diagonal_mean']:.4f}") print(f"Is PSD: {result['is_psd']}") # Verification suite v = verify_kernel() for check in v["checks"]: status = "PASS" if check["passed"] else "FAIL" print(f"[{status}] {check['name']}: {check['detail']}")
Try It Yourself
-
Scale up: Change
n_data_qubits=3and observe the qubit count jump to 7. How does computation time scale? -
Identical data: Pass two copies of the same data point. The fidelity should be exactly 1.0.
-
Orthogonal data: Use x=[0, 0] and y=[1, 1]. Is the fidelity close to 0? Why or why not? (Hint: depends on the encoding.)
-
Compare with inversion test: Implement
|⟨0|U†(y)U(x)|0⟩|²on n qubits (no ancilla) and compare with the SWAP test result. Do they agree?
What's Next
- Quantum Kernel SVM — Use fidelity kernels for classification
- Trainable Kernel — Add learnable parameters to the encoding
- Projected Quantum Kernel — Classical post-processing of quantum measurements
Comparison: Kernel Estimation Methods
| Method | Qubits | Circuit depth | Hardware access | Use case |
|---|---|---|---|---|
| SWAP test | 2n+1 | O(n) | Black-box states | State comparison |
| Inversion test | n | O(d) | Encoding circuit | Most QML |
| Compute-uncompute | n | 2×depth | Encoding circuit | Variational |
Applications
| Domain | Use case |
|---|---|
| State comparison | Comparing quantum states from different sources |
| Entanglement witness | Detecting entanglement via fidelity bounds |
| Quantum ML | Exact kernel computation for SVM/GP |
| Quantum cryptography | Quantum fingerprinting protocols |
References
- Buhrman, H. et al. (2001). "Quantum Fingerprinting." Physical Review Letters 87, 167902. DOI: 10.1103/PhysRevLett.87.167902
- Havlíček, V. et al. (2019). "Supervised learning with quantum-enhanced feature spaces." Nature 567, 209-212. DOI: 10.1038/s41586-019-0980-2
- Schuld, M. & Killoran, N. (2019). "Quantum Machine Learning in Feature Hilbert Spaces." Physical Review Letters 122, 040504. DOI: 10.1103/PhysRevLett.122.040504