Part IV · Learning with Amplitudes · Chapter 15
Feature Maps and Quantum Kernels
Chapter 14's kernel trick needs only inner products of feature vectors. So make the feature space a 2ⁿ-dimensional Hilbert space, and the inner product a quantum overlap — and ask, honestly, whether it helps.
Sources: Schuld & Petruccione, Chs. 5–6 · Havlíček et al. (2019) · Liu, Arunachalam & Temme (2021)
Recall the one idea that made nonlinear learning cheap in Chapter 14: a classifier never needs the feature vectors themselves, only their inner products . That is the door quantum computing walks through. A quantum circuit maps a data point to a state in an exponentially large space; the overlap of two such states is a kernel a classical computer may not be able to compute cheaply. Whether that ever wins is a real, open, dataset-by-dataset question — and this chapter answers it the only honest way, by building the kernel and measuring.
What this chapter covers
- 15.1Encoding data. Basis, amplitude, and angle encoding — how a classical vector becomes a quantum state.
- 15.2The feature map. The circuit U_φ(x) that lifts x into Hilbert space; the ZZ map of Havlíček et al.
- 15.3The quantum kernel. k(x,x′) = |⟨φ(x)|φ(x′)⟩|² — a fidelity, computed three independent ways (plus a closed form) that must agree.
- 15.4Classifying. The kernel trick carries over from Chapter 14; the solver is new — kernel ridge (LS-SVM), one closed-form solve of (K+λI)α = y.
- 15.5Any advantage?. The honest scorecard: no generic win; advantage is dataset-dependent and often dequantized.
- 15.6The Lab. A ZZ-feature-map kernel classifier in Rust, refereed against the classical RBF baseline.
15.1Encoding data into a quantum state
F · FormalismTo let a quantum computer touch classical data, we must first turn a data vector into a quantum state. Three encodings recur. Basis encoding writes a bitstring directly into computational-basis qubits — faithful but wasteful. Amplitude encoding packs real numbers into the amplitudes of qubits — exponentially compact, but preparing such a state is itself expensive. Angle encoding — the one we use — turns each feature into a rotation angle, or : cheap, shallow, and hardware-friendly.
Whatever the encoding, the result is a feature map , a function from data space into the Hilbert space of Chapter 1. The learning happens there.
15.2The feature map circuit
F · FormalismC · ConceptsA feature map is prepared by an encoding circuit acting on the all-zeros state, . We use the ZZ feature map of Havlíček et al.: a layer of Hadamards puts every qubit in superposition, single-qubit rotations imprint the data, and a two-qubit entangling block imprints the products — a genuinely nonlinear, entangling map. Repeating the block deepens the map.
15.3The quantum kernel
F · FormalismC · ConceptsWith feature states in hand, the kernel is their overlap — a fidelity (Chapter 4) between the two encoded states:
It is 1 when and shrinks as the feature states grow orthogonal. On real hardware you cannot read amplitudes, so you estimate the overlap by the compute–uncompute test: prepare , apply , and measure the probability of returning to , which is exactly . Move two points and watch the kernel respond:
Two data points, encoded into 2-qubit feature states by the ZZ map, then overlapped. The bar is the quantum kernel k(x,x′) = |⟨φ(x)|φ(x′)⟩|².
point x
point x′
Self-overlap k(x,x) = 1.000 (always 1). Nearby points give large kernels; the ZZ term makes the similarity nonlinear in x.
Equation (15.1) is a valid kernel — symmetric and positive semidefinite — because it is the Hilbert–Schmidt inner product of the projectors . That guarantee is what lets us drop it straight into a classical kernel machine.
15.4Classifying with the quantum kernel
F · FormalismP · PracticeWhat carries over from Chapter 14 is the kernel trick — a classifier that touches the data only through the Gram matrix — not the solver. Chapter 14 trained kernel logistic regression: a cross-entropy loss with a bias term, minimized by gradient descent on the dual weights. Here we pair the quantum kernel with the simplest kernel machine of all, least-squares kernel ridge (LS-SVM): the same representer form , but a squared loss, no bias, and a closed-form solution. Minimize the regularized squared error over the dual weights,
set the gradient to zero, and the kernel matrix factors out of the stationarity condition:
which is satisfied by solving the single linear system . Because is positive semidefinite and , the matrix is symmetric positive definite: one Cholesky solve yields , and the prediction is . The only quantum ingredient is that each is now an overlap of feature states. The lab does exactly this and reports the accuracy beside a classical radial-basis-function baseline trained by the same solve.
15.5Is there any advantage?
F · FormalismHere is the honest scorecard, and it matters. A quantum feature map gives a kernel that might be classically hard to estimate, and on carefully constructed datasets a provable separation exists. But there is no generic advantage. For most natural data a classical kernel does just as well, and Huang et al. (2021) showed that when the quantum kernel can be estimated well from data, a classical method can often match it. Advantage must be argued case by case — never assumed because the word “quantum” appears. The lab's own result is starker still: on its dataset the quantum kernel outright loses to the classical RBF baseline out of sample — and a referee gates that sentence, failing the lab if it ever stops being true.
15.6The Lab — a quantum kernel classifier
P · PracticeThe lab builds the ZZ feature map and computes the kernel three independent ways — directly from the statevectors, by the compute–uncompute overlap, and as the density-matrix trace from an explicitly built unitary — plus a pen-and-paper closed form at one repetition. All routes must agree to machine precision:
1/// The gate list for U_φ(x) on `n` qubits with `reps` repetitions:2/// [ H^⊗n , RZ(2 x_i)_i , { CNOT_{ij}, RZ(2(π−x_i)(π−x_j))_j, CNOT_{ij} }_{i<j} ] × reps.3fn feature_ops(x: &[f64], n: usize, reps: usize) -> Vec<Op> {4 let mut ops = Vec::new();5 for _ in 0..reps {6 for q in 0..n {7 ops.push(Op::H(q));8 }9 for q in 0..n {10 ops.push(Op::Rz(2.0 * x[q], q));11 }12 for i in 0..n {13 for j in (i + 1)..n {14 let phi = 2.0 * (PI - x[i]) * (PI - x[j]);15 ops.push(Op::Cx(i, j));16 ops.push(Op::Rz(phi, j));17 ops.push(Op::Cx(i, j));18 }19 }20 }21 ops22}
1/// DIRECT path: k = |⟨φ(x_i)|φ(x_j)⟩|² from the two statevectors.2fn kernel_direct(psi_i: &[C], psi_j: &[C]) -> f64 {3 let mut overlap = C::new(0.0, 0.0);4 for k in 0..psi_i.len() {5 overlap += psi_i[k].conj() * psi_j[k];6 }7 overlap.norm_sqr()8}910/// COMPUTE–UNCOMPUTE path: prepare U_φ(x_i)|0⟩, apply U_φ(x_j)†, read the11/// probability of the all-zeros outcome. Equals |⟨φ(x_j)|φ(x_i)⟩|² = k(x_i,x_j).12fn kernel_uncompute(xi: &[f64], xj: &[f64], n: usize, reps: usize) -> f64 {13 let mut s = feature_state(xi, n, reps);14 apply_feature_dagger(&mut s, xj, n, reps);15 s[0].norm_sqr()16}
10 referees then check that the kernel is a valid Gram matrix (symmetric across independent code paths, positive semidefinite, unit diagonal), that every kernel route agrees with every other to the tolerances listed in the panel, that the feature encoding is unitary, that the simulator reproduces the closed form of §15.3, and that the classifier clears an honest accuracy floor — with the quantum-vs-RBF verdict gated by a referee of its own. Everything below is read live from the lab's JSON:
cargo run --release in Rust-QML/ch15-qkernel)Run it with cargo run --release in Rust-QML/ch15-qkernel. The feature map is fixed here; the next chapter makes the circuit trainable and confronts what happens when you try to learn its parameters at scale.
15.7Exercises
1. (F) Prove that is a positive-semidefinite kernel. (Hint: write it as with .)
2. (C) In the kernel explorer, find two different points whose kernel is nearly 1. What does that say about the feature map's injectivity?
3. (F) Why is the compute–uncompute probability of the all-zeros outcome equal to ? Write out the amplitude of .
4. (P) Replace the ZZ entangler with a plain product (no gates) so the feature map factorizes. Show the kernel becomes a product of single-qubit kernels — and that it loses expressive power.
5. (P, hard) Construct a small dataset on which the quantum kernel beats the RBF kernel out-of-sample, then perturb it until the advantage vanishes. What structural feature of the data did the quantum kernel exploit?
The bridge → Chapter 16: Training Quantum Models
Where you stand. You can encode data into a quantum feature state, compute the quantum kernel k(x,x′)=|⟨φ(x)|φ(x′)⟩|² three agreeing ways, and classify with it — with an honest read on when it helps (here it loses outright to a classical RBF baseline).
The open question. The kernel approach freezes the circuit. The other route to quantum learning is to make the circuit's gates trainable parameters and optimize them end-to-end. How do you take the gradient of a quantum circuit — and what goes wrong when you scale it up?
What comes next. We build a variational quantum classifier, differentiate it exactly with the parameter-shift rule, and then meet the obstacle that haunts the whole field: barren plateaus, where the gradient's variance vanishes exponentially in the number of qubits and training grinds to a halt.