Running psi4 to populate the MolecularData class

The module run_psi4.py provides a user-friendly way of running psi4 calculations in FermiLib. The basic idea is that once one generates a MolecularData instance, one can then call psi4 with a specification of certain options (for instance, how much memory to use and what calculations to do) in order to compute things about the molecule, update the MolecularData object, and save results of the calculation. The most common calculations users will want in FermiLib would probably be self-consistent field (aka Hartree-Fock calculations). Our code uses these "SCF" calculations compute orbitals, integrals, Hartree-Fock energy, and more. Other common calculations are CISD and FCI calculations which also compute the 1-RDM and 2-RDM associated with their answers, CCSD calculations which also compute the CCSD amplitudes (useful for UCC) and MP2 calculations which can also be used to UCCSD initial guesses. Note that the "delete_input" and "delete_output" options indicate whether to save automatically generated psi4 input files or not.

To use this plugin, you will need to personally download psi4. Python code in psi4_template instructs psi4 (not python) to load the MolecularData object, populate it with information from calculations and then save the MolecularData object as an HDF5. The module run_psi4 uses subprocess to actually run_psi4 and then loads the pickled MolecularData. Let us look at a simple example where we compute the energy of H$_2$ at various bond lengths.

Warnings: electronic structure calculations are finicky. They sometimes fail for surprising reasons. If a particular calculation is not converging it is probably necessary to change some of the SCF options set in psi4_template. See the Psi4 documentation for more information or consult and electronic structure theory expert.


In [1]:
from fermilib.utils import MolecularData
from fermilibpluginpsi4 import run_psi4

# Set molecule parameters.
basis = 'sto-3g'
multiplicity = 1
bond_length_interval = 0.2
n_points = 10

# Set calculation parameters.
run_scf = 1
run_mp2 = 1
run_cisd = 0
run_ccsd = 0
run_fci = 1
delete_input = True
delete_output = True

# Generate molecule at different bond lengths.
hf_energies = []
fci_energies = []
bond_lengths = []
for point in range(1, n_points + 1):
    bond_length = bond_length_interval * float(point)
    bond_lengths += [bond_length]
    geometry = [('H', (0., 0., 0.)), ('H', (0., 0., bond_length))]
    molecule = MolecularData(
        geometry, basis, multiplicity,
        description=str(round(bond_length, 2)))
    
    # Run Psi4.
    molecule = run_psi4(molecule,
                        run_scf=run_scf,
                        run_mp2=run_mp2,
                        run_cisd=run_cisd,
                        run_ccsd=run_ccsd,
                        run_fci=run_fci)

    # Print out some results of calculation.
    print('\nAt bond length of {} angstrom, molecular hydrogen has:'.format(
        bond_length))
    print('Hartree-Fock energy of {} Hartree.'.format(molecule.hf_energy))
    print('MP2 energy of {} Hartree.'.format(molecule.mp2_energy))
    print('FCI energy of {} Hartree.'.format(molecule.fci_energy))
    print('Nuclear repulsion energy between protons is {} Hartree.'.format(
        molecule.nuclear_repulsion))
    for orbital in range(molecule.n_orbitals):
        print('Spatial orbital {} has energy of {} Hartree.'.format(
            orbital, molecule.orbital_energies[orbital]))
    hf_energies += [molecule.hf_energy]
    fci_energies += [molecule.fci_energy]

# Plot.
import matplotlib.pyplot as plt
%matplotlib inline

plt.figure(0)
plt.plot(bond_lengths, fci_energies, 'x-')
plt.plot(bond_lengths, hf_energies, 'o-')
plt.ylabel('Energy in Hartree')
plt.xlabel('Bond length in angstrom')
plt.show()


At bond length of 0.2 angstrom, molecular hydrogen has:
Hartree-Fock energy of 0.164175001415 Hartree.
MP2 energy of 0.159042663261 Hartree.
FCI energy of 0.157482124142 Hartree.
Nuclear repulsion energy between protons is 2.64588604295 Hartree.
Spatial orbital 0 has energy of -0.858824527315 Hartree.
Spatial orbital 1 has energy of 1.57236203029 Hartree.

