Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Run VMC starting from CISD wavefunctions #386

Closed
davidsousarj opened this issue May 1, 2023 · 8 comments
Closed

[Question] Run VMC starting from CISD wavefunctions #386

davidsousarj opened this issue May 1, 2023 · 8 comments

Comments

@davidsousarj
Copy link

davidsousarj commented May 1, 2023

Hi. I am trying to use PySCF / PyQMC to reproduce results from the paper “Density-matrix based determination of low-energy model Hamiltonians from ab initio wavefunctions” (J. Chem. Phys. 143, 102814, 2015). I tried to follow the example from the documentation (https://pyqmc.readthedocs.io/en/latest/wavefunction.html?highlight=multi-det#pyqmc.slater.Slater):

cisolver = pyscf.hci.SCI(mol)
cisolver.select_cutoff=0.1
nmo = mf.mo_coeff.shape[1]
nelec = mol.nelec
h1 = mf.mo_coeff.T.dot(mf.get_hcore()).dot(mf.mo_coeff)
h2 = pyscf.ao2mo.full(mol, mf.mo_coeff)
e, civec = cisolver.kernel(h1, h2, nmo, nelec, verbose=4)
cisolver.ci = civec[0]
wf = pyqmc.multislater.MultiSlater(mol, mf, cisolver, tol=0.1)

First of all, it seems like the documentation is wrong, because the multislater class does not exist. It seems it should be pyqmc.slater.Slater instead.

Secondly, this example uses the heat bath CI method, while the paper used CISD. I tried to use CISD instead, but it did not work:

import pyscf.gto, pyscf.dft, pyscf.ci
import pyqmc

(...)
mol = pyscf.gto.M(atom=benzene_coords, basis='bfd_vtz', ecp='bfd')
mf  = pyscf.dft.RKS(mol)
mf.xc='b3lyp'
mf.kernel()

cisolver = pyscf.ci.CISD(mf)
cisolver.frozen=list(range(25,258))
cisolver.kernel()

wf = pyqmc.slater.Slater(mol, mf, cisolver, tol=0.1)

The above code raises the following error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/tmp/ipykernel_34694/824422184.py in <module>
     61 #
     62 #cisolver.__dict__.update({'ncore':0,'ncas':25,'nelecas':2})
---> 63 wf = pyqmc.slater.Slater(mol, mf, cisolver, tol=0.1)

~/.local/lib/python3.10/site-packages/pyqmc/slater.py in __init__(self, mol, mf, mc, tol, twist, determinants)
    128             self._det_map,
    129             self.orbitals,
--> 130         ) = pyqmc.orbitals.choose_evaluator_from_pyscf(
    131             mol, mf, mc, twist=twist, determinants=determinants, tol=self.tol
    132         )

~/.local/lib/python3.10/site-packages/pyqmc/orbitals.py in choose_evaluator_from_pyscf(mol, mf, mc, twist, determinants, tol)
     62             mol, mf, determinants=determinants, tol=tol
     63         )
---> 64     return MoleculeOrbitalEvaluator.from_pyscf(
     65         mol, mf, mc, determinants=determinants, tol=tol
     66     )

~/.local/lib/python3.10/site-packages/pyqmc/orbitals.py in from_pyscf(self, mol, mf, mc, tol, determinants)
     88         obj = mc if hasattr(mc, "mo_coeff") else mf
     89         if mc is not None:
