squlearn.qrc
.QRCRegressor
- class squlearn.qrc.QRCRegressor(encoding_circuit: EncodingCircuitBase, executor: Executor, ml_model: str = 'linear', ml_model_options: dict | None = None, operators: ObservableBase | list[ObservableBase] | str = 'random_paulis', num_operators: int = 100, operator_seed: int = 0, param_ini: ndarray | None = None, param_op_ini: ndarray | None = None, parameter_seed: int | None = 0, caching: bool = True)
Quantum Reservoir Computing for regression.
This class implements a Quantum Reservoir Computing (QRC) framework designed for regression tasks. In QRC, data is encoded into a quantum system—referred to as the quantum reservoir—using an encoding circuit. The state of the quantum reservoir is then measured using a set of quantum operators, often randomly chosen. The measured values, also known as expectation values, are used as features for a classical machine learning model, usually a simple linear regression, to perform the regression.
- Parameters:
encoding_circuit (EncodingCircuitBase) – The encoding circuit to use for encoding the data into the reservoir.
executor (Executor) – Executor instance
ml_model (str) –
The classical machine learning model to use (default: linear). Possible values are:
"mlp"
for a multi-layer perceptron regression model."linear"
for a linear regression model."kernel"
for a kernel ridge regression with a linear kernel.
ml_model_options (dict) – The options for the machine learning model. Default options of the sklearn model are used if None.
operators (Union[ObservableBase, list[ObservableBase], str]) –
Strategy for generating the operators used to measure the quantum reservoir. Possible values are:
"random_paulis"
generates random Pauli operators (default)."single_paulis"
generates single qubit Pauli operators.
Alternatively, a list of ObservableBase objects can be provided.
num_operators (int) – The number of random Pauli operators to generate for
"operators = random_paulis"
(default: 100).operator_seed (int) – The seed for the random operator generation for
"operators = random_paulis"
(default: 0).param_ini (Union[np.ndarray, None]) – The parameters for the encoding circuit.
param_op_ini (Union[np.ndarray, None]) – The initial parameters for the operators.
parameter_seed (Union[int, None]) – The seed for the initial parameter generation if no parameters are given.
caching (bool) – Whether to cache the results of the evaluated expectation values.
See also
squlearn.qrc.QRCClassifier
Quantum Reservoir Computing for Classification.
squlearn.qrc.base_qrc.BaseQRC
Base class for Quantum Reservoir Computing.
Example: Fitting the logarithm function with Quantum Reservoir Computing
import numpy as np from squlearn import Executor from squlearn.encoding_circuit import HubregtsenEncodingCircuit from squlearn.qrc import QRCRegressor from sklearn.model_selection import train_test_split X, y = np.arange(0.1, 0.9, 0.01), np.log(np.arange(0.1, 0.9, 0.01)) X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.33, random_state=42) reg = QRCRegressor(HubregtsenEncodingCircuit(num_qubits=4, num_features=1), Executor(), ml_model="linear", operators="random_paulis", num_operators=300, ) reg.fit(X_train, y_train) y_pred = reg.predict(X_test)
Methods:
- fit(X, y) None
Fit a new Quantum Reservoir Computing model to data.
- Parameters:
X – Input data
y – Labels
- get_metadata_routing()
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
- Returns:
routing – A
MetadataRequest
encapsulating routing information.- Return type:
MetadataRequest
- get_params(deep: bool = True) dict
Returns a dictionary of parameters for the current object.
- Parameters:
deep – If True, includes the parameters from the base class.
- Returns:
A dictionary of parameters for the current object.
- Return type:
dict
- predict(X) ndarray
Predict using the Quantum Reservoir Computing.
- Parameters:
X – The input data.
- Returns:
The predicted values.
- Return type:
np.ndarray
- score(X, y, sample_weight=None)
Return the coefficient of determination of the prediction.
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_fitted
is 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
score
on a regressor usesmultioutput='uniform_average'
from version 0.23 to keep consistent with default value ofr2_score()
. This influences thescore
method of all the multioutput regressors (except forMultiOutputRegressor
).
- set_params(**params) BaseQRC
Sets the hyper-parameters of the QLEM model.
- Parameters:
params (dict) – A dictionary of hyper-parameters to set.
- Returns:
The modified QLEM model.
- Return type:
- set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') QRCRegressor
Request metadata passed to the
score
method.Note that this method is only relevant if
enable_metadata_routing=True
(seesklearn.set_config()
). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True
: metadata is requested, and passed toscore
if 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.
Note
This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a
Pipeline
. Otherwise it has no effect.- Parameters:
sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
sample_weight
parameter inscore
.- Returns:
self – The updated object.
- Return type:
object