Running variational algorithms

This framework comes with a collection of tools to efficiently describe and run variational quantum algorithm. This page introduces the basic mechanics allowing you to write variational schemes.

Writing variational circuits is discussed in another section.

Extending a QPU to support variational jobs

The simplest way to run a variational algorithm is to use a dedicated Plugin that will take care of the energy minimization.

The default variational Plugin wraps the scipy.optimize.minimize function: ScipyMinimizePlugin. This plugin extends an exisiting QPU by using the “|” operator

from qat.plugins import ScipyMinimizePlugin
from qat.qpus import get_default_qpu

qpu = (
    ScipyMinimizePlugin(method="COBYLA", tol=1e-3, options={"maxiter": 150})
    | get_default_qpu()
)

This new QPU will optimize the abstract variable parametrizing the submitted job, to minimize the measured average value of the observable attached to the job.

from qat.lang import qrout, RY, RZ
from qat.core import Observable, Term, Variable

# Define the observable
t = Variable("t")
obs = sum((1 - t) * Observable.sigma_x(idx) for idx in range(3))

# Define the Ansatz
@qrout
def ansatz(theta):
    " Dummy Ansatz"
    for qbit in range(3):
        RY(theta)(qbit)
        RZ(4 * theta)(qbit)

# Define job and submit it
job = ansatz.to_job(observable=obs)
result = qpu.submit(job)

print('final energy:', result.value)
print('best parameters:', result.parameter_map)
print('trace:', result.meta_data['optimization_trace'])
final energy: -10.33314885269944
best parameters: {'t': 5.738103231171286, 'theta': -0.8401445195416736}
trace: [0.04767873754462222, -0.1287546998730195, 0.8650814848186296, -1.8412385112077247, 0.5064195362332036, -1.9348488368113843, -0.22772042670809622, -2.2308609889933444, 0.19363070051357523, 0.17870750077915884, -3.344332596731177, -3.60883177203466, -3.0484118825050985, -3.7594951790997073, -3.8247844324561617, -4.0007828235607725, -4.016527679800079, -4.121889175258647, -4.198297441828046, -4.258428122800557, -4.327060560055903, -4.395964436325203, -4.464693463707951, -4.533063804713381, -4.601251482741537, -4.669128169372481, -4.7226003176133275, -4.794409306470055, -4.862609652928122, -4.9295940746973645, -4.998154695948941, -5.065652705852724, -5.133952403563445, -5.2021139344994225, -5.2702443033775985, -5.338377793650483, -5.406531950277747, -5.47466601595744, -5.542530665091483, -5.610757267165465, -5.678914753429082, -5.747032815293818, -5.815196751592426, -5.88332071132843, -5.951480452634371, -6.01962344701178, -6.087778320483143, -6.1559260470074, -6.22407912763018, -6.292230930636167, -6.360381874177179, -6.428532323996736, -6.496684734478464, -6.564835097490564, -6.632986556324575, -6.701138085100164, -6.769286945505955, -6.837436223915974, -6.90558395600117, -6.973711482524682, -7.041827846032724, -7.109953044954707, -7.178015651888327, -7.2404269827786765, -7.309807545326013, -7.371014047260204, -7.439815707335638, -7.499748150451222, -7.567332456924066, -7.624490055843433, -7.687510201266379, -7.740527762646417, -7.686561463731661, -7.7846700089106955, -7.835785940678084, -7.8512397638819795, -7.625919052040118, -7.953674938317892, -7.952148818399205, -7.96561703882369, -8.003330096197171, -8.03798765794221, -8.07188875107293, -8.105981239868472, -8.14005816202389, -8.17410846180244, -8.208193189587023, -8.242261343205776, -8.276336843846744, -8.310412333519068, -8.344487760733506, -8.378562977445831, -8.41263720984526, -8.446697073838017, -8.480762004050508, -8.514822476842053, -8.548822898492972, -8.582867546276187, -8.616885552681365, -8.650635875467614, -8.684628056079543, -8.718463426517989, -8.750885078734074, -8.784886866254148, -8.817833207467242, -8.851824240946824, -8.885367291364854, -8.919546022099647, -8.953373413191272, -8.987513540837066, -9.021564118063658, -9.055630848439671, -9.089709450971458, -9.123776239686128, -9.15785284823501, -9.191927519948736, -9.226003297380096, -9.260079256339734, -9.294153505681427, -9.32822799285198, -9.362304119980337, -9.396379958324287, -9.430454922312096, -9.464530925923189, -9.498605690272525, -9.532679873654123, -9.566755333438461, -9.60083041103649, -9.634903316872947, -9.668976963675439, -9.703048768965836, -9.737101073470793, -9.77116669534455, -9.805221330833835, -9.83916665906024, -9.873195342227122, -9.907154070425221, -9.940440137777202, -9.974341025978372, -10.007715132714344, -10.040854994700764, -10.074492589269797, -10.107532253567877, -10.13296504720742, -10.169633038339612, -10.19646409165416, -10.23221239780831, -10.264443353906449, -10.298905328566953, -10.33314885269944]

This plugin also supports a nice feature: it can read optimization parameters directly from the job’s meta data or directly from the result. This allows you to build a stack with no particular choice of optimization parameters and attach these parameters directly to the job when submitting it. In this setting, the previous example becomes:

import json
from qat.plugins import ScipyMinimizePlugin
from qat.qpus import get_default_qpu

