# VQE - How to get from expectation value to eigenvalue?

In VQE, for a single-qubit Hamiltonian, I can use a standard ansatz to make a state $$\psi$$ and use two products to compute the expectation value $$\langle\psi|{\cal H}|\psi\rangle$$. As I vary the parameters to the ansatz I get very close to the minimal eigenvalue. That's good.

In a physical setting, I can't do the multiplication. I have to measure in the Pauli Bases, which yields expectation values, and then - somehow - I should find the eigenvalues from the expectation values. There is a big hole in my understanding. For example, for this Hamiltonian: $${\cal H} = 2.0 X + 0.5 Z$$ The precise eigenvalues are [-2.0615528 2.0615528]. I make an ansatz with $$R_x(\theta)$$ and $$R_y(\phi)$$. Now, just as an initial experiment, I set these angles to 0.3, 0.7 to produce $$\psi$$ and compute $$\psi' = {\cal H}\psi$$. I measure $$\psi'$$ in the computational basis (z-basis, projecting onto $$|0\rangle$$ and $$|1\rangle$$) and get an expectation value of 1.74. $$\cal H$$ is not unitary, hence this unnormalized result. I modify the ansatz and add a Hadamard gate to "rotate" the x-axis into the z-axis. I measure again and get an expectation value of 2.403.

So now what. How do I get from here to an eigenvalue / minimal energy state?

Iterating over all angles $$\theta, \phi$$ does not seem to yield meaningful values.

First, note that we can only measure in the computational basis in quantum computing (at least at the moment). But this is not a problem since \begin{align} \langle \psi | H | \psi \rangle = \langle \psi | 2X + 0.5 Z | \psi \rangle &= 2\langle \psi | X |\psi \rangle + 0.5\langle \psi|Z|\psi\rangle \\ &= 2\langle \psi|HZH|\psi\rangle + 0.5\langle\psi|Z|\psi\rangle \\ &= 2\langle\psi H|Z|H\psi\rangle + 0.5\langle\psi|Z|\psi\rangle \end{align}

This means we need to create to different quantum circuits to the problem. One for each term below. Now $$|\psi \rangle = U |\psi\rangle_{ref}$$ where $$|\psi\rangle_{ref}$$ can be taken as $$|0\rangle$$ if you wish. The unitary matrix $$U$$ can be taken as what you see fit. For instance, if you believe that the eigenstate will have only real coefficients then you can use take $$U = RY(\theta)$$. If taken $$U$$ to be $$RY$$ then we would have the following two circuits: To compute the expectation, you would need to do sufficient enough shots to build a statistical distribution. Since we measure in the computational (Z) basis, $$|0\rangle$$ corresponds to the $$+1$$ eigenspace, and $$|1\rangle$$ corresponds to the $$-1$$ eigenspace.

To see this more clearly, let's suppose you run 1000 shots experiment on circuit 1, to calculate $$\langle \psi | X | \psi \rangle$$, and you recorded/measured 700 times the state $$|0\rangle$$ and 300 times the state $$|1\rangle$$ from your 1000 shots. This implies, that $$\langle \psi | X | \psi \rangle \approx \dfrac{700 - 300}{1000} = 0.4$$ Then similarly, you do 1000 shots experiment on your second circuit to calculate $$\langle \psi |Z| \psi \rangle$$ and you measured 800 times the state $$|0\rangle$$ and 200 times the state $$|1\rangle$$ then this implies $$\langle \psi | Z | \psi \rangle \approx \dfrac{800 - 200}{1000} = 0.8$$ Putting it altogether, we have that $$\langle \psi | H | \psi \rangle \approx 2\cdot 0.4 + 0.5\cdot0.8 = 1.2$$

Note that I used the approximation symbol, $$\approx$$ because to get the exact value, you must do $$\infty$$ number of shots.

Now that you have found what $$\langle \psi | H \psi \rangle$$ is for that specific $$\theta$$ value, you can change it in such a way that it will minimize $$\langle \psi | H \psi \rangle$$. So that at the next iteration, you expect something smaller, in our case, smaller than 1.2. There are many ways you can change $$\theta$$, depending on what optimizer you want to use. Gradient based or Gradient free. There are many classical optimizers and each has its pro and con. Here, take a look at some of the optimizer that you can use directly from Scipy. There are more sophisticated optimizer that aim specifically at Variational Quantum algorithms like VQE but those are not always available publicly so you have to write the code yourself. And you can invent your own optimizer that you think work best as well.

An important thing to note here is that our cost function, $$C(\theta) = \langle \psi | H | \psi \rangle$$ is bounded by the variational principle. that is, we know that $$\langle \psi | H | \psi \rangle \geq E_0$$ where $$E_0$$ is the minimum eigenvalue of $$H$$ and $$|\psi \rangle = U(\theta) |\psi \rangle_{ref}$$. Thus, by keep minimizing the expectation we found during our experiment by changing $$\theta$$ in the right direction, we will indeed approach this minimum value $$E_0$$.

If your Hamiltonian has a Pauli $$Y$$ in it, then you need to calculate $$\langle \psi | Y | \psi \rangle = \langle \psi | HSZHS^\dagger |\psi \rangle = \langle \psi HS | Z | HS^\dagger \psi \rangle$$ This means you need to first apply $$S^\dagger$$ follow by the Hadamard gate $$H$$ to your circuit before measuring in the computational (Z) basis. This can be visualized by the following circuit: • Thanks. This makes sense to me all the way down to the last equation where you plug in the known values 2.0 and 0.5. Somehow I assumed that I don't have to know these values. What I learned is that yes, you do need to know this factors to approximate the unknown minimal eigenvalue. Thanks much! Mar 21, 2021 at 4:54
• @rhundt No problem. So we do need to have those values, also known as Pauli weights, to compute the full expectation at the end. We only need the Pauli string/term to construct the circuits. But those Pauli weights are needed at the end to put together with the results you obtained from the quantum circuits to approximate the entire expectation of the Hamiltonian. Mar 21, 2021 at 4:59
• Thanks. BTW - what would be the circuit if the Hamiltonian would contain an Y-gate. I experimented with HS and HS^dagger, but the results are very inaccurate. Mar 21, 2021 at 5:41
• Make sure you apply the $S^\dagger$ first then $H$ gate. I added the circuit in the edited answer. Mar 21, 2021 at 6:01
• That's it. Thanks again. Mar 21, 2021 at 6:36