Solving optimization problems with VQE and qiskit with an example applied to chemistry

January 9, 2023

VQE is a quantum algorithm that can be used to find the ground state energy of a given Hamiltonian, which represents the energy of a quantum system. The ground state energy is the lowest possible energy that the system can have, and it is an important quantity in many fields, including chemistry, materials science, and condensed matter physics.

VQE is an iterative algorithm that uses a quantum circuit, called a "variational form," to approximate the ground state of the Hamiltonian. The quantum circuit is parameterized by a set of variables, and the VQE algorithm adjusts these variables to minimize the energy of the system. A classical optimization algorithm, such as COBYLA or L-BFGS-B, is used to adjust the variables of the quantum circuit.

To implement VQE in Qiskit, we can use the VQE class from the qiskit.aqua.algorithms module. This class takes in a qubit operator (which represents the Hamiltonian in terms of quantum bits, or qubits), a variational form, and an optimizer as input. The qubit operator and variational form can be constructed using other components in Qiskit, such as the Hamiltonian class from Qiskit Chemistry and the RY class from the qiskit.aqua.components.variational_forms module. The optimizer can be imported from the qiskit.aqua.components.optimizers module.

Here is an example of how to use the VQE class to find the ground state energy of a molecule:

from qiskit import Aer, execute
from qiskit.aqua.algorithms import VQE
from qiskit.aqua.components.variational_forms import RY
from qiskit.aqua.components.optimizers import COBYLA
from qiskit.chemistry.drivers import PySCFDriver, UnitsType
from qiskit.chemistry.core import Hamiltonian, TransformationType, QubitMappingType

# define the molecule
driver = PySCFDriver(atom='H .0 .0 .0; H .0 .0 0.735', unit=UnitsType.ANGSTROM, charge=0, spin=0, basis='sto3g')

# define the Hamiltonian
core = Hamiltonian(transformation=TransformationType.FULL, qubit_mapping=QubitMappingType.PARITY, two_qubit_reduction=False)
qubit_op, aux_ops = core.run(driver)

# use the COBYLA optimizer and the RY variational form
optimizer = COBYLA()
var_form = RY(qubit_op.num_qubits, depth=3, entangler_map=[[0, 1]])

# run VQE to find the ground state energy
vqe = VQE(qubit_op, var_form, optimizer)
backend = Aer.get_backend('statevector_simulator')
result = execute(vqe, backend).result()
energy = result.get_energy()
print('Ground state energy of H2 molecule: {}'.format(energy))

In this example, we are using quantum computing to find the ground state energy of a hydrogen molecule. The ground state energy is the lowest possible energy that the molecule can have, and it is an important quantity in chemistry as it determines many of the chemical properties of the molecule. For example, the ground state energy can determine the stability of the molecule, as well as its reactivity and spectroscopic properties.

In general, the lower the ground state energy of a molecule, the more stable it is. This means that the molecule is less likely to break apart or react with other molecules. The ground state energy can also affect the vibrational and rotational spectra of the molecule, which can be observed through techniques such as infrared spectroscopy and microwave spectroscopy. These spectra can provide information about the bond strengths and geometry of the molecule, as well as its energy levels and transition probabilities.

In addition to these properties, the ground state energy can also play a role in the feasibility of chemical reactions. For example, the energy required to break bonds in the reactant molecules must be lower than the energy released in the product molecules in order for a reaction to occur. The ground state energy can therefore help to predict the feasibility of a given reaction.

In the example I provided, the Hamiltonian for the hydrogen molecule is constructed using the Qiskit Chemistry package. This Hamiltonian is then used to generate a qubit operator, which represents the energy of the system in terms of quantum bits (qubits). The VQE algorithm is then used to find the ground state energy of the qubit operator by minimizing the energy using the COBYLA optimizer and the RY variational form. The RY form is a type of quantum circuit that consists of rotation gates applied to individual qubits, and the COBYLA optimizer is a classical optimization algorithm that is used to adjust the parameters of the quantum circuit.

This is part of a series of posts. Next up, we will show how to use qiskit and VQE to solve QUBOs. Check out our introductory post on QUBOs.