Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
print("dm", dm.shape)
pyscfke = np.real(np.einsum("ij,ji->", ke_mat, dm))
print("PySCF kinetic energy: {0}".format(pyscfke))
#####################################
## evaluate KE integral with VMC
#####################################
coords = pyqmc.initial_guess(mol, 1200, 0.7)
warmup = 10
start = time.time()
df, coords = pyqmc.vmc(
wf,
coords,
nsteps=100 + warmup,
tstep=1,
accumulators={"energy": pyqmc.accumulators.EnergyAccumulator(mol)},
verbose=False,
hdf_file=str(uuid.uuid4()),
)
print("VMC time", time.time() - start)
df = pd.DataFrame(df)
dfke = reblock(df["energyke"][warmup:], 10)
dfke /= mol.scale
vmcke, err = dfke.mean(), dfke.sem()
print("VMC kinetic energy: {0} +- {1}".format(vmcke, err))
assert (
np.abs(vmcke - pyscfke) < 5 * err
), "energy diff not within 5 sigma ({0:.6f}): energies \n{1} \n{2}".format(
5 * err, vmcke, pyscfke
)
nelec = coords.configs.shape[1]
epos, wrap = enforce_pbc(coords.lvecs, coords.configs)
coords = PeriodicConfigs(epos, coords.lvecs)
shift_ = np.random.randint(10, size=coords.configs.shape) - 5
phase = np.exp(2j * np.pi * np.einsum("ijk,k->ij", shift_, twist))
shift = np.dot(shift_, mol.lattice_vectors())
epos, wrap = enforce_pbc(coords.lvecs, epos + shift)
newcoords = PeriodicConfigs(epos, coords.lvecs, wrap=wrap)
assert np.linalg.norm(newcoords.configs - coords.configs) < 1e-12
ph0, val0 = wf0.recompute(coords)
pht, valt = wft.recompute(coords)
enacc = pyqmc.accumulators.EnergyAccumulator(mol, threshold=np.inf)
np.random.seed(0)
en0 = enacc(coords, wf0)
np.random.seed(0)
ent = enacc(coords, wft)
e = 0
rat0 = wf0.testvalue(e, newcoords.electron(e))
assert np.linalg.norm(rat0 - 1) < 1e-10, rat0 - 1
ratt = wft.testvalue(e, newcoords.electron(e))
rattdiff = ratt - phase[:, e]
assert np.linalg.norm(rattdiff) < 1e-9, [
np.round(rattdiff, 10),
np.amax(np.abs(rattdiff)),
]
ph0new, val0new = wf0.recompute(newcoords)
# Mean-field tbdm in IAO basis
mftbdm = singledet_tbdm(mf, mfobdm)
### Test TBDM calculation.
# VMC params
nconf = 500
n_vmc_steps = 400
vmc_tstep = 0.3
vmc_warmup = 30
# TBDM params
tbdm_sweeps = 4
tbdm_tstep = 0.5
wf = PySCFSlater(mol, mf) # Single-Slater (no jastrow) wf
configs = initial_guess(mol, nconf)
energy = EnergyAccumulator(mol)
obdm_up = OBDMAccumulator(mol=mol, orb_coeff=iaos[0], nsweeps=tbdm_sweeps, spin=0)
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...")
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)},
ekey=("energy", "total"),
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.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"])
mf = scf.RHF(mol).run()
nconf = 1000
coords = initial_guess(mol, nconf)
thresholds = [1e15, 100, 50, 20, 10, 5, 1]
label = ["S", "J", "SJ"]
ind = 0
for wf in [
PySCFSlater(mol, mf),
JastrowSpin(mol),
MultiplyWF(PySCFSlater(mol, mf), JastrowSpin(mol)),
]:
wf.recompute(coords)
print(label[ind])
ind += 1
for threshold in thresholds:
eacc = EnergyAccumulator(mol, threshold)
start = time.time()
eacc(coords, wf)
end = time.time()
print("Threshold=", threshold, np.around(end - start, 2), "s")
mc = mcscf.CASCI(mf, ncas=4, nelecas=(2, 2))
mc.kernel()
label = ["MS"]
ind = 0
for wf in [MultiSlater(mol, mf, mc)]:
wf.recompute(coords)
print(label[ind])
ind += 1
for threshold in thresholds:
eacc = EnergyAccumulator(mol, threshold)
start = time.time()
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:]
)
descriptors = {
"t": [[(1.0, (0, 1)), (1.0, (1, 0))], [(1.0, (0, 1)), (1.0, (1, 0))]],
"trace": [[(1.0, (0, 0)), (1.0, (1, 1))], [(1.0, (0, 0)), (1.0, (1, 1))]],
}
for i in [0, 1]:
descriptors[f"nup{i}"] = [[(1.0, (i, i))], []]
descriptors[f"ndown{i}"] = [[], [(1.0, (i, i))]]
tbdm_up_down = TBDMAccumulator(mol=mol, orb_coeff=np.array([a,a]), spin=(0,1), ijkl=[[0,0,0,0]])
tbdm_down_up = TBDMAccumulator(mol=mol, orb_coeff=np.array([a,a]), spin=(1,0), ijkl=[[0,0,0,0]])
descriptors_tbdm = {
"U": [[(1.0,(0))],[(1.0,(0))]]
}
acc = PGradDescriptor(
EnergyAccumulator(mol),
LinearTransform(wf.parameters, freeze=freeze),
{
'obdm': [obdm_up, obdm_down],
'tbdm': [tbdm_up_down, tbdm_down_up],
},
{
'obdm': DescriptorFromOBDM(descriptors, norm=2.0),
'tbdm': DescriptorFromTBDM(descriptors_tbdm, norm=2.0*(2.0-1.0)),
},
)
return {"wf": wf, "acc": acc, "mol": mol, "mf": mf, "descriptors": descriptors, "descriptors_tbdm": descriptors_tbdm}