Example #1
0
def test_wfs():
    """
    Ensure that the wave function objects are consistent in several situations.
    """

    from pyscf import lib, gto, scf
    from pyqmc.slateruhf import PySCFSlaterUHF
    from pyqmc.jastrowspin import JastrowSpin
    from pyqmc.multiplywf import MultiplyWF
    mol = gto.M(atom='Li 0. 0. 0.; H 0. 0. 1.5', basis='cc-pvtz', unit='bohr')
    mf = scf.RHF(mol).run()
    mf_rohf = scf.ROHF(mol).run()
    mf_uhf = scf.UHF(mol).run()
    epsilon = 1e-5
    nconf = 10
    epos = np.random.randn(nconf, 4, 3)
    for wf in [
            JastrowSpin(mol),
            MultiplyWF(PySCFSlaterUHF(mol, mf), JastrowSpin(mol)),
            PySCFSlaterUHF(mol, mf_uhf),
            PySCFSlaterUHF(mol, mf),
            PySCFSlaterUHF(mol, mf_rohf)
    ]:
        for k in wf.parameters:
            wf.parameters[k] = np.random.rand(*wf.parameters[k].shape)
        assert testwf.test_wf_gradient(wf, epos, delta=1e-5)[0] < epsilon
        assert testwf.test_wf_laplacian(wf, epos, delta=1e-5)[0] < epsilon
        assert testwf.test_wf_pgradient(wf, epos, delta=1e-5)[0] < epsilon

        for k, item in testwf.test_updateinternals(wf, epos).items():
            assert item < epsilon
Example #2
0
def test():
    from pyscf import lib, gto, scf
    import pyqmc.testwf as testwf

    mol = gto.M(atom="Li 0. 0. 0.; H 0. 0. 1.5", basis="cc-pvtz", unit="bohr", spin=0)
    for mf in [scf.RHF(mol).run(), scf.ROHF(mol).run(), scf.UHF(mol).run()]:
        print("")
        nconf = 10
        nelec = np.sum(mol.nelec)
        slater = PySCFSlaterUHF(mol, mf)
        configs = np.random.randn(nconf, nelec, 3)
        print("testing internals:", testwf.test_updateinternals(slater, configs))
        for delta in [1e-3, 1e-4, 1e-5, 1e-6, 1e-7]:
            print(
                "delta",
                delta,
                "Testing gradient",
                testwf.test_wf_gradient(slater, configs, delta=delta),
            )
            print(
                "delta",
                delta,
                "Testing laplacian",
                testwf.test_wf_laplacian(slater, configs, delta=delta),
            )
            print(
                "delta",
                delta,
                "Testing pgradient",
                testwf.test_wf_pgradient(slater, configs, delta=delta),
            )
Example #3
0
def test_pbc_wfs():
    """
    Ensure that the wave function objects are consistent in several situations.
    """

    from pyscf.pbc import lib, gto, scf
    from pyqmc.supercell import get_supercell
    from pyqmc.slater import Slater
    from pyqmc.multiplywf import MultiplyWF
    from pyqmc import default_jastrow
    import pyqmc

    mol = gto.M(
        atom="H 0. 0. 0.; H 1. 1. 1.",
        basis="sto-3g",
        unit="bohr",
        a=(np.ones((3, 3)) - np.eye(3)) * 4,
    )
    mf = scf.KRKS(mol, mol.make_kpts((2, 2, 2))).run()
    # mf_rohf = scf.KROKS(mol).run()
    # mf_uhf = scf.KUKS(mol).run()
    epsilon = 1e-5
    nconf = 10
    supercell = get_supercell(mol, S=(np.ones((3, 3)) - 2 * np.eye(3)))
    epos = pyqmc.initial_guess(supercell, nconf)
    # For multislaterpbc
    # kinds = 0, 3, 5, 6  # G, X, Y, Z
    # d1 = {kind: [0] for kind in kinds}
    # d2 = d1.copy()
    # d2.update({0: [], 3: [0, 1]})
    # detwt = [2 ** 0.5, 2 ** 0.5]
    # occup = [[d1, d2], [d1]]
    # map_dets = [[0, 1], [0, 0]]
    for wf in [
        MultiplyWF(Slater(supercell, mf), default_jastrow(supercell)[0]),
        Slater(supercell, mf),
    ]:
        for k in wf.parameters:
            if "mo_coeff" not in k and k != "det_coeff":
                wf.parameters[k] = np.random.rand(*wf.parameters[k].shape)

        _, epos = pyqmc.vmc(wf, epos, nblocks=1, nsteps=2, tstep=1)  # move off node

        for fname, func in zip(
            ["gradient", "laplacian", "pgradient"],
            [
                testwf.test_wf_gradient,
                testwf.test_wf_laplacian,
                testwf.test_wf_pgradient,
            ],
        ):
            err = []
            for delta in [1e-4, 1e-5, 1e-6, 1e-7, 1e-8]:
                err.append(func(wf, epos, delta))
            print(type(wf), fname, min(err))
            assert min(err) < epsilon

        for k, item in testwf.test_updateinternals(wf, epos).items():
            print(k, item)
            assert item < epsilon
