squlearn.qrc
.QRCClassifier
- class squlearn.qrc.QRCClassifier(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 classification.
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 to perform the classification. As a default a simple classification based on a single perceptron is used.
- 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 calsification model."linear"
for a single layer perceptron."kernel"
for a Support Vector Classifier with a RBF 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.QRCRegressor
Quantum Reservoir Computing for Regression.
squlearn.qrc.base_qrc.BaseQRC
Base class for Quantum Reservoir Computing.
Example: Classification of Moon example with Quantum Reservoir Computing
from squlearn import Executor from squlearn.encoding_circuit import HubregtsenEncodingCircuit from squlearn.qrc import QRCClassifier from sklearn.datasets import make_moons from sklearn.model_selection import train_test_split X, y = make_moons(n_samples=1000, noise=0.2, random_state=42) X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.33, random_state=42) clf = QRCClassifier(HubregtsenEncodingCircuit(num_qubits=4, num_features=2), Executor(), ml_model="linear", operators="random_paulis", num_operators=300, ) clf.fit(X_train, y_train) y_pred = clf.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 mean accuracy on the given test data and labels.
In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.
- Parameters:
X (array-like of shape (n_samples, n_features)) – Test samples.
y (array-like of shape (n_samples,) or (n_samples, n_outputs)) – True labels for X.
sample_weight (array-like of shape (n_samples,), default=None) – Sample weights.
- Returns:
score – Mean accuracy of
self.predict(X)
w.r.t. y.- Return type:
float
- 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$') QRCClassifier
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