Skip to content

Commit 510aeae

Browse files
committed
Added tests for FanPT
1 parent a5e9ecc commit 510aeae

File tree

3 files changed

+109
-22
lines changed

3 files changed

+109
-22
lines changed

pyci/fanci/fanpt_wrapper.py

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def solve_fanpt(
7575
norm_det = [(ref_sd, 1.0)]
7676
else:
7777
inorm = False
78-
norm_det = None
78+
norm_det = list()
7979

8080
# Select FANPT method
8181
if energy_active:
@@ -148,18 +148,8 @@ def solve_fanpt(
148148
results = fanci_wfn.optimize(fanpt_params, **solver_kwargs)
149149
params = results.x
150150

151-
# Output for debugging purposes
152-
results["energy"] = fanpt_params[-1]
153151
results["residuals"] = results.fun
154-
#output = {
155-
# "x": params,
156-
# "variables": {
157-
# "steps": steps,
158-
# "inorm": inorm,
159-
# "norm_det": norm_det,
160-
# "final_l": final_l,
161-
# },
162-
#}
152+
163153
return results
164154

165155

@@ -172,7 +162,7 @@ def update_fanci_wfn(ham, fanciwfn, norm_det, fill):
172162
nocc = fanciwfn.wfn.nocc_up
173163

174164
return fanci_class(
175-
ham, nocc, fanciwfn.nproj, fanciwfn.wfn
165+
ham, nocc, fanciwfn.nproj, fanciwfn.wfn, norm_det=norm_det, fill=fill,
176166
)
177167

178168

@@ -190,13 +180,5 @@ def reduce_to_fock(two_int, lambda_val=0):
190180
indices = np.arange(nspatial)
191181
fock_two_int[indices[:, None, None], indices[None, :, None], indices[None, None, :], indices[None, :, None]] = two_int[indices[:, None, None], indices[None, :, None], indices[None, None, :], indices[None, :, None]]
192182
fock_two_int[indices[:, None, None], indices[None, :, None], indices[None, :, None], indices[None, None, :]] = two_int[indices[:, None, None], indices[None, :, None], indices[None, :, None], indices[None, None, :]]
193-
#fock_two_int[indices[None, :, None], indices[:, None, None], indices[None, None, :], indices[None, :, None]] = two_int[indices[:, None, None], indices[None, :, None], indices[None, None, :], indices[None, :, None]]
194-
#fock_two_int[indices[None, :, None], indices[:, None, None], indices[None, :, None], indices[None, None, :]] = two_int[indices[:, None, None], indices[None, :, None], indices[None, :, None], indices[None, None, :]]
195-
196-
#occ_indices = np.arange(nelec // 2)
197-
#fock_two_int[indices[:, None, None], occ_indices[None, :, None], indices[None, None, :], occ_indices[None, :, None]] = two_int[indices[:, None, None], occ_indices[None, :, None], indices[None, None, :], occ_indices[None, :, None]]
198-
#fock_two_int[indices[:, None, None], occ_indices[None, :, None], occ_indices[None, :, None], indices[None, None, :]] = two_int[indices[:, None, None], occ_indices[None, :, None], occ_indices[None, :, None], indices[None, None, :]]
199-
#fock_two_int[occ_indices[None, :, None], indices[:, None, None], indices[None, None, :], occ_indices[None, :, None]] = two_int[indices[:, None, None], occ_indices[None, :, None], indices[None, None, :], occ_indices[None, :, None]]
200-
#fock_two_int[occ_indices[None, :, None], indices[:, None, None], occ_indices[None, :, None], indices[None, None, :]] = two_int[indices[:, None, None], occ_indices[None, :, None], occ_indices[None, :, None], indices[None, None, :]]
201183

202184
return fock_two_int

pyci/fanpt/base_fanpt_container.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ def __init__(
195195
self.ref_sd = ref_sd
196196
else:
197197
raise KeyError(
198-
"The normalization of the Slater determinant is not constrained"
198+
"The normalization of the Slater determinant is not constrained "
199199
"in the FanCI wavefunction."
200200
)
201201
else:

pyci/test/test_fanpt.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# This file is part of PyCI.
2+
#
3+
# PyCI is free software: you can redistribute it and/or modify it under
4+
# the terms of the GNU General Public License as published by the Free
5+
# Software Foundation, either version 3 of the License, or (at your
6+
# option) any later version.
7+
#
8+
# PyCI is distributed in the hope that it will be useful, but WITHOUT
9+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
11+
# for more details.
12+
#
13+
# You should have received a copy of the GNU General Public License
14+
# along with PyCI. If not, see <http://www.gnu.org/licenses/>.
15+
16+
import numpy as np
17+
import pyci
18+
from scipy.special import comb
19+
20+
import pytest
21+
from pyci.test import datafile
22+
from pyci.fanci import AP1roG
23+
24+
from pyci.fanci.fanpt_wrapper import reduce_to_fock, solve_fanpt
25+
from pyci.fanpt import FANPTUpdater, FANPTContainerEParam, FANPTContainerEFree
26+
27+
@pytest.mark.parametrize("filename, nocc, expected", [("he_ccpvqz", 1, -2.8868091056425156),
28+
("be_ccpvdz", 2, -14.600556820761211),
29+
("li2_ccpvdz", 3, -16.862861409549044),
30+
("lih_sto6g", 2, -8.963531095653355),
31+
("h2_631gdp", 1, -1.869682842154122),
32+
("h2o_ccpvdz", 5, -77.96987451399201)])
33+
def test_fanpt_e_free(filename, nocc, expected):
34+
nsteps = 10
35+
order = 1
36+
37+
# Define ham0 and ham1
38+
ham1 = pyci.hamiltonian(datafile("{0:s}.fcidump".format(filename)))
39+
ham0 = pyci.hamiltonian(ham1.ecore, ham1.one_mo, reduce_to_fock(ham1.two_mo))
40+
41+
# contruct empty fci wave function class instance from # of basis functions and occupation
42+
wfn0 = pyci.fullci_wfn(ham0.nbasis, nocc, nocc)
43+
wfn0.add_hartreefock_det()
44+
45+
# initialize sparse matrix operator (hamiltonian into wave function)
46+
op = pyci.sparse_op(ham0, wfn0)
47+
48+
# solve for the lowest eigenvalue and eigenvector
49+
e_hf, e_vecs0 = op.solve(n=1, tol=1.0e-8)
50+
51+
# Get params as the solution of the fanci wfn with ham0 (last element will be the energy of the "ideal" system).
52+
nproj = int(comb(ham1.nbasis, nocc))
53+
pyci_wfn = AP1roG(ham1, nocc, nproj=nproj)
54+
55+
params = np.zeros(pyci_wfn.nparam, dtype=pyci.c_double)
56+
params[-1] = e_hf[0]
57+
58+
fill = 'excitation'
59+
fanpt_results = solve_fanpt(pyci_wfn, ham0, pyci_wfn.ham, params,
60+
fill=fill, energy_active=False, resum=False, ref_sd=0,
61+
final_order=order, lambda_i=0.0, lambda_f=1.0, steps=nsteps,
62+
solver_kwargs={'mode':'lstsq', 'use_jac':True, 'xtol':1.0e-8,
63+
'ftol':1.0e-8, 'gtol':1.0e-5, 'max_nfev':pyci_wfn.nparam, 'verbose':2})
64+
65+
assert np.allclose(fanpt_results.x[-1], expected)
66+
67+
@pytest.mark.parametrize("filename, nocc, expected", [("he_ccpvqz", 1, -2.8868091056425156),
68+
("be_ccpvdz", 2, -14.600556842700215),
69+
("li2_ccpvdz", 3, -16.86286984124269),
70+
("lih_sto6g", 2, -8.96353109432708),
71+
("h2_631gdp", 1, -1.869682842154122),
72+
("h2o_ccpvdz", 5, -77.96987516399848)])
73+
def test_fanpt_e_param(filename, nocc, expected):
74+
nsteps = 10
75+
order = 1
76+
77+
# Define ham0 and ham1
78+
ham1 = pyci.hamiltonian(datafile("{0:s}.fcidump".format(filename)))
79+
ham0 = pyci.hamiltonian(ham1.ecore, ham1.one_mo, reduce_to_fock(ham1.two_mo))
80+
81+
# contruct empty fci wave function class instance from # of basis functions and occupation
82+
wfn0 = pyci.fullci_wfn(ham0.nbasis, nocc, nocc)
83+
wfn0.add_hartreefock_det()
84+
85+
# initialize sparse matrix operator (hamiltonian into wave function)
86+
op = pyci.sparse_op(ham0, wfn0)
87+
88+
# solve for the lowest eigenvalue and eigenvector
89+
e_hf, e_vecs0 = op.solve(n=1, tol=1.0e-8)
90+
91+
# Get params as the solution of the fanci wfn with ham0 (last element will be the energy of the "ideal" system).
92+
nproj = int(comb(ham1.nbasis, nocc))
93+
pyci_wfn = AP1roG(ham1, nocc, nproj=nproj)
94+
95+
params = np.zeros(pyci_wfn.nparam, dtype=pyci.c_double)
96+
params[-1] = e_hf[0]
97+
98+
fill = 'excitation'
99+
fanpt_results = solve_fanpt(pyci_wfn, ham0, pyci_wfn.ham, params,
100+
fill=fill, energy_active=True, resum=False, ref_sd=0,
101+
final_order=order, lambda_i=0.0, lambda_f=1.0, steps=nsteps,
102+
solver_kwargs={'mode':'lstsq', 'use_jac':True, 'xtol':1.0e-8,
103+
'ftol':1.0e-8, 'gtol':1.0e-5, 'max_nfev':pyci_wfn.nparam, 'verbose':2})
104+
105+
assert np.allclose(fanpt_results.x[-1], expected)

0 commit comments

Comments
 (0)