squlearn.kernel.lowlevel_kernel.ProjectedQuantumKernel

class squlearn.kernel.lowlevel_kernel.ProjectedQuantumKernel(encoding_circuit: EncodingCircuitBase, executor: Executor, measurement: str | ObservableBase | list = 'XYZ', outer_kernel: str | OuterKernelBase = 'gaussian', initial_parameters: ndarray | None = None, parameter_seed: int | None = 0, regularization: str | None = None, caching: bool = True, **kwargs)

Projected Quantum Kernel for Quantum Kernel Algorithms

The Projected Quantum Kernel embeds classical data into a quantum Hilbert space first and than projects down into a real space by measurements. The real space is than used to evaluate a classical kernel.

The projection is done by evaluating the expectation values of the encoding circuit with respect to given Pauli operators. This is achieved by supplying a list of squlearn.observable objects to the Projected Quantum Kernel. The expectation values are than used as features for the classical kernel, for which the different implementations of scikit-learn’s kernels can be used.

The implementation is based on Ref. [1].

As defaults, a Gaussian outer kernel and the expectation value of all three Pauli matrices \(\{\hat{X},\hat{Y},\hat{Z}\}\) are computed for every qubit.

Parameters:
  • encoding_circuit (EncodingCircuitBase) – Encoding circuit that is evaluated

  • executor (Executor) – Executor object

  • measurement (Union[str, ObservableBase, list]) – Expectation values that are computed from the encoding circuit. Either an operator, a list of operators or a combination of the string values X,``Y``,``Z``, e.g. XYZ

  • outer_kernel (Union[str, OuterKernelBase]) – OuterKernel that is applied to the expectation values. Possible string values are: Gaussian, Matern, ExpSineSquared, RationalQuadratic, DotProduct, PairwiseKernel

  • initial_parameters (np.ndarray) – Initial parameters of the encoding circuit and the operator (if parameterized)

  • parameter_seed (Union[int, None], default=0) – Seed for the random number generator for the parameter initialization, if initial_parameters is None.

  • regularization (Union[str, None], default=None) – Option for choosing different regularization techniques ("thresholding" or "tikhonov") after Ref. [2] for the training kernel matrix, prior to solving the linear system in the fit()-procedure.

  • caching (bool, default=True) – If True, the results of the low-level QNN are cached.

Attributes:

num_qubits

Number of qubits of the encoding circuit and the operators

Type:

int

num_features

Number of features of the encoding circuit

Type:

int

num_parameters

Number of trainable parameters of the encoding circuit

Type:

int

encoding_circuit

Encoding circuit that is evaluated

Type:

EncodingCircuitBase

measurement

Measurements that are performed on the encoding circuit

Type:

Union[str, ObservableBase, list]

outer_kernel

OuterKernel that is applied to the expectation values

Type:

Union[str, OuterKernelBase]

num_hyper_parameters

Number of hyper parameters of the outer kernel

Type:

int

name_hyper_parameters

Names of the hyper parameters of the outer kernel

Type:

List[str]

parameters

Parameters of the encoding circuit and the operator (if parameterized)

Type:

np.ndarray

Outer Kernels are implemented as follows:

\(d(\cdot,\cdot)\) is the Euclidean distance between two vectors.

Gaussian:

