paddle_quantum.gradtool

The module of the gradient tool.

paddle_quantum.gradtool.show_gradient(circuit, loss_func, ITR, LR, *args)

Calculate the gradient and loss function for every parameter in QNN.

Parameters:
  • circuit (Circuit) – QNN to be trained.

  • loss_func (Callable[[Circuit, Any], Tensor]) – Loss function that evaluates the QNN.

  • ITR (int) – Number of iterations.

  • LR (float) – Learning rate.

  • *args (Any) – Parameters for loss_func other than circuit.

Returns:

Contains following two elements.
  • loss_list: A list of losses for each iteration.

  • grad_list: A list of gradients for each iteration.

Return type:

Tuple[List[float], List[float]]

paddle_quantum.gradtool.plot_distribution(grad)

Plot the distribution map according to the input gradients.

Parameters:

grad (ndarray) – List of gradients with respect to a parameter.

paddle_quantum.gradtool.random_sample(circuit, loss_func, sample_num, *args, mode='single', if_plot=True, param=0)

Randomly sample the model. Obtain mean and variance of gradients according to different calculation modes.

Parameters:
  • circuit (Circuit) – QNN to be trained.

  • loss_func (Callable[[Circuit, Any], Tensor]) – Loss function that evaluates the QNN.

  • sample_num (int) – Number of samplings.

  • mode (str) – Mode for calculation. Defaults to 'single'.

  • if_plot (bool) – Whether plot the calculation. Defaults to True.

  • param (int) – Which parameter to be plotted in single mode, Defaults to 0, which means the first one.

  • *args (Any) – Parameters for loss_func other than circuit.

Note

This function provides three calculation modes: single, max and random.
  • In single mode, we calculate the mean and variance of gradients of every trainable parameter.

  • In max mode, we calculate the mean and variance of maximum gradients of for every trainable parameter.

  • In random mode, we calculate the mean and variance of data randomly extracted from gradients of every trainable parameter.

Returns:

Contains the following two elements.
  • loss_list: A list of losses for each iteration.

  • grad_list: A list of gradients for each iteration.

Return type:

Tuple[List[float], List[float]]

paddle_quantum.gradtool.plot_loss_grad(circuit, loss_func, ITR, LR, *args)

Plot the distribution maps between loss values & gradients and number of iterations.

Parameters:
  • circuit (Circuit) – QNN to be trained.

  • loss_func (Callable[[Circuit, Any], Tensor]) – Loss function that evaluate QNN.

  • ITR (int) – Number of iterations.

  • LR (float) – Learning rate.

  • *args (Any) – Parameters for loss_func other than circuit.

paddle_quantum.gradtool.plot_supervised_loss_grad(circuit, loss_func, N, EPOCH, LR, BATCH, TRAIN_X, TRAIN_Y, *args)

plot the distribution maps between loss values & gradients and number of iterations in supervised training

Parameters:
  • circuit (Circuit) – QNN ready to be trained.

  • loss_func (Callable[[Circuit, Any], Tensor]) – Loss function that evaluates the QNN.

  • N (int) – Number of qubits.

  • EPOCH (int) – Number of training iterations.

  • LR (float) – Learning rate.

  • BATCH (int) – Size of batches.

  • TRAIN_X (Tensor) – Data set .

  • TRAIN_Y (list) – Label set.

  • *args (Any) – Parameters for loss_func other than circuit.

Raises:

Exception – Training data should be paddle.Tensor type

Returns:

Contains the following two elements.
  • loss_list: A list of losses for each iteration.

  • grad_list: A list of gradients for each iteration.

Return type:

Tuple[List[float], List[float]]

paddle_quantum.gradtool.random_sample_supervised(circuit, loss_func, N, sample_num, BATCH, TRAIN_X, TRAIN_Y, *args, mode='single', if_plot=True, param=0)

Random sample the supervised model. Obtain mean and variance of gradients according to different calculation modes.

Parameters:
  • circuit (Circuit) – QNN to be trained.

  • loss_func (Callable[[Circuit, Any], Tensor]) – Loss function that evaluates the QNN.

  • N (int) – Number of qubits.

  • sample_num (int) – Number of samplings.

  • BATCH (int) – Size of batches.

  • TRAIN_X (Tensor) – Data set.

  • TRAIN_Y (Tensor) – Label set.

  • mode (str) – Mode for calculation. Defaults to 'single'.

  • if_plot (bool) – Whether plot the calculation. Defaults to True.

  • param (int) – Which parameter to be plotted in single mode. Defaults to 0, which means the first one.

  • *args (Any) – Parameters for loss_func other than circuit.

Note

This function provides three calculation modes: single, max and random.
  • In single mode, we calculate the mean and variance of gradients of every trainable parameters.

  • In max mode, we calculate the mean and variance of maximum gradients of for every trainable parameters.

  • In random mode, we calculate the mean and variance of data randomly extracted from gradients of every trainable parameters.

Raises:

Exception – Training data should be paddle.Tensor type

Returns:

Contains the following two elements.
  • loss_list: A list of losses for each iteration.

  • grad_list: A list of gradients for each iteration.

Return type:

Tuple[List[float], List[float]]