Skip to content

Quantum kernels

The last lesson showed that the encoding fixes what a variational model can express. This lesson follows that idea to its end. Drop the trainable gates altogether, and use the quantum computer for one job only: comparing data points with each other. Everything else is done classically.

Many classical learning methods never look at the data directly. They only ask how similar pairs of data points are. The function that answers is called a kernel, written k(x,x′)k(x, x'): large when xx and x′x' are alike, small when they are not.

Given training points x1,…,xmx_1, \dots, x_m with labels yi=±1y_i = \pm 1, a kernel classifier predicts the label of a new point xx from the sign of

g(x)=∑i=1mai yi k(x,xi).g(x) = \sum_{i=1}^{m} a_i\, y_i\, k(x, x_i).

Each training point votes for its own label, and its vote counts for more if it is similar to xx. The weights ai≥0a_i \ge 0 say how much each training point matters. In a support vector machine they are found by solving an optimisation problem that has a single best answer, so there are no bad local minima to get stuck in. That is a real attraction compared with training a variational circuit.

Where do kernels come from? A standard recipe is to map each data point to a vector φ(x)\varphi(x) in some large space, and take the kernel to be an inner product there. The map is called a feature map. The hope is that data which is tangled together in its original form becomes easy to separate in the larger space.

An encoding circuit is a feature map. It sends xx to the state

∣φ(x)⟩=U(x)∣0⋯0⟩,\ket{\varphi(x)} = U(x)\ket{0\cdots0},

a vector in a space of dimension 2n2^n. The natural kernel is the overlap of two such states, as defined in Density matrices:

k(x,x′)=∣⟨φ(x)∣φ(x′)⟩∣2.k(x, x') = |\langle\varphi(x)|\varphi(x')\rangle|^2 .

It is 1 when the two states are the same and 0 when they are orthogonal.

Example. Take one qubit and U(x)=RY(x)U(x) = R_Y(x), so that ∣φ(x)⟩=cos⁡x2∣0⟩+sin⁡x2∣1⟩\ket{\varphi(x)} = \cos\tfrac{x}{2}\ket{0} + \sin\tfrac{x}{2}\ket{1}. Then

⟨φ(x)∣φ(x′)⟩=cos⁡x2cos⁡x′2+sin⁡x2sin⁡x′2=cos⁡x−x′2,k(x,x′)=cos⁡2x−x′2.\langle\varphi(x)|\varphi(x')\rangle = \cos\tfrac{x}{2}\cos\tfrac{x'}{2} + \sin\tfrac{x}{2}\sin\tfrac{x'}{2} = \cos\tfrac{x - x'}{2}, \qquad k(x, x') = \cos^2\tfrac{x - x'}{2}.

Two inputs are similar when their angles are close, and completely dissimilar when they differ by π\pi.

On hardware you cannot read off an inner product. You estimate it like this:

  1. Prepare ∣φ(x)⟩\ket{\varphi(x)} by running U(x)U(x).
  2. Run the inverse of the other circuit, U(x′)†U(x')^\dagger.
  3. Measure every qubit and note whether all of them read 0.

The probability of all zeros is ∣⟨0⋯0∣U(x′)†U(x)∣0⋯0⟩∣2|\bra{0\cdots0}U(x')^\dagger U(x)\ket{0\cdots0}|^2, which is exactly k(x,x′)k(x, x'). If the two states are the same, the second circuit undoes the first and you always get zeros.

Try it with fixed gates. Use the feature map U(x)=RZ(x) HU(x) = R_Z(x)\,H, for which the kernel is again cos⁡2x−x′2\cos^2\tfrac{x - x'}{2}. Up to a global phase, RZ(π2)R_Z(\tfrac{\pi}{2}) is the SS gate and RZ(π4)R_Z(\tfrac{\pi}{4}) is the TT gate.

PuzzleMeasure an overlap

The wire holds ∣φ(π4)⟩\ket{\varphi(\tfrac{\pi}{4})}, made by HH and then TT. Apply the inverse of U(π2)U(\tfrac{\pi}{2}). Then press Measure ×100: the fraction of zeros estimates k(π2,π4)=cos⁡2π8≈0.854k(\tfrac{\pi}{2}, \tfrac{\pi}{4}) = \cos^2\tfrac{\pi}{8} \approx 0.854.

This puzzle needs JavaScript.

Hint

U(π2)U(\tfrac{\pi}{2}) is HH followed by SS. To invert a circuit, apply the inverse of each gate in the opposite order. HH is its own inverse.

Your hundred shots probably did not give exactly 85 zeros. Every kernel value is an estimate, with an error that shrinks like one over the square root of the number of shots. And a training set of mm points needs a value for every pair.

Check

A training set has 100 points. How many kernel values must be estimated on the quantum computer?

Once the table of values, the kernel matrix, has been filled in, the quantum computer’s job is finished. A classical computer finds the weights.

The simulator uses the feature map that applies RY(x)R_Y(x) to each of nn qubits. The overlaps of the individual qubits multiply, so

k(x,x′)=cos⁡2nx−x′2.k(x, x') = \cos^{2n}\tfrac{x - x'}{2}.

There are eight training points on a circle of angles, with labels that alternate in pairs. To keep things simple every weight aia_i is 1.

SimulatorA kernel and its classifier

|φ(x)⟩ = RY(x)|0⟩ on each of n qubits

The curve is the score: the labels of the training points, each weighted by its similarity to x. ● label +1● label −1. A ring marks a point on the wrong side.

Things to try:

  1. One qubit. Only half the points are classified correctly, and the score is nearly zero everywhere. With n=1n = 1 the kernel is 12(1+cos⁡(x−x′))\tfrac12(1 + \cos(x - x')), which contains only frequency 1, while the labels go around the circle twice. This is the limit from the last lesson again.
  2. Two qubits. All eight are right. The kernel now contains frequency 2.
  3. Keep adding qubits. The kernel gets narrower. By n=64n = 64 the matrix is almost the identity: each point is similar to itself and to nothing else. The score has shrunk to narrow bumps around the training points, and in the gaps between the groups the classifier has nothing to say. Go on to n=256n = 256 and the bumps become spikes.
  4. Switch to 100-shot estimates at n=128n = 128 or more. Nearly every off-diagonal entry reads 0.00. The small differences between them, which are all the kernel knows about the data, are invisible at this precision.

What you saw in step 4 is a mild version of a serious problem. Here the kernel narrowed because we chose a feature map that makes it narrow, and rescaling the inputs would undo it.

For feature maps that use entangling gates to spread states widely over many qubits, there is no such easy fix. The space has dimension 2n2^n, and two states picked from all over it are almost orthogonal: their overlap is typically of order 2−n2^{-n}. The kernel values for essentially all pairs of different inputs then crowd around one tiny number. The information is in the differences between them, and those differences are exponentially small, so resolving them takes a number of shots that grows exponentially with the number of qubits. This is called exponential concentration. Too much expressive power makes every pair of points look equally unrelated.

The same effect, seen through gradients instead of kernels, is the subject of the next lesson.

The kernel in the simulator has a one-line formula. Nobody needs a quantum computer to evaluate it. That is true of every feature map that produces unentangled states, and of many entangling ones as well, since circuits with limited entanglement can be simulated classically.

So a quantum kernel is only interesting if it meets two conditions at once:

  • it is hard to compute classically, and
  • it is well matched to the data, so that similar labels go with large kernel values.

The first condition is easy to arrange with circuits that are believed to be hard to simulate, an idea proposed by Havlíček and co-workers in 2019. The second is the difficult one. A kernel being hard to compute says nothing about whether it is useful, and a kernel that concentrates is useless however hard it is.

Both conditions have been met together in a rigorous example. In 2021 Liu, Arunachalam and Temme constructed a classification problem, based on the discrete logarithm, on which a quantum kernel method provably succeeds while no efficient classical learner can, assuming discrete logarithms are hard to compute classically. The data in it was designed for the purpose. It shows that an advantage is possible in principle, and leaves open whether any naturally occurring data has that kind of structure.

One more connection is worth knowing. A variational classifier with a fixed encoding also works in this feature space: its output depends on the data only through ∣φ(x)⟩\ket{\varphi(x)}. Such models and kernel methods are therefore closely related, and limits on one tend to carry over to the other.

Check

Which of these statements is true?

RecallAnswer from memory, then check
LessonFinish this lesson