qpu = (
    ScipyMinimizePlugin()
    | get_default_qpu()
)

from qat.lang import qrout, RY, RZ
from qat.core import Observable, Term, Variable

# Define the observable
t = Variable("t")
obs = sum((1 - t) * Observable.sigma_x(idx) for idx in range(3))

# Define the Ansatz
@qrout
def ansatz(theta):
    " Dummy Ansatz"
    for qbit in range(3):
        RY(theta)(qbit)
        RZ(4 * theta)(qbit)

# Define job and submit it
job = ansatz.to_job(observable=obs)

optimizer_args = {
    "method": "COBYLA",
    "tol": 1e-3,
    "options": {"maxiter": 150}
}

result = qpu.submit(job, meta_data={"ScipyMinimizePlugin": json.dumps(optimizer_args)})

print('final energy:', result.value)
print('best parameters:', result.parameter_map)
print('trace:', result.meta_data['optimization_trace'])
final energy: -11.542795719866923
best parameters: {'t': -4.292762906451374, 'theta': 0.840460161984927}
trace: [-0.1277739837659709, 1.8800508784652141, 0.022768016906906285, -2.307775143767119, -4.376575301341603, -4.9491145154546405, -0.6674915559599709, 9.620679927664595, 1.1657581106162729, -6.453517321722424, -0.9032377784153116, -6.874421887494513, -3.4027436442069714, -6.631584343457858, -6.749868519289338, -6.704052064753306, -7.014758557482536, -7.080915213237304, -6.985137517522313, -7.113660209893403, -7.147309368479772, -7.181389192871169, -7.215463471245276, -7.249510531498799, -7.28359128729779, -7.317669880170163, -7.351744918775804, -7.385820722248123, -7.419896109481008, -7.4539685838730305, -7.488033231870057, -7.52210767870214, -7.5561819859837005, -7.590255749464503, -7.624327080149143, -7.658359127147072, -7.692447336735958, -7.726493680769883, -7.760574146913731, -7.794647820485796, -7.828723835750253, -7.862799784983261, -7.8968756490905925, -7.930951279732129, -7.965024888555863, -7.999093104715971, -8.033167530138222, -8.067241123344978, -8.1013115070583, -8.135315734740354, -8.169410816079477, -8.20344399402478, -8.237528901903756, -8.271604071895482, -8.305679998350646, -8.339751781903649, -8.373827559448614, -8.407903488233718, -8.441976336380854, -8.476052372511955, -8.510128306287818, -8.544202020802189, -8.578278016601583, -8.612353970973679, -8.646427553332792, -8.680503827790606, -8.71457975179397, -8.748654786049235, -8.78273050232804, -8.816806525303143, -8.85085741428042, -8.884940462114225, -8.919013120216839, -8.953087791994273, -8.987160224857423, -9.021205426078389, -9.055285195651784, -9.089336349538435, -9.12341290386862, -9.157478759270182, -9.191557763987792, -9.22563021869597, -9.259706643597347, -9.293782993757098, -9.3278585419352, -9.361934635552362, -9.396010522804522, -9.430086491664852, -9.464162406602695, -9.498237862165812, -9.532312123133622, -9.566387865720307, -9.60046363266836, -9.63453942274175, -9.668615267797144, -9.702691315404918, -9.73676788152151, -9.770844075882097, -9.804919766055054, -9.838995852293447, -9.873071780792507, -9.907147776208156, -9.941223772984305, -9.975299768172565, -10.009375767489466, -10.043451766897045, -10.07752776605731, -10.11160376501386, -10.145679763900397, -10.179755762834542, -10.213831761731257, -10.247907760665214, -10.281983759578202, -10.316059758509944, -10.350135757433142, -10.384211756360402, -10.41828775528781, -10.452363754215412, -10.486439753143221, -10.520515752071011, -10.554591750998455, -10.588667749925815, -10.622743748852287, -10.656819747779526, -10.690895746706655, -10.724971745633962, -10.75904774456124, -10.793123743488065, -10.82719974241531, -10.861275741342094, -10.895351740268048, -10.929427739195164, -10.963503738122345, -10.997579737049646, -11.031655735977248, -11.065731734905029, -11.099807733832193, -11.133883732759557, -11.167959731686878, -11.202035730614089, -11.23611172954151, -11.270187728468596, -11.304263727395698, -11.338339726322907, -11.372415725249848, -11.406491724169957, -11.44056772309829, -11.474643722018268, -11.508719720945285, -11.542795719866923]

An alternative is the SeqOptim Plugin, which only works for a certain class of circuits but stands out for its shot-noise resilience.

Differentiating jobs

Many variational algorithms require computing the gradient of the cost function \(E(\vec{\theta}) = \langle \psi(\vec{\theta}) | H | \psi(\vec{\theta})\rangle\). The gradient can be used in gradient-based optimization methods. Qaptiva jobs come with methods to compute the derivative of \(E(\vec{\theta})\) automically: differentiate() and gradient().

Examples of use of this feature are given in the "differentiating jobs" notebook.

Testing for several initializations of the parameters

Variational optimization tends to be sensitive to the initial parameters it started from. You can insert the MultipleLaunchesAnalyzer Plugin before a VQE optimizer to automatically perform several VQE runs and keep the lowest-energy result. This is illustrated in the notebook Making several VQE runs and keeping the best result with the MultipleLaunchesAnalyzer plugin.