def freeze_core(h1e, chol, ecore, nc, ncas, verbose=True): # 1. Construct one-body hamiltonian print(ecore, type(h1e), type(chol)) nbasis = h1e.shape[-1] chol = chol.reshape((-1, nbasis, nbasis)) system = dotdict({ 'H1': numpy.array([h1e, h1e]), 'chol_vecs': chol, 'ecore': ecore, 'nbasis': nbasis }) psi = numpy.identity(nbasis)[:, :nc] Gcore = gab(psi, psi) ecore = local_energy_generic_cholesky(system, [Gcore, Gcore])[0] (hc_a, hc_b) = core_contribution_cholesky(system.chol_vecs, [Gcore, Gcore]) h1e = numpy.array([h1e, h1e]) h1e[0] = h1e[0] + 2 * hc_a h1e[1] = h1e[1] + 2 * hc_b h1e = h1e[:, nc:nc + ncas, nc:nc + ncas] nchol = chol.shape[0] chol = chol[:, nc:nc + ncas, nc:nc + ncas].reshape((nchol, -1)) # 4. Subtract one-body term from writing H2 as sum of squares. if verbose: print(" # Number of active orbitals: %d" % ncas) print(" # Freezing %d core electrons and %d virtuals." % (2 * nc, nbasis - nc - ncas)) print(" # Frozen core energy : %13.8e" % ecore.real) return h1e, chol, ecore
def test_pw(): options = {'rs': 2, 'nup': 7, 'ndown': 7, 'ecut': 2, 'write_integrals': True} system = UEG(inputs=options) occ = numpy.eye(system.nbasis)[:,:system.nup] wfn = numpy.zeros((1,system.nbasis,system.nup+system.ndown), dtype=numpy.complex128) wfn[0,:,:system.nup] = occ wfn[0,:,system.nup:] = occ coeffs = numpy.array([1+0j]) trial = MultiSlater(system, (coeffs, wfn)) qmc = dotdict({'dt': 0.005, 'nstblz': 5}) prop = PlaneWave(system, trial, qmc) walker = SingleDetWalker({}, system, trial) numpy.random.seed(7) a = numpy.random.rand(system.nbasis*(system.nup+system.ndown)) b = numpy.random.rand(system.nbasis*(system.nup+system.ndown)) wfn = (a + 1j*b).reshape((system.nbasis,system.nup+system.ndown)) walker.phi = wfn.copy() walker.greens_function(trial) # fb = prop.construct_force_bias_slow(system, walker, trial) fb = prop.construct_force_bias(system, walker, trial) assert numpy.linalg.norm(fb) == pytest.approx(0.16660828645573392) xi = numpy.random.rand(system.nfields) vhs = prop.construct_VHS(system, xi-fb) assert numpy.linalg.norm(vhs) == pytest.approx(0.1467322554815581)
def test_walker_overlap(): system = dotdict({ 'nup': 5, 'ndown': 5, 'nbasis': 10, 'nelec': (5, 5), 'ne': 10 }) numpy.random.seed(7) a = numpy.random.rand(3 * system.nbasis * (system.nup + system.ndown)) b = numpy.random.rand(3 * system.nbasis * (system.nup + system.ndown)) wfn = (a + 1j * b).reshape((3, system.nbasis, system.nup + system.ndown)) coeffs = numpy.array([0.5 + 0j, 0.3 + 0j, 0.1 + 0j]) trial = MultiSlater(system, (coeffs, wfn)) walker = MultiDetWalker({}, system, trial) def calc_ovlp(a, b): return numpy.linalg.det(numpy.dot(a.conj().T, b)) ovlp = 0.0 + 0j na = system.nup pa = trial.psi[0, :, :na] pb = trial.psi[0, :, na:] for i, d in enumerate(trial.psi): ovlp += coeffs[i].conj() * calc_ovlp(d[:, :na], pa) * calc_ovlp( d[:, na:], pb) assert ovlp.real == pytest.approx(walker.ovlp.real) assert ovlp.imag == pytest.approx(walker.ovlp.imag) # Test PH type wavefunction. orbs = numpy.arange(system.nbasis) oa = [c for c in itertools.combinations(orbs, system.nup)] ob = [c for c in itertools.combinations(orbs, system.ndown)] oa, ob = zip(*itertools.product(oa, ob)) oa = oa[:5] ob = ob[:5] coeffs = numpy.array([0.9, 0.01, 0.01, 0.02, 0.04], dtype=numpy.complex128) wfn = (coeffs, oa, ob) a = numpy.random.rand(system.nbasis * (system.nup + system.ndown)) b = numpy.random.rand(system.nbasis * (system.nup + system.ndown)) init = (a + 1j * b).reshape((system.nbasis, system.nup + system.ndown)) trial = MultiSlater(system, wfn, init=init) walker = MultiDetWalker({}, system, trial) I = numpy.eye(system.nbasis) ovlp_sum = 0.0 for idet, (c, occa, occb) in enumerate(zip(coeffs, oa, ob)): psia = I[:, occa] psib = I[:, occb] sa = numpy.dot(psia.conj().T, init[:, :system.nup]) sb = numpy.dot(psib.conj().T, init[:, system.nup:]) isa = numpy.linalg.inv(sa) isb = numpy.linalg.inv(sb) ga = numpy.dot(init[:, :system.nup], numpy.dot(isa, psia.conj().T)).T gb = numpy.dot(init[:, system.nup:], numpy.dot(isb, psib.conj().T)).T ovlp = numpy.linalg.det(sa) * numpy.linalg.det(sb) ovlp_sum += c.conj() * ovlp walk_ovlp = walker.ovlps[idet] assert ovlp == pytest.approx(walk_ovlp) assert numpy.linalg.norm(ga - walker.Gi[idet, 0]) == pytest.approx(0) assert numpy.linalg.norm(gb - walker.Gi[idet, 1]) == pytest.approx(0) assert ovlp_sum == pytest.approx(walker.ovlp)
def get_estimator_enum(thermal=False): keys = [ 'uweight', 'weight', 'enumer', 'edenom', 'eproj', 'e1b', 'e2b', 'ehyb', 'ovlp' ] if thermal: keys.append('nav') keys.append('time') enum = {} for v, k in enumerate(keys): enum[k] = v return dotdict(enum)
def test_back_prop(): sys = UEG({'rs': 2, 'nup': 7, 'ndown': 7, 'ecut': 1.0}) bp_opt = {'tau_bp': 1.0, 'nsplit': 4} qmc = dotdict({'dt': 0.05, 'nstblz': 10, 'nwalkers': 1}) trial = HartreeFock(sys, True, {}) numpy.random.seed(8) prop = Continuous(sys, trial, qmc) est = BackPropagation(bp_opt, True, 'estimates.0.h5', qmc, sys, trial, numpy.complex128, prop.BT_BP) walkers = Walkers({}, sys, trial, qmc, nbp=est.nmax, nprop_tot=est.nmax) wlk = walkers.walkers[0] from mpi4py import MPI comm = MPI.COMM_WORLD for i in range(0, 2 * est.nmax): prop.propagate_walker(wlk, sys, trial, 0) if i % 10 == 0: walkers.orthogonalise(trial, False) est.update_uhf(sys, qmc, trial, walkers, 100) est.print_step(comm, comm.size, i, 10)
def test_hubbard(): options = {'nx': 4, 'ny': 4, 'U': 4, 'mu': 1.0, 'nup': 7, 'ndown': 7} system = Hubbard(options, verbose=False) comm = MPI.COMM_WORLD beta = 2.0 dt = 0.05 nslice = int(round(beta / dt)) trial = OneBody(comm, system, beta, dt) numpy.random.seed(7) qmc = dotdict({'dt': dt, 'nstblz': 10}) prop = ThermalDiscrete({}, qmc, system, trial, verbose=False) walker1 = ThermalWalker({ 'stack_size': 1, 'low_rank': False }, system, trial, verbose=False) for ts in range(0, nslice): prop.propagate_walker(system, walker1, ts, 0) walker1.weight /= 1.0e6 numpy.random.seed(7) walker2 = ThermalWalker({ 'stack_size': 10, 'low_rank': False }, system, trial, verbose=False) energies = [] for ts in range(0, nslice): prop.propagate_walker(system, walker2, ts, 0) walker2.weight /= 1.0e6 # if ts % 10 == 0: # energies.append(walker2.local_energy(system)[0]) # import matplotlib.pyplot as pl # pl.plot(energies, markersize=2) # pl.show() assert walker1.weight == pytest.approx(walker2.weight) assert numpy.linalg.norm(walker1.G - walker2.G) == pytest.approx(0) assert walker1.local_energy(system)[0] == pytest.approx( walker2.local_energy(system)[0])
def test_phmsd(): numpy.random.seed(7) nmo = 10 nelec = (5, 5) options = {'sparse': False} h1e, chol, enuc, eri = generate_hamiltonian(nmo, nelec, cplx=False) system = Generic(nelec=nelec, h1e=h1e, chol=chol, ecore=0, inputs=options) wfn = get_random_nomsd(system, ndet=3) trial = MultiSlater(system, wfn) walker = MultiDetWalker({}, system, trial) qmc = dotdict({'dt': 0.005, 'nstblz': 5}) prop = GenericContinuous(system, trial, qmc) fb = prop.construct_force_bias(system, walker, trial) prop.construct_VHS(system, fb) # Test PH type wavefunction. wfn, init = get_random_phmsd(system, ndet=3, init=True) trial = MultiSlater(system, wfn, init=init) prop = GenericContinuous(system, trial, qmc) walker = MultiDetWalker({}, system, trial) fb = prop.construct_force_bias(system, walker, trial) vhs = prop.construct_VHS(system, fb)
def test_propagate_walker(): options = {'nx': 4, 'ny': 4, 'U': 4, 'mu': 1.0, 'nup': 7, 'ndown': 7} system = Hubbard(options, verbose=False) comm = MPI.COMM_WORLD beta = 2.0 dt = 0.05 nslice = int(round(beta / dt)) trial = OneBody(comm, system, beta, dt) numpy.random.seed(7) qmc = dotdict({'dt': dt, 'nstblz': 1}) prop = ThermalDiscrete({}, qmc, system, trial, verbose=False) walker1 = ThermalWalker({ 'stack_size': 1, 'low_rank': False }, system, trial, verbose=False) walker2 = ThermalWalker({ 'stack_size': 1, 'low_rank': False }, system, trial, verbose=False) rands = numpy.random.random(system.nbasis) I = numpy.eye(system.nbasis) BV = numpy.zeros((2, system.nbasis)) BV[0] = 1.0 BV[1] = 1.0 walker2.greens_function(trial, slice_ix=0) walker1.greens_function(trial, slice_ix=0) for it in range(0, nslice): rands = numpy.random.random(system.nbasis) BV = numpy.zeros((2, system.nbasis)) BV[0] = 1.0 BV[1] = 1.0 for i in range(system.nbasis): if rands[i] > 0.5: xi = 0 else: xi = 1 BV[0, i] = prop.auxf[xi, 0] BV[1, i] = prop.auxf[xi, 1] # Check overlap ratio if it % 20 == 0: probs1 = prop.calculate_overlap_ratio(walker1, i) G2old = walker2.greens_function(trial, slice_ix=it, inplace=False) B = numpy.einsum('ki,kij->kij', BV, prop.BH1) walker2.stack.stack[it] = B walker2.greens_function(trial, slice_ix=it) G2 = walker2.G pdirect = numpy.linalg.det(G2old[0]) / numpy.linalg.det(G2[0]) pdirect *= 0.5 * numpy.linalg.det(G2old[1]) / numpy.linalg.det( G2[1]) pdirect == pytest.approx(probs1[xi]) prop.update_greens_function(walker1, i, xi) B = numpy.einsum('ki,kij->kij', BV, prop.BH1) walker1.stack.update(B) if it % prop.nstblz == 0: walker1.greens_function(None, walker1.stack.time_slice - 1) walker2.stack.stack[it] = B walker2.greens_function(trial, slice_ix=it) numpy.linalg.norm(walker1.G - walker2.G) == pytest.approx(0.0) prop.propagate_greens_function(walker1)