theochem/PyCI

Integrate FANCI with PyCI

Closed this issue · 17 comments

Integrate FANCI with PyCI

The repo for FanCI is https://github.com/QuantumElephant/FanCI.
We can take the master branch from there to be the one we port (it is the latest one, although there are few unmerged branches)

We have to put the methods there as a module of PyCI. Probably it can be just copying the folder fanci and put it under the pyci folder in this repo.

Do not include the fanpt module or its tests (files starting with test_fanpt)

Tentative tasks:

  • Copy fanci folder to pyci (the call to the fanci methods would look like pyci.fanci)
  • Move FanCI tests to PyCI test folder
  • Fix the imports so that things work

@msricher do we include the fanpt module too?

I don't think we should include fanpt now, since it doesn't work, and its just Ramon's draft implementation.

We also need to make sure that the open-shell method we include actually does work.

@msricher Do you want to have to separate folders for test? One just for pyci and one for fanci, or I should merge everything in one folder?

Also, running tests on Fanpy are very slow. Is it expected behaviour?

Oh, yes, the FanPy is very slow. I can speed it up by removing the logic for frozen parameters which we don't use.

Okay, cool
It seems like 2 tests are failing too, but the rest are fine

__________________________________________________________ test_4e_5mos_pccds_overlap ___________________________________________________________

dummy_ham = <pyci.pyci.secondquant_op object at 0x7fe56d8c8270>

    def test_4e_5mos_pccds_overlap(dummy_ham):
        pccsd = pCCDS(dummy_ham, 2, 2, nproj=None)
        nparams = pccsd.nocc_up * pccsd.nvir_up + pccsd.wfn.nocc * pccsd.wfn.nvir
        params = np.arange(nparams, dtype=pyci.c_double) + 1
    
        mat_s = params[pccsd.nocc_up * pccsd.nvir_up:].reshape(pccsd.wfn.nocc, pccsd.wfn.nvir)
        mat_p = params[:pccsd.nocc_up * pccsd.nvir_up].reshape(pccsd.wfn.nocc_up, pccsd.wfn.nvir_up)
    
        # Reference occs
        # alphas: [11000]
        # betas:  [11000]
        occsv = np.array([[[0,1],[0,1]]])
        ovl = pccsd.compute_overlap(params, occsv)
        assert np.allclose(ovl[0], 1.)
        # Exc=1, seniority=2
        # alphas: [11000]
        # betas:  [10100]
        occsv = np.array([[[0,1],[0,2]]])
        ovl = pccsd.compute_overlap(params, occsv)
        assert np.allclose(ovl[0], mat_s[3,3])
        # Exc=2, seniority=0
        # alphas: [10100]
        # betas:  [10100]
        occsv = np.array([[[0,2],[0,2]]])
        ovl = pccsd.compute_overlap(params, occsv)
        expected = mat_p[1,0] + (mat_s[1,0]*mat_s[3,3] + mat_s[1,3]*mat_s[3,0])
>       assert np.allclose(ovl[0], expected)
E       assert False
E        +  where False = <function allclose at 0x7fe5ec0fc430>(4.0, 768.0)
E        +    where <function allclose at 0x7fe5ec0fc430> = np.allclose

pyci/test_fanci/test_pccds.py:59: AssertionError
__________________________________________________________ test_2e_5mos_pccds_overlap ___________________________________________________________

