Number of Shots

Hello everyone,
what is the number of shots? when I use a default.qubit device. Is the number of shots affects the performance of the circuit.
Thanks.

Hi @ZainabAli!

When you submit a job to a quantum hardware device, the device will execute the submitted circuit, and then perform measurements on each wire — on a qubit device, the output will be a list of '0’s and '1’s.

By generating a large number of samples, we can estimate interesting quantum statistics — including the expectation value and variance of observables, or quantum probability distributions.

The more samples, or shots, we take, the better the estimate of the output statistic.

In PennyLane, you specify how many shots you want to use to estimate these statistics when you create your device:

dev = qml.device("qiskit.ibmq", backend="ibmq_rome", wires=5, shots=1000)

@qml.qnode(dev)
def circuit(x):
    qml.RX(x, wires=0)
    return qml.expval(qml.PauliZ(0))

When we execute this circuit, we will get the expectation value of the Z observable using 1000 shots:

>>> circuit(0.54)
0.32543
>>> circuit(0.54)
0.33764

The output has some level of stochasticity — repeated evaluations will give slightly varying output.

When using simulators, we also have the option of calculating the exact statistics. The simulator will compute the wavefunction of the circuit prior to measurement, and calculate the exact result:

dev = qml.device("default.qubit", wires=5, analytic=True)

This is the result you would expect if you increase the number of shots on a sampling device \rightarrow\infty. Here, repeated circuit evaluation will always return the same, exact, result.

So to answer your question: on devices that sample, such as hardware devices or simulators with analytic=False, increasing the number of shots increases the accuracy of the output.

Some simulators, however, can compute these statistics exactly, so are not affected by changing the number of shots.

Hope that helps!

Hi @josh,
Thanks a lot for your explanation.
Can I use another gate to measure expectation values instead of Z gate?

Hi, @ZainabAli, definitely! A list of available observables is available here: https://pennylane.readthedocs.io/en/stable/introduction/operations.html#qubit-observables

I am working on the Pennylane codebook and am stuck in a weird situation in I.11 module called Multi-Qubit Systems.


In the above, I have not put constraints on shots. And it is giving me a slightly wrong answer. It is giving me somewhat 0.999999999996
And this answer is constant over many trials that is, it doesn’t change much if I re-run the code.

But the answer should be 1. So, I put 100 as my number of shots and I get 1. I put in 10 shots, and I get 1. So, I thought that the default number of shots should be very less or what? So, I put the number of shots as 1, and still get 1. So, I am getting a round-off error in default settings but not in the situation when I put some numbers in shots. This seems some different route is been taken while calculating.

What is happening at the backend? Why isn’t the answer changing over different trials?

Hi @Prabhat_Kumar, welcome to the Forum!

On real quantum hardware, the larger the number of shots, the more accurate the answer will be. In this case however we’re working with simulators. default.qubit is in fact a simulator. If you don’t define the number of shots, the expectation value is calculated as if you had an infinite number of shots. However this requires numerical computations that have a certain level of precision. In this case the digit that fails is the 16th after the decimal point. It’s so small that we can basically say that the number is 1.

I hope this helps answer your question!

1 Like