Computation procedure when defining number of shots in qml.device


I have a question about how the stochastic outputs of the quantum device are estimated based on the number of shots. I know that the number of shots will affect how accurate can be estimation, but how does it influence the results?

Like, when the wanted output of a circuit is the expectation value of some arbitrary hamiltonian, how the number of shots is used? Does the hamiltonian have to be diagonalized and then the estimation is sampled based in the eigenvalues and associated probabilites? Also, what would be the case when we consider qml.counts?

Thank you so much!

Hi @GICorrer,

The expectation value is the mean of the samples. If you specify a small number of samples then the expectation value will be less accurate.

The following example should be helpful.

In the circuit below you’re applying only one Hadamard. If you measure using qml.sample() on the PauliZ basis, half of the time your measurement will land in the positive PauliZ and half of the times in the negative PauliZ, so if you have 100 shots you should see 100 values that are ‘1’ or ‘-1’. The samples are drawn from the eigenvalues {λi} of the observable. The probability of drawing eigenvalue λi is given by p(λi)=|⟨ξi|ψ⟩|2, where |ξi⟩ is the corresponding basis state from the observable’s eigenbasis.

If you calculate the expectation value you should get a value that is close to zero, which is the mean of 100 samples. If you change to shots=1 then your expectation value will only be calculated based on one sample, so it will be either 1 or -1. If you don’t specify the number of shots then you get the exact expectation value. In this case PennyLane calculates the probabilities of measuring each eigenvalue and then multiplies it by the eigenvalue.

dev = qml.device('default.qubit',wires=1,shots=100)

def circuit():
    return qml.sample(qml.PauliZ(0)) # qml.expval(qml.PauliZ(0))


qml.counts() draws the samples according to the number of shots provided and then arranges them in a dictionary.

Does this answer your question?

1 Like

Thank you, Catalina! This helps me a lot!

I would like to address only one other specific case. When I write a specific hamiltonian using the qml.hamiltonian() method, the procedure is the diagonalization of the hamiltonian in the computational basis and then the same applied for the case of the PauliZ gate? Because I’m trying to build a Heisenberg hamiltonian ground state, but when I sample only one time (i.e. shots=1) to see what happens, I get mean values below the analytic ground state energy, and this seems strange to me only sampling eigenvalues, because then it should at most gives as an output the ground state energy, right?

Hi @GICorrer, can you please share an example code? I’m not sure I’m understanding exactly the situation.

If you’re calculating expval consider that this is an expectation value, or a mean of several measurements. Some measurements will be higher and some will be lower than the mean. Notice that your hamiltonian probably has several terms with several coefficients so if you combine these with shots=1 sometimes you may end up with an expectation value that is lower than the actual ground state energy, but it doesn’t mean that this is a real energy that can be achieved. Again, think that for the mean to be the mean there have to be some lower and some higher values, but it’s the mean that gives you the actual result. Going back to the Hadamard example of last time, if you measure a -1 it doesn’t mean that a Hadamard can actually produce that result, this is an effect of measuring with a low number of shots.

Please let me know if this is more clear or still unclear, or if I understood your question wrong.