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)

estimator = Estimator()

result = estimator.run(testCircuit, 'I').result()

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?


2 Answers 2


This is how you do it with the latest Qiskit Packages:

Step 1: Expectation Value of an Operator

$$E = \langle \psi |O|\psi\rangle$$

Make the circuit you want to measure the $\psi$ with

from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister

circuit = QuantumCircuit(2)
#op = CircuitOp(circuit)

Step 2: The Operator

Make the operator you want to measure the expectation value of:

from qiskit.quantum_info import Pauli, SparsePauliOp

X = Pauli('X')
Y = Pauli('Y')
Z = Pauli('Z')
I = Pauli('I')
operator = SparsePauliOp(["II", "IZ", "ZI","ZZ","XX" ], coeffs = [-1.052373245772859, 0.39793742484318045,-0.39793742484318045,
                                                                  -0.01128010425623538,0.18093119978423156 ])
#op = (-1.052373245772859 * I^I) + (0.39793742484318045 * I^Z) +
# (-0.39793742484318045 * Z^I) + (-0.01128010425623538 * Z^Z) + 
#(0.18093119978423156 * X^X)


Step 3: The State

The state you want to measure against:

from qiskit.quantum_info import SparsePauliOp, Statevector
from qiskit.primitives import Estimator
psi = QuantumCircuit(2)
estimator = Estimator()
#psi = Statevector(psi)
expectation_value = estimator.run(psi, operator).result().values.real

The result


This follows the youtube tutorial you linked, but with the latest qiskit packages, you can change as per you need.


The expectation value can be calculated in a straightforward way using Statevector.expectation_value like this:

from qiskit import QuantumCircuit
from qiskit.quantum_info import Operator, Statevector
from sympy import nsimplify

testCircuit = QuantumCircuit(1)
H = Operator(testCircuit) # define the operator

ket_0 = Statevector.from_label('0') # define the vector
expectation_value = ket_0.expectation_value(H) # calculate the expectation value

display(nsimplify(expectation_value)) # display an exact answer if the decimal expression is recognized

enter image description here


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.