Skip to content

Variational Quantum Eigensolver (VQE) with Classiq

View on GitHub

The variational quantum eigensolver (VQE) is arguably the most common variational quantum algorithm. It combines quantum and classical techniques to solve optimization problems from industries such as finance, logistics, and chemistry. VQE works because of the variational principle, which defines the relationship between the lowest energy of a system and the expectation value of a given state, declaring a lower bound to the expectation value.

We are going to create a VQE algorithm to estimate the minimum energy(eigenvalue) of the following Hamiltonian


where \(Z\) and \(X\) are Pauli Operators and the ansatz we take is a single qubit \(RY\) gate

from typing import List

from classiq import *

# Defining the Hamiltonian from the problem
    "HAMILTONIAN", List[PauliTerm], [PauliTerm([Pauli.Z], 1), PauliTerm([Pauli.X], 0.1)]

# Defining the Ansatz for the Problem
def main(q: Output[QBit], angles: CArray[CReal, 1]) -> None:
    allocate(1, q)
    RY(angles[0], q)

# Defining the Variational Quantum Eigensolver primitives with proper paramters
def cmain() -> None:
    res = vqe(
        HAMILTONIAN,  # Hamiltonian of the problem
        False,  # Maximize Parameter
        optimizer=Optimizer.COBYLA,  # Classical Optimizer
    save({"result": res})

qmod = create_model(main, classical_execution_function=cmain)
qprog = synthesize(qmod)
write_qmod(qmod, name="vqe_primitives")

We can now model and get the results for the created vqe primitive:


The list of other optimizers can be found here: Link

estimation = execute(qprog)
# res.open_in_ide()
vqe_result = estimation.result()[0].value
print("Minimal energy of the Hamiltonian",
print("Optimal parameters for the Ansatz", vqe_result.optimal_parameters)
Minimal energy of the Hamiltonian -1.01240234375
Optimal parameters for the Ansatz {'angles_0': 9.566986160448005}

Mathematical Background

To find the minimal energy of the Hamiltonian \(H = Z + 0.1X\) using the Variational Quantum Eigensolver (VQE) with an ansatz that consists of a simple \(RY\) gate, we can follow these steps:

First we define the Hamiltonian:
The Hamiltonian is given by:

\[H = Z + 0.1X\]

where \(Z\) and \(X\) are the Pauli-Z and Pauli-X operators, respectively.

We can write the given Pauli matrices:
The Pauli-Z and Pauli-X matrices are:

\[Z = \begin{pmatrix} 1 & 0 \\ 0 & -1 \end{pmatrix}, \quad X = \begin{pmatrix} 0 & 1 \\ 1 & 0 \end{pmatrix}\]

Taking the Ansatz:
We use a single \(RY\) gate as the ansatz. The \(RY(\theta)\) gate is defined as:

\[RY(\theta) = \begin{pmatrix} \cos\left(\frac{\theta}{2}\right) & -\sin\left(\frac{\theta}{2}\right) \\ \sin\left(\frac{\theta}{2}\right) & \cos\left(\frac{\theta}{2}\right) \end{pmatrix}\]

The ansatz state can be written as:

\[|\psi(\theta)\rangle = RY(\theta)|0\rangle\]

We now proceed to the expectation value calculation:
The goal is to find the expectation value of the Hamiltonian \(H\) with respect to the ansatz state \(|\psi(\theta)\rangle\):

\[E(\theta) = \langle \psi(\theta) | H | \psi(\theta) \rangle\]

This involves calculating the expectation values of \(Z\) and \(X\) with respect to \(|\psi(\theta)\rangle\).

Calculations can be proceed:
The expectation value of \(Z\) with respect to \(|\psi(\theta)\rangle\) is:

\[\langle \psi(\theta) | Z | \psi(\theta) \rangle = \cos(\theta)\]

The expectation value of \(X\) with respect to \(|\psi(\theta)\rangle\) is:

\[\langle \psi(\theta) | X | \psi(\theta) \rangle = \sin(\theta)\]

Therefore, the total expectation value is:

\[E(\theta) = \cos(\theta) + 0.1\sin(\theta)\]

Minimizing the Energy:
To find the minimal energy, we need to minimize \(E(\theta)\) with respect to \(\theta\). This can be done by solving the equation:

\[\frac{dE(\theta)}{d\theta} = -\sin(\theta) + 0.1\cos(\theta) = 0\]

Solving this gives:

\[\tan(\theta) = 0.1\]
\[\theta = \tan^{-1}(0.1)\]

Minimal Energy:
Substitute \(\theta\) back into the expression for \(E(\theta)\):

\[E_{\text{min}} = \cos(\tan^{-1}(0.1)) + 0.1\sin(\tan^{-1}(0.1))\]

Using the identities:

\[\cos(\tan^{-1}(x)) = \frac{1}{\sqrt{1+x^2}}, \quad \sin(\tan^{-1}(x)) = \frac{x}{\sqrt{1+x^2}}\]

For \(x = 0.1\):

\[E_{\text{min}} = \frac{1}{\sqrt{1+0.01}} + 0.1 \cdot \frac{0.1}{\sqrt{1+0.01}} = \frac{1}{\sqrt{1.01}} + \frac{0.01}{\sqrt{1.01}} = \frac{1.01}{\sqrt{1.01}} = \sqrt{1.01}\]

Thus, the minimal energy is:

\[E_{\text{min}} = \sqrt{1.01}\]

However, considering that the eigenvalues of \(Z\) are \(\pm 1\), the minimal energy will actually be:

\[E_{\text{min}} = -\sqrt{1.01}\]

Thus, the minimal energy is:

\[E_{\text{min}} = -\sqrt{1.01} \approx -1.004987562112089\]

It is very similar to the result which we observed based on the VQE primitive in the Classiq simulator.