Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
obdm_down = OBDMAccumulator(mol=mol, orb_coeff=iaos[1], nsweeps=tbdm_sweeps, spin=1)
tbdm_upup = TBDMAccumulator(
mol=mol, orb_coeff=iaos, nsweeps=tbdm_sweeps, tstep=tbdm_tstep, spin=(0, 0)
)
tbdm_updown = TBDMAccumulator(
mol=mol, orb_coeff=iaos, nsweeps=tbdm_sweeps, tstep=tbdm_tstep, spin=(0, 1)
)
tbdm_downup = TBDMAccumulator(
mol=mol, orb_coeff=iaos, nsweeps=tbdm_sweeps, tstep=tbdm_tstep, spin=(1, 0)
)
tbdm_downdown = TBDMAccumulator(
mol=mol, orb_coeff=iaos, nsweeps=tbdm_sweeps, tstep=tbdm_tstep, spin=(1, 1)
)
print("VMC...")
df, coords = vmc(
wf,
configs,
nsteps=n_vmc_steps,
tstep=vmc_tstep,
accumulators={
"energy": energy,
"obdm_up": obdm_up,
"obdm_down": obdm_down,
"tbdm_upup": tbdm_upup,
"tbdm_updown": tbdm_updown,
"tbdm_downup": tbdm_downup,
"tbdm_downdown": tbdm_downdown,
},
verbose=True,
)
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"])
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
)
#With blocks
coords = initial_guess(mol, nconf)
df, coords = vmc(
wf, coords, nblocks=int(nsteps/30), nsteps_per_block=30,
accumulators={"energy": EnergyAccumulator(mol)}
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
# make AO to localized orbital coefficients.
mfobdm = [np.einsum("ij,j,kj->ik", l.conj(), o, l) for l, o in zip(lreps, occs)]
### Test OBDM calculation.
nconf = 800
nsteps = 50
warmup = 6
wf = PySCFSlater(mol, mf)
configs = initial_guess(mol, nconf)
obdm_dict = dict(mol=mol, orb_coeff=lowdin, kpts=kpts, nsweeps=4, warmup=10)
obdm = OBDMAccumulator(**obdm_dict)
obdm_up = OBDMAccumulator(**obdm_dict, spin=0)
obdm_down = OBDMAccumulator(**obdm_dict, spin=1)
df, coords = vmc(
wf,
configs,
nsteps=nsteps,
accumulators={"obdm": obdm, "obdm_up": obdm_up, "obdm_down": obdm_down},
verbose=True,
)
obdm_est = {}
for k in ["obdm", "obdm_up", "obdm_down"]:
avg_norm = np.mean(df[k + "norm"][warmup:], axis=0)
avg_obdm = np.mean(df[k + "value"][warmup:], axis=0)
obdm_est[k] = normalize_obdm(avg_obdm, avg_norm)
print("Average OBDM(orb,orb)", obdm_est["obdm"].round(3))
mfobdm = scipy.linalg.block_diag(*mfobdm)
print("mf obdm", mfobdm.round(3))
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
dfdmc, configs_, weights_ = rundmc(
wf,
configs,
nsteps=4000 + warmup,
branchtime=5,
accumulators={"energy": EnergyAccumulator(mol)},
def test_ecp():
mol = gto.M(atom="C 0. 0. 0.", ecp="bfd", basis="bfd_vtz")
mf = scf.RHF(mol).run()
nconf = 5000
wf = PySCFSlater(mol, mf)
coords = initial_guess(mol, nconf)
df, coords = vmc(
wf, coords, nsteps=100, accumulators={"energy": EnergyAccumulator(mol)}
)
df = pd.DataFrame(df)
warmup = 30
print(
"mean field",
mf.energy_tot(),
"vmc estimation",
np.mean(df["energytotal"][warmup:]),
np.std(df["energytotal"][warmup:]),
)
assert abs(mf.energy_tot() - np.mean(df["energytotal"][warmup:])) <= np.std(
df["energytotal"][warmup:]
)
def gradient_energy_function(x, coords):
newparms = pgrad_acc.transform.deserialize(x)
for k in newparms:
wf.parameters[k] = newparms[k]
df, coords = pyqmc.mc.vmc(
wf,
coords,
accumulators={"pgrad": pgrad_acc},
client=client,
npartitions=npartitions,
**vmcoptions
)
en = np.real(np.mean(df["pgradtotal"], axis=0))
en_err = np.std(df["pgradtotal"], axis=0) / np.sqrt(df["pgradtotal"].shape[0])
sigma = np.std(df["pgradtotal"], axis=0) * np.sqrt(np.mean(df["nconfig"]))
dpH = np.mean(df["pgraddpH"], axis=0)
dp = np.mean(df["pgraddppsi"], axis=0)
dpdp = np.mean(df["pgraddpidpj"], axis=0)
grad = 2 * np.real(dpH - en * dp)
Sij = np.real(dpdp - np.einsum("i,j->ij", dp, dp))
weights: The final weights from this calculation
"""
# Restart from HDF file
if hdf_file is not None and os.path.isfile(hdf_file):
with h5py.File(hdf_file, "r") as hdf:
stepoffset = hdf["step"][-1] + 1
configs.load_hdf(hdf)
weights = np.array(hdf["weights"])
eref = hdf["eref"][-1]
esigma = hdf["esigma"][-1]
if verbose:
print("Restarted calculation")
else:
warmup = 2
df, configs = mc.vmc(
wf,
configs,
accumulators=accumulators,
client=client,
npartitions=npartitions,
verbose=verbose,
)
en = df[ekey[0] + ekey[1]][warmup:]
eref = np.mean(en).real
esigma = np.sqrt(np.var(en) * np.mean(df["nconfig"]))
if verbose:
print("eref start", eref, "esigma", esigma)
nconfig = configs.configs.shape[0]
if weights is None:
weights = np.ones(nconfig)