Example #4
0
def test(): 
    from pyscf import lib, gto, scf
    np.random.seed(10)
    
    mol = gto.M(atom='Li 0. 0. 0.; H 0. 0. 1.5', basis='cc-pvtz',unit='bohr')
    l = dir(mol)
    nconf=20
    configs=np.random.randn(nconf,np.sum(mol.nelec),3)
    
    abasis=[GaussianFunction(0.2),GaussianFunction(0.4)]
    bbasis=[GaussianFunction(0.2),GaussianFunction(0.4)]
    jastrow=JastrowSpin(mol,a_basis=abasis,b_basis=bbasis)
    jastrow.parameters['bcoeff']=np.random.random(jastrow.parameters['bcoeff'].shape)
    jastrow.parameters['acoeff']=np.random.random(jastrow.parameters['acoeff'].shape)
    import pyqmc.testwf as testwf
    for key, val in testwf.test_updateinternals(jastrow, configs).items():
        print(key, val)

    print()
    for delta in [1e-3,1e-4,1e-5,1e-6,1e-7]:
        print('delta', delta, "Testing gradient",
              testwf.test_wf_gradient(jastrow,configs,delta=delta))
        print('delta', delta, "Testing laplacian",
              testwf.test_wf_laplacian(jastrow,configs,delta=delta))
        print('delta', delta, "Testing pgradient",
              testwf.test_wf_pgradient(jastrow,configs,delta=delta))
        print()
Example #5
0
def run_tests(wf, epos, epsilon):

    _, epos = pyq.vmc(wf, epos, nblocks=1, nsteps=2, tstep=1)  # move off node

    for k, item in testwf.test_updateinternals(wf, epos).items():
        print(k, item)
        assert item < epsilon

    testwf.test_mask(wf, 0, epos)

    for fname, func in zip(
        ["gradient", "laplacian", "pgradient"],
        [
            testwf.test_wf_gradient,
            testwf.test_wf_laplacian,
            testwf.test_wf_pgradient,
        ],
    ):
        err = [
            func(wf, epos, delta) for delta in [1e-4, 1e-5, 1e-6, 1e-7, 1e-8]
        ]
        assert min(err) < epsilon, "epsilon {0}".format(epsilon)

    for fname, func in zip(
        ["gradient_value", "gradient_laplacian"],
        [
            testwf.test_wf_gradient_value,
            testwf.test_wf_gradient_laplacian,
        ],
    ):
        d = func(wf, epos)
        for k, v in d.items():
            assert v < 1e-10, (k, v)
Example #6
0
def test_wfs():
    """
    Ensure that the wave function objects are consistent in several situations.
    """

    from pyscf import lib, gto, scf
    from pyqmc.slater import PySCFSlater
    from pyqmc.jastrowspin import JastrowSpin
    from pyqmc.multiplywf import MultiplyWF
    from pyqmc.manybody_jastrow import J3
    import pyqmc

    mol = gto.M(atom="Li 0. 0. 0.; H 0. 0. 1.5", basis="sto-3g", unit="bohr")
    mf = scf.RHF(mol).run()
    mf_rohf = scf.ROHF(mol).run()
    mf_uhf = scf.UHF(mol).run()
    epsilon = 1e-5
    nconf = 10
    epos = pyqmc.initial_guess(mol, nconf)
    for wf in [
            JastrowSpin(mol),
            J3(mol),
            MultiplyWF(PySCFSlater(mol, mf), JastrowSpin(mol)),
            MultiplyWF(PySCFSlater(mol, mf), JastrowSpin(mol), J3(mol)),
            PySCFSlater(mol, mf_uhf),
            PySCFSlater(mol, mf),
            PySCFSlater(mol, mf_rohf),
    ]:
        for k in wf.parameters:
            if k != "mo_coeff":
                wf.parameters[k] = np.random.rand(*wf.parameters[k].shape)
        for k, item in testwf.test_updateinternals(wf, epos).items():
            print(k, item)
            assert item < epsilon

        testwf.test_mask(wf, 0, epos)

        _, epos = pyqmc.vmc(wf, epos, nblocks=1, nsteps=2,
                            tstep=1)  # move off node

        for fname, func in zip(
            ["gradient", "laplacian", "pgradient"],
            [
                testwf.test_wf_gradient,
                testwf.test_wf_laplacian,
                testwf.test_wf_pgradient,
            ],
        ):
            err = []
            for delta in [1e-4, 1e-5, 1e-6, 1e-7, 1e-8]:
                err.append(func(wf, epos, delta)[0])
            print(type(wf), fname, min(err))
            assert min(err) < epsilon, "epsilon {0}".format(epsilon)
