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 2.13840528e-03 3.53443849e-03 4.53102793e-01
  1.57275520e-01 8.14031946e-02 1.12098547e-02 9.10194367e-02
  7.59778725e-04 5.56955316e-02]
 [2.13840528e-03 1.00000000e+00 9.75760773e-01 3.72371216e-02
  1.49548798e-01 2.68309328e-01 1.74835824e-05 3.57421533e-05
  1.88282890e-05 3.53022346e-01]
 [3.53443849e-03 9.75760773e-01 1.00000000e+00 5.89513528e-02
  2.17338703e-01 3.68311685e-01 1.92449614e-05 4.64117122e-05
  1.73401498e-05 4.68517120e-01]
 [4.53102793e-01 3.72371216e-02 5.89513528e-02 1.00000000e+00
  7.77950243e-01 5.71364362e-01 6.44838134e-04 5.84031268e-03
  7.19145425e-05 4.62373704e-01]
 [1.57275520e-01 1.49548798e-01 2.17338703e-01 7.77950243e-01
  1.00000000e+00 9.38592547e-01 1.59527467e-04 1.17192333e-03
  2.99597988e-05 8.61811182e-01]
 [8.14031946e-02 2.68309328e-01 3.68311685e-01 5.71364362e-01
  9.38592547e-01 1.00000000e+00 8.77027772e-05 5.45323915e-04
  2.22563146e-05 9.82060953e-01]
 [1.12098547e-02 1.74835824e-05 1.92449614e-05 6.44838134e-04
  1.59527467e-04 8.77027772e-05 1.00000000e+00 6.18461023e-01
  4.96066710e-01 6.58577705e-05]
 [9.10194367e-02 3.57421533e-05 4.64117122e-05 5.84031268e-03
  1.17192333e-03 5.45323915e-04 6.18461023e-01 1.00000000e+00
  1.09754597e-01 3.69273691e-04]
 [7.59778725e-04 1.88282890e-05 1.73401498e-05 7.19145425e-05
  2.99597988e-05 2.22563146e-05 4.96066710e-01 1.09754597e-01
  1.00000000e+00 1.97571005e-05]
 [5.56955316e-02 3.53022346e-01 4.68517120e-01 4.62373704e-01
  8.61811182e-01 9.82060953e-01 6.58577705e-05 3.69273691e-04
  1.97571005e-05 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