Package: qaoa
QAOAThis package is a flexible python implementation of theQuantum Approximate Optimization Algorithm/Quantum Alternating Operator ansatz(QAOA)aimed at researchersto readily test the performance of a new ansatz, a new classical optimizers, etc. By default it uses qiskit as a backend.Install withpip install qaoaorpip install -e ..BackgroundGiven acost function$$c: \lbrace 0, 1\rbrace^n \rightarrow \mathbb{R}$$
one defines aproblem Hamiltonian$H_P$ through the action on computational basis states via$$ H_P |x\rangle = c(x) |x\rangle,$$which means that ground states minimize the cost function $c$.
Given a parametrized ansatz $ | \gamma, \beta \rangle$, a classical optimizer is used to minimize the energy$$ \langle \gamma, \beta | H_P | \gamma, \beta \rangle.$$QAOA of depth $p$ consist of the followingansatz:$$ |\gamma, \beta \rangle = \prod_{l=1}^p \left( U_M(\beta_l) U_P(\gamma_l)\right) | s\rangle, $$where$U_P$ is a family ofphase-separating operators,$U_M$ is a family ofmixingoperators, and$|s\rangle$ is a "simple"initialstate.In plain vanilla QAOA these have the form
$U_M(\beta_l)=e^{-i\beta_l X^{\otimes n}}$,  $U_P(\gamma_l)=e^{-i\gamma_l H_P}$, and the uniform superposition $| s \rangle = |+\rangle^{\otimes n}$ as initial state.Create a custom ansatzIn order to create a custom QAOA ansatz, one needs to specify aproblem, amixer, and aninitial state. These base classes have an abstract methoddef create_circuit:which needs to be implemented. The problem base class additionally has an abstract methoddef cost:.This library already contains several standard implementations.The followingproblemcases are already available:MaxCutQUBOExact coverPortfolioThe followingmixercases are already available:X-mixerXY-mixerGrover-mixerThe followinginitial statecases are already available:PlusStatevectorDickeIt isvery easy to extend this listby 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 $|+\rangle^{\otimes n}$  one can create an instance like this:qaoa = QAOA(
	initialstate=initialstates.Plus(),
	problem=problems.MaxCut(G="some networkx instance"),
	mixer=mixers.X()
)Run optimization at depth $p$For depth $p=1$ the expectation value can be sampled on an $n\times m$ Cartesian grid over the domain $[0,\gamma_\text{max}]\times[0,\beta_\text{max}]$ with:qaoa.sample_cost_landscape()Sampling high-dimensional target functions quickly becomes intractable for depth $p>1$. We thereforeiteratively increase the depth. At each depth alocal optimizationalgorithm, e.g. COBYLA, is used to find a local minimum. Asinitial guessthe following is used: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 aninterpolation-based heuristicof the optimal values at the previous depth.Running this iterative local optimization to depth $p$ can be done by the following call:qaoa.optimize(depth=p)The function will callsample_cost_landscapeif not already done, before iteratively increasing the depth.Further parametersQAOA supports the following keywords:qaoa = QAOA( ...,
	backend= ,
	noisemodel= ,
	optimizer= ,
	precision= ,
	shots= ,
	alpha=
)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,alpha: the value forconditional value at risk (CVAR), defaults to1, which are the standard moments.Extract resultsOnceqaoa.optimize(depth=p)is run, one can extract, the expectation value, variance, and parametres for each depth $1\leq i \leq p$ by respectively calling: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, theangles, expectation value, variance, maximum cost, minimum cost,andnumber of shotsare stored inqaoa.optimization_results[i]Example usageSeeexamples here.