|q⟩ Bad Qubits

advanced · Programming · Quantum Machine Learning (Advanced)

Quantum Kernels in Depth

A quantum kernel measures similarity between data points by embedding them as quantum states and taking their overlap. The classical learner — typically a support-vector machine — never sees the quantum state; it only sees the kernel matrix. The whole quantum content lives in the feature map.

Feature map and kernel

A feature map is a fixed (untrained) circuit that turns a classical vector x\boldsymbol{x} into a state:

Φ:  x    ϕ(x)=UΦ(x)0n.\Phi:\; \boldsymbol{x} \;\longmapsto\; |\phi(\boldsymbol{x})\rangle = U_\Phi(\boldsymbol{x})\,|0\rangle^{\otimes n}.

The kernel is the squared overlap of two such states,

k(x,x)=ϕ(x)ϕ(x)2,k(\boldsymbol{x},\boldsymbol{x}') = \bigl|\langle \phi(\boldsymbol{x}) | \phi(\boldsymbol{x}') \rangle\bigr|^2,

which is a positive-definite similarity measure — exactly what a kernel SVM requires. Havlíček et al. (2019) and Schuld & Killoran (2018) established this feature-space view of quantum models.

Angle encoding

The simplest useful feature map is angle encoding: rotate each qubit by an angle proportional to a feature. With one qubit per feature and a YY-rotation,

UΦ(x)=iRY(2xi),RY(θ)=(cosθ2sinθ2sinθ2cosθ2).U_\Phi(\boldsymbol{x}) = \bigotimes_{i} R_Y(2 x_i), \qquad R_Y(\theta) = \begin{pmatrix} \cos\tfrac{\theta}{2} & -\sin\tfrac{\theta}{2} \\[2pt] \sin\tfrac{\theta}{2} & \cos\tfrac{\theta}{2}\end{pmatrix}.

The factor of 22 is conventional: with RY(2xi)R_Y(2x_i) the qubit's amplitude on 1|1\rangle becomes sin(xi)\sin(x_i), so the feature xix_i controls the rotation angle directly. For a single qubit,

RY(2x)0=cos(x)0+sin(x)1.R_Y(2x)\,|0\rangle = \cos(x)\,|0\rangle + \sin(x)\,|1\rangle.

For a two-feature point x=(x0,x1)\boldsymbol{x}=(x_0,x_1) on two qubits the encoded state is the tensor product

ϕ(x)=(cosx00+sinx01)(cosx10+sinx11).|\phi(\boldsymbol{x})\rangle = \bigl(\cos x_0\,|0\rangle + \sin x_0\,|1\rangle\bigr)\otimes\bigl(\cos x_1\,|0\rangle + \sin x_1\,|1\rangle\bigr).

Computing a kernel entry on hardware

You never have the amplitudes directly. The standard trick is the overlap (inversion) test: run UΦ(x)UΦ(x)U_\Phi(\boldsymbol{x}')^\dagger U_\Phi(\boldsymbol{x}) on 0|0\rangle and measure the probability of returning to 0n|0\rangle^{\otimes n} — that probability is k(x,x)k(\boldsymbol{x},\boldsymbol{x}'). The kernel matrix is filled in by repeating this over all pairs, then handed to a classical SVM.

Try it

Build the angle-encoding feature map state for the two-feature data point x=(0.6, 1.2)\boldsymbol{x} = (0.6,\ 1.2) on two qubits, using RY(2xi)R_Y(2x_i) on qubit ii. The grader checks the full state vector, so the amplitudes must match cos(0.6)cos(1.2), cos(0.6)sin(1.2), sin(0.6)cos(1.2), sin(0.6)sin(1.2)\cos(0.6)\cos(1.2),\ \cos(0.6)\sin(1.2),\ \sin(0.6)\cos(1.2),\ \sin(0.6)\sin(1.2).

Run your code to see the quantum state.

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