FidelityQuantumKernel¶
- class FidelityQuantumKernel(*, feature_map=None, fidelity=None, enforce_psd=True, evaluate_duplicates='off_diagonal')[source]¶
Bases:
BaseKernel
An implementation of the quantum kernel interface based on the
BaseStateFidelity
algorithm.The general task of machine learning is to find and study patterns in data. For many algorithms, the datapoints are better understood in a higher dimensional feature space, through the use of a kernel function:
\[K(x, y) = \langle f(x), f(y)\rangle.\]Here K is the kernel function, x, y are n dimensional inputs. f is a map from n-dimension to m-dimension space. \(\langle x, y \rangle\) denotes the dot product. Usually m is much larger than n.
The quantum kernel algorithm calculates a kernel matrix, given datapoints x and y and feature map f, all of n dimension. This kernel matrix can then be used in classical machine learning algorithms such as support vector classification, spectral clustering or ridge regression.
Here, the kernel function is defined as the overlap of two quantum states defined by a parametrized quantum circuit (called feature map):
\[K(x,y) = |\langle \phi(x) | \phi(y) \rangle|^2\]- Parameters:
feature_map (QuantumCircuit | None) -- Parameterized circuit to be used as the feature map. If
None
is given,ZZFeatureMap
is used with two qubits. If there's a mismatch in the number of qubits of the feature map and the number of features in the dataset, then the kernel will try to adjust the feature map to reflect the number of features.fidelity (BaseStateFidelity | None) -- An instance of the
BaseStateFidelity
primitive to be used to compute fidelity between states. Default isComputeUncompute
which is created on top of the reference sampler defined bySampler
.enforce_psd (bool) -- Project to the closest positive semidefinite matrix if
x = y
. DefaultTrue
.evaluate_duplicates (str) --
Defines a strategy how kernel matrix elements are evaluated if duplicate samples are found. Possible values are:
all
means that all kernel matrix elements are evaluated, even the diagonal ones when training. This may introduce additional noise in the matrix.off_diagonal
when training the matrix diagonal is set to 1, the rest elements are fully evaluated, e.g., for two identical samples in the dataset. When inferring, all elements are evaluated. This is the default value.none
when training the diagonal is set to 1 and if two identical samples are found in the dataset the corresponding matrix element is set to 1. When inferring, matrix elements for identical samples are set to 1.
- Raises:
ValueError -- When unsupported value is passed to evaluate_duplicates.
Attributes
Returns the strategy used by this kernel to evaluate kernel matrix elements if duplicate samples are found.
Returns the fidelity primitive used by this kernel.
Methods
evaluate
(x_vec[, y_vec])Construct kernel matrix for given data.