squlearn.qnn.base_qnn.BaseQNN

class squlearn.qnn.base_qnn.BaseQNN(encoding_circuit: EncodingCircuitBase, operator: ObservableBase | list[ObservableBase], executor: Executor, loss: QNNLossBase, 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, **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.

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, 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

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.

set_fit_request(*, weights: bool | None | str = '$UNCHANGED$') → BaseQNN

Configure whether metadata should be requested to be passed to the fit method.

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 (see sklearn.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 to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • 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:

weights (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for weights parameter in fit.

Returns:

self – The updated object.

Return type:

object

set_params(**params) → BaseQNN

Sets the hyper-parameters of the BaseQNN.

Parameters:

params – Hyper-parameters of the BaseQNN.

Returns:

updated BaseQNN