Loading...
Loading...
Quantum kernels with classical SVM
Quantum Support Vector Machines leverage quantum feature maps to compute kernel functions that may be intractable for classical computers. By estimating kernel entries on a quantum device and feeding them into a classical SVM optimizer, the algorithm achieves a powerful hybrid approach to classification.
Support Vector Machines are a cornerstone of classical machine learning, relying on the kernel trick to implicitly map data into high-dimensional feature spaces where linear separation becomes possible. The choice of kernel function determines the geometry of this feature space and directly impacts classification performance.
In the quantum variant, we replace the classical kernel with a quantum kernel computed by encoding data into quantum states and measuring their overlap. A quantum feature map φ(x) prepares a quantum state |φ(x)⟩ from a classical data point x. The kernel entry between two points is then given by the squared absolute value of the inner product of their encoded quantum states.
For certain feature maps inspired by quantum many-body physics, it has been proven that computing the kernel classically requires exponential resources in the number of qubits, while the quantum computer can estimate each kernel entry in polynomial time. This establishes a genuine quantum advantage for kernel-based learning, provided the dataset exhibits structure that the quantum feature map can exploit.
Quantum kernel definition
SVM decision function
Feature map state
The practical implementation of a quantum SVM follows a clear separation of labor between the quantum and classical subsystems. The classical host generates the training dataset, normalizes the features, and initializes the SVM solver. Whenever the solver requires a kernel value, it dispatches a quantum job to the QPU.
On the quantum side, the kernel estimation circuit typically consists of two consecutive feature map applications. The first encodes x_i, the second encodes x_j either directly or via its adjoint. Measuring the probability of the all-zero state yields the kernel entry. Modern libraries such as Qiskit Machine Learning automate this workflow by providing FidelityQuantumKernel, which uses primitives and state-fidelity routines to construct the full kernel matrix.
Once the kernel matrix is assembled, the classical SVM solver proceeds exactly as in the classical case. The support vectors are identified, the Lagrange multipliers α_i are optimized, and the resulting model can classify new data points by estimating their kernel entries against the stored support vectors. This modularity means that any improvement in quantum hardware or feature map design directly translates into better model performance without rewriting the classical optimization stack.
Kernel matrix element
Dual optimization
Runnable implementations you can copy and experiment with.
This example uses Qiskit Machine Learning's FidelityQuantumKernel together with scikit-learn's SVC. A ZZFeatureMap encodes two-dimensional data into a quantum state, and the kernel matrix is computed on a simulator.
from qiskit.circuit.library import ZZFeatureMap
from qiskit_machine_learning.kernels import FidelityQuantumKernel
from qiskit_machine_learning.state_fidelities import ComputeUncompute
from qiskit.primitives import Sampler
from sklearn.svm import SVC
import numpy as np
feature_map = ZZFeatureMap(feature_dimension=2, reps=2, entanglement="linear")
sampler = Sampler()
fidelity = ComputeUncompute(sampler=sampler)
kernel = FidelityQuantumKernel(feature_map=feature_map, fidelity=fidelity)
train_features = np.array([[0, 0], [1, 1], [0, 1], [1, 0]])
train_labels = np.array([0, 0, 1, 1])
kernel_matrix = kernel.evaluate(x_vec=train_features)
clf = SVC(kernel="precomputed")
clf.fit(kernel_matrix, train_labels)
test_features = np.array([[0.1, 0.1], [0.9, 0.9]])
k_test = kernel.evaluate(x_vec=test_features, y_vec=train_features)
predictions = clf.predict(k_test.T)
print("Predictions:", predictions)This PennyLane example defines a quantum kernel via AngleEmbedding and its adjoint. The probability of the all-zero state serves as the kernel value, which is assembled into a kernel matrix for scikit-learn's SVC.
import pennylane as qml
import pennylane.numpy as np
from sklearn.svm import SVC
n_qubits = 2
dev = qml.device("default.qubit", wires=n_qubits)
@qml.qnode(dev)
def kernel_circuit(x1, x2):
qml.AngleEmbedding(x1, wires=range(n_qubits))
qml.adjoint(qml.AngleEmbedding)(x2, wires=range(n_qubits))
return qml.probs(wires=range(n_qubits))
def kernel(x1, x2):
return kernel_circuit(x1, x2)[0]
X = np.array([[0, 0], [1, 1], [0, 1], [1, 0]], requires_grad=False)
K = np.array([[kernel(xi, xj) for xj in X] for xi in X])
clf = SVC(kernel="precomputed")
clf.fit(K, [0, 0, 1, 1])
print("Training complete. Support vectors:", clf.support_.tolist())