66from .fanci import FanCI
77from ..fanpt import FANPTUpdater , FANPTContainerEParam , FANPTContainerEFree
88
9+
910def solve_fanpt (
1011 fanci_wfn ,
1112 ham0 ,
1213 ham1 ,
13- guess_params ,
14+ params ,
1415 fill ,
1516 energy_active = True ,
1617 resum = False ,
@@ -27,7 +28,7 @@ def solve_fanpt(
2728 Args:
2829 fanci_wfn : FanCI class
2930 FanCI wavefunction.
30- guess_params : np.ndarray
31+ params : np.ndarray
3132 Initial guess for wave function parameters.
3233 ham0 : pyci.hamiltonian
3334 PyCI Hamiltonian of the ideal system.
@@ -85,9 +86,7 @@ def solve_fanpt(
8586
8687 if resum :
8788 if energy_active :
88- raise ValueError (
89- "The energy parameter must be inactive with the resumation option."
90- )
89+ raise ValueError ("The energy parameter must be inactive with the resumation option." )
9190 nequation = fanci_wfn .nequation
9291 nparams = len (fanci_wfn .wfn_params )
9392 steps = 1
@@ -97,13 +96,11 @@ def solve_fanpt(
9796 fanci_wfn .remove_constraint (f"<\\ psi_{{{ ref_sd } }}|\\ Psi> - v_{{{ ref_sd } }}" )
9897 inorm = False
9998 else :
100- raise ValueError (
101- "The necesary condition of a determined system of equations is not met."
102- )
99+ raise ValueError ("The necesary condition of a determined system of equations is not met." )
103100
104101 # Get initial guess for parameters at initial lambda value.
105- results = fanci_wfn . optimize ( guess_params , ** solver_kwargs )
106- params = results . x
102+ numerical_zero = 1e-12
103+ params = np . where ( params == 0 , numerical_zero , params )
107104
108105 # Solve FANPT equations
109106 for l in np .linspace (lambda_i , lambda_f , steps , endpoint = False ):
@@ -119,7 +116,7 @@ def solve_fanpt(
119116 )
120117
121118 final_l = l + (lambda_f - lambda_i ) / steps
122- print (f' Solving FanPT problem at lambda={ final_l } ' )
119+ print (f" Solving FanPT problem at lambda={ final_l } " )
123120
124121 fanpt_updater = FANPTUpdater (
125122 fanpt_container = fanpt_container ,
@@ -133,8 +130,8 @@ def solve_fanpt(
133130
134131 # These params serve as initial guess to solve the fanci equations for the given lambda.
135132 fanpt_params = np .append (new_wfn_params , new_energy )
136- print (' Frobenius Norm of parameters: {}' .format (np .linalg .norm (fanpt_params - params )))
137- print (' Energy change: {}' .format (np .linalg .norm (fanpt_params [- 1 ] - params [- 1 ])))
133+ print (" Frobenius Norm of parameters: {}" .format (np .linalg .norm (fanpt_params - params )))
134+ print (" Energy change: {}" .format (np .linalg .norm (fanpt_params [- 1 ] - params [- 1 ])))
138135
139136 # Initialize perturbed Hamiltonian with the current value of lambda using the static method of fanpt_container.
140137 ham = fanpt_container .linear_comb_ham (ham1 , ham0 , final_l , 1 - final_l )
@@ -162,7 +159,12 @@ def update_fanci_wfn(ham, fanciwfn, norm_det, fill):
162159 nocc = fanciwfn .wfn .nocc_up
163160
164161 return fanci_class (
165- ham , nocc , fanciwfn .nproj , fanciwfn .wfn , norm_det = norm_det , fill = fill ,
162+ ham ,
163+ nocc ,
164+ fanciwfn .nproj ,
165+ fanciwfn .wfn ,
166+ norm_det = norm_det ,
167+ fill = fill ,
166168 )
167169
168170
@@ -178,7 +180,27 @@ def reduce_to_fock(two_int, lambda_val=0):
178180 fock_two_int = two_int * lambda_val
179181 nspatial = two_int .shape [0 ]
180182 indices = np .arange (nspatial )
181- 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 ]]
182- 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 , :]]
183+ fock_two_int [
184+ indices [:, None , None ],
185+ indices [None , :, None ],
186+ indices [None , None , :],
187+ indices [None , :, None ],
188+ ] = two_int [
189+ indices [:, None , None ],
190+ indices [None , :, None ],
191+ indices [None , None , :],
192+ indices [None , :, None ],
193+ ]
194+ fock_two_int [
195+ indices [:, None , None ],
196+ indices [None , :, None ],
197+ indices [None , :, None ],
198+ indices [None , None , :],
199+ ] = two_int [
200+ indices [:, None , None ],
201+ indices [None , :, None ],
202+ indices [None , :, None ],
203+ indices [None , None , :],
204+ ]
183205
184206 return fock_two_int
0 commit comments