In Qiskit, if you want to compute the expectation value of a given operator $\hat{O}$ using the matrix_product_state
-based simulation, you can use the primitive BackendEstimator
as follows:
from qiskit.circuit.random import random_circuit
from qiskit.quantum_info import SparsePauliOp
from qiskit_aer import AerSimulator
from qiskit.primitives import BackendEstimator
num_qubits = 5
psi = random_circuit(num_qubits=num_qubits, depth=10, seed=42)
O = SparsePauliOp('Z' * num_qubits)
mps_simulator = AerSimulator(method='matrix_product_state')
estimator = BackendEstimator(backend=mps_simulator)
job = estimator.run(circuits=[psi], observables=[O])
print(job.result())
However, I don't think it is possible to compute $\langle \psi | \hat{O} | \psi \rangle$ exactly even if you pass shots=None
to the method BackendEstimator.run()
. The default number of shots is 1024 as you can see by printing the job result.
I also suggest to take a look at tenpy
, which is a pure Python library for the simulation of quantum systems with tensor networks.