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.
Learning from similarity
Section titled “Learning from similarity”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 : large when and are alike, small when they are not.
Given training points with labels , a kernel classifier predicts the label of a new point from the sign of
Each training point votes for its own label, and its vote counts for more if it is similar to . The weights 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 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.
A feature map into state space
Section titled “A feature map into state space”An encoding circuit is a feature map. It sends to the state
a vector in a space of dimension . The natural kernel is the overlap of two such states, as defined in Density matrices:
It is 1 when the two states are the same and 0 when they are orthogonal.
Example. Take one qubit and , so that . Then
Two inputs are similar when their angles are close, and completely dissimilar when they differ by .
Measuring one kernel value
Section titled “Measuring one kernel value”On hardware you cannot read off an inner product. You estimate it like this:
- Prepare by running .
- Run the inverse of the other circuit, .
- Measure every qubit and note whether all of them read 0.
The probability of all zeros is , which is exactly . 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 , for which the kernel is again . Up to a global phase, is the gate and is the gate.
The wire holds , made by and then . Apply the inverse of . Then press Measure ×100: the fraction of zeros estimates .
This puzzle needs JavaScript.
Hint
is followed by . To invert a circuit, apply the inverse of each gate in the opposite order. 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 points needs a value for every pair.
A training set has 100 points. How many kernel values must be estimated on the quantum computer?
There are pairs of different points. The kernel is symmetric and , so those are the only values to measure. The number of circuits grows with the square of the dataset.
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.
A kernel you can watch
Section titled “A kernel you can watch”The simulator uses the feature map that applies to each of qubits. The overlaps of the individual qubits multiply, so
There are eight training points on a circle of angles, with labels that alternate in pairs. To keep things simple every weight is 1.
|φ(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:
- One qubit. Only half the points are classified correctly, and the score is nearly zero everywhere. With the kernel is , which contains only frequency 1, while the labels go around the circle twice. This is the limit from the last lesson again.
- Two qubits. All eight are right. The kernel now contains frequency 2.
- Keep adding qubits. The kernel gets narrower. By 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 and the bumps become spikes.
- Switch to 100-shot estimates at 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.
Concentration
Section titled “Concentration”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 , and two states picked from all over it are almost orthogonal: their overlap is typically of order . 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.
When could a quantum kernel help?
Section titled “When could a quantum kernel help?”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 . Such models and kernel methods are therefore closely related, and limits on one tend to carry over to the other.
Which of these statements is true?
If a classical computer can evaluate the kernel, it can run the whole method. Hardness is necessary but not sufficient: the kernel must also suit the data, and its values must be large enough to measure.
Remember it
Section titled “Remember it”How is a quantum kernel defined from a feature map ?
, the overlap of the two encoded states.
How do you estimate one value on a quantum computer?
Run , then the inverse , and measure. The fraction of runs in which every qubit reads 0 estimates the kernel.
In a quantum kernel method, which part is quantum and which is classical?
The quantum computer estimates the kernel value for each pair of data points. Finding the weights and making predictions is classical.
What is exponential concentration of a quantum kernel?
For very expressive feature maps on many qubits, the kernel values of almost all pairs of inputs crowd around one tiny number. Telling them apart takes exponentially many shots.
What two conditions must a quantum kernel meet to give an advantage?
It must be hard to compute classically, and it must suit the data. Hardness alone is not usefulness.