Plugins for variational algorithms

The following plugins inherit from the Optimizer class and provide an easy way to include your favorite classical optimizer inside a QLM stack.

These plugins require the input job to have abstract parameters (i.e the circuit and/or the observable should be parametrized).

We can, for instance, build a very simple variational job as follows:

from qat.plugins import ScipyMinimizePlugin
from qat.qpus import get_default_qpu
from qat.lang.AQASM import Program, RY
from qat.core import Observable

prog = Program()
qbits = prog.qalloc(1)
RY(prog.new_var(float, "a"))(qbits)
job = prog.to_circ().to_job(observable=Observable.sigma_z(0, 1))

stack = ScipyMinimizePlugin(method="COBYLA") | get_default_qpu()

result = stack.submit(job)

print("Final energy:", result.value)
print("Minimization trace:", result.meta_data["optimization_trace"])
print("Best parameters:", result.meta_data["parameters"])
Final energy: -0.9999999950686326
Minimization trace: [0.9359478721097356, 0.20938039437528144, -0.7096904523406455, -0.9762751700797891, -0.3452769987711959, -0.752950335524142, -0.999496404883138, -0.9605737942130195, -0.987741789682681, -0.9995268744567191, -0.9980778467560215, -0.9998854316356179, -0.9999998811267532, -0.999870194988923, -0.9999655544295529, -0.9999941563963229, -0.9999970214503905, -0.9999998804541982, -0.9999995238351976, -0.9999999703655793, -0.9999999999997629, -0.9999999700293019, -0.9999999949308932, -0.9999999950686326]
Best parameters: [3.141493342286287]

Below is the detailed documentation of the available plugins:

Scipy Minimizers

class qat.plugins.ScipyMinimizePlugin

A variational plugin for hybrid quantum classical optimization based on the scipy.optimize.minimize method.

It is a particularization of the Optimizer class.

  • x0 (np.array, optional) – initial value of the parameters. Defaults to None, in which case we assume random initilization.

  • binding_args (dict) – arguments passed to the variable binding method

  • kwargs – minimize parameters (see scipy.optimize.minimize documentation), e.g ‘method’, ‘tol’, ‘options’…


Parameters can be changed dynamically using the qat.plugins.ScipyMinimizePlugin.set_options() method.


import numpy as np
form qat.plugins import ScipyMinimizePlugin
from qat.linalg import LinAlg

plugin = ScipyMinimizePlugin(x0=np.random.random(10),
                             options={"maxiter": 300})

stack = plugin | LinAlg()
# Any job will run, as long as it contains 10 variables!
result = stack.submit(my_job)

Sets new options for the minimize function.


kwargs – a new set of options

Simultaneous Perturbation Stochastic Approximation

class qat.plugins.SPSAMinimizePlugin

A variational minimizer based on the Simultaneous Perturbation Stochastic Approximation (SPSA) algorithm.

This is a particularization of the Optimizer class.

  • x0 (np array) – initial values

  • binding_args (dict, optional) – arguments to bind job variables (see Optimizer documentation). Defaults to None.

  • maxiter (int, optional) – maximal number of iterations. Defaults to 100.

  • a (int, optional) – \(a\) parameter. Defaults to 1.

  • A (int, optional) – \(A\) parameter. Defaults to 10.

  • alpha (float, optional) – \(\alpha\) parameter. Defaults to 0.602.

  • c (float, optional) – \(c\) parameter. Defaults to 0.01.

  • gamma (float, optional) – \(\gamma\) parameter. Defaults to 0.101.

  • precision (float, optional) – stop after ‘stop_condition’ consecutive times when difference between two successive function evalutions are lower than precision. Defaults to 1e-4.

  • stop_condition (int, optional) – see ‘precision’. Defaults to 5.

  • a_calibration (bool, optional) – whether to calibrate a param. Defaults to False

  • n_calibrations (int, optional) – Defaults to 25.

  • seed (int, optional) – seed for random number generator. Defaults to None.


Particle Swarm Optimization

class qat.plugins.PSOMinimizePlugin

A variational minimizer based on the Particle Swarm Optimization (PSO) algorithm.

It is a particularization of the Optimizer class.

  • x0 (numpy.ndarray) – Initial position of one Particle. If you have no idea, take a positive integer which is equal to the number of parameters to optimize.

  • binding_args (optional, dict) – arguments passed to the variable binding method

  • choose_inertia_function (str, optional) – method for picking the inertia weights. It may be ‘constant’, ‘random’, ‘linearly_decreasing’ (default), ‘chaotic_random’.

  • name_method_used (str, optional) – Kind of PSO to be used. It may be ‘PSO’ (default), ‘CLPSO’, ‘FDR_PSO’, ‘LIPS’, ‘HPSO_TVAC’.

  • max_iter (int, optional) – maximum number of iterations. Defaults to 10.

  • swarm_number (int, optional) – size of the swarm. Defaults to 10.

  • size_initial_vector_space (float, optional) – size of the hypercube. Defaults to 1.

  • inertia_kwargs (float, optional) – depending on the inertia_function used. See class Inertia_weight for more details.