Ejemplo n.º 1
0
def test_pyscf_methods(method, geometry, basis_set):
    mol = tq.Molecule(geometry=geometry, basis_set=basis_set)
    e1 = mol.compute_energy(method=method)
    c, h1, h2 = mol.get_integrals()
    mol = tq.Molecule(geometry=geometry,
                      basis_set=basis_set,
                      nuclear_repulsion=c,
                      one_body_integrals=h1,
                      two_body_integrals=h2,
                      backend="pyscf")
    e2 = mol.compute_energy(method)

    assert numpy.isclose(e1,e2, atol=1.e-4)
Ejemplo n.º 2
0
def test_hcb(trafo):
    geomstring = "Be 0.0 0.0 0.0\n H 0.0 0.0 1.6\n H 0.0 0.0 -1.6"
    mol1 = tq.Molecule(geometry=geomstring, active_orbitals=[1,2,3,4,5,6], basis_set="sto-3g", transformation="ReorderedJordanWigner")
    H = mol1.make_hardcore_boson_hamiltonian()
    U = mol1.make_upccgsd_ansatz(name="HCB-UpCCGD")

    E = tq.ExpectationValue(H=H, U=U)
    energy1 = tq.minimize(E).energy
    assert numpy.isclose(energy1, -15.527740838656282, atol=1.e-3)

    mol2 = tq.Molecule(geometry=geomstring, active_orbitals=[1,2,3,4,5,6], basis_set="sto-3g", transformation=trafo)
    H = mol2.make_hamiltonian()
    U = mol2.make_upccgsd_ansatz(name="UpCCGD", use_hcb=False)
    E = tq.ExpectationValue(H=H, U=U)
    energy2 = tq.minimize(E).energy

    assert numpy.isclose(energy1, energy2)
def test_madness_pyscf_bridge():
    mol = tq.Molecule(name="balanced_be", geometry="Be 0.0 0.0 0.0", n_pno=2, pno={"diagonal": True, "maxrank": 1}, )
    H = mol.make_hamiltonian()
    e1 = numpy.linalg.eigvalsh(H.to_matrix())[0]
    e2 = mol.compute_energy("fci")
    e3 = mol.compute_energy("ccsd")
    e4 = mol.compute_energy("ccsd(t)")
    assert numpy.isclose(e1, e2)
    # CCSD(T) and CCSD are not exact but close in this case
    # primarily testing a functioning interface and keywords here
    assert numpy.isclose(e1, e4, atol=1.e-3)
    assert numpy.isclose(e3, e4, atol=1.e-3)
def test_madness_full_be():
    # relies on madness being compiled and MAD_ROOT_DIR exported
    # or pno_integrals in the path
    geomstring = "Be 0.0 0.0 0.0"
    molecule = tq.Molecule(name="be", geometry=geomstring, n_pno=3, frozen_core=True)
    H = molecule.make_hamiltonian()
    UHF = molecule.prepare_reference()
    EHF = tq.simulate(tq.ExpectationValue(H=H, U=UHF))
    assert (numpy.isclose(-14.57269300, EHF, atol=1.e-5))
    U = molecule.make_upccgsd_ansatz()
    E = tq.ExpectationValue(H=H, U=U)
    result = tq.minimize(method="bfgs", objective=E, initial_values=0.0, silent=True)
    assert (numpy.isclose(-14.614662051580321, result.energy, atol=1.e-3))
def test_madness_full_li_plus():
    # relies on madness being compiled and MAD_ROOT_DIR exported
    # or pno_integrals in the path
    geomstring = "Li 0.0 0.0 0.0"
    molecule = tq.Molecule(name="li+", geometry=geomstring, n_pno=1, charge=1, frozen_core=False) # need to deactivate frozen_core, otherwise there is no active orbital
    H = molecule.make_hamiltonian()
    UHF = molecule.prepare_reference()
    EHF = tq.simulate(tq.ExpectationValue(H=H, U=UHF))
    assert (numpy.isclose(-7.236247e+00, EHF, atol=1.e-5))
    U = molecule.make_upccgsd_ansatz()
    E = tq.ExpectationValue(H=H, U=U)
    result = tq.minimize(method="bfgs", objective=E, initial_values=0.0, silent=True)
    assert (numpy.isclose(-7.251177798, result.energy, atol=1.e-5))
def test_madness_full_he():
    # relies on madness being compiled and MAD_ROOT_DIR exported
    # or pno_integrals in the path
    geomstring = "He 0.0 0.0 0.0"
    molecule = tq.Molecule(geometry=geomstring, n_pno=1)
    H = molecule.make_hamiltonian()
    UHF = molecule.prepare_reference()
    EHF = tq.simulate(tq.ExpectationValue(H=H, U=UHF))
    assert (numpy.isclose(-2.861522e+00, EHF, atol=1.e-5))
    U = molecule.make_upccgsd_ansatz()
    E = tq.ExpectationValue(H=H, U=U)
    result = tq.minimize(method="bfgs", objective=E, initial_values=0.0, silent=True)
    assert (numpy.isclose(-2.87761809, result.energy, atol=1.e-5))
