Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def runtest(mol, mf, kind=0):
for k, occ in enumerate(mf.mo_occ):
print(k, occ)
kpt = mf.kpts[kind]
twist = np.dot(kpt, mol.lattice_vectors().T / (2 * np.pi))
print("kpt", kpt)
print("twist", twist)
wf0 = pyqmc.PySCFSlater(mol, mf)
wft = pyqmc.PySCFSlater(mol, mf, twist=twist)
#####################################
## compare values across boundary
## psi, KE, ecp,
#####################################
nconfig = 100
coords = pyqmc.initial_guess(mol, nconfig, 1)
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)
def runtest(mol, mf, kind=0):
for k, occ in enumerate(mf.mo_occ):
print(k, occ)
kpt = mf.kpts[kind]
twist = np.dot(kpt, mol.lattice_vectors().T / (2 * np.pi))
print("kpt", kpt)
print("twist", twist)
wf0 = pyqmc.PySCFSlater(mol, mf)
wft = pyqmc.PySCFSlater(mol, mf, twist=twist)
#####################################
## compare values across boundary
## psi, KE, ecp,
#####################################
nconfig = 100
coords = pyqmc.initial_guess(mol, nconfig, 1)
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())
def runtest(mol, mf, kind=0, do_mc=False):
if do_mc:
from pyscf import mcscf
mc = mcscf.CASCI(mf, ncas=4, nelecas=(1, 1))
mc.kernel()
wf = pyqmc.default_msj(mol, mf, mc)[0]
kpt = mf.kpt
dm = mc.make_rdm1()
if len(dm.shape) == 4:
dm = np.sum(dm, axis=0)
else:
kpt = mf.kpts[kind]
wf = pyqmc.PySCFSlater(mol, mf)
dm = mf.make_rdm1()
print("original dm shape", dm.shape)
if len(dm.shape) == 4:
dm = np.sum(dm, axis=0)
dm = dm[kind]
#####################################
## 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))
#####################################
# Mean-field obdm in IAO basis
mfobdm = mf.make_rdm1(mo, mf.mo_occ)
# 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)
)
mf = scf.RHF(mol).run()
# Lowdin orthogonalized AO basis.
lowdin = lo.orth_ao(mol, "lowdin")
# MOs in the Lowdin basis.
mo = solve(lowdin, mf.mo_coeff)
# make AO to localized orbital coefficients.
mfobdm = mf.make_rdm1(mo, mf.mo_occ)
### Test OBDM calculation.
nconf = 500
nsteps = 400
warmup = 15
wf = PySCFSlater(mol, mf)
configs = initial_guess(mol, nconf)
obdm_dict = dict(mol=mol, orb_coeff=lowdin, nsweeps=5, warmup=15)
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},
)
obdm_est = {}
for k in ["obdm", "obdm_up", "obdm_down"]:
avg_norm = np.mean(df[k + "norm"][warmup:], axis=0)
# lowdin = lo.orth_ao(mol, "lowdin")
loiao = lo.iao.iao(mol.original_cell, mf.mo_coeff, kpts=kpts)
occs = [mf.mo_occ[k] for k in kinds]
coefs = [mf.mo_coeff[k] for k in kinds]
ovlp = mf.get_ovlp()[kinds]
lowdin = [lo.vec_lowdin(l, o) for l, o in zip(loiao, ovlp)]
lreps = [np.linalg.multi_dot([l.T, o, c]) for l, o, c in zip(lowdin, ovlp, coefs)]
# 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"]: