Computing Library › Worked Examples
Worked Examples

VQE for the Hydrogen Molecule

Estimate the ground-state energy of H2 with a variational quantum eigensolver: a parameterised circuit plus a classical optimizer.

The problem

The electronic ground-state energy of H2 in a minimal (STO-3G) basis can be mapped to a two-qubit Hamiltonian after freezing core and using symmetry. The Hamiltonian is a weighted sum of Pauli strings: H = c0 I + c1 Z0 + c2 Z1 + c3 Z0Z1 + c4 X0X1, with coefficients that depend on bond length.

The ansatz

Kronos motion — classical vs quantum

A one-parameter ansatz suffices near equilibrium: start in the Hartree-Fock state |01>, then apply an entangling rotation exp(-i theta X0Y1/2). The single angle theta captures the dominant double excitation.

The loop

python
import numpy as np
from scipy.optimize import minimize_scalar
# example coefficients near R=0.735 A
c=dict(I=-1.0523,Z0=0.3979,Z1=-0.3979,Z0Z1=-0.0113,X0X1=0.1809)
def state(t):
    # |01> rotated toward |10> by the excitation angle
    return np.array([0,np.cos(t/2),-np.sin(t/2),0])
def energy(t):
    p=state(t)
    Z0=np.diag([1,1,-1,-1]); Z1=np.diag([1,-1,1,-1])
    X0X1=np.fliplr(np.eye(4))
    H=c['I']*np.eye(4)+c['Z0']*Z0+c['Z1']*Z1+c['Z0Z1']*Z0@Z1+c['X0X1']*X0X1
    return p@H@p
r=minimize_scalar(energy,bounds=(-np.pi,np.pi),method='bounded')
print(round(r.fun,4),'Hartree at theta=',round(r.x,4))

Why VQE

VQE is variational: the measured energy is always an upper bound on the true ground state, so optimization can only improve it. It keeps circuits shallow by offloading the search to a classical optimizer, which makes it a leading candidate for near-term devices before full fault tolerance arrives.