-
Notifications
You must be signed in to change notification settings - Fork 4
Getting Started
This first tutorial walks you through your first FANCI calculation using Fanpy. In this example, we will calculate the electronic structure of the
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)
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')
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')
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')
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
Fanpy is developed by the Miranda Quintana Group.