squlearn.kernel.matrix.ProjectedQuantumKernel

class squlearn.kernel.matrix.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

See also

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.matrix import ProjectedQuantumKernel
from squlearn.util import Executor

fm = ChebyshevTower(num_qubits=4, num_features=1, 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 9.37039000e-01 7.45993522e-03 2.64031031e-05
  9.86694741e-05 6.15082666e-05 9.15087431e-01 2.65386260e-01
  3.80990397e-05 6.18786411e-01]
 [9.37039000e-01 1.00000000e+00 1.68358480e-02 2.10478362e-05
  1.83891746e-04 1.06825951e-04 9.98149636e-01 4.36702453e-01
  6.02654557e-05 8.22772938e-01]
 [7.45993522e-03 1.68358480e-02 1.00000000e+00 8.88148150e-05
  1.31069756e-01 7.25466019e-02 1.92679992e-02 2.19857474e-01
  3.34620771e-02 6.39076129e-02]
 [2.64031031e-05 2.10478362e-05 8.88148150e-05 1.00000000e+00
  6.41838377e-03 1.30273302e-02 2.03760543e-05 1.68546444e-05
  2.98136648e-02 1.65699869e-05]
 [9.86694741e-05 1.83891746e-04 1.31069756e-01 6.41838377e-03
  1.00000000e+00 9.52474685e-01 2.05745694e-04 2.84375660e-03
  7.90757301e-01 6.36306418e-04]
 [6.15082666e-05 1.06825951e-04 7.25466019e-02 1.30273302e-02
  9.52474685e-01 1.00000000e+00 1.18215615e-04 1.40939215e-03
  9.32054449e-01 3.35175322e-04]
 [9.15087431e-01 9.98149636e-01 1.92679992e-02 2.03760543e-05
  2.05745694e-04 1.18215615e-04 1.00000000e+00 4.70026560e-01
  6.57024715e-05 8.52817110e-01]
 [2.65386260e-01 4.36702453e-01 2.19857474e-01 1.68546444e-05
  2.84375660e-03 1.40939215e-03 4.70026560e-01 1.00000000e+00
  6.24124870e-04 7.95643135e-01]
 [3.80990397e-05 6.02654557e-05 3.34620771e-02 2.98136648e-02
  7.90757301e-01 9.32054449e-01 6.57024715e-05 6.24124870e-04
  1.00000000e+00 1.64577632e-04]
 [6.18786411e-01 8.22772938e-01 6.39076129e-02 1.65699869e-05
  6.36306418e-04 3.35175322e-04 8.52817110e-01 7.95643135e-01
  1.64577632e-04 1.00000000e+00]]

Example: Change measurement and outer kernel

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

fm = ChebyshevTower(num_qubits=4, num_features=1, 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. Possible values are: dKdx, dKdy, dKdxdx

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