# Compute measurement values of observables by Qiskit

Is there a way to get certain measurement results by variational quantum eigensolver in qiskit? More specifically, I want to get measurement results for certain operators, like $$\langle a^\dagger_ia_j\rangle$$ (which I assume that I later transform it into the qubit version using Jordan-Wigner transformation) for every $$i,j$$ in the system. Therefore, I need to get the measurement results for the matrix for, say, $$10^4$$ shots. I noticed that the VQE class provides the function to calculate the expection value, can I use this function to get my job done, or there are any other ways to achieve this?

• Do you have the state with respect to which you want to compute the expectation value available, or do you need to prepare it first with the VQE? If you have it available, you can use Qiskit's operators directly. Otherwise, if you first need to optimize your state with the VQE, you can pass aux_operators to VQE.compute_minimum_eigenvalue to evaluate the observable at the final state. Apr 11, 2021 at 12:42

$$\newcommand{\Ket}{\left|#1\right>}$$ $$\newcommand{\ket}{|#1\rangle}$$ $$\newcommand{\bra}{\left<#1\right|}$$

I think you can find the answer to your question here: Circuit for VQE Expectation Value Finding

If you want to get the expectation value (average value) of an operator corresponding to an observable on a quantum state you can follow this formula:

$$E(M) = \braψM\ketψ$$

Here an example code:

from qiskit import QuantumCircuit
import numpy as np
from qiskit.aqua.operators import StateFn
from qiskit.aqua.operators import Z

qc = QuantumCircuit(2)
qc.rz(np.pi/3, )
qc.rx(np.pi/3, )
qc.cx(0, 1)

operator = Z
psi = StateFn(qc)
expectation_value = (~psi @ operator @ psi).eval()
print(expectation_value.real)

• Is this calculation or measurement of the code given? I need to run the circuit on a real quantum device, so I need to measure the expectation value of the obeservable which contains a lot of anti-commute term. Apr 13, 2021 at 6:52
• Moreover, Can I input a matrix or tensor (each term is a Pauli string) to the expectation function? Apr 13, 2021 at 7:21
• The code above is the calculation of expectation value. If you want to measure the expectation value you should check this link: qiskit.org/documentation/apidoc/… Apr 13, 2021 at 9:27
• You can input a matrix and tensor to the expectation function as well Apr 13, 2021 at 9:28
• Thanks. Is there any requirements for the tensor (in my case, it is a four-index tensor), like the data structure? In my case, each element of the tensor is a Pauli string, and should I claim the whole tensor as something in qiksit? Apr 13, 2021 at 12:22