Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
#####################################
## evaluate KE in PySCF
#####################################
ke_mat = mol.pbc_intor("int1e_kin", hermi=1, kpts=np.array(kpt))
print("ke_mat", ke_mat.shape)
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 (
mol=gto.M(atom='H 0. 0. 0.; H 0. 0. 2.0',unit='bohr',
ecp='bfd', basis='bfd_vtz')
mf = scf.RHF(mol).run()
mol.output=None
mol.stdout=None
mf.output=None
mf.stdout=None
mf.chkfile=None
from pyqmc import ExpCuspFunction,GaussianFunction,MultiplyWF,PySCFSlaterRHF,JastrowSpin,initial_guess,EnergyAccumulator
from pyqmc.accumulators import PGradTransform,LinearTransform
nconf=1600
basis=[ExpCuspFunction(2.0,1.5),GaussianFunction(0.5),GaussianFunction(2.0),GaussianFunction(.25),GaussianFunction(1.0),GaussianFunction(4.0),GaussianFunction(8.0) ]
wf=MultiplyWF(PySCFSlaterRHF(mol,mf),JastrowSpin(mol,basis,basis))
coords = initial_guess(mol,nconf)
df, coords = vmc(wf,coords,nsteps=30) #warmup
energy_acc=EnergyAccumulator(mol)
pgrad_acc=PGradTransform(energy_acc,LinearTransform(wf.parameters,['wf2acoeff','wf2bcoeff']))
from pyqmc.optsr import gradient_descent
from pyqmc.parsltools import distvmc
gradient_lines(wf,coords,pgrad_acc,warmup=4, iters=5,vmc=distvmc,verbose=1,
vmcoptions={'npartitions':ncore,'nsteps':100,'nsteps_per':100}
)
obdm_up = np.mean(np.array(vmc_hdf["obdm_upvalue"]), axis=0)
obdm_down = np.mean(np.array(vmc_hdf["obdm_downvalue"]), axis=0)
basis_up = gen_basis(mol, sys["mf"], obdm_up)
basis_down = gen_basis(mol, sys["mf"], obdm_down)
obdm_up_acc = OBDMAccumulator(mol=mol, orb_coeff=basis_up, spin=0)
obdm_down_acc = OBDMAccumulator(mol=mol, orb_coeff=basis_down, spin=1)
tbdm = TBDMAccumulator(mol, np.array([basis_up, basis_down]), spin=(0, 1))
acc = {
"energy": EnergyAccumulator(mol),
"obdm_up": obdm_up_acc,
"obdm_down": obdm_down_acc,
"tbdm": tbdm,
}
configs = pyqmc.initial_guess(sys["mol"], 1000)
pyqmc.vmc(sys["wf"], configs, nsteps=500, hdf_file=hdf_final, accumulators=acc)
nconfig = 100
S = np.eye(3) * 2 # 2x2x2 supercell
supercell = get_supercell(cell, S)
wf, to_opt = pyqmc.default_sj(supercell, kmf)
configs = pyqmc.initial_guess(supercell, nconfig)
# Initialize energy accumulator (and Ewald)
pgrad = pyqmc.gradient_generator(supercell, wf, to_opt=to_opt)
# Optimize jastrow
wf, lm_df = pyqmc.line_minimization(
wf, configs, pgrad, hdf_file="pbc_he_linemin.hdf", verbose=True
)
# Run VMC
df, configs = pyqmc.vmc(
wf,
configs,
nblocks=100,
accumulators={"energy": pgrad.enacc},
hdf_file="pbc_he_vmc.hdf",
verbose=True,
)
# Run DMC
pyqmc.rundmc(
wf,
configs,
nsteps=1000,
accumulators={"energy": pgrad.enacc},
hdf_file="pbc_he_dmc.hdf",
verbose=True,
"excited1_vmc": "excited1_vmc.hdf5",
"excited2_vmc": "excited2_vmc.hdf5",
"linemin_final": "linemin_final.hdf5",
"excited1_final": "excited1_final.hdf5",
"excited2_final": "excited2_final.hdf5",
}
for k, it in savefiles.items():
if os.path.isfile(it):
os.remove(it)
# Run
setuph2(savefiles["mf"], "test")
sys = pyqmc_from_hdf(savefiles["mf"])
df, coords = vmc(
sys["wf"], pyqmc.initial_guess(sys["mol"], nconfig), client=client, nsteps=10, npartitions=ncore,
)
line_minimization(
sys["wf"],
coords,
sys["pgrad"],
hdf_file=savefiles["linemin"],
client=client,
npartitions=ncore,
verbose=True,
)
# First excited state
wfs = [sys["wf"], deepcopy(sys["wf"])]
optimize_orthogonal(
Args:
wf : a wave function object
configs : starting configurations for VMC. Ideally equilibrated with wf.
acc : A PGradDescriptor object which generates descriptors
objective : A dictionary which has one value for every descriptor returned by acc
forcing : A dictionary which has one value for every descriptor returned by acc
"""
import pandas as pd
if vmc is None:
vmc = pyqmc.vmc
if vmcoptions is None:
vmcoptions = {}
if lm is None:
lm = lm_cvmc
if lmoptions is None:
lmoptions = {}
if update_kws is None:
update_kws = {}
# Restart
if hdf_file is not None:
with h5py.File(hdf_file, "a") as hdf:
if "wf" in hdf.keys():
grp = hdf["wf"]
for k in grp.keys():
wf.parameters[k] = np.array(grp[k])