Variational Quantum Eigensolver

Nazim Turan
Quantum Sphere
Published in
5 min readJan 13, 2024

The Variational Quantum Eigensolver (VQE) is a method for determining eigenvalues and requires significantly fewer qubits compared to other quantum algorithms. The aim is to find an upper limit for the lowest eigenvalue of a given Hamiltonian operator. VQE requires a constant amount of time for individual measurements on the Quantum Processing Unit (QPU), meaning that the time on the QPU is not significantly limited by decoherence. Moreover, it offers an exponential speedup, similar to the QAOA algorithm, compared to the best classical algorithm.

We first try to determine the expectation values of Hermitian operators. As we know, Pauli strings are measurable, so we represent the Hermitian matrices as a linear combination of Pauli strings. Let’s consider the following theorem for this purpose.

Meaning of the Theorem

The significance of this theorem is that we have an arbitrary Hermitian operator Oi. Consequently, a real linear combination of Hermitian operators is also Hermitian, and the expected value <O>v in the state V is the linear combination with the same coefficients of the expected values of the individual Oi’s that we have selected. The aim is to be able to determine the expected values of Hermitian operators. This is based firstly on the measurement of elements of the Pauli group on the QPU and then on the summation of the measurements on the CPU. Let’s consider the algorithmic flowchart for the expected values:

By applying the variational principle, we obtain:

This also indicates that

is the upper bound for the smallest eigenvalue.

VQE Algorithm

We have

The eigenvalue lambda_min is therefore less than or equal to the expected value of the operator <O> in state v, whose eigenvalue we wish to determine. This in turn is the sum of the linear combination of the corresponding Pauli strings in state v:

Step 1: Choose an arbitrary vector v(a), which depends on the parameter

Step 2: Determine alpha such that

is minimized.

Step 2.1: For this, we first determine the expectation values of Pauli strings in the state v(alpha)

Step 2.2: Subsequently, we calculate the linear combination of the measured expectation values.

Step 2.3: Finally, we use classical optimization methods to minimize (on CPU)

Step 3: Then

VQE Circuit

VQE Algorithm in Python

We will look at two code examples for the VQE algorithm. First, we apply the VQE to calculate a simple Hamiltonian H = Z, using the pyQuil and Grove programming libraries, and in the second example, we implement the VQE with artificial noise.

import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import minimize
from grove.pyvqe.vqe import VQE
from pyquil.quil import Program
from pyquil.paulis import sZ
from pyquil.gates import RX
import pyquil.api as api

# Function to create the ansatz
def small_ansatz(params):
return Program(RX(params[0], 0))

# Create a Hamiltonian H = Z_0
hamiltonian = sZ(0)

initial_angle = [0.0]

# Create a VQE instance with the Nelder-Mead method
vqe_inst = VQE(minimizer=minimize, minimizer_kwargs={'method': 'nelder-mead'})

angle = 2.0 # Manually testing VQE at angle 2.0
vqe_inst.expectation(small_ansatz([angle]), hamiltonian, 10000, qvm)

# Create a loop over a range of angles and expected plots without sampling
angle_range = np.linspace(0.0, 2 * np.pi, 20)
data = [vqe_inst.expectation(small_ansatz([angle]), hamiltonian, None, qvm) for angle in angle_range]

# Create a loop over a range of angles and expected plots with sampling
sampled_data = [vqe_inst.expectation(small_ansatz([angle]), hamiltonian, 1000, qvm) for angle in angle_range]

# Result
result = vqe_inst.vqe_run(small_ansatz, hamiltonian, initial_angle, None, qvm=qvm)
print(result)
# Output: {'fun': -1, 'x': array([3.1416])}

Let’s take a closer look at the code:

First, we import all the necessary libraries and then define our ansatz. In fact, any Python function that takes a list of numerical parameters and returns a pyQuil program can be considered as an ansatz function.

The next step is to define the Hamiltonian. As we know, any Hamiltonian can be expressed as a linear combination of tensor products of Pauli operators, as these form a basis for Hermitian matrices. In our example, we consider the simple case of a Pauli operator H = Z.

Thanks to the Grove programming library, we are able to create an instance of the VQE algorithm, which we use to calculate the eigenvalue for an angle O in the range [0, 2pi) in the ansatz. Here, we use the Nelder-Mead optimization algorithm. We could also use other optimization algorithms, for example from the SciPy library. As we can see, the simple program is capable of finding the correct angle O = pi for the global energy minimum

Photo by Markus Spiske on Unsplash

Thanks for reading!

There will soon be more articles about the topic followed by Python code.

--

--