|q⟩ Bad Qubits

intermediate · Programming · Quantum Machine Learning (Intro)

Quantum Kernels

Kernel methods are a classical machine-learning idea: instead of operating on raw feature vectors, compare data points through a kernel function k(x,y)k(x, y) that measures similarity in a high-dimensional feature space. Quantum computers can evaluate kernels that are classically hard to compute, by using the quantum state itself as the feature vector.

The quantum kernel

Given a data-encoding unitary Uϕ(x)U_\phi(x) (a feature map), the corresponding quantum state is

ϕ(x)=Uϕ(x)0.|\phi(x)\rangle = U_\phi(x)|0\rangle.

The quantum kernel between two data points xx and yy is the squared overlap of their feature states:

k(x,y)=ϕ(y)ϕ(x)2.k(x, y) = |\langle\phi(y)|\phi(x)\rangle|^2.

Because the inner product lives in a Hilbert space whose dimension grows exponentially with the number of qubits, the feature space is vastly larger than anything a classical computer can represent explicitly.

The overlap circuit

The key insight is that k(x,y)k(x,y) is a measurement probability. Consider the circuit

Uϕ(y)Uϕ(x)0.U_\phi(y)^\dagger \, U_\phi(x) |0\rangle.

The probability that this state measures as 0|0\rangle is

P(0)=0Uϕ(y)Uϕ(x)02=ϕ(y)ϕ(x)2=k(x,y).P(|0\rangle) = |\langle 0|\, U_\phi(y)^\dagger U_\phi(x)\,|0\rangle|^2 = |\langle\phi(y)|\phi(x)\rangle|^2 = k(x, y).

So the recipe is: encode xx, then invert the encoding of yy, then measure. The fraction of shots landing on the all-zero outcome estimates the kernel.

Angle encoding and the cosine kernel

For single-qubit angle encoding, Uϕ(x)=Ry(x)U_\phi(x) = R_y(x), the feature state is

ϕ(x)=cos ⁣x20+sin ⁣x21.|\phi(x)\rangle = \cos\!\tfrac{x}{2}|0\rangle + \sin\!\tfrac{x}{2}|1\rangle.

The inner product is

ϕ(y)ϕ(x)=cos ⁣x2cos ⁣y2+sin ⁣x2sin ⁣y2=cos ⁣xy2,\langle\phi(y)|\phi(x)\rangle = \cos\!\tfrac{x}{2}\cos\!\tfrac{y}{2} + \sin\!\tfrac{x}{2}\sin\!\tfrac{y}{2} = \cos\!\tfrac{x - y}{2},

so the kernel is the squared cosine:

k(x,y)=cos2 ⁣ ⁣xy2.k(x, y) = \cos^2\!\!\tfrac{x - y}{2}.

For x=π/3x = \pi/3 and y=2π/3y = 2\pi/3:

k ⁣(π3,2π3)=cos2 ⁣ ⁣(π/32π/32)=cos2 ⁣ ⁣(π6)=(32)2=34.k\!\left(\frac{\pi}{3},\frac{2\pi}{3}\right) = \cos^2\!\!\left(\frac{\pi/3 - 2\pi/3}{2}\right) = \cos^2\!\!\left(-\frac{\pi}{6}\right) = \left(\frac{\sqrt{3}}{2}\right)^2 = \frac{3}{4}.

The kernel circuit combines both rotations into a single net rotation Ry(π/32π/3)=Ry(π/3)R_y(\pi/3 - 2\pi/3) = R_y(-\pi/3), and P(0)=cos2(π/6)=3/4P(|0\rangle) = \cos^2(\pi/6) = 3/4 as expected.

From a kernel to a classifier

Once a kernel matrix Kij=k(xi,xj)K_{ij} = k(x_i, x_j) is available, any classical kernel method (support vector machine, kernel ridge regression) can use it directly. The quantum computer evaluates the matrix entries; the classical optimizer does the rest.

Try it

Build the overlap circuit for x=π/3x = \pi/3 and y=2π/3y = 2\pi/3. Apply the encoding Ry(π/3)R_y(\pi/3), then the inverse encoding Ry(2π/3)R_y(-2\pi/3), and measure. The grader checks the output distribution: P(0)P(0) should be 3/43/4.

Run your code to see the quantum state.

Sign in on the full site to ask questions and join the discussion.