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?

  • 1
    $\begingroup$ 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. $\endgroup$
    – Cryoris
    Apr 11, 2021 at 12:42

1 Answer 1


$\newcommand{\Ket}[1]{\left|#1\right>}$ $\newcommand{\ket}[1]{|#1\rangle}$ $\newcommand{\bra}[1]{\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, [0])
qc.rx(np.pi/3, [0])
qc.cx(0, 1)

operator = Z
psi = StateFn(qc)
expectation_value = (~psi @ operator @ psi).eval()
  • $\begingroup$ 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. $\endgroup$ Apr 13, 2021 at 6:52
  • $\begingroup$ Moreover, Can I input a matrix or tensor (each term is a Pauli string) to the expectation function? $\endgroup$ Apr 13, 2021 at 7:21
  • $\begingroup$ 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/… $\endgroup$ Apr 13, 2021 at 9:27
  • $\begingroup$ You can input a matrix and tensor to the expectation function as well $\endgroup$ Apr 13, 2021 at 9:28
  • $\begingroup$ 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? $\endgroup$ Apr 13, 2021 at 12:22

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.