I am trying to calculate the expectation value of a given observable for a certain state $\psi$ using qiskit primitives Sampler (using Sampler requires some further calculations) and Estimator. I expect the two values to be the same. However, I notice they differ significantly for nearly all test cases. I probably calculated something wrong, but I cannot figure out what or where? I would appreciate it if someone could shed some light on the subject.
According to qiskit documentation, the Estimator calculates expectation value using the following formula: $$\left\langle\hat{H}\right\rangle_\psi=\sum_{\lambda} p_\lambda\lambda=\left\langle\psi\left|\hat{H}\right|\psi\right\rangle$$
Where $\hat{H}$ is expressed as a linear combination of Pauli operators.
Here is an example: Defining $\hat{H}=X+Z$, I will calculate for $\left\langle\hat{H}\right\rangle_+=\left\langle+\left|\hat{H}\right|+\right\rangle$.
observable = SparsePauliOp.from_list(
[
("X", 1),
("Z", 1)
]
)
circuit = QuantumCircuit(1)
circuit.rx(np.pi/2, 0)
Using Sampler primitive:
cir = circuit.copy()
cir.measure_all()
sampler = Sampler()
job = sampler.run(cir)
probabilities = job.result().quasi_dists[0]
observableMat = observable.to_matrix()
probabilitiesAr = np.sqrt(
np.array([probabilities.get(i, 0) for i in range(observable.num_qubits+1)])
)
expectationValue = np.inner(np.conj(probabilitiesAr), np.dot(observableMat, probabilitiesAr))
print(expectationValue.real)
#outputs 1.0
Using Estimator primitive:
Estimator().run(circuit, observable).result().values[0].real
#outputs 2.220446049250313e-16
Note that the two values are vastly different. However, I expect the result to be that indicated by the Sampler method: $$\left(\frac{1}{\sqrt2}\right)^2\left[\begin{matrix}1&1\\\end{matrix}\right]\left[\begin{matrix}1&1\\1&-1\\\end{matrix}\right]\left[\begin{matrix}1\\1\\\end{matrix}\right]=1$$
My question is : why does the Estimator output 0?