Let's compute these quantum kernels. As introduced, the core idea is to leverage a quantum computer to estimate the similarity between data points xi and xj as reflected in the inner product of their corresponding quantum feature states, ∣ϕ(xi)⟩ and ∣ϕ(xj)⟩. We represent these states using feature map circuits Uϕ(x) acting on an initial ∣0⟩⊗n state: ∣ϕ(x)⟩=Uϕ(x)∣0⟩⊗n. The kernel entry is typically defined as the squared magnitude of this inner product:
k(xi,xj)=∣⟨ϕ(xj)∣ϕ(xi)⟩∣2
Directly calculating this inner product by simulating the state vectors ∣ϕ(xi)⟩ and ∣ϕ(xj)⟩ is only feasible for a very small number of qubits, as the Hilbert space dimension grows exponentially (2n). Quantum computation provides methods to estimate this value efficiently, even when the dimension is large.
Estimating Kernel Entries with Quantum Circuits
There are a few common quantum circuit designs used to estimate k(xi,xj).
1. The Overlap Method (Inverse Circuit Method)
This is arguably the most straightforward and frequently used method, especially in simulations and when integrating with variational algorithms. The core idea is to prepare the state ∣ϕ(xi)⟩ and then apply the inverse of the feature map for xj, denoted Uϕ(xj)†.
The steps are:
Initialization: Start with the n-qubit system in the all-zero state: ∣0⟩⊗n.
Apply First Feature Map: Apply the unitary circuit Uϕ(xi) corresponding to the first data point xi. The state becomes ∣ψ1⟩=Uϕ(xi)∣0⟩⊗n=∣ϕ(xi)⟩.
Apply Inverse Second Feature Map: Apply the inverse (conjugate transpose) circuit Uϕ(xj)† corresponding to the second data point xj. The final state is ∣ψfinal⟩=Uϕ(xj)†Uϕ(xi)∣0⟩⊗n.
Measure: Perform a computational basis measurement on all n qubits.
Estimate Probability: Repeat steps 1-4 many times (let's say Nshots times) and count how many times the outcome is the all-zero state ∣0⟩⊗n. The estimated probability is Pest(∣0⟩⊗n)=NshotsCount(∣0⟩⊗n).
Why does this work? The probability of measuring the all-zero state ∣0⟩⊗n from the final state ∣ψfinal⟩ is given by the Born rule:
Recognizing that ⟨0∣⊗nUϕ(xj)† is the bra ⟨ϕ(xj)∣ and Uϕ(xi)∣0⟩⊗n is the ket ∣ϕ(xi)⟩, we see that:
P(∣0⟩⊗n)=∣⟨ϕ(xj)∣ϕ(xi)⟩∣2=k(xi,xj)
So, the desired kernel value is simply the probability of measuring the all-zero state after running this combined circuit. This method requires a quantum circuit with a depth roughly equivalent to the sum of the depths of Uϕ(xi) and Uϕ(xj).
Here's a diagram illustrating the circuit structure:
Circuit diagram for estimating k(xi,xj)=∣⟨ϕ(xj)∣ϕ(xi)⟩∣2 using the overlap method. The system is initialized, the feature map Uϕ(xi) is applied, followed by the inverse feature map Uϕ(xj)†, and finally all qubits are measured. The probability of the ∣0...0⟩ outcome estimates the kernel entry.
2. The SWAP Test Variant
Another common approach involves an auxiliary qubit (ancilla). While sometimes called the "SWAP Test", the circuit typically used for kernel estimation is slightly simpler than the full SWAP test used for state comparison.
Initialization: Start with n qubits for ∣ϕ(xi)⟩, n qubits for ∣ϕ(xj)⟩, and one ancilla qubit. Initialize to ∣0⟩anc∣0⟩⊗n∣0⟩⊗n.
Prepare States: Apply Uϕ(xi) to the second register and Uϕ(xj) to the third register. State is ∣0⟩anc∣ϕ(xi)⟩∣ϕ(xj)⟩.
Hadamard: Apply a Hadamard gate to the ancilla: 21(∣0⟩+∣1⟩)anc∣ϕ(xi)⟩∣ϕ(xj)⟩.
Controlled-SWAP: Apply a SWAP operation between the second and third registers, controlled by the ancilla qubit. The state becomes 21(∣0⟩anc∣ϕ(xi)⟩∣ϕ(xj)⟩+∣1⟩anc∣ϕ(xj)⟩∣ϕ(xi)⟩).
Hadamard: Apply another Hadamard gate to the ancilla.
Measure Ancilla: Measure the ancilla qubit in the computational basis.
Estimate Probability: Repeat steps 1-6 Nshots times and estimate the probability Pest(∣0⟩anc) of measuring the ancilla in state ∣0⟩.
After the second Hadamard on the ancilla, the coefficient of the ∣0⟩anc component is 21(∣ϕ(xi)⟩∣ϕ(xj)⟩+∣ϕ(xj)⟩∣ϕ(xi)⟩). The probability of measuring ∣0⟩ is the squared norm of this component projected onto the ancilla ∣0⟩ state, which simplifies to:
P(∣0⟩anc)=21(1+∣⟨ϕ(xj)∣ϕ(xi)⟩∣2)
Therefore, the kernel value can be extracted:
k(xi,xj)=∣⟨ϕ(xj)∣ϕ(xi)⟩∣2=2⋅P(∣0⟩anc)−1
This method requires 2n+1 qubits and involves a potentially complex controlled-SWAP operation, which can significantly increase circuit depth and gate count compared to the overlap method, especially if the SWAP needs decomposition into native gates. However, it provides a direct estimate related to the inner product squared.
In practice, the overlap method (inverse circuit) is often preferred due to its reduced qubit requirement and potentially shallower circuits, making it more suitable for near-term quantum devices and simulators.
Statistical Estimation and Sampling Noise
Crucially, both methods yield the kernel value indirectly through probability estimation. Because quantum measurements are inherently probabilistic, we need to execute the relevant circuit multiple times (Nshots) and use the frequency of the target outcome (e.g., ∣0⟩⊗n or ∣0⟩anc) to estimate the true probability.
This introduces statistical or "sampling" noise. The accuracy of our kernel entry estimate kest(xi,xj) depends on Nshots. The standard deviation of the estimate typically scales as O(1/Nshots). Achieving high precision requires a large number of shots, increasing the computational cost.
Constructing the Full Kernel Matrix
A classical kernel-based algorithm, like Support Vector Machines (SVM), requires the entire Gram matrix K, where the entry Kij=k(xi,xj) represents the kernel evaluation between the i-th and j-th data points in the training set {x1,x2,...,xm}.
To construct this m×m matrix using a quantum approach:
Iterate through pairs: Loop through all unique pairs of data points (xi,xj) where 1≤i≤j≤m. Since k(xi,xj)=k(xj,xi) (because ∣⟨ϕ(xj)∣ϕ(xi)⟩∣2=∣⟨ϕ(xi)∣ϕ(xj)⟩∣2), we only need to compute the upper (or lower) triangle including the diagonal. This amounts to m(m+1)/2 unique pairs.
Estimate each entry: For each pair (xi,xj), construct and execute the chosen quantum circuit (e.g., overlap method circuit Uϕ(xj)†Uϕ(xi)) for Nshots times.
Calculate Kij: Compute the kernel value Kij from the measurement statistics (e.g., Kij=Pest(∣0⟩⊗n) for the overlap method).
Populate Matrix: Store the computed value in Kij and Kji.
The total cost involves running ≈m2/2 different quantum circuits, each for Nshots times. This quadratic scaling with the dataset size m, combined with the cost per estimation (Nshots), is a significant factor in the overall runtime of quantum kernel algorithms.
Implementation Notes: Simulators vs. Hardware
How you calculate the kernel matrix depends heavily on the execution backend:
Statevector Simulators: If the number of qubits n is small enough (typically < 30), classical simulators can store the full quantum state vector. In this case, you can simulate the circuits Uϕ(xi) and Uϕ(xj) to obtain the state vectors ∣ϕ(xi)⟩ and ∣ϕ(xj)⟩ directly, and then compute their inner product ⟨ϕ(xj)∣ϕ(xi)⟩ classically. This avoids sampling noise entirely but is limited by classical memory. It's excellent for debugging and small-scale tests.
Qasm Simulators / Real Hardware: For larger qubit counts or when running on actual quantum processors, you must use the circuit-based estimation methods described above (Overlap or SWAP test).
Qasm Simulators: These simulate the probabilistic nature of quantum computation by sampling outcomes, mimicking real hardware but without the physical noise. You need to specify Nshots.
Quantum Hardware: Executing on hardware introduces physical noise (decoherence, gate errors, readout errors) in addition to the inherent sampling noise. The estimated probabilities Pest will be distorted by this noise. Techniques discussed in Chapter 7 (Hardware Considerations and Error Mitigation) become essential for obtaining meaningful results from hardware.
The following pseudocode sketches the process for calculating a single kernel entry using the overlap method on a backend that requires shots:
# Python-like pseudocode
import numpy as np
# Assume existence of a QuantumCircuit builder and Backend runner
# (e.g., from Qiskit, PennyLane, Cirq, etc.)
def build_feature_map_circuit(data_point, num_qubits):
"""Constructs the quantum circuit U_phi(x) for a given data point."""
qc = QuantumCircuit(num_qubits)
# ... Add gates based on data_point and the chosen feature map strategy ...
# Example: ZZFeatureMap, PauliFeatureMap, or custom circuit
# qc.rx(data_point[0], 0)
# qc.ry(data_point[1], 1)
# qc.cz(0, 1)
# ...
return qc
def estimate_kernel_entry_overlap(x_i, x_j, feature_map_builder, num_qubits, backend, num_shots):
"""Estimates k(xi, xj) using the overlap method."""
# 1. Build U_phi(xi)
circuit_i = feature_map_builder(x_i, num_qubits)
# 2. Build U_phi(xj) and get its inverse
circuit_j = feature_map_builder(x_j, num_qubits)
try:
circuit_j_dagger = circuit_j.inverse()
except Exception as e:
print(f"Warning: Could not auto-invert circuit for xj. Ensure builder supports inversion. Error: {e}")
# Or handle inversion manually if needed for the specific feature map
raise e
# 3. Combine circuits: U_phi(xj)^dagger * U_phi(xi)
# Note: Circuit composition order might vary by library (qc1.compose(qc2) vs qc2(qc1))
# Assuming qc1.compose(qc2) applies qc2 first, then qc1
measurement_circuit = circuit_j_dagger.compose(circuit_i)
# 4. Add measurement to computational basis
# Ensure measurement occurs *after* all unitary operations
measurement_circuit.measure_all() # Or measure specific qubits if needed
# 5. Execute on backend
# Transpilation might happen here implicitly or explicitly
job = backend.run(measurement_circuit, shots=num_shots)
result = job.result()
counts = result.get_counts(measurement_circuit)
# 6. Calculate probability P(|0...0>)
zero_string = '0' * num_qubits
prob_zero = counts.get(zero_string, 0) / num_shots
kernel_value = prob_zero
return kernel_value
# --- Example Usage ---
# Define your feature map logic
# def my_feature_map(data_point, num_qubits): ...
# num_qubits = 4
# data_point_1 = np.array([0.1, 0.2, 0.3, 0.4])
# data_point_2 = np.array([0.5, 0.6, 0.7, 0.8])
# backend = Aer.get_backend('qasm_simulator') # Example Qiskit simulator
# num_shots = 8192
# k_12 = estimate_kernel_entry_overlap(
# data_point_1,
# data_point_2,
# my_feature_map,
# num_qubits,
# backend,
# num_shots
# )
# print(f"Estimated kernel value k(x1, x2): {k_12}")
This process forms the computational core of applying quantum kernel methods. Understanding how these entries are estimated, the associated costs, and the difference between simulation and hardware execution is fundamental before exploring specific algorithms like QSVM.
Was this section helpful?
Supervised learning with quantum-enhanced feature spaces, Vojtech Havlíček, Antonio D. Córcoles, Kristan Temme, Aram W. Harrow, Abhinav Kandala, Jerry M. Chow, and Jay M. Gambetta, 2019Nature, Vol. 567 (Nature Portfolio)DOI: 10.1038/s41586-019-0980-2 - A foundational paper introducing a widely used quantum feature map and demonstrating quantum kernel-based classification on near-term quantum hardware, detailing the overlap method for kernel estimation.
Quantum Machine Learning, Maria Schuld, Francesco Petruccione, 2018 (Springer)DOI: 10.1007/978-3-319-96424-9 - A textbook covering the theoretical foundations and algorithms of quantum machine learning, including explanations of quantum kernel methods and their estimation.
Quantum Kernel Estimation, Qiskit Developers, 2025 (IBM Quantum) - An official tutorial from a leading quantum computing framework, illustrating the practical implementation of quantum kernel estimation, including the overlap method, with code examples and explanations.