\[k(x_i, x_j) = \text{exp}\left(-\gamma |(QNN(x_i)- QNN(x_j)|^2 \right)\]

Keyword Args:

gamma (float):

hyperparameter \(\gamma\) of the Gaussian kernel

Matern:

\[k(x_i, x_j) = \frac{1}{\Gamma(\nu)2^{\nu-1}}\Bigg( \!\frac{\sqrt{2\nu}}{l} d(QNN(x_i) , QNN(x_j))\! \Bigg)^\nu K_\nu\Bigg( \!\frac{\sqrt{2\nu}}{l} d(QNN(x_i) , QNN(x_j))\!\Bigg)\]

Keyword Args:

nu (float):

hyperparameter \(\nu\) of the Matern kernel (Typically 0.5, 1.5 or 2.5)

length_scale (float):

hyperparameter \(l\) of the Matern kernel

ExpSineSquared:

\[k(x_i, x_j) = \text{exp}\left(- \frac{ 2\sin^2(\pi d(QNN(x_i), QNN(x_j))/p) }{ l^ 2} \right)\]

Keyword Args:

periodicity (float):

hyperparameter \(p\) of the ExpSineSquared kernel

length_scale (float):

hyperparameter \(l\) of the ExpSineSquared kernel

RationalQuadratic:

\[k(x_i, x_j) = \left( 1 + \frac{d(QNN(x_i), QNN(x_j))^2 }{ 2\alpha l^2}\right)^{-\alpha}\]

Keyword Args:

alpha (float):

hyperparameter \(\alpha\) of the RationalQuadratic kernel

length_scale (float):

hyperparameter \(l\) of the RationalQuadratic kernel

DotProduct:

\[k(x_i, x_j) = \sigma_0 ^ 2 + x_i \cdot x_j\]

Keyword Args:

sigma_0 (float):

hyperparameter \(\sigma_0\) of the DotProduct kernel

PairwiseKernel:

scikit-learn’s PairwiseKernel is used.

Keyword Args:

gamma (float):

Hyperparameter gamma of the PairwiseKernel kernel, specified by the metric

metric (str):

Metric of the PairwiseKernel kernel, can be linear, additive_chi2, chi2, poly, polynomial, rbf, laplacian, sigmoid, cosine

References

[1] Huang, HY., Broughton, M., Mohseni, M. et al., “Power of data in quantum machine learning”, Nat Commun 12, 2631 (2021).

[2] T. Hubregtsen et al., “Training Quantum Embedding Kernels on Near-Term Quantum Computers”, arXiv:2105.02276v1 (2021).

Example: Calculate a kernel matrix with the Projected Quantum Kernel

import numpy as np
from squlearn.encoding_circuit import ChebyshevTower
from squlearn.kernel.lowlevel_kernel import ProjectedQuantumKernel
from squlearn.util import Executor

fm = ChebyshevTower(num_qubits=4, num_chebyshev=4)
kernel = ProjectedQuantumKernel(encoding_circuit=fm, executor=Executor())
x = np.random.rand(10)
kernel_matrix = kernel.evaluate(x.reshape(-1, 1), x.reshape(-1, 1))
print(kernel_matrix)
[[1.00000000e+00 1.40686864e-01 5.60612084e-03 1.64097510e-05
  6.20880950e-01 9.94844316e-01 7.16588606e-02 1.44363591e-01
  8.66947525e-01 5.80914208e-05]
 [1.40686864e-01 1.00000000e+00 8.60985125e-05 6.17720920e-05
  5.72565182e-01 1.68069269e-01 7.82177327e-04 9.99894558e-01
  3.31410484e-01 1.62339207e-05]
 [5.60612084e-03 8.60985125e-05 1.00000000e+00 8.08252724e-04
  6.26243849e-04 4.44504389e-03 5.11863298e-01 8.80775473e-05
  1.66384756e-03 8.55528024e-02]
 [1.64097510e-05 6.17720920e-05 8.08252724e-04 1.00000000e+00
  2.53935416e-05 1.67453540e-05 8.81801546e-05 6.09436378e-05
  1.95521199e-05 1.22821339e-01]
 [6.20880950e-01 5.72565182e-01 6.26243849e-04 2.53935416e-05
  1.00000000e+00 6.80854942e-01 8.56904794e-03 5.81111951e-01
  9.04620156e-01 2.12991003e-05]
 [9.94844316e-01 1.68069269e-01 4.44504389e-03 1.67453540e-05
  6.80854942e-01 1.00000000e+00 5.84471035e-02 1.72299072e-01
  9.10406431e-01 5.06699743e-05]
 [7.16588606e-02 7.82177327e-04 5.11863298e-01 8.81801546e-05
  8.56904794e-03 5.84471035e-02 1.00000000e+00 8.06964489e-04
  2.33582727e-02 6.89193209e-03]
 [1.44363591e-01 9.99894558e-01 8.80775473e-05 6.09436378e-05
  5.81111951e-01 1.72299072e-01 8.06964489e-04 1.00000000e+00
  3.38206821e-01 1.62070360e-05]
 [8.66947525e-01 3.31410484e-01 1.66384756e-03 1.95521199e-05
  9.04620156e-01 9.10406431e-01 2.33582727e-02 3.38206821e-01
  1.00000000e+00 3.07571796e-05]
 [5.80914208e-05 1.62339207e-05 8.55528024e-02 1.22821339e-01
  2.12991003e-05 5.06699743e-05 6.89193209e-03 1.62070360e-05
  3.07571796e-05 1.00000000e+00]]

Example: Change measurement and outer kernel

import numpy as np
from squlearn.encoding_circuit import ChebyshevTower
from squlearn.kernel.lowlevel_kernel import ProjectedQuantumKernel
from squlearn.util import Executor
from squlearn.observables import CustomObservable
from squlearn.kernel import QKRR

fm = ChebyshevTower(num_qubits=4, num_chebyshev=4)

# Create custom observables
measuments = []
measuments.append(CustomObservable(4,"ZZZZ"))
measuments.append(CustomObservable(4,"YYYY"))
measuments.append(CustomObservable(4,"XXXX"))

# Use Matern Outer kernel with nu=0.5 as a outer kernel hyperparameter
kernel = ProjectedQuantumKernel(encoding_circuit=fm,
                                executor=Executor(),
                                measurement=measuments,
                                outer_kernel="matern",
                                nu=0.5)
ml_method = QKRR(quantum_kernel=kernel)

Methods:

assign_parameters(parameters)

Fix the training parameters of the encoding circuit to numerical values

Parameters:

parameters (np.ndarray) – Array containing numerical values to be assigned to the trainable parameters of the encoding circuit

evaluate(x: ndarray, y: ndarray = None) ndarray

Evaluates the Projected Quantum Kernel for the given data points x and y.

Parameters:
  • x (np.ndarray) – Data points x

  • y (np.ndarray) – Data points y, if None y = x is used

Returns:

The evaluated projected quantum kernel as numpy array

evaluate_derivatives(x: ndarray, y: ndarray = None, values: str | tuple = 'dKdx') dict

Evaluates the Projected Quantum Kernel and its derivatives for the given data points x and y.

Parameters:
  • x (np.ndarray) – Data points x

  • y (np.ndarray) – Data points y, if None y = x is used

  • values (Union[str, tuple]) – Values to evaluate. Can be a string or a tuple of strings.

  • are (Possible values) – dKdx, dKdy, dKdxdx, dKdp

Returns:

Dictionary with the evaluated values

evaluate_pairwise(x: ndarray, y: ndarray = None) float

Computes the quantum kernel matrix.

Parameters:
  • x (np.ndarray) – Vector of training or test data for which the kernel matrix is evaluated

  • y (np.ndarray, default=None) – Vector of training or test data for which the kernel matrix is evaluated

evaluate_qnn(x: ndarray) ndarray

Evaluates the QNN for the given data x.

Parameters:

x (np.ndarray) – Data points x

Returns:

The evaluated output of the QNN as numpy array

evaluate_with_parameters(x: ndarray, y: ndarray, parameters: ndarray) ndarray

Computes the quantum kernel matrix with assigned parameters

Parameters:
  • x (np.ndarray) – Vector of training or test data for which the kernel matrix is evaluated

  • y (np.ndarray) – Vector of training or test data for which the kernel matrix is evaluated

  • parameters (np.ndarray) – Array containing numerical values to be assigned to the trainable parameters of the encoding circuit

get_params(deep: bool = True) dict

Returns hyper-parameters and their values of the Projected Quantum Kernel.

Parameters:

deep (bool) – If True, also the parameters for contained objects are returned (default=True).

Returns:

Dictionary with hyper-parameters and values.

set_params(**params)

Sets value of the Projected Quantum Kernel hyper-parameters.

Parameters:

params – Hyper-parameters and their values, e.g. num_qubits=2