Background
In 2022 Qiskit released a very nice tutorial detailing how the expectation value of an operator could be found using CircuitOp
. As of 2024, these techniques (and others that I have found on the quantum computing stack exchange) are unusable as they are deprecated. The migration guide suggests using Estimators. As a test, I tried writing some code to calculate the exact (non-simulated) expected value of $\langle0|H|0\rangle$ which, I believe, should be equal to $\dfrac{1}{\sqrt{2}}$. Instead, I got $0.9999999999999999$. I include my code below (as it is quite short).
The Code
from qiskit import QuantumCircuit
from qiskit.primitives import Estimator
testCircuit = QuantumCircuit(1)
testCircuit.h(0)
estimator = Estimator()
result = estimator.run(testCircuit, 'I').result()
print(result.values[0])
The Question
What is the best practice (in 2024) for calculating the exact expected value of any observable specified as a matrix and/or circuit using Qiskit?