I have a SparsePauliOp object $H$ acting on $N = 21$ qubits, and a statevector $|\psi \rangle$ acting on $N=21$ qubits. I need the expectation of $H$ using this state $|\psi \rangle$: $$\langle H \rangle = \langle \psi|H|\psi\rangle$$ Unfortunately, due to the sheer size of the system, I cannot convert it to a matrix and then use numpy multiplication. Is there any other way I can find this expectation value efficiently enough?
1 Answer
$\begingroup$
$\endgroup$
You can use Statevector.expectation_value()
method as follows:
from qiskit.quantum_info.random import random_statevector, random_pauli_list
from qiskit.quantum_info import SparsePauliOp
# Generate a 21-qubit random Statevector:
N = 21
sv = random_statevector(2 ** N)
# Generate random observable of 5 Paulis:
pauli_list = random_pauli_list(num_qubits=N, size=5)
H = SparsePauliOp(pauli_list)
# Compute the expectation value:
exp_val = sv.expectation_value(H)
print(exp_val)