# Expectation values using qiskit

Expectation values can be calculated using

$$\bf{Matrix}$$ $$\bf{mechanics}:$$

$$A$$ has eigenvalues $$\lambda_j$$ and eigenstates $$\Phi_j$$. Then the expectation value of $$A$$ with respect to a state $$\Psi=\sum_j c_j\Phi_j$$ is calculated as

$$\left\langle A\right\rangle_{\Psi}=\left\langle \Psi |A|\Psi\right\rangle=\sum_j \lambda_j c_j|\left\langle\Psi|\Phi_j \right\rangle |^2=\sum_j \lambda_j |c_j|^2$$, where $$c_j=\left\langle\Phi_j|\Psi \right\rangle$$

or

$$\bf{Quantum}$$ $$\bf{algorithms}:$$

The state $$\Psi$$ is created by a unitary transform V, such that $$|{\Psi}\rangle=V|0,0,0...\rangle$$.

Therefore $$\left\langle A\right\rangle_{\Psi}=\left\langle \Psi |A|\Psi\right\rangle=\left\langle 0,0,0...|V^\dagger A V|0,0,0...\right\rangle$$.

So we repeatedly measure the state $$V^\dagger A V|{0,0,0...}\rangle$$ in the computational basis and determine the probabilty to measure $$|{0,0,0...}\rangle$$, which yields $$\left\langle A\right\rangle_{\Psi}$$.

So I create a state $$\Psi=V|{0,0,0...}\rangle$$ and operator A and compute

which agrees with results from Matrix Mechanics.

However, if I build a circuit from operators $$V^\dagger,A,V$$ and measure the probabilities for $$|{0,0,0...}\rangle$$, the results do not agree. What am I doing wrong?

Note that $$A$$ isn't actually a quantum gate because it isn't necessarily unitary, so $$V^\dagger AV| \mathbf{0} \rangle$$ is not even a valid quantum circuit. Suppose that $$A$$ is unitary (for example a Pauli product); the probability of measuring $$\mathbf{0}$$ still isn't $$\langle A \rangle_\psi := \langle \mathbf{0} | V^\dagger AV | \mathbf{0} \rangle$$, but rather $$\langle A \rangle_{\psi}^2$$. To see this, suppose $$|\phi\rangle = [\phi_0, \phi_1, ... \phi_{n-1}]$$ Then $$\langle 0 | \phi \rangle = \phi_0$$, which is generally a complex number; the probability of measuring $$\mathbf{0}$$ is a real number $$|\phi_0|^2$$.

To measure the expectation value $$\langle A \rangle_\psi$$ of an observable $$A$$ in the state $$| \psi \rangle := V| \mathbf{0} \rangle$$:

1. Construct the circuit to prepare the state $$| \psi \rangle$$
2. Apply gates to the end of your circuit that will rotate the state into the diagonal basis of $$A$$
3. Run the circuit and read the measurements
4. Calculate the mean of the eigenvalues corresponding to each measurement result

Notice that at no point do we ever directly use the operators $$A$$ or $$V^\dagger$$.

For example, suppose your observable is $$Y_0 X_1$$. The unitaries that diagonalize $$Y$$ and $$X$$ are $$HS^\dagger$$ and $$H$$ respectively; to see this, let's look at $$X$$: $$\langle \psi | X | \psi \rangle = \langle \psi | H^\dagger ZH| \psi \rangle = \langle \psi | HZH| \psi \rangle$$ As such, you need to add the gate sequence $$[Z, S, H]$$ to qubit 0 and $$H$$ to qubit 1. Suppose then that you run 10 shots, getting the results $$[11, 00, 00, 01, 10, 10, 11, 11, 00, 00]$$ The eigenvalues corresponding to the outcomes $$[00, 01, 10, 11]$$ are $$[1, -1, -1, 1]$$ respectively, so your estimated expectation is $$\langle A \rangle_\psi \approx \frac{1 + 1 + 1 - 1 - 1 - 1 + 1 + 1 + 1 + 1}{10} = 0.4$$

However, in Qiskit, you don't have to add the basis rotations or do any calculations with the measurements; instead, an Estimator takes care of all that for you:

circuit = QuantumCircuit(num_qubits)
circuit...  # build circuit implementing V
estimator = Estimator(...)
op = ...  # observable A
expectation_value = estimator.run(circuit , op).result().values


If you do decide to do the calculations from the measurements manually, remember that Qiskit results are little-endian; the results are ordered from the last qubit to the first.

• Interesting, thanks a lot. I have to put some thought into this. Jan 25 at 15:17
• I want to learn as much as possible from my mistakes. I understand the method that you suggest. I do not understand though why the method that I suggest does not work in case that A is a unitary operator. Is there somethig wrong with the general idea? I checked my code for the case that A=Identity, then the result makes sense since i get the $|0,0,..\rangle$ state as output. Jan 26 at 6:02
• Edited my answer for additional clarification! Remember that the probability of a given basis state is the squared modulus of the amplitude; $P(k) = |\psi_k|^2$, not $\psi_k$. Jan 27 at 3:27