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.9999999959462045
Minimization trace: [0.9380334554546228, 0.21521432005137245, -0.7054718686954221, -0.9775504748138564, -0.35087368259342083, -0.7568656301384117, -0.9992891521532854, -0.9588968699901771, -0.9867922929166001, -0.9996926845421643, -0.9984300364603328, -0.9999579798120614, -0.9999791496825979, -0.9998981836497581, -0.9999967452724093, -0.999999082142224, -0.9999861602563839, -0.9999945285368944, -0.9999999284356991, -0.9999998210550017, -0.9999999939546368, -0.9999998210550017, -0.9999999373071398, -0.9999999999504207, -0.9999999959462045]
Best parameters: [3.141682695751232]

Below is the detailed documentation of the available plugins:

Scipy Minimizers

class qat.plugins.ScipyMinimizePlugin(collective=False, binding_args=None, **kwargs)

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

It is a particularization of the Optimizer class.

Parameters
  • 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’…

Note

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

Example:

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

plugin = ScipyMinimizePlugin(x0=np.random.random(10),
                             method="COBYLA",
                             tol=1e-3,
                             options={"maxiter": 300})

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

Sets new options for the minimize function.

Parameters

kwargs – a new set of options

Simultaneous Perturbation Stochastic Approximation

class qat.plugins.SPSAMinimizePlugin(x0, binding_args=None, **kwargs)

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

This is a particularization of the Optimizer class.

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

References

https://en.wikipedia.org/wiki/Simultaneous_perturbation_stochastic_approximation

Particle Swarm Optimization

class qat.plugins.PSOMinimizePlugin(x0, binding_args=None, **kwargs)

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

It is a particularization of the Optimizer class.

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

Sequential Optimization