def test_madness_he_data():
    # relies that he_xtensor.npy are present (x=g,h)
    geomstring = "He 0.0 0.0 0.0"
    molecule = tq.Molecule(name="he", geometry=geomstring)
    H = molecule.make_hamiltonian()
    UHF = molecule.prepare_reference()
    EHF = tq.simulate(tq.ExpectationValue(H=H, U=UHF))
    assert (numpy.isclose(-2.861522e+00, EHF, atol=1.e-5))
    U = molecule.make_upccgsd_ansatz()
    E = tq.ExpectationValue(H=H, U=U)
    result = tq.minimize(method="bfgs", objective=E, initial_values=0.0, silent=True)
    print(result.energy)
    assert (numpy.isclose(-2.87761809, result.energy, atol=1.e-5))
Ejemplo n.º 8
0
def make_test_molecule():
    one = numpy.array([[-1.94102524, -0.31651552],
                       [-0.31651552, -0.0887454]])
    two = numpy.array([[[[1.02689005, 0.31648659],
                         [0.31648659, 0.22767214]],

                        [[0.31648659, 0.22767214],
                         [0.85813498, 0.25556095]]],

                       [[[0.31648659, 0.85813498],
                         [0.22767214, 0.25556095]],

                        [[0.22767214, 0.25556095],
                         [0.25556095, 0.76637672]]]])

    return tq.Molecule(geometry="He 0.0 0.0 0.0", backend="base", one_body_integrals=one, two_body_integrals=two)
Ejemplo n.º 9
0
def run_madness(geometry, n_pno, mra_threshold=1.e-4, localize="boys", orthogonalization_method="cholesky", **kwargs):

    dft={"econv":mra_threshold, "localize":localize}
    if "dft" in kwargs:
        dft = {**dft, **kwargs["dft"]}

    pnoint={"orthog":orthogonalization_method}
    if "pnoint" in kwargs:
        pnoint = {**pnoint, **kwargs["pnoint"]}

    kwargs["pnoint"]=pnoint
    kwargs["dft"]=dft


    #exe = tq.quantumchemistry.QuantumChemistryMadness.find_executable("/app/madroot/")
    #mol = tq.Molecule(geometry=geometry, n_pno=n_pno, executable=exe, **kwargs)
    mol = tq.Molecule(geometry=geometry, n_pno=n_pno, **kwargs)
    return mol
Ejemplo n.º 10
0
def run_madness(geometry,
                n_pno,
                mra_threshold=1.e-4,
                localize="boys",
                orthogonalization_method="symmetric",
                diagonal=True,
                maxrank=None,
                **kwargs):
    """
    geometry: a string that either points to an xyz file (e.g. geometry="my_file.xyz") or carries the molecular structure
              in xyz syntax (without the preamble, e.g. geometry="H 0.0 0.0 0.0\nH 0.0 0.0 0.7")
    n_pno: number of pnos to compute (number of qubits is then 2*n_pno + n_electrons)
    mra_threshold: MRA accuracy threshold (1.e-4 is usually fine)
    localize: localization method ("boys", "pm", "canon")
    orthogonalization_method: Global orthogonalization of the PNOs ("cholesky", "symmetric")
    diagonal: Apply a diagonal approximation to the MP2 surrogate (only compute diagonal pairs)
    maxrank: maximum number of PNOs computed for each MP2 pair (not the number picked in the end; in some cases it's good to be able to control that)
    """
    dft = {"econv": mra_threshold, "localize": localize}
    if "dft" in kwargs:
        dft = {**dft, **kwargs["dft"]}

    pnoint = {"orthog": orthogonalization_method}
    if "pnoint" in kwargs:
        pnoint = {**pnoint, **kwargs["pnoint"]}

    pno = {"thresh": mra_threshold, "diagonal": diagonal}
    if "pno" in kwargs:
        pno = {**pno, **kwargs["pno"]}

    if maxrank is not None:
        pno["maxrank"] = maxrank

    kwargs["pnoint"] = pnoint
    kwargs["dft"] = dft
    kwargs["pno"] = pno

    #exe = tq.quantumchemistry.QuantumChemistryMadness.find_executable("/app/madroot/")
    #mol = tq.Molecule(geometry=geometry, n_pno=n_pno, executable=exe, **kwargs)
    mol = tq.Molecule(geometry=geometry, n_pno=n_pno, **kwargs)
    return mol
