Exemplo n.º 1
0
def test_walker_energy():
    numpy.random.seed(7)
    nelec = (2, 2)
    nmo = 5
    h1e, chol, enuc, eri = generate_hamiltonian(nmo, nelec, cplx=False)
    system = Generic(nelec=nelec,
                     h1e=h1e,
                     chol=chol,
                     ecore=enuc,
                     inputs={'integral_tensor': False})
    (e0, ev), (d, oa, ob) = simple_fci(system, gen_dets=True)
    na = system.nup
    init = get_random_wavefunction(nelec, nmo)
    init[:, :na], R = reortho(init[:, :na])
    init[:, na:], R = reortho(init[:, na:])
    trial = MultiSlater(system, (ev[:, 0], oa, ob), init=init)
    trial.calculate_energy(system)
    walker = MultiDetWalker({}, system, trial)
    nume = 0
    deno = 0
    for i in range(trial.ndets):
        psia = trial.psi[i, :, :na]
        psib = trial.psi[i, :, na:]
        oa = numpy.dot(psia.conj().T, init[:, :na])
        ob = numpy.dot(psib.conj().T, init[:, na:])
        isa = numpy.linalg.inv(oa)
        isb = numpy.linalg.inv(ob)
        ovlp = numpy.linalg.det(oa) * numpy.linalg.det(ob)
        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
        e = local_energy(system, numpy.array([ga, gb]), opt=False)[0]
        nume += trial.coeffs[i].conj() * ovlp * e
        deno += trial.coeffs[i].conj() * ovlp
    print(nume / deno, nume, deno, e0[0])
Exemplo n.º 2
0
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)
Exemplo n.º 3
0
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)
Exemplo n.º 4
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)
Exemplo n.º 5
0
def test_local_energy_cholesky_opt():
    numpy.random.seed(7)
    nmo = 24
    nelec = (4,2)
    h1e, chol, enuc, eri = generate_hamiltonian(nmo, nelec, cplx=False)
    sys = Generic(nelec=nelec, h1e=h1e, chol=chol, ecore=enuc)
    wfn = get_random_nomsd(sys, ndet=1, cplx=False)
    trial = MultiSlater(sys, wfn)
    sys.construct_integral_tensors_real(trial)
    e = local_energy_generic_cholesky_opt(sys, trial.G, Ghalf=trial.GH)
    assert e[0] == pytest.approx(20.6826247016273)
    assert e[1] == pytest.approx(23.0173528796140)
    assert e[2] == pytest.approx(-2.3347281779866)
Exemplo n.º 6
0
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])
Exemplo n.º 7
0
def get_trial_wavefunction(system,
                           options={},
                           mf=None,
                           parallel=False,
                           verbose=0):
    """Wrapper to select trial wavefunction class.

    Parameters
    ----------
    options : dict
        Trial wavefunction input options.
    system : class
        System class.
    cplx : bool
        If true then trial wavefunction will be complex.
    parallel : bool
        If true then running in parallel.

    Returns
    -------
    trial : class or None
        Trial wavfunction class.
    """
    wfn_file = get_input_value(options,
                               'filename',
                               default=None,
                               alias=['wavefunction_file'],
                               verbose=verbose)
    if wfn_file is not None:
        if verbose:
            print("# Reading wavefunction from {}.".format(wfn_file))
        read, psi0 = read_qmcpack_wfn_hdf(wfn_file)
        thresh = options.get('threshold', None)
        if thresh is not None:
            coeff = read[0]
            ndets = len(coeff[abs(coeff) > thresh])
            if verbose:
                print("# Discarding determinants with weight "
                      "  below {}.".format(thresh))
        else:
            ndets = options.get('ndets', None)
        if verbose:
            print("# Numeber of determinants in trial wavefunction: {}".format(
                ndets))
        if ndets is not None:
            wfn = []
            for x in read:
                wfn.append(x[:ndets])
        else:
            wfn = read
        trial = MultiSlater(system,
                            wfn,
                            options=options,
                            parallel=parallel,
                            verbose=verbose,
                            init=psi0)
    elif options['name'] == 'MultiSlater':
        if verbose:
            print("# Guessing RHF trial wavefunction.")
        na = system.nup
        nb = system.ndown
        wfn = numpy.zeros((1, system.nbasis, system.nup + system.ndown),
                          dtype=numpy.complex128)
        coeffs = numpy.array([1.0 + 0j])
        I = numpy.identity(system.nbasis, dtype=numpy.complex128)
        wfn[0, :, :na] = I[:, :na]
        wfn[0, :, na:] = I[:, :nb]
        trial = MultiSlater(system, (coeffs, wfn),
                            options=options,
                            parallel=parallel,
                            verbose=verbose)
    elif options['name'] == 'hartree_fock':
        trial = HartreeFock(system,
                            True,
                            options,
                            parallel=parallel,
                            verbose=verbose)
    elif options['name'] == 'free_electron':
        trial = FreeElectron(system, True, options, parallel, verbose)
    elif options['name'] == 'UHF':
        trial = UHF(system, True, options, parallel, verbose)
    else:
        print("Unknown trial wavefunction type.")
        sys.exit()

    return trial