Example #7
0
def test_pbc_wfs():
    """
    Ensure that the wave function objects are consistent in several situations.
    """

    from pyscf.pbc import lib, gto, scf
    from pyqmc.slaterpbc import PySCFSlaterPBC, get_supercell
    from pyqmc.jastrowspin import JastrowSpin
    from pyqmc.multiplywf import MultiplyWF
    from pyqmc.coord import OpenConfigs
    import pyqmc

    mol = gto.M(atom="H 0. 0. 0.; H 1. 1. 1.",
                basis="sto-3g",
                unit="bohr",
                a=np.eye(3) * 4)
    mf = scf.KRKS(mol).run()
    # mf_rohf = scf.KROKS(mol).run()
    # mf_uhf = scf.KUKS(mol).run()
    epsilon = 1e-5
    nconf = 10
    supercell = get_supercell(mol, S=np.eye(3))
    epos = pyqmc.initial_guess(supercell, nconf)
    for wf in [
            MultiplyWF(PySCFSlaterPBC(supercell, mf), JastrowSpin(mol)),
            PySCFSlaterPBC(supercell, mf),
            # PySCFSlaterPBC(supercell, mf_uhf),
            # PySCFSlaterPBC(supercell, mf_rohf),
    ]:
        for k in wf.parameters:
            if k != "mo_coeff":
                wf.parameters[k] = np.random.rand(*wf.parameters[k].shape)
        for fname, func in zip(
            ["gradient", "laplacian", "pgradient"],
            [
                testwf.test_wf_gradient,
                testwf.test_wf_laplacian,
                testwf.test_wf_pgradient,
            ],
        ):
            err = []
            for delta in [1e-4, 1e-5, 1e-6, 1e-7, 1e-8]:
                err.append(func(wf, epos, delta)[0])
            print(fname, min(err))
            assert min(err) < epsilon

        for k, item in testwf.test_updateinternals(wf, epos).items():
            print(k, item)
            assert item < epsilon
Example #8
0
def test_wfs():
    """
    Ensure that the wave function objects are consistent in several situations.
    """

    from pyscf import lib, gto, scf
    from pyqmc.slateruhf import PySCFSlaterUHF
    from pyqmc.jastrowspin import JastrowSpin
    from pyqmc.multiplywf import MultiplyWF
    from pyqmc.coord import OpenConfigs
    import pyqmc

    mol = gto.M(atom="Li 0. 0. 0.; H 0. 0. 1.5", basis="cc-pvtz", unit="bohr")
    mf = scf.RHF(mol).run()
    mf_rohf = scf.ROHF(mol).run()
    mf_uhf = scf.UHF(mol).run()
    epsilon = 1e-5
    nconf = 10
    epos = pyqmc.initial_guess(mol, nconf)
    for wf in [
            JastrowSpin(mol),
            MultiplyWF(PySCFSlaterUHF(mol, mf), JastrowSpin(mol)),
            PySCFSlaterUHF(mol, mf_uhf),
            PySCFSlaterUHF(mol, mf),
            PySCFSlaterUHF(mol, mf_rohf),
    ]:
        for k in wf.parameters:
            if k != "mo_coeff":
                wf.parameters[k] = np.random.rand(*wf.parameters[k].shape)
        for fname, func in zip(
            ["gradient", "laplacian", "pgradient"],
            [
                testwf.test_wf_gradient,
                testwf.test_wf_laplacian,
                testwf.test_wf_pgradient,
            ],
        ):
            err = []
            for delta in [1e-4, 1e-5, 1e-6, 1e-7, 1e-8]:
                err.append(func(wf, epos, delta)[0])
            print(fname, min(err))
            assert min(err) < epsilon

        for k, item in testwf.test_updateinternals(wf, epos).items():
            print(k, item)
            assert item < epsilon
