squlearn.kernel.QKODE
- class squlearn.kernel.QKODE(quantum_kernel: KernelMatrixBase | str, loss: KernelLossBase, optimizer: OptimizerBase, alpha_seed: int = 0, k_train: ndarray = None, dkdx_train: ndarray = None, dkdxdx_train: ndarray = None, **kwargs)
Quantum Kernel Ordinary Differential Equation (QKODE) solver.
This class implements a quantum kernel-based solver for ordinary differential equations (ODEs) using the mixed model regression method as described in Ref. [1].
- Parameters:
quantum_kernel (Union[KernelMatrixBase, str]) – Quantum kernel to be used in the model. If set to “precomputed”, the derivatives of the kernel matrix have to be provided.
loss (KernelLossBase) – Loss function to be used for training the model.
optimizer (OptimizerBase) – Optimizer to be used for minimizing the loss function.
alpha_seed (int, default=0) – Seed for random initialization of dual coefficients.
k_train (np.ndarray) – Precomputed training kernel matrix of shape (n_train, n_train). Required if quantum_kernel is “precomputed”.
dkdx_train (np.ndarray) – Precomputed first derivatives of the training kernel matrix. Required if quantum_kernel is “precomputed”.
dkdxdx_train (np.ndarray) – Precomputed second derivatives of the training kernel matrix. Required if quantum_kernel is “precomputed” and the ODE is of order 2.
**kwargs – Additional keyword arguments to be passed to the base class.
Attributes:
- dual_coeff (np.ndarray) :
Array containing the weight vector in kernel space.
- k_train (np.ndarray) :
Training kernel matrix of shape (n_train, n_train) which is available after calling the fit procedure.
- k_testtrain (np.ndarray) :
Kernel matrix of shape (n_test, n_train) which is evaluated at the predict step.
See also
squlearn.kernel.loss.ODELossLoss function for ODEs.
References
[1]: A. Paine et al., “Quantum kernel methods for solving regression problems and differential equations”, Phys. Rev. A 107, 032428
Methods:
- dump(target: str | IO[bytes]) None
Serializes the model object to a file or file-like object. :param target: The target file path or file-like object where the model will be serialized. :type target: Union[str, IO[bytes]]
- fit(X, y)
Fit the Quantum Kernel ODE model.
- Parameters:
X (np.ndarray) – Samples of data of shape (n_samples, n_features) used for fitting the QKODE model.
y (np.ndarray) – Labels of shape (n_samples,) used for fitting the QKODE model.
- get_metadata_routing()
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
- Returns:
routing – A
MetadataRequestencapsulating routing information.- Return type:
MetadataRequest
- get_params(deep: bool = True) dict
Returns hyperparameters and their values of the QKRR method.
- Parameters:
deep (bool) – If True, also the parameters for contained objects are returned (default=True).
- Returns:
Dictionary with hyperparameters and values.
- classmethod load(source: str | IO[bytes], executor: Executor) T
Deserializes the model object from a file or file-like object, injecting the provided Executor. :param source: The source file path or file-like object from which the model will be deserialized. :type source: Union[str, IO[bytes]] :param executor: The Executor instance to be injected into the deserialized model. :type executor: Executor
- Returns:
The deserialized model object with the injected Executor.
- predict(X: ndarray) ndarray
Predict using the Quantum Kernel Ridge model.
- Parameters:
X (np.ndarray) – Samples of data of shape (n_samples, n_features) on which QKRR model makes predictions. If quantum_kernel == “precomputed” this is instead a precomputed (test-train) kernel matrix of shape (n_samples, n_samples_fitted), where n_samples_fitted is the number of samples used in the fitting.
- Returns:
Returns predicted labels (at X) of shape (n_samples,)
- Return type:
np.ndarray
- score(X, y, sample_weight=None)
Return coefficient of determination on test data.
The coefficient of determination, \(R^2\), is defined as \((1 - \frac{u}{v})\), where \(u\) is the residual sum of squares
((y_true - y_pred)** 2).sum()and \(v\) is the total sum of squares((y_true - y_true.mean()) ** 2).sum(). The best possible score is 1.0 and it can be negative (because the model can be arbitrarily worse). A constant model that always predicts the expected value of y, disregarding the input features, would get a \(R^2\) score of 0.0.- Parameters:
X (array-like of shape (n_samples, n_features)) – Test samples. For some estimators this may be a precomputed kernel matrix or a list of generic objects instead with shape
(n_samples, n_samples_fitted), wheren_samples_fittedis the number of samples used in the fitting for the estimator.y (array-like of shape (n_samples,) or (n_samples, n_outputs)) – True values for X.
sample_weight (array-like of shape (n_samples,), default=None) – Sample weights.
- Returns:
score – \(R^2\) of
self.predict(X)w.r.t. y.- Return type:
float
Notes
The \(R^2\) score used when calling
scoreon a regressor usesmultioutput='uniform_average'from version 0.23 to keep consistent with default value ofr2_score(). This influences thescoremethod of all the multioutput regressors (except forMultiOutputRegressor).
- set_params(**params) None
Sets value of the encoding circuit hyperparameters.
- Parameters:
params – Hyperparameters and their values, e.g.
num_qubits=2.
- set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') QKODE
Configure whether metadata should be requested to be passed to the
scoremethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed toscoreif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it toscore.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
- Parameters:
sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
sample_weightparameter inscore.- Returns:
self – The updated object.
- Return type:
object