---> 90             detcoeff, occup, det_map = pyqmc.determinant_tools.interpret_ci(mc, tol)
     91         elif determinants is not None:
     92             detcoeff, occup, det_map = pyqmc.determinant_tools.create_packed_objects(

~/.local/lib/python3.10/site-packages/pyqmc/determinant_tools.py in interpret_ci(mc, tol)
     67         deters = deters_from_hci(mc, tol)
     68     else:
---> 69         deters = fci.addons.large_ci(mc.ci, mc.ncas, mc.nelecas, tol=-1)
     70     return create_packed_objects(deters, ncore, tol)
     71 

AttributeError: 'RCISD' object has no attribute 'ncas'

It seems like the function expects that the cisolver object has an attribute it does not have. Even if I artificially introduce the attributes in it (for example: cisolver.__dict__.update({'ncore':0,'ncas':25,'nelecas':2}), it does not work:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/tmp/ipykernel_34694/661576048.py in <module>
     61 #
     62 cisolver.__dict__.update({'ncore':0,'ncas':25,'nelecas':2})
---> 63 wf = pyqmc.slater.Slater(mol, mf, cisolver, tol=0.1)

~/.local/lib/python3.10/site-packages/pyqmc/slater.py in __init__(self, mol, mf, mc, tol, twist, determinants)
    118             if not hasattr(ncore, "__len__"):
    119                 ncore = [ncore, ncore]
--> 120             self._nelec = (mc.nelecas[0] + ncore[0], mc.nelecas[1] + ncore[1])
    121         else:
    122             self._nelec = mol.nelec

TypeError: 'int' object is not subscriptable

It seems to me that the Slater function is not meant to be used with CISD. Is there any other option to construct a Slater-Jastrow function in PyQMC from CISD?

@lkwagner
Copy link
Contributor

lkwagner commented May 2, 2023

Hi David,

Thanks for the issue! The documentation has been updated there. You might find the HOWTOs more useful than the developer documentation:
https://pyqmc.readthedocs.io/en/latest/snippets.html

We currently don't support CISD wave functions natively. See Issue #241 It is possible to construct a determinant list by hand, if you like. It wouldn't be too hard to do that.

However, these days we don't typically use CISD because it's almost always worse than HCI or CASSCF/CASCI. You will probably get similar results to the article if you do something like the following, which will get you the ground-state Jastrow:

def run_casci(scf_checkfile, ci_checkfile):
    mol, mf = pyq.recover_pyscf(scf_checkfile, cancel_outputs=False)
    mc = mcscf.CASCI(mf, 6, 6)
    mc.nroots=12
    mc.kernel()

    with h5py.File(ci_checkfile, "a") as f:
        f.create_group("mc")
        f["mc/ncas"] = mc.ncas
        f["mc/nelecas"] = list(mc.nelecas)
        f["mc/ci"] = mc.ci
        f["mc/mo_coeff"] = mc.mo_coeff
    return mc



if __name__ == "__main__":
    run_mf("mf.chk")
    run_casscf("mf.chk", "cas.chk")
    pyq.OPTIMIZE("mf.chk", "opt.chk", ci_checkfile="cas.chk", nconfig=1000)

After that, you will want to attach the ground state Jastrow to the CAS roots to get approximate excited states. I have some examples of that somewhere, but let me know if this works for you.

@davidsousarj
Copy link
Author

Hi @lkwagner .

I tried to run these lines of code but it does not work. Here is the error message:

Traceback (most recent call last):
  File "/home/david/MEGA/ACADÊMICO/A_POST_DOC_EUA/QMC/6_Benzene_LucasWagner.py", line 122, in <module>
    pyq.OPTIMIZE("mf.chk", "opt.chk", ci_checkfile="cas.chk", nconfig=1000)
  File "/home/david/.local/lib/python3.10/site-packages/pyqmc/recipes.py", line 45, in OPTIMIZE
    wf, configs, acc = initialize_qmc_objects(
  File "/home/david/.local/lib/python3.10/site-packages/pyqmc/recipes.py", line 186, in initialize_qmc_objects
    mol, mf, mc = pyscftools.recover_pyscf(dft_checkfile, ci_checkfile=ci_checkfile)
  File "/home/david/.local/lib/python3.10/site-packages/pyqmc/pyscftools.py", line 69, in recover_pyscf
    if len(casdict["mo_coeff"].shape) == 3:
TypeError: 'NoneType' object is not subscriptable

@willwheelera
Copy link
Collaborator

Hi David, I apologize for the confusion. The example code I added to the documentation was outdated. Could you try this instead? The label "mc" is changed to "ci"

def run_casci(scf_checkfile, ci_checkfile):
    mol, mf = pyq.recover_pyscf(scf_checkfile, cancel_outputs=False)
    mc = mcscf.CASCI(mf, 2, 2)
    mc.kernel()

    with h5py.File(ci_checkfile, "a") as f:
        f.create_group("ci")
        f["ci/ncas"] = mc.ncas
        f["ci/nelecas"] = list(mc.nelecas)
        f["ci/ci"] = mc.ci
        f["ci/mo_coeff"] = mc.mo_coeff
    return mc

@davidsousarj
Copy link
Author

Hi @willwheelera . I tried this one. Now it raises other error message:

Traceback (most recent call last):
  File "/home/david/MEGA/ACADÊMICO/A_POST_DOC_EUA/QMC/6_Benzene_LucasWagner.py", line 133, in <module>
    pyq.OPTIMIZE("mf.chk", "opt.chk", ci_checkfile="cas.chk", nconfig=1000)
  File "/home/david/.local/lib/python3.10/site-packages/pyqmc/recipes.py", line 45, in OPTIMIZE
    wf, configs, acc = initialize_qmc_objects(
  File "/home/david/.local/lib/python3.10/site-packages/pyqmc/recipes.py", line 189, in initialize_qmc_objects
    elif mc.orbitals is None:
AttributeError: 'CASCI' object has no attribute 'orbitals'

@willwheelera
Copy link
Collaborator

Hi David, thanks for trying that and following up. I recognize where your error is coming from, and I think our current version of the code no longer does that. Can you try it again using the current version? If you installed with pip, you can do pip install git+git://github.com/WagnerGroup/pyqmc --upgrade

@davidsousarj
Copy link
Author

Thank you @willwheelera . I tried to install using pip but it did not work for some reason. Then I downloaded the zip file from github and installed it manually.

I am sorry, but it is still not working. There is another error now:

Traceback (most recent call last):
  File "/home/dms7983/MEGAsync/ACADÊMICO/A_POST_DOC_EUA/QMC/6_Benzene_LucasWagner.py", line 127, in <module>
    pyq.OPTIMIZE("mf.chk", "opt.chk", ci_checkfile="cas.chk", nconfig=1000)
  File "/home/dms7983/.local/lib/python3.10/site-packages/pyqmc/recipes.py", line 45, in OPTIMIZE
    wf, configs, acc = initialize_qmc_objects(
  File "/home/dms7983/.local/lib/python3.10/site-packages/pyqmc/recipes.py", line 199, in initialize_qmc_objects
    wf, to_opt = wftools.generate_wf(
  File "/home/dms7983/.local/lib/python3.10/site-packages/pyqmc/wftools.py", line 157, in generate_wf
    wf1, to_opt1 = generate_slater(mol, mf, mc=mc, **slater_kws)
  File "/home/dms7983/.local/lib/python3.10/site-packages/pyqmc/wftools.py", line 28, in generate_slater
    wf = slater.Slater(mol, mf, **kwargs)
  File "/home/dms7983/.local/lib/python3.10/site-packages/pyqmc/slater.py", line 166, in __init__
    ) = pyqmc.orbitals.choose_evaluator_from_pyscf(
  File "/home/dms7983/.local/lib/python3.10/site-packages/pyqmc/orbitals.py", line 59, in choose_evaluator_from_pyscf
    return MoleculeOrbitalEvaluator.from_pyscf(
  File "/home/dms7983/.local/lib/python3.10/site-packages/pyqmc/orbitals.py", line 87, in from_pyscf
    detcoeff, occup, det_map = pyqmc.determinant_tools.interpret_ci(mc, tol)
  File "/home/dms7983/.local/lib/python3.10/site-packages/pyqmc/determinant_tools.py", line 69, in interpret_ci
    deters = fci.addons.large_ci(mc.ci, mc.ncas, mc.nelecas, tol=-1)
  File "/home/dms7983/.local/lib/python3.10/site-packages/pyscf/fci/addons.py", line 36, in large_ci
    assert (ci.shape == (na, nb))
AssertionError

I will send you my script just to make sure I am not doing anything wrong.
6_Benzene_LucasWagner.py.txt

@willwheelera
Copy link
Collaborator

Hi David, thanks for following up and thanks for your patience with all the errors.

We just added a fix that I think should solve this error. Could you try it again with the update?

@davidsousarj
Copy link
Author

@willwheelera sorry for the delay. I run the script but it took over 24 hours to finish. It run without errors.

@lkwagner lkwagner closed this as completed Jun 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants