Quantum technology is still in its infancy. And like any infant, it is small, noisy and lovable, a state of affairs captured by the acronym NISQ (Noisy Intermediate-Scale Quantum). To realistically model the machines our circuits will run on, and the robustness of the algorithms themselves, we need to add noise to our simulations. Thankfully, PennyLane has a variety of ways for doing this! This how-to guide briefly explores three different methods: classical parametric randomness, PennyLane’s built-in default.mixed
device, and plugins to Cirq and Qiskit. So, without further ado, let’s make some noise for NISQ!

Classical randomization
We’ll start with a cheap, direct method for creating noise: classically randomizing parameters. This reflects, for instance, the fact that experimentalists can only twiddle knobs with finite precision. Let’s pause briefly and think about what this does. We know that measurement in quantum mechanics is an intrinsically random process. Even if we have a completely deterministic circuit producing a state
over many measurements is well-defined. But if
will also fluctuate with
import pennylane as qml
from pennylane import numpy as np
dev1 = qml.device("default.qubit", wires=1)
@qml.qnode(dev1)
def rot_circuit(prec):
rand_angle = np.pi + prec*np.random.rand()
# np.random.rand() uniformly samples from [0, 1)
qml.RX(rand_angle, wires=0)
return qml.expval(qml.PauliZ(0))
Our random angle is
>>> print(rot_circuit(4))
0.8340865579113448
>>> print(rot_circuit(4))
-0.910006272110444
>>> print(rot_circuit(0.1))
-0.9999434937900038
>>> print(rot_circuit(0.1))
-0.9982625520423022
This behaves as we expect! We see that basic classical noise is easy to add by hand.
Density matrices
Instead of working with classical noise, we can wrangle noise quantum-mechanically using the formalism of density matrices. PennyLane lets us describe density matrices using the default.mixed
device, invoked as follows:
dev2 = qml.device('default.mixed', wires=1)
Recall that the most general physical operations acting on density matrices are quantum channels
Each
PennyLane provides various standard noise operations. The simplest example is the bit flip channel, which swaps
This may seem too trivial to be of physical interest, but turns out to be very important when considering the effects of cosmic rays on terrestrial computation (sort of). The bit flip channel is implemented by the BitFlip
method in PennyLane:
@qml.qnode(dev2)
def bitflip_circuit(p):
qml.BitFlip(p, wires=0)
return qml.expval(qml.PauliZ(0))
As before, we can measure default.mixed
is initialized to the density
>>> print(bitflip_circuit(0.01))
0.98
>>> print(bitflip_circuit(0.99))
-0.98
Once again, this is what we expect! Other channels include PhaseFlip
(randomly applies AmplitudeDamping
(randomly destroy amplitude information), PhaseDamping
(randomly destroy phase information) and the DepolarizingChannel
(randomly destroy all information).
Finally, you can use QubitChannel
to customize your noise! As a simple example, let’s make the bit phase flip channel, which applies
which we can input to the QubitChannel
method as follows:
@qml.qnode(dev2)
def bitphaseflip_circuit(p):
K0 = np.sqrt(1-p)*np.eye(2)
K1 = np.sqrt(p)*np.array([[0,1j],[1j,0]])
qml.QubitChannel([K0, K1], wires=0)
return qml.expval(qml.PauliZ(0))
We get the same results as the bit flip channel, since the effect is the same up to phase (as the name of the channel suggests):
>>> print(bitphaseflip_circuit(0.01))
0.98
>>> print(bitphaseflip_circuit(0.99))
-0.98
Cirq
Quantum circuits may be run on a variety of backends, some of which have their own associated programming languages and simulators. PennyLane can interface with these other languages via plugins. We will look at two examples: Cirq and Qiskit. Before proceeding, ensure the plugins are installed by running
pip install pennylane-cirq
and
pip install pennylane-qiskit
from the command line. Let’s start with Cirq. We simply import ops
, which gives access to Cirq’s quantum channels, and run on the cirq.mixedsimulator
device:
from pennylane_cirq import ops as cirq_ops
dev3 = qml.device("cirq.mixedsimulator", wires=1)
@qml.qnode(dev3)
def bitflip_circuit_cirq(p):
cirq_ops.BitFlip(p, wires=0)
return qml.expval(qml.PauliZ(0))
Let’s try this out:
>>> print(bitflip_circuit_cirq(0.01))
0.980000008828938
>>> print(bitflip_circuit_cirq(0.99))
-0.980000008828938
See this tutorial for more on Cirq’s noise operations.
Qiskit
The Qiskit simulator Aer is provided by qiskit.aer
, with noise operations living in qiskit.providers.aer.noise
. Qiskit has a different approach, incorporating a noise model into the device itself. We’ll do a slightly more interesting example, and attach a bit flip error to a Hadamard gate:
import qiskit
import qiskit.providers.aer.noise as noise
# create a bit flip error with probability p = 0.01
p = 0.01
my_bitflip = noise.pauli_error([('X', p), ('I', 1 - p)])
# create an empty noise model
my_noise_model = noise.NoiseModel()
# attach the error to the hadamard gate 'h'
my_noise_model.add_quantum_error(my_bitflip, ['h'], [0])
dev4 = qml.device('qiskit.aer', wires=1, noise_model = my_noise_model)
@qml.qnode(dev4)
def bitflip_circuit_aer():
qml.Hadamard(0)
return qml.expval(qml.PauliZ(0))
Let’s check the output of our circuit:
>>> print(bitflip_circuit_aer())
-0.080078125
Since the probability of a bit flip is small, and the expectation of
This completes our whistlestop tour of noise simulation in PennyLane. You now know how to simulate a jittery knob, a cosmic ray event, or an arbitrary Kraus operator, and how to interface with noisy simulation in Cirq and Qiskit. Time for you to make your own noise!
About the author
David Wakeham
Having fun at the intersection of quantum algorithms, symmetry, statistical learning theory and large language models.