## DEV Community is a community of 643,475 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

loading...

# Get Started VQE with Blueqat

Blueqat, the quantum computing library assists to write variational quantum eigensolver (VQE) algorithm.

This article explains how to write VQE for 2x2 Hamiltonian in Blueqat.

If you want to know the overview of VQE, this article is prefer.

# All you need is Hamiltonian and Ansatz

First, you have to make a Hamiltonian, it is the problem to solve with VQE.
Second, you have to prepare "ansatz", it is parametrized quantum circuit.

Blueqat prepares some built in ansatzes like QAOA ansatz. However, sometimes, it is not enough to solve your Hamiltonian.

In this article, we define new ansatz for 1 qubit (2x2 Hamiltonian) VQE.

# `blueqat.pauli` helps to make the Hamiltonian

Making Hamiltonian by Pauli matrices is very easy.

``````from blueqat.pauli import X, Y, Z, I

h = 1.23 * I - 4.56 * X(0) + 2.45 * Y(0) + 2.34 * Z(0)
``````

## Making randomized Hamiltonian

We make randomized Hamiltonian.

``````from random import random
from blueqat.pauli import X, Y, Z, I

h = random() * I + random() * X(0) + random() * Y(0) + random() * Z(0)
``````

Now, `h` is a Hamiltonian with random matrix coefficient.

# Ansatz is a class which inherits `blueqat.vqe.AnsatzBase`

Ansatz is a class which inherits `blueqat.vqe.AnsatzBase` and is implemented `get_circuit()` method.

To make a arbitrary 1 qubit gate, apply a RY gate and a RZ gate is enough.
Let's make the ansatz.

``````from blueqat import Circuit
from blueqat.vqe import AnsatzBase

class OneQubitAnsatz(AnsatzBase):
def __init__(self, hamiltonian):
AnsatzBase.__init__(hamiltonian, 2) # Hamiltonian and the number of parameters

def get_circuit(self, params):
# Return a circuit which applied a RY gate and a RZ gate.
a, b = params
return Circuit().ry(a).rz(b)
``````

Very simple.

# VQE works with a Hamiltonian and an ansatz

``````from blueqat.vqe import Vqe

h = random() * I + random() * X(0) + random() * Y(0) + random() * Z(0)
runner = Vqe(OneQubitAnsatz(h))
result = runner.run()

print('Result by VQE')
print(runner.ansatz.get_energy(result.circuit, runner.sampler))
``````

`runner = Vqe(OneQubitAnsatz(h))`: Make a `runner` to run the VQE with the ansatz.
`result = runner.run()`: Run the VQE and get the result.

`runner.ansatz.get_energy(result.circuit, runner.sampler)`: Get the energy from result.
This is the most complicated line in this article. Copy and paste is your friend!

Now, you obtained the energy (smallest eigenvalue of the Hamiltonian matrix).

## Compare the result with numpy's solution

Is our result correct answer? Let's compare with numpy's solution.

`blueqat.pauli` can transform Hamiltonian to numpy's array by `to_matrix()` method.
We can get the eigenvalue by `np.linalg.eigh`.

``````import numpy as np
# Hamiltonian to matrix
mat = h.to_matrix()
# Calculate by numpy
print('Result by numpy')
print(np.linalg.eigh(mat))
``````

# Run from the beginning to the end

Whole source code is as following:

``````from random import random
import numpy as np
from blueqat import Circuit
from blueqat.pauli import X, Y, Z, I
from blueqat.vqe import AnsatzBase, Vqe

class OneQubitAnsatz(AnsatzBase):
def __init__(self, hamiltonian):
super().__init__(hamiltonian, 2)

def get_circuit(self, params):
a, b = params
return Circuit().ry(a).rz(b)

# Calculate by VQE
h = random() * I + random() * X(0) + random() * Y(0) + random() * Z(0)
runner = Vqe(OneQubitAnsatz(h))
result = runner.run()

print('Result by VQE')
print(runner.ansatz.get_energy(result.circuit, runner.sampler))

# Hamiltonian to matrix
mat = h.to_matrix()
# Calculate by numpy
print('Result by numpy')
print(np.linalg.eigh(mat))
``````

This is the example of result.
You can see VQE result and numpy result are almost same value.

``````Result by VQE
-0.7072131424381115
Result by numpy
-0.7072139372798669
``````

# Summary

Blueqat library can write the VQE easily.