paddle_quantum.model
General model templates for Quantum Neural Network
- paddle_quantum.model.reset_settings()
Set the current
rcParamsto Default settings.
- paddle_quantum.model.random_batch(data, label, batch_size=None)
Randomly return a data batch from dataset and label set
- Parameters:
data (Iterable) – dataset
label (Iterable) – label set
batch_size (int | None) – size of data batch. Defaults to be half of the data size.
- Returns:
a random data batch
- Return type:
Tuple[List, List]
- class paddle_quantum.model.NullScheduler(learning_rate=0.1, last_epoch=-1, verbose=False)
Bases:
LRSchedulerAn empty scheduler class, for users who expect no schedulers in Model. Can be activated by command
from paddle_quantum.model import rcParams rcParams['scheduler'] = None
- get_lr()
- class paddle_quantum.model.Model(network, name='Model')
Bases:
objectGeneral template for models of QNN
- Parameters:
network (Layer) – a quantum neural network
name (str) – name of this model. Default to be
"Model"
- clear_data()
Clear the current data
- parameters()
Return the parameters of this network
- prepare(loss_fcn, metric_fcn=None, metric_name=None)
General function setup for QNN
- Parameters:
loss_fcn (Callable[[State | Circuit | Sequential, Any], Any]) – loss function for the QNN
metric_fcn (Callable[[Circuit | Sequential], float] | None) – metric function for the QNN, which does not mess with the training process. Defaults to be
Nonemetric_name (str | None) – name of the metric function. Defaults to be
None
- Raises:
ValueError – the output datatype of metric function must be float
Note
The prepare function will also take the settings of
rcParams
- check_prepared()
Assert whether Model is prepared for training
- train(loss_generator=None)
General template for QNN training
- Parameters:
loss_generator (Callable[[Any], Any] | None) – loss generator of the QNN, with
Model.networkas input. Defaults toNonei.e.Model.prepare (use the loss function defined in) –
- Returns:
a list of loss values
a list of metric values if a metric function is given
- Return type:
contains the following elements
- evaluate(loss_generator=None)
General template for QNN evaluation
- Parameters:
loss_generator (Callable[[Any], Any] | None) – loss generator of the QNN, with
Model.networkas input. Defaults toNonei.e.Model.prepare (use the loss function defined in) –
- Returns:
a loss value
a metric value if a metric function is given
- Return type:
contains the following elements
- fit(train_data, train_label, test_data, test_label)
General template for QNN data fitting
- Parameters:
train_data (Iterable) – data of the train set
train_label (Iterable) – label of the train set
test_data (Iterable) – data of the test set
test_label (Iterable) – label of the test set
- plot(include_metric, apply_log, has_epoch)
Plot the trained data
- Parameters:
include_metric (bool) – whether include the metric data
apply_log (bool) – whether apply the log function on the data
has_epoch (bool) – whether the data list is composed by data in several epochs
- class paddle_quantum.model.OptModel(circuit, name='OptModel')
Bases:
ModelClass for optimization-based QNN model
- Parameters:
circuit (Circuit) – a
Circuitinstance ready to be optimizedname (str) – name of this model. Default to be
"OptModel"
- prepare(loss_fcn, metric_fcn=None, metric_name=None, *loss_args)
Prepare and check the function setup for optimized-based QNN
- Parameters:
loss_fcn (Callable[[Circuit, Any], Tensor]) – loss function for the QNN
metric_fcn (Callable[[Circuit | Sequential], float] | None) – metric function for the QNN, which does not mess with the training process. Defaults to be
Nonemetric_name (str | None) – name of the metric function. Defaults to be
Noneloss_args (Any) – function arguments for loss_fcn other than QNN input
- Raises:
ValueError – the output datatype of loss function must be paddle.Tensor
Note
The prepare function will also take the settings of
rcParams
- optimize()
Optimize the circuit in terms of the loss function
- Returns:
a list of loss values
a list of metric values if a metric function is given
- Return type:
contains the following elements
- evaluate()
Evaluate the loss and metric value of the current QNN
- Returns:
a loss value
a metric value if a metric function is given
- Return type:
contains the following elements
- fit()
- Raises:
NotImplementedError – Optimization model does not have fit function
- plot(include_metric=True, apply_log=False)
Plot the loss (and metric) data
- Parameters:
include_metric (bool) – include the metric data. Defaults to be
True.apply_log (bool) – whether apply the log function on the data. Defaults to be
False.
- class paddle_quantum.model.LearningModel(circuit, name='LearningModel')
Bases:
ModelClass for learning-based QNN model
- Parameters:
circuit (Circuit) – a
Circuitinstance ready to be trainedname (str) – name of this model. Default to be
"LearningModel"
- prepare(loss_fcn, metric_fcn=None, metric_name=None, *loss_args)
Prepare and check the function setup for learning-based QNN
- Parameters:
loss_fcn (Callable[[State, Any, Any], Any]) – loss function for the output data of QNN
metric_fcn (Callable[[Circuit | Sequential], float] | None) – metric function for the QNN, which does not mess with the training process. Defaults to be
Nonemetric_name (str | None) – name of the metric function. Defaults to be
Noneloss_args (Any) – function arguments for loss_fcn other than QNN and label inputs
Note
The
datainput of this Model needs to bepaddle_quantum.State. UseEncodingModelif data is expected to be encoded into quantum circuitsThe prepare function will take the settings of
rcParams
- train_batch(data, label)
Train the circuit by input batch data
- Parameters:
data (List[State]) – list of input
Stateslabel (List[Any]) – expected label
- Returns:
a list of loss values
a list of metric values if a metric function is given
- Return type:
contains the following elements
- eval_batch(data, label)
Evaluate the circuit by input batch data
- Parameters:
data (List[State]) – list of input
Stateslabel (List[Any]) – expected label
- Returns:
a loss value
a metric value if a metric function is given
- Return type:
contains the following elements
- fit(train_data, train_label, test_data, test_label)
Fit the circuit by input train_data
- plot(include_metric=True, apply_log=False)
Plot the loss (and metric) data
- Parameters:
include_metric (bool) – include the metric data. Defaults to be
True.apply_log (bool) – whether apply the log function on the data. Defaults to be
False.
- class paddle_quantum.model.EncodingNetwork(encoding_func, param_shape, initial_state)
Bases:
LayerQNN for Encoding model
- Parameters:
Note
Used for
paddle_quantum.model.EncodingModelonly.
- class paddle_quantum.model.EncodingModel(encoding_fcn, param_shape, initial_state=None, name='EncodingModel')
Bases:
ModelClass for encoding-based QNN model
- Parameters:
encoding_fcn (Callable[[Any, Tensor], Circuit]) – an encoding function that determines how to construct quantum circuits by the encoding
- data and the parameters.
param_shape: the shape of input parameters for
encoding_funcinitial_state: the initial state of circuits. Default to beNonei.e. the zero state name: name of this model. Default to be"EncodingModel"
Note
Unlike LearningModel, the data of
EncodingModelis encoded into quantum circuits instead of states. Therefore,EncodingModelrequires the information of how circuits are encoded by input classical data.EncodingModelwill automatically generate the parameters according to inputparam_shape.- prepare(loss_fcn, metric_fcn=None, metric_name=None, *loss_args)
Prepare and check the function setup for encoding-based QNN
- Parameters:
loss_fcn (Callable[[State, Any, Any], Any]) – loss function for the output data of QNN
metric_fcn (Callable[[Circuit | Sequential], float] | None) – metric function for the QNN, which does not mess with the training process. Defaults to be
Nonemetric_name (str | None) – name of the metric function. Defaults to be
Noneloss_args (Any) – function arguments for loss_fcn other than QNN and label inputs
Note
The prepare function will take the settings of
rcParams
- train_batch(data, label)
Train the circuit by input batch data
- Parameters:
data (Iterable) – list of input data
label (Iterable) – expected label
- Returns:
a list of loss values
a list of metric values if a metric function is given
- Return type:
contains the following elements
- eval_batch(data, label)
Evaluate the circuit by input batch data
- Parameters:
data (Iterable) – list of input data
label (Iterable) – expected label
- Returns:
a loss value
a metric value if a metric function is given
- Return type:
contains the following elements
- fit(train_data, train_label, test_data, test_label)
Fit the circuit by input train_data
- Parameters:
train_data (Iterable) – data of the train set
train_label (Iterable) – label of the train set
test_data (Iterable) – data of the test set
test_label (Iterable) – label of the test set
- plot(include_metric=True, apply_log=False)
Plot the loss (and metric) data
- Parameters:
include_metric (bool) – include the metric data. Defaults to be
True.apply_log (bool) – whether apply the log function on the data. Defaults to be
False.