Ejemplo n.º 11
0
def mol_from_json(json_data:str, name=None, **kwargs):

    if hasattr(json_data, "lower") and ".json" in json_data.lower():
        with open(json_data, "r") as f:
            json_dict = json.load(f)
            print(type(json_dict))
            print(json_dict)
    elif hasattr(json_data, "lower()"):
        json_dict = json.loads(json_data)
    else:
        json_dict = json_data

    if "mol" in json_dict:
        json_dict=json.loads(json_dict["mol"])

    parameters = json_dict["parameters"]
    if name is None:
        name=parameters["name"]
    else:
        parameters["name"]=name
    del parameters["multiplicity"]


    obi_data=json_dict["one_body_integrals"]
    one_body_integrals=numpy.asarray(obi_data["data"], dtype=float).reshape(obi_data["shape"])
    tbi_data=json_dict["two_body_integrals"]
    two_body_integrals=numpy.asarray(tbi_data["data"], dtype=float).reshape(tbi_data["shape"])
    numpy.save("{}_htensor.npy".format(name), arr=one_body_integrals)
    numpy.save("{}_gtensor.npy".format(name), arr=two_body_integrals)
    orbital_data = json_dict["orbital_data"]
    pairinfo = orbital_data["pairinfo"]
    occinfo = orbital_data["occinfo"]
    with open("{}_pnoinfo.txt".format(name), "w") as f:
        print("MADNESS MRA-PNO INFORMATION", file=f)
        print("pairinfo={}".format(pairinfo), file=f)
        print("nuclear_repulsion={}".format(json_dict["nuclear_repulsion"]), file=f)
        print("occinfo={}".format(occinfo), file=f)
    mol = tq.Molecule(n_pno=None, **parameters, **kwargs)
    return mol
def test_madness_upccgsd(trafo):
    n_pno = 2
    if os.path.isfile('balanced_be_gtensor.npy'):
        n_pno = None
    mol = tq.Molecule(name="balanced_be", frozen_core=False, geometry="Be 0.0 0.0 0.0", n_pno=n_pno, pno={"diagonal": True, "maxrank": 1},
                      transformation=trafo)

    H = mol.make_hardcore_boson_hamiltonian()
    oigawert=numpy.linalg.eigvalsh(H.to_matrix())[0]
    U = mol.make_upccgsd_ansatz(name="HCB-UpCCGD", direct_compiling=True)
    E = tq.ExpectationValue(H=H, U=U)
    assert (len(E.extract_variables()) == 6)
    result = tq.minimize(E)
    assert numpy.isclose(result.energy, oigawert, atol=1.e-3)

    U = mol.make_upccgsd_ansatz(name="HCB-PNO-UpCCD")
    E = tq.ExpectationValue(H=H, U=U)
    assert (len(E.extract_variables()) == 2)
    result = tq.minimize(E)
    assert numpy.isclose(result.energy, oigawert, atol=1.e-3)

    H = mol.make_hamiltonian()
    oigawert2=numpy.linalg.eigvalsh(H.to_matrix())[0]
    U = mol.make_upccgsd_ansatz(name="SPA-D")
    E = tq.ExpectationValue(H=H, U=U)
    assert (len(E.extract_variables()) == 2)
    variables = result.variables
    if "bravyi" in trafo.lower():
        # signs of angles change in BK compared to JW-like HCB
        variables = {k: -v for k, v in variables.items()}
    energy = tq.simulate(E, variables)
    result = tq.minimize(E)
    assert numpy.isclose(result.energy, oigawert, atol=1.e-3)
    assert numpy.isclose(energy, oigawert, atol=1.e-3)
    e3 = mol.compute_energy(method="SPA")
    assert numpy.isclose(result.energy, e3)
    e3 = mol.compute_energy(method="HCB-SPA")
    assert numpy.isclose(result.energy, e3)
    e3 = mol.compute_energy(method="SPA-UpCCD")
    assert numpy.isclose(result.energy, e3)

    U = mol.make_upccgsd_ansatz(name="SPA-UpCCSD")
    E = tq.ExpectationValue(H=H, U=U)
    assert (len(E.extract_variables()) == 4)
    result = tq.minimize(E)
    assert numpy.isclose(result.energy, -14.60266198, atol=1.e-3)

    U = mol.make_upccgsd_ansatz(name="SPA-UpCCGSD") # in this case no difference to SPA-UpCCSD
    E = tq.ExpectationValue(H=H, U=U)
    assert (len(E.extract_variables()) == 4)
    result = tq.minimize(E)
    assert numpy.isclose(result.energy, -14.60266198, atol=1.e-3)

    U = mol.make_upccgsd_ansatz(name="UpCCSD")
    E = tq.ExpectationValue(H=H, U=U)
    print(U)
    assert (len(E.extract_variables()) == 8)
    result = tq.minimize(E)
    assert numpy.isclose(result.energy, oigawert, atol=1.e-3)

    U = mol.make_upccgsd_ansatz(name="UpCCGSD")
    E = tq.ExpectationValue(H=H, U=U)
    assert (len(E.extract_variables()) == 12)
    result = tq.minimize(E)
    assert numpy.isclose(result.energy, oigawert, atol=1.e-3)

    U = mol.make_upccgsd_ansatz(name="UpCCGSD", direct_compiling=False)
    E = tq.ExpectationValue(H=H, U=U)
    assert (len(E.extract_variables()) == 12)
    result = tq.minimize(E)
    assert numpy.isclose(result.energy, oigawert, atol=1.e-3)