def test(): from pyscf import lib, gto, scf import pyqmc.testwf as testwf mol = gto.M(atom="Li 0. 0. 0.; H 0. 0. 1.5", basis="cc-pvtz", unit="bohr", spin=0) for mf in [scf.RHF(mol).run(), scf.ROHF(mol).run(), scf.UHF(mol).run()]: print("") nconf = 10 nelec = np.sum(mol.nelec) slater = PySCFSlaterUHF(mol, mf) configs = np.random.randn(nconf, nelec, 3) print("testing internals:", testwf.test_updateinternals(slater, configs)) for delta in [1e-3, 1e-4, 1e-5, 1e-6, 1e-7]: print( "delta", delta, "Testing gradient", testwf.test_wf_gradient(slater, configs, delta=delta), ) print( "delta", delta, "Testing laplacian", testwf.test_wf_laplacian(slater, configs, delta=delta), ) print( "delta", delta, "Testing pgradient", testwf.test_wf_pgradient(slater, configs, delta=delta), )
def test_wfs(): """ Ensure that the wave function objects are consistent in several situations. """ from pyscf import lib, gto, scf from pyqmc.slateruhf import PySCFSlaterUHF from pyqmc.jastrowspin import JastrowSpin from pyqmc.multiplywf import MultiplyWF mol = gto.M(atom='Li 0. 0. 0.; H 0. 0. 1.5', basis='cc-pvtz', unit='bohr') mf = scf.RHF(mol).run() mf_rohf = scf.ROHF(mol).run() mf_uhf = scf.UHF(mol).run() epsilon = 1e-5 nconf = 10 epos = np.random.randn(nconf, 4, 3) for wf in [ JastrowSpin(mol), MultiplyWF(PySCFSlaterUHF(mol, mf), JastrowSpin(mol)), PySCFSlaterUHF(mol, mf_uhf), PySCFSlaterUHF(mol, mf), PySCFSlaterUHF(mol, mf_rohf) ]: for k in wf.parameters: wf.parameters[k] = np.random.rand(*wf.parameters[k].shape) assert testwf.test_wf_gradient(wf, epos, delta=1e-5)[0] < epsilon assert testwf.test_wf_laplacian(wf, epos, delta=1e-5)[0] < epsilon assert testwf.test_wf_pgradient(wf, epos, delta=1e-5)[0] < epsilon for k, item in testwf.test_updateinternals(wf, epos).items(): assert item < epsilon
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(): from pyscf import lib,gto,scf from pyqmc.jastrow import Jastrow2B from pyqmc.slater import PySCFSlaterRHF nconf=10 mol = gto.M(atom='Li 0. 0. 0.; H 0. 0. 1.5', basis='cc-pvtz',unit='bohr') mf = scf.RHF(mol).run() slater=PySCFSlaterRHF(nconf,mol,mf) jastrow=Jastrow2B(nconf,mol) jastrow.parameters['coeff']=np.random.random(jastrow.parameters['coeff'].shape) configs=np.random.randn(nconf,4,3) wf=MultiplyWF(nconf,slater,jastrow) wf.parameters['wf2coeff']=np.ones(len(wf.parameters['wf2coeff'])) print(wf.wf2.parameters['coeff']) print(wf.parameters) import pyqmc.testwf as testwf for delta in [1e-3,1e-4,1e-5,1e-6,1e-7]: print('delta', delta, "Testing gradient",testwf.test_wf_gradient(wf,configs,delta=delta)) print('delta', delta, "Testing laplacian", testwf.test_wf_laplacian(wf,configs,delta=delta)) print('delta', delta, "Testing pgradient", testwf.test_wf_pgradient(wf,configs,delta=delta))
def test(): """ Tests that the multi-slater wave function value, gradient and parameter gradient evaluations are working correctly. Also checks that VMC energy matches energy calculated in PySCF """ mol = gto.M(atom="Li 0. 0. 0.; H 0. 0. 1.5", basis="cc-pvtz", unit="bohr", spin=0) epsilon = 1e-4 delta = 1e-5 nsteps = 200 warmup = 10 for mf in [scf.RHF(mol).run(), scf.ROHF(mol).run(), scf.UHF(mol).run()]: # Test same number of elecs mc = mcscf.CASCI(mf, ncas=4, nelecas=(1, 1)) mc.kernel() wf = MultiSlater(mol, mf, mc) nconf = 10 nelec = np.sum(mol.nelec) epos = initial_guess(mol, nconf) for k, item in testwf.test_updateinternals(wf, epos).items(): assert item < epsilon assert testwf.test_wf_gradient(wf, epos, delta=delta)[0] < epsilon assert testwf.test_wf_laplacian(wf, epos, delta=delta)[0] < epsilon assert testwf.test_wf_pgradient(wf, epos, delta=delta)[0] < epsilon # Test same number of elecs mc = mcscf.CASCI(mf, ncas=4, nelecas=(1, 1)) mc.kernel() wf = pyqmc.default_msj(mol, mf, mc)[0] nelec = np.sum(mol.nelec) epos = initial_guess(mol, nconf) for k, item in testwf.test_updateinternals(wf, epos).items(): assert item < epsilon assert testwf.test_wf_gradient(wf, epos, delta=delta)[0] < epsilon assert testwf.test_wf_laplacian(wf, epos, delta=delta)[0] < epsilon assert testwf.test_wf_pgradient(wf, epos, delta=delta)[0] < epsilon # Test different number of elecs mc = mcscf.CASCI(mf, ncas=4, nelecas=(2, 0)) mc.kernel() wf = MultiSlater(mol, mf, mc) nelec = np.sum(mol.nelec) epos = initial_guess(mol, nconf) for k, item in testwf.test_updateinternals(wf, epos).items(): assert item < epsilon assert testwf.test_wf_gradient(wf, epos, delta=delta)[0] < epsilon assert testwf.test_wf_laplacian(wf, epos, delta=delta)[0] < epsilon assert testwf.test_wf_pgradient(wf, epos, delta=delta)[0] < epsilon # Quick VMC test nconf = 1000 coords = initial_guess(mol, nconf) df, coords = vmc(wf, coords, nsteps=nsteps, accumulators={"energy": EnergyAccumulator(mol)}) df = pd.DataFrame(df) df = reblock(df["energytotal"][warmup:], 20) en = df.mean() err = df.sem() assert en - mc.e_tot < 5 * err
print("p1/p2", p1 / p2) print("log magnitude") print("m1", m1) print("m2", m2) print("m1-m2", m1 - m2) p_err = np.linalg.norm(p1 / p2 - p1[0] / p2[0]) m_err = np.linalg.norm(m1 - m2 - m1[0] + m2[0]) assert p_err < 1e-10, (p_err, m_err) assert m_err < 1e-1, (p_err, m_err) if __name__ == "__main__": from pyqmc.testwf import ( test_updateinternals, test_wf_gradient, test_wf_laplacian, test_wf_gradient_laplacian, ) wf1, wf2, configs = generate_test_inputs() test_recompute(wf1, wf2, configs) test_updateinternals(wf1, configs) test_updateinternals(wf2, configs) test_wf_gradient(wf1, configs) test_wf_gradient(wf2, configs) test_wf_laplacian(wf1, configs) test_wf_laplacian(wf2, configs) test_wf_gradient_laplacian(wf1, configs) test_wf_gradient_laplacian(wf2, configs)