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 thefit()
-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:
- 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
or2.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
Quantum Fidelity Kernel:
squlearn.kernel.lowlevel_kernel.FidelityKernel
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_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 7.50058241e-03 9.78758544e-01 1.42044262e-01 9.72676130e-01 8.59049974e-01 2.57691427e-04 7.57079558e-03 2.45825930e-01 1.43408873e-01] [7.50058241e-03 1.00000000e+00 1.19970190e-02 1.06573178e-04 4.38355637e-03 2.57974768e-02 3.14632564e-01 9.99991620e-01 2.38849231e-01 3.81317226e-01] [9.78758544e-01 1.19970190e-02 1.00000000e+00 9.70107609e-02 9.06905300e-01 9.42229727e-01 3.87796864e-04 1.21081391e-02 3.33998033e-01 2.04307554e-01] [1.42044262e-01 1.06573178e-04 9.70107609e-02 1.00000000e+00 2.12050659e-01 4.88614304e-02 1.93228142e-05 1.07293789e-04 3.69808080e-03 1.78003227e-03] [9.72676130e-01 4.38355637e-03 9.06905300e-01 2.12050659e-01 1.00000000e+00 7.35347533e-01 1.66239512e-04 4.42469498e-03 1.67120777e-01 9.28839497e-02] [8.59049974e-01 2.57974768e-02 9.42229727e-01 4.88614304e-02 7.35347533e-01 1.00000000e+00 7.97292129e-04 2.60280026e-02 5.15780858e-01 3.44474909e-01] [2.57691427e-04 3.14632564e-01 3.87796864e-04 1.93228142e-05 1.66239512e-04 7.97292129e-04 1.00000000e+00 3.12791321e-01 1.09079401e-02 2.24138469e-02] [7.57079558e-03 9.99991620e-01 1.21081391e-02 1.07293789e-04 4.42469498e-03 2.60280026e-02 3.12791321e-01 1.00000000e+00 2.40387236e-01 3.83382980e-01] [2.45825930e-01 2.38849231e-01 3.33998033e-01 3.69808080e-03 1.67120777e-01 5.15780858e-01 1.09079401e-02 2.40387236e-01 1.00000000e+00 9.49224998e-01] [1.43408873e-01 3.81317226e-01 2.04307554e-01 1.78003227e-03 9.28839497e-02 3.44474909e-01 2.24138469e-02 3.83382980e-01 9.49224998e-01 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_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.
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