def test_ueg(): sys = UEG({'rs': 2, 'nup': 2, 'ndown': 2, 'ecut': 0.5}) sys.ecore = 0 eig, evec = simple_fci(sys) assert len(eig) == 441 assert eig[0] == pytest.approx(1.327088181107) assert eig[231] == pytest.approx(2.883365264420) assert eig[424] == pytest.approx(3.039496944900) assert eig[-1] == pytest.approx(3.207573492596)
def test_constructor(): options = { 'verbosity': 0, 'get_sha1': False, 'qmc': { 'timestep': 0.01, 'num_steps': 10, 'blocks': 10, 'rng_seed': 8, }, 'estimates': { 'mixed': { 'energy_eval_freq': 1 } } } model = { 'name': "UEG", 'rs': 2.44, 'ecut': 4, 'nup': 7, 'ndown': 7, } system = UEG(model) trial = HartreeFock(system, True, {}) comm = MPI.COMM_WORLD afqmc = AFQMC(comm=comm, options=options, system=system, trial=trial) afqmc.finalise(verbose=0) assert afqmc.trial.energy.real == pytest.approx(1.7796083856572522)
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 get_system(sys_opts=None, verbose=0, chol_cut=1e-5): """Wrapper to select system class Parameters ---------- sys_opts : dict System input options. verbose : bool Output verbosity. Returns ------- system : object System class. """ if sys_opts['name'] == 'Hubbard': system = Hubbard(sys_opts, verbose) elif sys_opts['name'] == 'Generic': system = Generic(inputs=sys_opts, verbose=verbose) elif sys_opts['name'] == 'UEG': system = UEG(sys_opts, verbose) else: system = None return system
def test_nomsd(): system = UEG({'nup': 7, 'ndown': 7, 'rs': 5, 'ecut': 4, 'thermal': True}) import os path = os.path.dirname(os.path.abspath(__file__)) wfn, psi0 = read_qmcpack_wfn_hdf(path + '/wfn.h5') trial = MultiSlater(system, wfn, init=psi0) trial.recompute_ci_coeffs(system) # TODO: Fix python3.7 cython issue. trial.calculate_energy(system) ndets = trial.ndets H = numpy.zeros((ndets, ndets), dtype=numpy.complex128) S = numpy.zeros((ndets, ndets), dtype=numpy.complex128) variational_energy_multi_det(system, trial.psi, trial.coeffs, H=H, S=S) e, ev = scipy.linalg.eigh(H, S) evar = variational_energy_multi_det(system, trial.psi, ev[:, 0])
def find_mu_opt(options): comm = MPI.COMM_WORLD # Guess initial chemical potential from trial density matrix (mu_xc < 0) system = UEG(options['system']) qmcopt = QMCOpts(options['qmc'], system) trial = OneBody(comm, system, qmcopt.beta, qmcopt.dt) mu0 = trial.mu # guess for bracket. mu1 = mu0 - 0.5 * abs(mu0) mu_opt = secant(comm, options, mu0, mu1, system.ne) if comm.rank == 0: print("# Converged mu: {}".format(mu_opt)) # Run longer simulation at optimal mu. sys_opts['mu'] = mu_opt qmc['nsteps'] = 50 estim['basename'] = 'optimal' afqmc = ThermalAFQMC(comm, options=options, verbose=(comm.rank == 0)) afqmc.run(comm=comm, verbose=True)
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 unit_test(): from pauxy.systems.ueg import UEG from pauxy.qmc.options import QMCOpts from pauxy.trial_wavefunction.hartree_fock import HartreeFock inputs = { 'nup': 1, 'ndown': 1, 'rs': 1.0, 'ecut': 1.0, 'dt': 0.05, 'nwalkers': 10 } system = UEG(inputs, True) qmc = QMCOpts(inputs, system, True) trial = HartreeFock(system, False, inputs, True) driver = Continuous({}, qmc, system, trial, True)
def unit_test(): from numpy import linalg as LA from pyscf import gto, scf, ao2mo, mcscf, fci, ci, cc, tdscf, gw, hci from pauxy.systems.ueg import UEG from pauxy.trial_wavefunction.hartree_fock import HartreeFock from pauxy.trial_wavefunction.free_electron import FreeElectron ecut = 1.0 inputs = {'nup': 7, 'ndown': 7, 'rs': 1.0, 'thermal': False, 'ecut': ecut} system = PW_FFT(inputs, True) trial = FreeElectron(system, False, inputs, True) from pauxy.qmc.options import QMCOpts from pauxy.propagation.continuous import Continuous system2 = UEG(inputs, True) qmc = QMCOpts(inputs, system2, True) trial = HartreeFock(system2, False, inputs, True)
def unit_test(): from pauxy.systems.ueg import UEG import numpy as np inputs = {'nup': 7, 'ndown': 7, 'rs': 1.0, 'ecut': 2.0} system = UEG(inputs, True) nbsf = system.nbasis Pa = np.zeros([nbsf, nbsf], dtype=np.complex128) Pb = np.zeros([nbsf, nbsf], dtype=np.complex128) na = system.nup nb = system.ndown for i in range(na): Pa[i, i] = 1.0 for i in range(nb): Pb[i, i] = 1.0 P = np.array([Pa, Pb]) etot, ekin, epot = local_energy_ueg(system, G=P) print("ERHF = {}, {}, {}".format(etot, ekin, epot)) from pauxy.utils.linalg import exponentiate_matrix, reortho from pauxy.estimators.greens_function import gab # numpy.random.seed() rCa = numpy.random.randn(nbsf, na) zCa = numpy.random.randn(nbsf, na) rCb = numpy.random.randn(nbsf, nb) zCb = numpy.random.randn(nbsf, nb) Ca = rCa + 1j * zCa Cb = rCb + 1j * zCb Ca, detR = reortho(Ca) Cb, detR = reortho(Cb) # S = print(Ca.dot(Cb.T)) # print(S) # exit() Ca = numpy.array(Ca, dtype=numpy.complex128) Cb = numpy.array(Cb, dtype=numpy.complex128) P = [gab(Ca, Ca), gab(Cb, Cb)]
def unit_test(): from pauxy.systems.ueg import UEG from pauxy.trial_density_matrices.onebody import OneBody from pauxy.thermal_propagation.planewave import PlaneWave from pauxy.qmc.options import QMCOpts inputs = { 'nup': 1, 'ndown': 1, 'rs': 1.0, 'ecut': 0.5, "name": "one_body", "mu": 1.94046021, "beta": 0.5, "dt": 0.05, "optimised": True } beta = inputs['beta'] dt = inputs['dt'] system = UEG(inputs, verbose=False) qmc = QMCOpts(inputs, system, True) trial = OneBody(inputs, system, beta, dt, system.H1, verbose=False) walker = ThermalWalker(inputs, system, trial, True) # walker.greens_function(trial) E, T, V = walker.local_energy(system) numpy.random.seed(0) inputs['optimised'] = False propagator = PlaneWave(inputs, qmc, system, trial, verbose=False) propagator.propagate_walker_free(system, walker, trial, False) Gold = walker.G[0].copy() system = UEG(inputs, verbose=False) qmc = QMCOpts(inputs, system, verbose=False) trial = OneBody(inputs, system, beta, dt, system.H1, verbose=False) propagator = PlaneWave(inputs, qmc, system, trial, True) walker = ThermalWalker(inputs, system, trial, verbose=False) # walker.greens_function(trial) E, T, V = walker.local_energy(system) numpy.random.seed(0) inputs['optimised'] = True propagator = PlaneWave(inputs, qmc, system, trial, verbose=False) propagator.propagate_walker_free(system, walker, trial, False) Gnew = walker.G[0].copy() assert (scipy.linalg.norm(Gold[:, 0] - Gnew[:, 0]) < 1e-10) inputs['stack_size'] = 1 walker = ThermalWalker(inputs, system, trial, verbose=False) numpy.random.seed(0) propagator = PlaneWave(inputs, qmc, system, trial, verbose=False) for i in range(0, 5): propagator.propagate_walker(system, walker, trial) Gs1 = walker.G[0].copy() for ts in range(walker.stack_length): walker.greens_function(trial, slice_ix=ts * walker.stack_size) E, T, V = walker.local_energy(system) # print(E) inputs['stack_size'] = 5 walker = ThermalWalker(inputs, system, trial, verbose=False) numpy.random.seed(0) propagator = PlaneWave(inputs, qmc, system, trial, verbose=False) for i in range(0, 5): propagator.propagate_walker(system, walker, trial) Gs5 = walker.G[0].copy() for ts in range(walker.stack_length): walker.greens_function(trial, slice_ix=ts * walker.stack_size) E, T, V = walker.local_energy(system) # print(E) assert (numpy.linalg.norm(Gs1 - Gs5) < 1e-10) N = 5 A = numpy.random.rand(N, N) Q, R, P = scipy.linalg.qr(A, pivoting=True) #### test permutation start # Pmat = numpy.zeros((N,N)) # for i in range (N): # Pmat[P[i],i] = 1 # print(P) # tmp = Q.dot(R)#.dot(Pmat.T) # print(tmp) # print("==================") # tmp2 = tmp.dot(Pmat.T) # print(tmp2) # print("==================") # tmp[:,P] = tmp [:,range(N)] # print(tmp) #### test permutation end B = numpy.random.rand(N, N) (Q1, R1, P1) = scipy.linalg.qr(B, pivoting=True, check_finite=False) # Form permutation matrix P1mat = numpy.zeros(B.shape, B.dtype) P1mat[P1, range(len(P1))] = 1.0 # Form D matrices D1 = numpy.diag(R1.diagonal()) D1inv = numpy.diag(1.0 / R1.diagonal()) T1 = numpy.dot(numpy.dot(D1inv, R1), P1mat.T) assert (numpy.linalg.norm(B - numpy.einsum('ij,jj->ij', Q1, D1).dot(T1)) < 1e-10) # tmp[:,:] = tmp[:,P] # print(A - tmp) # print(Q * Q.T) # print(R) # Test walker green's function. from pauxy.systems.hubbard import Hubbard from pauxy.estimators.thermal import greens_function, one_rdm_from_G from pauxy.estimators.hubbard import local_energy_hubbard sys_dict = { 'name': 'Hubbard', 'nx': 4, 'ny': 4, 'nup': 7, 'ndown': 7, 'U': 4, 't': 1 } system = Hubbard(sys_dict) beta = 4 mu = 1 trial = OneBody({"mu": mu}, system, beta, dt, verbose=True) dt = 0.05 num_slices = int(beta / dt) eref = 0 for ek in system.eks: eref += 2 * ek * 1.0 / (numpy.exp(beta * (ek - mu)) + 1) walker = ThermalWalker({"stack_size": 1}, system, trial) Gs1 = walker.G[0].copy() rdm = one_rdm_from_G(walker.G) ekin = local_energy_hubbard(system, rdm)[1] try: assert (abs(eref - ekin) < 1e-8) except AssertionError: print("Error in kinetic energy check. Ref: %13.8e Calc:%13.8e" % (eref, ekin)) walker = ThermalWalker({"stack_size": 10}, system, trial) rdm = one_rdm_from_G(walker.G) ekin = local_energy_hubbard(system, rdm)[1] try: assert (abs(eref - ekin) < 1e-8) except AssertionError: print("Error in kinetic energy check. Ref: %13.10e Calc: %13.10e" " Error: %13.8e" % (eref.real, ekin.real, abs(eref - ekin))) for ts in range(walker.stack_length): walker.greens_function(trial, slice_ix=ts * walker.stack_size) assert (numpy.linalg.norm(Gs1 - walker.G[0]) < 1e-10)
def unit_test(): import cProfile from pauxy.propagation.continuous import Continuous from pauxy.systems.ueg import UEG from pauxy.systems.pw_fft import PW_FFT from pauxy.qmc.options import QMCOpts from pauxy.walkers.single_det import SingleDetWalker from pauxy.trial_wavefunction.hartree_fock import HartreeFock from pauxy.trial_wavefunction.free_electron import FreeElectron inputs = { 'nup': 1, 'ndown': 1, 'rs': 1.0, 'ecut': 20.0, 'dt': 0.05, 'nwalkers': 1, 'expansion_order': 6 } numpy.random.seed(7) system = PW_FFT(inputs, True) qmc = QMCOpts(inputs, system, True) trial = HartreeFock(system, False, inputs, True) rpsi = numpy.random.rand(system.nbasis, system.nup + system.ndown) zpsi = numpy.random.rand(system.nbasis, system.nup + system.ndown) trial.psi = rpsi + 1.j * zpsi propagator = PW(system, trial, qmc, inputs, verbose=True) walker = SingleDetWalker({}, system, trial, index=0) walker.greens_function(trial) rphi = numpy.random.rand(system.nbasis, system.nup + system.ndown) zphi = numpy.random.rand(system.nbasis, system.nup + system.ndown) walker.phi = rphi + 1.j * zphi eshift = 0.0 + 0.0j # print(walker.phi) pr = cProfile.Profile() pr.enable() propagator.propagate_walker_phaseless(walker=walker, system=system, trial=trial, eshift=eshift) propagator.propagate_walker_phaseless(walker=walker, system=system, trial=trial, eshift=eshift) propagator.propagate_walker_phaseless(walker=walker, system=system, trial=trial, eshift=eshift) propagator.propagate_walker_phaseless(walker=walker, system=system, trial=trial, eshift=eshift) propagator.propagate_walker_phaseless(walker=walker, system=system, trial=trial, eshift=eshift) pr.disable() pr.print_stats(sort='tottime') # print(walker.phi) # phi_ref = walker.phi.copy() sort_basis = numpy.argsort(numpy.diag(system.H1[0]), kind='mergesort') numpy.random.seed(7) system = UEG(inputs, True) qmc = QMCOpts(inputs, system, True) trial = HartreeFock(system, False, inputs, True) rpsi = numpy.random.rand(system.nbasis, system.nup + system.ndown) zpsi = numpy.random.rand(system.nbasis, system.nup + system.ndown) trial.psi = rpsi + 1.j * zpsi trial.psi[:, :] = trial.psi[sort_basis, :] propagator = Continuous(system, trial, qmc, inputs, verbose=True) walker = SingleDetWalker({}, system, trial, index=0) walker.greens_function(trial) rphi = numpy.random.rand(system.nbasis, system.nup + system.ndown) zphi = numpy.random.rand(system.nbasis, system.nup + system.ndown) walker.phi = rphi + 1.j * zphi walker.phi[:, :] = walker.phi[sort_basis, :] # print(walker.phi) eshift = 0.0 + 0.0j pr = cProfile.Profile() pr.enable() propagator.propagate_walker_phaseless(walker=walker, system=system, trial=trial, eshift=eshift) propagator.propagate_walker_phaseless(walker=walker, system=system, trial=trial, eshift=eshift) propagator.propagate_walker_phaseless(walker=walker, system=system, trial=trial, eshift=eshift) propagator.propagate_walker_phaseless(walker=walker, system=system, trial=trial, eshift=eshift) propagator.propagate_walker_phaseless(walker=walker, system=system, trial=trial, eshift=eshift) pr.disable() pr.print_stats(sort='tottime')
def unit_test(): import cProfile from pauxy.systems.ueg import UEG from pauxy.systems.pw_fft import PW_FFT from pauxy.estimators.ueg import local_energy_ueg from pauxy.estimators.pw_fft import local_energy_pw_fft from pauxy.qmc.options import QMCOpts from pauxy.trial_density_matrices.onebody import OneBody from pauxy.qmc.comm import FakeComm from pauxy.walkers.thermal import ThermalWalker beta = 16.0 dt = 0.005 # beta = 0.5 # dt = 0.05 lowrank = True # lowrank = False stack_size = 10 ecuts = [4.0, 8.0, 10.0, 12.0, 16.0, 21.0, 21.5, 32.0] for ecut in ecuts: inputs = { 'nup': 33, 'ndown': 33, 'thermal': True, 'beta': beta, 'rs': 1.0, 'ecut': ecut, 'dt': dt, 'nwalkers': 10, 'lowrank': lowrank, 'stack_size': stack_size } system = UEG(inputs, True) qmc = QMCOpts(inputs, system, True) comm = FakeComm() trial = OneBody(comm, system, beta, dt, options=inputs, verbose=True) propagator = PlaneWave(system, trial, qmc, inputs, True) walker = ThermalWalker( { 'stack_size': trial.stack_size, 'low_rank': lowrank }, system, trial, verbose=True) eshift = 0.0 + 0.0j numpy.random.seed(7) pr = cProfile.Profile() pr.enable() for ts in range(0, walker.num_slices): propagator.propagate_walker_phaseless(walker=walker, system=system, trial=trial, eshift=eshift) if (lowrank): system = PW_FFT(inputs, False) sort_basis = numpy.argsort(numpy.diag(system.H1[0]), kind='mergesort') inv_sort_basis = numpy.zeros_like(sort_basis) for i, idx in enumerate(sort_basis): inv_sort_basis[idx] = i mT = walker.stack.mT Ctrial = numpy.zeros((system.nbasis, walker.stack.mT * 2), dtype=numpy.complex128) Ctrial[:, :mT] = walker.stack.CT[0][:, :mT] Ctrial[:, mT:] = walker.stack.CT[1][:, :mT] P = one_rdm_from_G(walker.G) # Ptmp = Ctrial[:,:mT].conj().dot(walker.stack.theta[0,:mT,:]) # Reorder to FFT P[:, :, :] = P[:, inv_sort_basis, :] P[:, :, :] = P[:, :, inv_sort_basis] Theta = walker.stack.theta[:, :mT, :] Theta[:, :, :] = Theta[:, :, inv_sort_basis] Ctrial = Ctrial[inv_sort_basis, :] print("E = {}".format( local_energy_pw_fft(system, G=P, Ghalf=Theta, trial=Ctrial))) else: P = one_rdm_from_G(walker.G) print(numpy.diag(walker.G[0].real)) print("weight = {}".format(walker.weight)) print("E = {}".format(local_energy_ueg(system, P))) pr.disable() pr.print_stats(sort='tottime')