def test_single_opt(): from pyqmc.accumulators import EnergyAccumulator from pyscf import lib, gto, scf import pandas as pd from pyqmc.multiplywf import MultiplyWF from pyqmc.jastrow import Jastrow2B from pyqmc.func3d import GaussianFunction from pyqmc.slater import PySCFSlaterRHF from pyqmc.multiplywf import MultiplyWF from pyqmc.jastrow import Jastrow2B from pyqmc.mc import initial_guess,vmc mol = gto.M(atom='Li 0. 0. 0.; Li 0. 0. 1.5', basis='bfd_vtz',ecp='bfd',unit='bohr',verbose=5) mf = scf.RHF(mol).run() nconf=1000 nsteps=10 coords = initial_guess(mol,nconf) wf=MultiplyWF(PySCFSlaterRHF(mol,mf),Jastrow2B(mol, basis=[GaussianFunction(1.0),GaussianFunction(2.0)])) vmc(wf,coords,nsteps=nsteps) opt_var,wf=optvariance(EnergyAccumulator(mol),wf,coords,['wf2coeff']) print('Final variance:',opt_var)
def __init__(self, mol, a_basis=None, b_basis=None): """ Args: mol : a pyscf molecule object a_basis : list of func3d objects that comprise the electron-ion basis b_basis : list of func3d objects that comprise the electron-electron basis """ if b_basis is None: nexpand = 5 self.b_basis = [ GaussianFunction(0.2 * 2**n) for n in range(1, nexpand + 1) ] else: nexpand = len(b_basis) self.b_basis = b_basis if a_basis is None: aexpand = 4 self.a_basis = [ GaussianFunction(0.2 * 2**n) for n in range(1, aexpand + 1) ] else: aexpand = len(a_basis) self.a_basis = a_basis self.parameters = {} self._nelec = np.sum(mol.nelec) self._mol = mol self.parameters["bcoeff"] = np.zeros((nexpand, 3)) self.parameters["acoeff"] = np.zeros((self._mol.natm, aexpand, 2))
def test(): from pyscf import lib, gto, scf np.random.seed(10) mol = gto.M(atom='Li 0. 0. 0.; H 0. 0. 1.5', basis='cc-pvtz',unit='bohr') l = dir(mol) nconf=20 configs=np.random.randn(nconf,np.sum(mol.nelec),3) abasis=[GaussianFunction(0.2),GaussianFunction(0.4)] bbasis=[GaussianFunction(0.2),GaussianFunction(0.4)] jastrow=JastrowSpin(mol,a_basis=abasis,b_basis=bbasis) jastrow.parameters['bcoeff']=np.random.random(jastrow.parameters['bcoeff'].shape) jastrow.parameters['acoeff']=np.random.random(jastrow.parameters['acoeff'].shape) import pyqmc.testwf as testwf for key, val in testwf.test_updateinternals(jastrow, configs).items(): print(key, val) print() for delta in [1e-3,1e-4,1e-5,1e-6,1e-7]: print('delta', delta, "Testing gradient", testwf.test_wf_gradient(jastrow,configs,delta=delta)) print('delta', delta, "Testing laplacian", testwf.test_wf_laplacian(jastrow,configs,delta=delta)) print('delta', delta, "Testing pgradient", testwf.test_wf_pgradient(jastrow,configs,delta=delta)) print()
def test_func3d(): """ Ensure that the 3-dimensional functions correctly compute their gradient and laplacian """ from pyqmc.func3d import ( PadeFunction, PolyPadeFunction, GaussianFunction, ExpCuspFunction, test_func3d_gradient, test_func3d_laplacian, ) test_functions = { "Pade": PadeFunction(0.2), "PolyPade": PolyPadeFunction(2.0, 1.5), "ExpCusp": ExpCuspFunction(2.0, 1.5), "Gaussian": GaussianFunction(0.4), } delta = 1e-6 epsilon = 1e-5 for name, func in test_functions.items(): assert test_func3d_gradient(func, delta=delta)[0] < epsilon assert test_func3d_laplacian(func, delta=delta)[0] < epsilon
def test_func3d(): """ Ensure that the 3-dimensional functions correctly compute their gradient and laplacian """ from pyqmc.func3d import ( PadeFunction, PolyPadeFunction, GaussianFunction, CutoffCuspFunction, test_func3d_gradient, test_func3d_laplacian, test_func3d_gradient_laplacian, test_func3d_pgradient, ) test_functions = { "Pade": PadeFunction(0.2), "PolyPade": PolyPadeFunction(2.0, 1.5), "CutoffCusp": CutoffCuspFunction(2.0, 1.5), "Gaussian": GaussianFunction(0.4), } delta = 1e-6 epsilon = 1e-5 for name, func in test_functions.items(): grad = test_func3d_gradient(func, delta=delta)[0] lap = test_func3d_laplacian(func, delta=delta)[0] andg, andl = test_func3d_gradient_laplacian(func) pgrad = test_func3d_pgradient(func, delta=1e-9)[0] print(name, grad, lap, "both:", andg, andl) print(name, pgrad) assert grad < epsilon assert lap < epsilon assert andg < epsilon assert andl < epsilon for k, v in pgrad.items(): assert v < epsilon, (name, k, v) #Check CutoffCusp does not diverge at r/rcut = 1 rcut = 1.5 f = CutoffCuspFunction(2.0, rcut) gamma = 2.0 rc = 1.5 basis = CutoffCuspFunction(gamma, rc) rvec = np.array([0, 0, rc])[np.newaxis, :] r = np.linalg.norm(rvec)[np.newaxis] v = basis.value(rvec, r) g = basis.gradient(rvec, r) l = basis.laplacian(rvec, r) g_both, l_both = basis.gradient_laplacian(rvec, r) assert abs(v).sum() == 0 assert abs(g).sum() == 0 assert abs(l).sum() == 0 assert abs(g_both).sum() == 0 assert abs(l_both).sum() == 0
def test(): from pyscf import lib, gto, scf from pyqmc.accumulators import EnergyAccumulator, PGradTransform, LinearTransform from pyqmc.multiplywf import MultiplyWF from pyqmc.jastrow import Jastrow2B from pyqmc.func3d import GaussianFunction from pyqmc.slater import PySCFSlaterRHF from pyqmc.mc import initial_guess mol = gto.M(atom='H 0. 0. 0.; H 0. 0. 1.5', basis='cc-pvtz', unit='bohr', verbose=5) mf = scf.RHF(mol).run() nconf = 2500 nsteps = 70 warmup = 20 coords = initial_guess(mol, nconf) basis = { 'wf2coeff': [GaussianFunction(0.2), GaussianFunction(0.4), GaussianFunction(0.6)] } wf = MultiplyWF(PySCFSlaterRHF(mol, mf), Jastrow2B(mol, basis['wf2coeff'])) params0 = {'wf2coeff': np.array([-0.8, -0.2, 0.4])} for k, p in wf.parameters.items(): if k in params0: wf.parameters[k] = params0[k] energy_acc = EnergyAccumulator(mol) pgrad_acc = PGradTransform(energy_acc, LinearTransform(wf.parameters)) # Gradient descent wf, data = gradient_descent(wf, coords, pgrad_acc, vmcoptions={'nsteps': nsteps}, warmup=warmup, step=0.5, eps=0.1, maxiters=50, datafile='sropt.json')
def test_func3d(): """ Ensure that the 3-dimensional functions correctly compute their gradient and laplacian """ from pyqmc.func3d import PadeFunction, GaussianFunction, test_func3d_gradient, test_func3d_laplacian test_functions = { 'Pade': PadeFunction(0.2), 'Gaussian': GaussianFunction(0.4) } delta = 1e-6 epsilon = 1e-5 for name, func in test_functions.items(): assert test_func3d_gradient(func, delta=delta)[0] < epsilon assert test_func3d_laplacian(func, delta=delta)[0] < epsilon
def slater_jastrow(mol, mf, abasis=None, bbasis=None): if abasis is None: abasis = [GaussianFunction(0.8), GaussianFunction(1.6), GaussianFunction(3.2)] if bbasis is None: bbasis = [ CutoffCuspFunction(2.0, 1.5), GaussianFunction(0.8), GaussianFunction(1.6), GaussianFunction(3.2), ] wf = MultiplyWF( PySCFSlaterUHF(mol, mf), JastrowSpin(mol, a_basis=abasis, b_basis=bbasis) ) return wf