Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_accumulator():
""" Tests that the accumulator gets inserted into the data output correctly.
"""
import pandas as pd
from pyqmc.mc import vmc, initial_guess
from pyscf import gto, scf
from pyqmc.energy import energy
from pyqmc.slateruhf import PySCFSlaterUHF
from pyqmc.accumulators import EnergyAccumulator
mol = gto.M(
atom="Li 0. 0. 0.; Li 0. 0. 1.5", basis="cc-pvtz", unit="bohr", verbose=5
)
mf = scf.RHF(mol).run()
nconf = 5000
wf = PySCFSlaterUHF(mol, mf)
coords = initial_guess(mol, nconf)
df, coords = vmc(
wf, coords, nsteps=30, accumulators={"energy": EnergyAccumulator(mol)}
)
df = pd.DataFrame(df)
eaccum = EnergyAccumulator(mol)
eaccum_energy = eaccum(coords, wf)
df = pd.DataFrame(df)
print(df["energytotal"][29] == np.average(eaccum_energy["total"]))
assert df["energytotal"][29] == np.average(eaccum_energy["total"])
from pyqmc.multiplywf import MultiplyWF
from pyqmc.coord import OpenConfigs
import pyqmc
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-4
nconf = 10
epos = pyqmc.initial_guess(mol, nconf)
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:
if k != "mo_coeff":
wf.parameters[k] = np.random.rand(*wf.parameters[k].shape)
for fname, func in zip(
["gradient", "laplacian", "pgradient"],
[
testwf.test_wf_gradient,
testwf.test_wf_laplacian,
testwf.test_wf_pgradient,
],
):
err = []
for delta in [1e-4, 1e-5, 1e-6, 1e-7, 1e-8]:
err.append(func(wf, epos, delta)[0])
from pyscf import lib, gto, scf
from pyqmc.slateruhf import PySCFSlaterUHF
from pyqmc.jastrowspin import JastrowSpin
from pyqmc.dmc import limdrift, rundmc
from pyqmc.mc import vmc
from pyqmc.accumulators import EnergyAccumulator
from pyqmc.func3d import CutoffCuspFunction
from pyqmc.multiplywf import MultiplyWF
from pyqmc.coord import OpenConfigs
import pandas as pd
mol = gto.M(atom="H 0. 0. 0.", basis="sto-3g", unit="bohr", spin=1)
mf = scf.UHF(mol).run()
nconf = 1000
configs = OpenConfigs(np.random.randn(nconf, 1, 3))
wf1 = PySCFSlaterUHF(mol, mf)
wf = wf1
wf2 = JastrowSpin(mol, a_basis=[CutoffCuspFunction(5, 0.2)], b_basis=[])
wf2.parameters["acoeff"] = np.asarray([[[1.0, 0]]])
wf = MultiplyWF(wf1, wf2)
dfvmc, configs_ = vmc(
wf, configs, nsteps=50, accumulators={"energy": EnergyAccumulator(mol)}
)
dfvmc = pd.DataFrame(dfvmc)
print(
"vmc energy",
np.mean(dfvmc["energytotal"]),
np.std(dfvmc["energytotal"]) / np.sqrt(len(dfvmc)),
)
warmup = 200
def test_vmc():
"""
Test that a VMC calculation of a Slater determinant matches Hartree-Fock within error bars.
"""
nconf = 1000
mol = gto.M(
atom="Li 0. 0. 0.; Li 0. 0. 1.5", basis="cc-pvtz", unit="bohr", verbose=1
)
mf_rhf = scf.RHF(mol).run()
mf_uhf = scf.UHF(mol).run()
nsteps = 300
warmup = 30
for wf, mf in [
(PySCFSlaterUHF(mol, mf_rhf), mf_rhf),
(PySCFSlaterUHF(mol, mf_uhf), mf_uhf),
]:
#Without blocks
coords = initial_guess(mol, nconf)
df, coords = vmc(
wf, coords, nsteps=nsteps, accumulators={"energy": EnergyAccumulator(mol)}, verbose=True
)
df = pd.DataFrame(df)
df = reblock(df["energytotal"][warmup:], 20)
en = df.mean()
err = df.sem()
assert en - mf.energy_tot() < 5 * err, "pyscf {0}, vmc {1}, err {2}".format(
mf.energy_tot(), en, err
)
"""
Test that a VMC calculation of a Slater determinant matches Hartree-Fock within error bars.
"""
nconf = 1000
mol = gto.M(
atom="Li 0. 0. 0.; Li 0. 0. 1.5", basis="cc-pvtz", unit="bohr", verbose=1
)
mf_rhf = scf.RHF(mol).run()
mf_uhf = scf.UHF(mol).run()
nsteps = 300
warmup = 30
for wf, mf in [
(PySCFSlaterUHF(mol, mf_rhf), mf_rhf),
(PySCFSlaterUHF(mol, mf_uhf), mf_uhf),
]:
#Without blocks
coords = initial_guess(mol, nconf)
df, coords = vmc(
wf, coords, nsteps=nsteps, accumulators={"energy": EnergyAccumulator(mol)}, verbose=True
)
df = pd.DataFrame(df)
df = reblock(df["energytotal"][warmup:], 20)
en = df.mean()
err = df.sem()
assert en - mf.energy_tot() < 5 * err, "pyscf {0}, vmc {1}, err {2}".format(
mf.energy_tot(), en, err
)
from pyqmc.coord import OpenConfigs
import pyqmc
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-4
nconf = 10
epos = pyqmc.initial_guess(mol, nconf)
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:
if k != "mo_coeff":
wf.parameters[k] = np.random.rand(*wf.parameters[k].shape)
for fname, func in zip(
["gradient", "laplacian", "pgradient"],
[
testwf.test_wf_gradient,
testwf.test_wf_laplacian,
testwf.test_wf_pgradient,
],
):
err = []
for delta in [1e-4, 1e-5, 1e-6, 1e-7, 1e-8]:
err.append(func(wf, epos, delta)[0])
print(fname, min(err))