dummy_ham = <pyci.pyci.secondquant_op object at 0x7fe5e2cb1f70>

    def test_2e_5mos_pccds_overlap(dummy_ham):
        pccsd = pCCDS(dummy_ham, 1, 1, nproj=None)
        nparams = pccsd.nocc_up * pccsd.nvir_up + pccsd.wfn.nocc * pccsd.wfn.nvir
        params = np.arange(nparams, dtype=pyci.c_double) + 1
    
        mat_s = params[pccsd.nocc_up * pccsd.nvir_up:].reshape(pccsd.wfn.nocc, pccsd.wfn.nvir)
        mat_p = params[:pccsd.nocc_up * pccsd.nvir_up].reshape(pccsd.wfn.nocc_up, pccsd.wfn.nvir_up)
    
        # Reference occs
        # alphas: [10000]
        # betas:  [10000]
        occsv = np.array([[[0],[0]]])
        ovl = pccsd.compute_overlap(params, occsv)
        print(ovl)
        assert np.allclose(ovl[0], 1.)
        # Exc=1, seniority=2
        # alphas: [10000]
        # betas:  [01000]
        occsv = np.array([[[0],[1]]])
        ovl = pccsd.compute_overlap(params, occsv)
        print(mat_s[1,4], ovl[0])
        assert np.allclose(ovl[0], mat_s[1,4])
        # Exc=1, seniority=2
        # alphas: [01000]
        # betas:  [10000]
        occsv = np.array([[[1],[0]]])
        ovl = pccsd.compute_overlap(params, occsv)
        print(mat_s[0,0], ovl[0])
        assert np.allclose(ovl[0], mat_s[0,0])
        # Exc=1, seniority=2
        # alphas: [01000]
        # betas:  [00100]
        occsv = np.array([[[1],[2]]])
        ovl = pccsd.compute_overlap(params, occsv)
        expected = mat_s[0,0]*mat_s[1,5] + mat_s[0,5]*mat_s[1,0]
        print(expected, ovl[0])
>       assert np.allclose(ovl[0], expected)
E       assert False
E        +  where False = <function allclose at 0x7fe5ec0fc430>(0.0, 220.0)
E        +    where <function allclose at 0x7fe5ec0fc430> = np.allclose

pyci/test_fanci/test_pccds.py:130: AssertionError

hmm, yeah, at the time we stopped working on this, there was difficulty with the optimization for pCCD+S.

Are you working on this, already, then? I was going to start on the weekend. Let me know.

If you want to go ahead with it, then I can just remove the active/frozen parameter stuff myself later.

There is a pull request that merged the fanci py pyci
The only question is if you want to have 2 different folders for tests on pyci and fanci. Let me tag you at pull request.
But yes, they are merged. I was talking to @gabrielasd if we can remove the dependency on solver for FanCI, though.
If you approve this pull request, we can start working on separating fanci functionality from optimizers

I think we have a couple minor issues associated with deprecation of NumPy alias for bool and int variables in the fanci module, specifically here:
np.bool in fanci.py (lines 222, 225, 232)
np.int in apig (lines 200, 201)

I tried building PyCI after the merge in a different virtual env, and the tests for apig were failing because of these. My test venv has NumPy 1.26.3, and apparently after version 1.24 they removed these alias.

Michelle since you may change the use of masked parameters, and that is where I saw the np.bool being used, I won't change this now.

The other thing I remember is, I had to install the library for the stochastic optimized, cma. So maybe this should also be added to the dependencies listed on PyCI Readme.

I specified the version of numpy in the make file, so I’m surprised it’s not working

NVM, i specified the version in the .yml file, not the make file
I’ll fix it

as for the cma dependency, I think if we move the optimization outside of pyci, than we can get rid of any cma too

I merged a version with frozen params and fanpt removed, and cma added to requirements.

It seems like both pCCDS + determinant ratio works already! Did we do this sometime in the past few years, @gabrielasd? They're a bit slow, though, but I think that's fine for a proof-of-concept. What do you think @PaulWAyers?

I agree. Det. ratio is also nice since it works both for open-shell and closed-shell (odd vs. even number of determinants).

@msricher, on my side I haven't work on them for quite some time, so I need to refresh my mind on their status.
What I remember about our current pCCDS implementation, though, is that it only supports closed-shell cases; and I think it is the same for the determinant ratio one.

Well, open-shell singlets are supported. And that's good enough for me, for now.

This is complete. I may port some FanCI functionality to C++ eventually, and we may add more FanCI wfns, but the two (PyCI,FanCI) are integrated now.