-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathh2_molecule_exact.py
36 lines (29 loc) · 1.18 KB
/
h2_molecule_exact.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"""Purpose: Generate exact ground state values for H2 molecule with lots of different bond lengths"""
import openfermion as of
from openfermion import MolecularData
import openfermionpyscf as ofpyscf
import numpy as np
import scipy
import matplotlib.pyplot as plt
basis = 'sto-3g'
multiplicity = 1
bond_length_step = 0.05
min_bond_length = 0.3
max_bond_length = 2.5
hf_energies = []
bond_lengths = np.arange(min_bond_length, max_bond_length, bond_length_step)
for bond_length in bond_lengths:
geometry = [('H', (0., 0., 0.)), ('H', (0., 0., bond_length))]
molecule = MolecularData(geometry, basis, multiplicity, description="")
molecule.load()
hamiltonian = ofpyscf.generate_molecular_hamiltonian(geometry, basis, multiplicity, charge=0)
hamiltonian_ferm_op = of.get_fermion_operator(hamiltonian)
hamiltonian_jw = of.jordan_wigner(hamiltonian_ferm_op)
hamiltonian_jw_sparse = of.get_sparse_operator(hamiltonian_jw)
# Compute ground state energy
eigs, _ = [e.real for e in scipy.linalg.eig(hamiltonian_jw_sparse.toarray())]
ground_energy = np.min(eigs)
plt.plot(bond_lengths, hf_energies)
plt.xlabel("Bond length")
plt.ylabel("Ground state energy")
plt.show()