Parallelizing MB-QML protocolsΒΆ

Note

This tutorial is under construction

In this tutorial, we will see how to parallelize the MB-QML protocols in mentpy. Similar to the previous tutorial, we first need a MB-QML model to work with.

In [1]: gs = mp.templates.muta(2,1, one_column=True)

In [2]: gs[3] = mp.Ment('X')

In [3]: gs[8] = mp.Ment('X')

In [4]: ps = mp.PatternSimulator(gs)

In [5]: mp.draw(gs)
Out[5]: (<Figure size 800x300 with 1 Axes>, <Axes: >)
savefig/muta_mbqml.png

Then, when we define a loss function, we can use the pathos package to parallelize the computation of the infidelity between the target states and the output states.

In [6]: from pathos.multiprocessing import ProcessingPool as Pool

In [7]: def loss(output, target):
   ...:     avg_fidelity = 0
   ...:     for sty, out in zip(target, output):
   ...:         sty = mp.calculator.pure2density(sty)
   ...:         avg_fidelity += 1-mp.calculator.fidelity(sty, out)
   ...:     ans = (avg_fidelity/len(target))
   ...:     return ans
   ...: 

In [8]: def prediction_single_state(thetas, st):
   ...:     ps.reset(input_state=st)
   ...:     statek = ps(thetas)
   ...:     return statek
   ...: 

In [9]: def prediction(thetas, statesx):
   ...:     thetas = np.copy(thetas)
   ...:     pool = Pool()
   ...:     output = pool.map(prediction_single_state, [thetas]*len(statesx), statesx)
   ...:     return output
   ...: 

In [10]: def cost(thetas, statesx, statesy):
   ....:     outputs = prediction(thetas, statesx)
   ....:     return loss(outputs, statesy)
   ....: 

This will significantly speed up the computation of the loss function!