I have several mixing unitary circuits written using Pennylane to be used in the QAOA algorithm. Furthermore, I'd like to write unit tests for these mixing circuits to ensure that the code is doing what it is supposed to in the future as changes are made to the codebase. Consider the basic example:

def x_mixer(beta, wires):
    for i in wires:
        qml.RX(beta, wires=i)

Currently, I'm thinking of using assert statements to check that the output of:

dev = qml.device('lightning.qubit', wires=2, shots=10000)
circuit = qml.QNode(mixer_circuit, dev)
result = circuit(0.5, wires=[0, 1])

is a certain value. Now one issue is that the results themselves are probabilistic and change during each run. My first question: What's the best way to get around this? Can you set random_seed in any of the simulator devices?

In general if someone has ideas on how to do unit testing for Pennylane circuits, it would be really appreciated.


1 Answer 1


Simulator devices, like 'lightning.qubit' or 'default.qubit', can usually be run analytically. This is the default for most devices, but can be explicitly specified by setting shots=None.

Devices inheriting from QubitDevice, like "default.qubit" and "lightning.qubit" currently rely on numpy.random for their random number generation. So you can also specify the global numpy seed to get reproducible results:

import numpy as np

Hope that helps :)

  • $\begingroup$ Thank you very much! Exactly the answer that I was looking for. $\endgroup$ Jun 18, 2021 at 12:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.