Example #9
0
def test():
    """ 
    Tests that the multi-slater wave function value, gradient and 
    parameter gradient evaluations are working correctly. Also 
    checks that VMC energy matches energy calculated in PySCF
    """
    mol = gto.M(atom="Li 0. 0. 0.; H 0. 0. 1.5",
                basis="cc-pvtz",
                unit="bohr",
                spin=0)
    epsilon = 1e-4
    delta = 1e-5
    nsteps = 200
    warmup = 10
    for mf in [scf.RHF(mol).run(), scf.ROHF(mol).run(), scf.UHF(mol).run()]:
        # Test same number of elecs
        mc = mcscf.CASCI(mf, ncas=4, nelecas=(1, 1))
        mc.kernel()
        wf = MultiSlater(mol, mf, mc)

        nconf = 10

        nelec = np.sum(mol.nelec)
        epos = initial_guess(mol, nconf)

        for k, item in testwf.test_updateinternals(wf, epos).items():
            assert item < epsilon
        assert testwf.test_wf_gradient(wf, epos, delta=delta)[0] < epsilon
        assert testwf.test_wf_laplacian(wf, epos, delta=delta)[0] < epsilon
        assert testwf.test_wf_pgradient(wf, epos, delta=delta)[0] < epsilon

        # Test same number of elecs
        mc = mcscf.CASCI(mf, ncas=4, nelecas=(1, 1))
        mc.kernel()
        wf = pyqmc.default_msj(mol, mf, mc)[0]

        nelec = np.sum(mol.nelec)
        epos = initial_guess(mol, nconf)

        for k, item in testwf.test_updateinternals(wf, epos).items():
            assert item < epsilon
        assert testwf.test_wf_gradient(wf, epos, delta=delta)[0] < epsilon
        assert testwf.test_wf_laplacian(wf, epos, delta=delta)[0] < epsilon
        assert testwf.test_wf_pgradient(wf, epos, delta=delta)[0] < epsilon

        # Test different number of elecs
        mc = mcscf.CASCI(mf, ncas=4, nelecas=(2, 0))
        mc.kernel()
        wf = MultiSlater(mol, mf, mc)

        nelec = np.sum(mol.nelec)
        epos = initial_guess(mol, nconf)

        for k, item in testwf.test_updateinternals(wf, epos).items():
            assert item < epsilon
        assert testwf.test_wf_gradient(wf, epos, delta=delta)[0] < epsilon
        assert testwf.test_wf_laplacian(wf, epos, delta=delta)[0] < epsilon
        assert testwf.test_wf_pgradient(wf, epos, delta=delta)[0] < epsilon

        # Quick VMC test
        nconf = 1000
        coords = initial_guess(mol, nconf)
        df, coords = vmc(wf,
                         coords,
                         nsteps=nsteps,
                         accumulators={"energy": EnergyAccumulator(mol)})

        df = pd.DataFrame(df)
        df = reblock(df["energytotal"][warmup:], 20)
        en = df.mean()
        err = df.sem()
        assert en - mc.e_tot < 5 * err
Example #10
0
    print("p1/p2", p1 / p2)
    print("log magnitude")
    print("m1", m1)
    print("m2", m2)
    print("m1-m2", m1 - m2)

    p_err = np.linalg.norm(p1 / p2 - p1[0] / p2[0])
    m_err = np.linalg.norm(m1 - m2 - m1[0] + m2[0])
    assert p_err < 1e-10, (p_err, m_err)
    assert m_err < 1e-1, (p_err, m_err)


if __name__ == "__main__":
    from pyqmc.testwf import (
        test_updateinternals,
        test_wf_gradient,
        test_wf_laplacian,
        test_wf_gradient_laplacian,
    )

    wf1, wf2, configs = generate_test_inputs()
    test_recompute(wf1, wf2, configs)
    test_updateinternals(wf1, configs)
    test_updateinternals(wf2, configs)
    test_wf_gradient(wf1, configs)
    test_wf_gradient(wf2, configs)
    test_wf_laplacian(wf1, configs)
    test_wf_laplacian(wf2, configs)
    test_wf_gradient_laplacian(wf1, configs)
    test_wf_gradient_laplacian(wf2, configs)