Skip to content

Getting Started

Carlos E. V. de Moura edited this page Dec 9, 2024 · 1 revision

First FANCI Calculation Using Fanpy

This first tutorial walks you through your first FANCI calculation using Fanpy. In this example, we will calculate the electronic structure of the $\ce{BeH2}$ molecule using the Configuration Interaction Singles and Doubles (CISD) wavefunction constructed using FANCI.


Step 1: Obtain One- and Two-Electron Integrals from PySCF

To begin, we will use PySCF to define the molecular system and perform a Hartree-Fock calculation. This step provides the one- and two-electron integrals needed for the FANCI calculation.

from pyscf import gto, scf

# Define the molecular system: BeH2
print("\n>> >> PySCF")
mol = gto.M(atom='''
                    Be   0.0000  0.0000  0.0000
                    H    0.0000  0.0000 -1.2200
                    H    0.0000  0.0000  1.2200
                 ''',
            basis='sto3g')

# Perform Hartree-Fock calculation
mf = scf.HF(mol)
e_hf = mf.kernel()
mf.analyze()

# Interface PySCF with Fanpy
print("\n>> >> Fanpy")
import fanpy.interface as interface
interface = interface.pyscf.PYSCF(mf)

Step 2: Initialize the Wavefunction Object

Define the wavefunction ansatz for the calculation. Here, we use a CISD (Configuration Interaction Singles and Doubles) wavefunction.

from fanpy.wfn.ci.cisd import CISD

# Define the wavefunction object
wfn = CISD(interface.nelec, interface.nspinorb)
print('Wavefunction: CISD')

Step 3: Initialize the Hamiltonian Object

Define a restricted molecular Hamiltonian as the system's Hamiltonian using the integrals obtained from PySCF.

from fanpy.ham.restricted_chemical import RestrictedMolecularHamiltonian

# Define the Hamiltonian
ham = RestrictedMolecularHamiltonian(interface.one_int, interface.two_int)
print('Hamiltonian: RestrictedMolecularHamiltonian')

Step 4: Define the Objective for the Projected Schrödinger Equation

Set up the objective function, defining the equation to be solved during optimization.

from fanpy.eqn.projected import ProjectedSchrodinger

# Define the objective
objective = ProjectedSchrodinger(wfn, ham, energy_type='compute')

Step 5: Solve the Optimization Problem

Use Fanpy's Least Squares solver to optimize the wavefunction parameters and calculate the electronic energy.

print('Optimizing wavefunction: least_squares solver')
from fanpy.solver.system import least_squares

# Perform optimization
results = least_squares(objective)

# Display results
if results['success']:
    print('Optimization was successful')
else:
    print('Optimization was not successful: {}'.format(results['message']))
print('Final Electronic Energy: {}'.format(results['energy']))
print('Final Total Energy: {}'.format(results['energy'] + interface.energy_nuc))

Congratulations! You have completed your first FANCI calculation using Fanpy for the $\ce{BeH2}$ molecule.