This package is a flexible python implementation of the Quantum Approximate Optimization Algorithm /Quantum Alternating Operator ansatz (QAOA) aimed at researchers to readily test the performance of a new ansatz, a new classical optimizers, etc. By default it uses qiskit as a backend.
Install with pip install qaoa
or pip install -e .
.
Given a cost function
which means that ground states minimize the cost function
QAOA of depth
where
-
$U_P$ is a family of phase-separating operators, -
$U_M$ is a family of mixing operators, and -
$|s\rangle$ is a "simple" initial state.
In plain vanilla QAOA these have the form
In order to create a custom QAOA ansatz, one needs to specify a problem, a mixer, and an initial state. These base classes have an abstract method def create_circuit:
which needs to be implemented. The problem base class additionally has an abstract method def cost:
.
This library already contains several standard implementations.
- The following problem cases are already available:
- The following mixer cases are already available:
- The following initial state cases are already available:
It is very easy to extend this list by providing an implementation of a circuit/cost of the base classes mentioned above. Feel free to fork the repo and create a pull request :-)
To make an ansatz for the MaxCut problem, the X-mixer and the initial state
qaoa = QAOA(
initialstate=initialstates.Plus(),
problem=problems.MaxCut(G="some networkx instance"),
mixer=mixers.X()
)
For depth
qaoa.sample_cost_landscape()
Sampling high-dimensional target functions quickly becomes intractable for depth
- At depth
$p=1$ initial parameters$(\gamma, \beta)$ are given by the lowest value of the sampled cost landscape. - At depth
$p>1$ initial parameters$(\gamma, \beta)$ are based on an interpolation-based heuristic of the optimal values at the previous depth.
Running this iterative local optimization to depth
qaoa.optimize(depth=p)
The function will call sample_cost_landscape
if not already done, before iteratively increasing the depth.
QAOA supports the following keywords:
qaoa = QAOA( ...,
backend= ,
noisemodel= ,
optimizer= ,
precision= ,
shots= ,
cvar=
)
-
backend
: the backend to be used, defaults toAer.get_backend("qasm_simulator")
-
noisemodel
: the noise model to be used, default toNone
, -
optimizer
: a list of the optimizer to be used from qiskit-algorithms together with options, defaults to[COBYLA, {}]
, -
precision
: sampel until a certain precision of the expectation value is reached based on$\text{error}=\frac{\text{variance}}{\sqrt{\text{shots}}}$ , defaults toNone
, -
shots
: number of shots to be used, defaults to1024
, -
cvar
: the value for conditional value at risk (CVAR), defaults to1
, which are the standard moments.
Once qaoa.optimize(depth=p)
is run, one can extract, the expectation value, variance, and parametres for each depth
qaoa.get_Exp(depth=i)
qaoa.get_Var(depth=i)
qaoa.get_gamma(depth=i)
qaoa.get_beta(depth=i)
Additionally, for each depth every time the loss function is called, the angles, expectation value, variance, maximum cost, minimum cost, and number of shots are stored in
qaoa.optimization_results[i]
See examples here.
We would like to thank for funding of the work by the Research Council of Norway through project number 33202.