paddle_quantum.model

General model templates for Quantum Neural Network

paddle_quantum.model.reset_settings()

Set the current rcParams to 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: LRScheduler

An 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: object

General 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 None

  • metric_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.network as input. Defaults to None i.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.network as input. Defaults to None i.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: Model

Class for optimization-based QNN model

Parameters:
  • circuit (Circuit) – a Circuit instance ready to be optimized

  • name (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 None

  • metric_name (str | None) – name of the metric function. Defaults to be None

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

Class for learning-based QNN model

Parameters:
  • circuit (Circuit) – a Circuit instance ready to be trained

  • name (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 None

  • metric_name (str | None) – name of the metric function. Defaults to be None

  • loss_args (Any) – function arguments for loss_fcn other than QNN and label inputs

Note

  • The data input of this Model needs to be paddle_quantum.State. Use EncodingModel if data is expected to be encoded into quantum circuits

  • The 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 State s

  • label (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 State s

  • label (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

Parameters:
  • train_data (List[State]) – data of the train set

  • train_label (Iterable) – label of the train set

  • test_data (List[State]) – 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.

class paddle_quantum.model.EncodingNetwork(encoding_func, param_shape, initial_state)

Bases: Layer

QNN for Encoding model

Parameters:
  • encoding_func (Callable[[Any, Tensor], Circuit]) – an encoding function that determines how to construct quantum circuits

  • param_shape (Iterable[int]) – the shape of input parameters

  • initial_state (State) – the initial state of circuits

Note

Used for paddle_quantum.model.EncodingModel only.

forward(input_data)

Compute the output states corresponding to the input data

Parameters:

input_data (List[Any]) – the list of input data that encodes circuits

Returns:

the output states from these circuits

Return type:

List[State]

class paddle_quantum.model.EncodingModel(encoding_fcn, param_shape, initial_state=None, name='EncodingModel')

Bases: Model

Class 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_func initial_state: the initial state of circuits. Default to be None i.e. the zero state name: name of this model. Default to be "EncodingModel"

Note

Unlike LearningModel, the data of EncodingModel is encoded into quantum circuits instead of states. Therefore, EncodingModel requires the information of how circuits are encoded by input classical data. EncodingModel will automatically generate the parameters according to input param_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 None

  • metric_name (str | None) – name of the metric function. Defaults to be None

  • loss_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.