At bond length of 0.4 angstrom, molecular hydrogen has:
Hartree-Fock energy of -0.904361397714 Hartree.
MP2 energy of -0.9114367297 Hartree.
FCI energy of -0.914149708214 Hartree.
Nuclear repulsion energy between protons is 1.32294302147 Hartree.
Spatial orbital 0 has energy of -0.745212533291 Hartree.
Spatial orbital 1 has energy of 1.16741639504 Hartree.

At bond length of 0.6 angstrom, molecular hydrogen has:
Hartree-Fock energy of -1.10112824314 Hartree.
MP2 energy of -1.11133176748 Hartree.
FCI energy of -1.11628600783 Hartree.
Nuclear repulsion energy between protons is 0.881962014317 Hartree.
Spatial orbital 0 has energy of -0.640876264399 Hartree.
Spatial orbital 1 has energy of 0.838084978149 Hartree.

At bond length of 0.8 angstrom, molecular hydrogen has:
Hartree-Fock energy of -1.11085039699 Hartree.
MP2 energy of -1.12545309198 Hartree.
FCI energy of -1.13414766636 Hartree.
Nuclear repulsion energy between protons is 0.661471510737 Hartree.
Spatial orbital 0 has energy of -0.554495879764 Hartree.
Spatial orbital 1 has energy of 0.612618083493 Hartree.

At bond length of 1.0 angstrom, molecular hydrogen has:
Hartree-Fock energy of -1.06610864808 Hartree.
MP2 energy of -1.08666480357 Hartree.
FCI energy of -1.1011503293 Hartree.
Nuclear repulsion energy between protons is 0.52917720859 Hartree.
Spatial orbital 0 has energy of -0.484441678962 Hartree.
Spatial orbital 1 has energy of 0.457501936164 Hartree.

At bond length of 1.2 angstrom, molecular hydrogen has:
Hartree-Fock energy of -1.00510670488 Hartree.
MP2 energy of -1.03366209661 Hartree.
FCI energy of -1.05674074513 Hartree.
Nuclear repulsion energy between protons is 0.440981007158 Hartree.
Spatial orbital 0 has energy of -0.426502640723 Hartree.
Spatial orbital 1 has energy of 0.344126878784 Hartree.

At bond length of 1.4 angstrom, molecular hydrogen has:
Hartree-Fock energy of -0.941480652784 Hartree.
MP2 energy of -0.980568732859 Hartree.
FCI energy of -1.01546824814 Hartree.
Nuclear repulsion energy between protons is 0.377983720421 Hartree.
Spatial orbital 0 has energy of -0.377322823969 Hartree.
Spatial orbital 1 has energy of 0.258901972313 Hartree.

At bond length of 1.6 angstrom, molecular hydrogen has:
Hartree-Fock energy of -0.881732447952 Hartree.
MP2 energy of -0.934241169572 Hartree.
FCI energy of -0.983472728093 Hartree.
Nuclear repulsion energy between protons is 0.330735755369 Hartree.
Spatial orbital 0 has energy of -0.335296350589 Hartree.
Spatial orbital 1 has energy of 0.1945979554 Hartree.

At bond length of 1.8 angstrom, molecular hydrogen has:
Hartree-Fock energy of -0.828848145985 Hartree.
MP2 energy of -0.897880538041 Hartree.
FCI energy of -0.96181695212 Hartree.
Nuclear repulsion energy between protons is 0.293987338106 Hartree.
Spatial orbital 0 has energy of -0.299563220223 Hartree.
Spatial orbital 1 has energy of 0.145960296625 Hartree.

At bond length of 2.0 angstrom, molecular hydrogen has:
Hartree-Fock energy of -0.783792652466 Hartree.
MP2 energy of -0.872510060481 Hartree.
FCI energy of -0.948641111742 Hartree.
Nuclear repulsion energy between protons is 0.264588604295 Hartree.
Spatial orbital 0 has energy of -0.269459222444 Hartree.
Spatial orbital 1 has energy of 0.108997368132 Hartree.