# Calculating the expectation value of an observable in Qiskit

Consider the following quantum circuit that consists of a three qubit quantum register and an ancilla qubit:

Let $$W$$ and $$V$$ be unitary operators. $$U(t)$$ is an implementation of unitary time evolution. Followed by measurement in the computational basis, the two alternate paths on the end supposedly allow for extraction of the expectation of $$\sigma_x$$ and $$\sigma_y$$. So, consider $$\sigma_x$$. Suppose that I perform a measurement after applying the Hadamard. How do I obtain the expectation of $$\sigma_x$$ from that measurement? Namely, is there a way to do this using a function in qiskit?

Note: If it is helpful, this circuit is actually an ancilla qubit measurement algorithm to compute dynamic spin correlation functions. Namely, the expectation of $$\sigma_x$$ and $$\sigma_y$$ are proportional to the real and imaginary parts of the correlation function, respectively. See Section 3F. of the following paper:

https://arxiv.org/pdf/1907.03505.pdf

## 2 Answers

When measuring in qiskit you are always measuring in the $$Z$$ base ($$|0\rangle,|1\rangle$$). So you just need to change the base by inserting the proper gates before measurement. No need to call special functions.

Let me explain. If you want to get $$\langle \sigma_z \rangle$$, you know that you can get two results: +1 (associated to measuring $$|0\rangle$$) and $$-1$$ (associated to measuring $$|1\rangle$$). So if you measure $$N$$ times, where $$n_0$$ times gives $$|0\rangle$$ and $$n_1$$ times $$|1\rangle$$ then $$\langle \sigma_z \rangle=\frac{n_0(+1)+n_1(-1)}{N}=\frac{n_0-n_1}{N}$$.

The same applies to the eigenstates of $$\sigma_x$$ named $$|+\rangle$$ and $$|-\rangle$$:

$$\langle \sigma_x \rangle=\frac{n_+(+1)+n_-(-1)}{N}=\frac{n_+-n_-}{N}$$, where $$n_\pm$$ is the number of times that $$|\pm\rangle$$ is measured.

However as we cannot measure in the $$|\pm\rangle$$ basis, we need to change basis. One way to do that is to add a Hadamard gate at the end of the measurement. Notice that the Hadamard gate transforms states and operators related to $$x$$ into operators of $$z$$, most importantly $$H|0\rangle=|+\rangle$$ and $$H|1\rangle=|-\rangle$$. So if instead of measuring directly, you apply a Hadamard gate first and then measure so you are extracting the counts in the $$|\pm\rangle$$ basis (with the new $$n_0$$ for $$n_+$$, and $$n_1$$ for $$n_-$$). This works because $$n_+=\langle +|\psi\rangle=\langle0|H|\psi\rangle$$ and $$n_-=\langle -|\psi\rangle=\langle 1|H|\psi\rangle$$

For a similar gate for $$\sigma_y$$, you need a more complicated gate that is given by $$HS^\dagger$$ or $$R_x(\pi/2)$$ (be careful as these gates are not Hermitian). So in order to measure $$\sigma_y$$ you need to apply $$HS^\dagger$$ to your state before measuring.

This is well hinted already in the picture of the circuit.

I don't know how efficient it is to do using qiskit, but you can easily find expectation values using 'tequila'.