Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Variational Quantum Eigensolver (VQE) for the Hydrogen Molecule

Tags: algorithm chemistry variational

This tutorial demonstrates how to implement the Variational Quantum Eigensolver (VQE) algorithm to find the ground state energy of the hydrogen molecule (H₂). We use OpenFermion for generating molecular Hamiltonians.

The workflow is as follows:

  1. Convert the molecular Hamiltonian to qubit operators

  2. Create a parametrized quantum circuit (ansatz)

  3. Implement VQE optimization

  4. Analyze the energy landscape across different atomic distances

We show how to solve quantum chemistry problems using quantum computing, focusing on finding the minimum-energy structure of the H₂ molecule.

Creating the Hamiltonian of the Hydrogen Molecule

Converting to a Qamomile Hamiltonian

In this section, we convert the OpenFermion Hamiltonian to the Qamomile format. After applying the Jordan–Wigner transformation to convert fermionic operators to qubit operators, we use custom conversion functions to create a Hamiltonian representation compatible with Qamomile.

Creating the VQE Ansatz

In this section, we create a Hardware Efficient SU(2) ansatz for the VQE algorithm using the @qkernel decorator. An ansatz is a parametrized quantum circuit that prepares a trial wavefunction. We build it by combining ry_layer, rz_layer, and a linear CX entangling layer, and finally compute the expectation value of the Hamiltonian using expval.

Running VQE with Qiskit

In this section, we transpile the VQE kernel to an executable object using QiskitTranspiler. The default executor runs this object and returns the expectation value, which the defined qkernel computes using expval. Thus, the user only needs to implement the optimisation loop.

<Figure size 2210.55x785.944 with 1 Axes>
         Current function value: -1.105860
         Iterations: 50
         Function evaluations: 2091
         Gradient evaluations: 51
  message: Maximum number of iterations has been exceeded.
  success: False
   status: 1
      fun: -1.1058596669662146
        x: [ 3.123e+00  1.022e+00 ...  1.306e+00  2.499e+00]
      nit: 50
      jac: [-2.432e-04  7.428e-05 ... -2.050e-04 -1.992e-04]
 hess_inv: [[ 3.780e+00  5.350e-01 ...  5.605e-01  4.342e-01]
            [ 5.350e-01  1.987e+00 ... -7.798e-01 -2.818e-01]
            ...
            [ 5.605e-01 -7.798e-01 ...  3.530e+00  2.205e+00]
            [ 4.342e-01 -2.818e-01 ...  2.205e+00  3.405e+00]]
     nfev: 2091
     njev: 51
<Figure size 640x480 with 1 Axes>

Changing the Distance Between Atoms

distance:  0.2 energy:  0.15749274532446333 fci_energy:  0.15748213479836526
distance:  0.29285714285714287 energy:  -0.5679183288459648 fci_energy:  -0.5679447209710013
distance:  0.38571428571428573 energy:  -0.8833482168143287 fci_energy:  -0.8833596636183383
distance:  0.4785714285714286 energy:  -1.0335823410283806 fci_energy:  -1.0336011797110967
distance:  0.5714285714285714 energy:  -1.104177464301668 fci_energy:  -1.1042094222435166
distance:  0.6642857142857144 energy:  -1.1316543840491953 fci_energy:  -1.1323508827075506
distance:  0.7571428571428571 energy:  -1.1368969053665618 fci_energy:  -1.1369026717971333
distance:  0.8500000000000001 energy:  -1.128359326360385 fci_energy:  -1.128361878458112
distance:  0.9428571428571428 energy:  -1.1127239361512136 fci_energy:  -1.1127252078468768
distance:  1.0357142857142858 energy:  -1.0934760878958496 fci_energy:  -1.093476088229404
distance:  1.1285714285714286 energy:  -1.0727482370243173 fci_energy:  -1.0727578805453502
distance:  1.2214285714285713 energy:  -1.052007109639021 fci_energy:  -1.052008162170845
distance:  1.3142857142857143 energy:  -1.0322163819392465 fci_energy:  -1.0322400306247084
distance:  1.4071428571428573 energy:  -1.0141386084973292 fci_energy:  -1.0141470586695496
distance:  1.5 energy:  -0.9980888898562652 fci_energy:  -0.9981493534714101
<Figure size 640x480 with 1 Axes>