Optimizers#

Optimizers are used automatically by the minimize methods of qibo.models.VQE and qibo.evolution.AdiabaticEvolution models. The user does not have to use any of the optimizer methods included in the current section, however the required options of each optimization method can be passed when calling the minimize method of the respective Qibo variational model.

qibo.optimizers.optimize(loss, initial_parameters, args=(), method='Powell', jac=None, hess=None, hessp=None, bounds=None, constraints=(), tol=None, callback=None, options=None, compile=False, processes=None, backend=None)[source]#
Main optimization method. Selects one of the following optimizers:
Parameters:
  • loss (callable) – Loss as a function of parameters and optional extra arguments. Make sure the loss function returns a tensor for method=sgd and numpy object for all the other methods.

  • initial_parameters (np.ndarray) – Initial guess for the variational parameters that are optimized.

  • args (tuple) – optional arguments for the loss function.

  • method (str) – Name of optimizer to use. Can be 'cma', 'sgd' or one of the Newtonian methods supported by qibo.optimizers.newtonian() and 'parallel_L-BFGS-B'. sgd is only available for backends based on tensorflow.

  • jac (dict) – Method for computing the gradient vector for scipy optimizers.

  • hess (dict) – Method for computing the hessian matrix for scipy optimizers.

  • hessp (callable) – Hessian of objective function times an arbitrary vector for scipy optimizers.

  • bounds (sequence or Bounds) – Bounds on variables for scipy optimizers.

  • constraints (dict) – Constraints definition for scipy optimizers.

  • tol (float) – Tolerance of termination for scipy optimizers.

  • callback (callable) – Called after each iteration for scipy optimizers.

  • options (dict) – Dictionary with options. See the specific optimizer bellow for a list of the supported options.

  • compile (bool) – If True the Tensorflow optimization graph is compiled. This is relevant only for the 'sgd' optimizer.

  • processes (int) – number of processes when using the parallel BFGS method.

Returns:

Final best loss value; best parameters obtained by the optimizer; extra: optimizer-specific return object. For scipy methods it returns the OptimizeResult, for 'cma' the CMAEvolutionStrategy.result, and for 'sgd' the options used during the optimization.

Return type:

(float, float, custom)

Example

import numpy as np
from qibo import Circuit, gates
from qibo.optimizers import optimize

# create custom loss function
# make sure the return type matches the optimizer requirements.
def myloss(parameters, circuit):
    circuit.set_parameters(parameters)
    return np.square(np.sum(circuit().state())) # returns numpy array

# create circuit ansatz for two qubits
circuit = Circuit(2)
circuit.add(gates.RY(0, theta=0))

# optimize using random initial variational parameters
initial_parameters = np.random.uniform(0, 2, 1)
best, params, extra = optimize(myloss, initial_parameters, args=(circuit))

# set parameters to circuit
circuit.set_parameters(params)
qibo.optimizers.cmaes(loss, initial_parameters, args=(), callback=None, options=None)[source]#

Genetic optimizer based on pycma.

Parameters:
  • loss (callable) – Loss as a function of variational parameters to be optimized.

  • initial_parameters (np.ndarray) – Initial guess for the variational parameters.

  • args (tuple) – optional arguments for the loss function.

  • callback (list[callable]) – List of callable called after each optimization iteration. According to cma-es implementation take CMAEvolutionStrategy instance as argument. See: https://cma-es.github.io/apidocs-pycma/cma.evolution_strategy.CMAEvolutionStrategy.html.

  • options (dict) – Dictionary with options accepted by the cma optimizer. The user can use import cma; cma.CMAOptions() to view the available options.

qibo.optimizers.newtonian(loss, initial_parameters, args=(), method='Powell', jac=None, hess=None, hessp=None, bounds=None, constraints=(), tol=None, callback=None, options=None, processes=None, backend=None)[source]#

Newtonian optimization approaches based on scipy.optimize.minimize.

For more details check the scipy documentation.

Note

When using the method parallel_L-BFGS-B the processes option controls the number of processes used by the parallel L-BFGS-B algorithm through the multiprocessing library. By default processes=None, in this case the total number of logical cores are used. Make sure to select the appropriate number of processes for your computer specification, taking in consideration memory and physical cores. In order to obtain optimal results you can control the number of threads used by each process with the qibo.set_threads method. For example, for small-medium size circuits you may benefit from single thread per process, thus set qibo.set_threads(1) before running the optimization.

Parameters:
  • loss (callable) – Loss as a function of variational parameters to be optimized.

  • initial_parameters (np.ndarray) – Initial guess for the variational parameters.

  • args (tuple) – optional arguments for the loss function.

  • method (str) – Name of method supported by scipy.optimize.minimize and 'parallel_L-BFGS-B' for a parallel version of L-BFGS-B algorithm.

  • jac (dict) – Method for computing the gradient vector for scipy optimizers.

  • hess (dict) – Method for computing the hessian matrix for scipy optimizers.

  • hessp (callable) – Hessian of objective function times an arbitrary vector for scipy optimizers.

  • bounds (sequence or Bounds) – Bounds on variables for scipy optimizers.

  • constraints (dict) – Constraints definition for scipy optimizers.

  • tol (float) – Tolerance of termination for scipy optimizers.

  • callback (callable) – Called after each iteration for scipy optimizers.

  • options (dict) – Dictionary with options accepted by scipy.optimize.minimize.

  • processes (int) – number of processes when using the parallel BFGS method.

qibo.optimizers.sgd(loss, initial_parameters, args=(), callback=None, options=None, compile=False, backend=None)[source]#

Stochastic Gradient Descent (SGD) optimizer using Tensorflow backpropagation.

See tf.keras.Optimizers for a list of the available optimizers for Tensorflow. See torch.optim for a list of the available optimizers for PyTorch.

Parameters:
  • loss (callable) – Loss as a function of variational parameters to be optimized.

  • initial_parameters (np.ndarray) – Initial guess for the variational parameters.

  • args (tuple) – optional arguments for the loss function.

  • callback (callable) – Called after each iteration.

  • options (dict) –

    Dictionary with options for the SGD optimizer. Supports the following keys:

    • 'optimizer' (str, default: 'Adagrad'): Name of optimizer.

    • 'learning_rate' (float, default: '1e-3'): Learning rate.

    • 'nepochs' (int, default: 1e6): Number of epochs for optimization.

    • 'nmessage' (int, default: 1e3): Every how many epochs to print a message of the loss function.