squlearn.qnn.base_qnn
.BaseQNN
- class squlearn.qnn.base_qnn.BaseQNN(encoding_circuit: EncodingCircuitBase, operator: ObservableBase | list[ObservableBase], executor: Executor, loss: LossBase, optimizer: OptimizerBase, param_ini: ndarray | None = None, param_op_ini: ndarray | None = None, batch_size: int = None, epochs: int = None, shuffle: bool = None, opt_param_op: bool = True, variance: float | Callable = None, shot_control: ShotControlBase = None, parameter_seed: int | None = 0, caching: bool = True, pretrained: bool = False, callback: Callable | str | None = None, primitive: str | None = None, **kwargs)
Base Class for Quantum Neural Networks.
- Parameters:
encoding_circuit – Parameterized quantum circuit in encoding circuit format
operator – Operator that are used in the expectation value of the QNN. Can be a list for multiple outputs.
executor – Executor instance
optimizer – Optimizer instance
param_ini – Initialization values of the parameters of the PQC
param_op_ini – Initialization values of the cost operator
batch_size – Number of data points in each batch, for SGDMixin optimizers
epochs – Number of epochs of SGD to perform, for SGDMixin optimizers
shuffle – If True, data points get shuffled before each epoch (default: False), for SGDMixin optimizers
opt_param_op – If True, operators parameters get optimized
variance – Variance factor
parameter_seed – Seed for the random number generator for the parameter initialization
caching – If True, the results of the QNN are cached.
pretrained – Set to true if the supplied parameters are already trained.
callback (Union[Callable, str, None], default=None) – A callback for the optimization loop. Can be either a Callable, “pbar” (which uses a
tqdm.tqdm
process bar) or None. If None, the optimizers (default) callback will be used.primitive – The Qiskit primitive that is utilized in the qnn, if a Qiskit backend is used in the executor (not supported for PennyLane backends) Default primitive is the one specified in the executor initialization, if nothing is specified, the estimator will used. Possible values are
"estimator"
or"sampler"
.
- fit(X, y, weights: ndarray = None) None
Fit a new model to data.
This method will reinitialize the models parameters and fit it to the provided data.
- Parameters:
X – array-like or sparse matrix of shape (n_samples, n_features) Input data
y – array-like of shape (n_samples,) Labels
weights – Weights for each data point
- 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
- set_fit_request(*, weights: bool | None | str = '$UNCHANGED$') BaseQNN
Request metadata passed to the
fit
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 tofit
if provided. The request is ignored if metadata is not provided.False
: metadata is not requested and the meta-estimator will not pass it tofit
.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:
weights (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
weights
parameter infit
.- Returns:
self – The updated object.
- Return type:
object