# How does Qiskit Primitives Estimator compute expectation values?

The Qiskit Primitives Estimator class is implemented to work in two different ways depending on the shots parameter nature:

shots (None or int) – The number of shots. If None, it calculates the exact expectation values. Otherwise, it samples from normal distributions with standard errors as standard deviations using normal distribution approximation.

I guess that, with an integer number of shots, the quantum circuit execution is repeated many times and each measurement is collected in order to get the final result as the measurements average value. But how does Estimator calculate exact expectation values when shots=None?

Taking a look to the qiskit.primitives.estimator.Estimator source code, you can figure out that, when shots=None, the expectation value $$\langle O \rangle = \langle \psi | O | \psi \rangle$$ is computed by something like:

final_state = Statevector(circ)
expectation_value = final_state.expectation_value(obs)


where circ is the QuantumCircuit preparing your state $$\psi$$ and obs is your operator $$O$$. This means that $$\langle O \rangle$$ is computed exactly by performing all the linear algebra calculations implemented in the method Statevector.expectation_value.

More interestingly, when an integer number of shots $$N$$ is passed, Qiskit does not run the simulation $$N$$ times to calculate the mean value of the measurement outcomes; instead, it calls the numpy random generator to sample from a normal distribution $$\mathcal{N}_{\mu,\sigma}$$:

expectation_value = numpy.random.default_rng().normal(μ, σ)


where $$\mu = \langle O \rangle$$ (computed exactly as in the previous case) and $$\sigma = \sqrt{\frac{\langle O^2 \rangle - \langle O \rangle^2}{N}}$$.

• The primitives can be used to access the real hardware devices. But 'Qiskit does not run the simulation N times' applies to simulations only, correct? Jan 31, 2023 at 15:07
• Yes, that's correct. If you run your circuit on real hardware devices you can't even have access to the Statevector object of course. However, you can use Qiskit primitives on IBM simulators as well! Feb 15, 2023 at 12:35

By calculating:

$$\langle \psi | O |\psi\rangle$$

Where $$O$$ is the observable being measured and $$| \psi\rangle$$ is the state before measurement.