コード例 #1
0
ファイル: test_chemistry.py プロジェクト: shukob/tequila
def test_rdms_psi4():
    rdm1_ref = numpy.array([[1.97710662, 0.0], [0.0, 0.02289338]])
    rdm2_ref = numpy.array([[[[1.97710662, 0.0], [0.0, -0.21275021]], [[0.0, 0.0], [0.0, 0.0]]],
                            [[[0.0, 0.0], [0.0, 0.0]], [[-0.21275021, 0.0], [0.0, 0.02289338]]]])
    mol = qc.Molecule(geometry="data/h2.xyz", basis_set="sto-3g", backend="psi4", transformation="jw")
    # Check matrices by psi4
    mol.compute_rdms(U=None, psi4_method="detci", psi4_options={"detci__ex_level": 2,
                                                                "detci__opdm": True, "detci__tpdm": True})
    rdm1, rdm2 = mol.rdm1, mol.rdm2
    assert (numpy.allclose(rdm1, rdm1_ref, atol=1e-8))
    assert (numpy.allclose(rdm2, rdm2_ref, atol=1e-8))
コード例 #2
0
def test_rdms(trafo):
    rdm1_ref = numpy.array([[1.99137832, -0.00532359],
                            [-0.00532359, 0.00862168]])
    rdm2_ref = numpy.array([[[[1.99136197e+00, -5.69817110e-03],
                              [-5.69817110e-03, -1.30905760e-01]],
                             [[-5.69817110e-03, 1.63522163e-05],
                              [1.62577807e-05, 3.74579524e-04]]],
                            [[[-5.69817110e-03, 1.62577807e-05],
                              [1.63522163e-05, 3.74579524e-04]],
                             [[-1.30905760e-01, 3.74579524e-04],
                              [3.74579524e-04, 8.60532554e-03]]]])

    def rdm_circuit(angles) -> tq.circuit.QCircuit:
        # Handwritten circuit for Helium-atom in minimal basis
        U = tq.gates.X(target=0)
        U += tq.gates.X(target=1)
        U += tq.gates.Ry(target=3, control=0, angle=-1 / 2 * angles[0])
        U += tq.gates.X(target=0)
        U += tq.gates.X(target=1, control=3)
        U += tq.gates.Ry(target=2, control=1, angle=-1 / 2 * angles[1])
        U += tq.gates.X(target=1)
        U += tq.gates.Ry(target=2, control=1, angle=-1 / 2 * angles[2])
        U += tq.gates.X(target=1)
        U += tq.gates.X(target=2)
        U += tq.gates.X(target=0, control=2)
        U += tq.gates.X(target=2)
        return U

    mol = qc.Molecule(geometry="data/he.xyz",
                      basis_set="6-31g",
                      transformation=trafo)
    # Random angles - check consistency of spin-free, spin-ful matrices
    ang = numpy.random.uniform(low=0, high=1, size=3)
    U_random = rdm_circuit(angles=ang)
    # Spin-free matrices
    mol.compute_rdms(U=U_random, spin_free=True)
    rdm1_sfree, rdm2_sfree = mol.rdm1, mol.rdm2
    # Spin-orbital matrices
    mol.compute_rdms(U=U_random, spin_free=False)
    rdm1_spinsum, rdm2_spinsum = mol.rdm_spinsum(sum_rdm1=True, sum_rdm2=True)
    assert (numpy.allclose(rdm1_sfree, rdm1_spinsum, atol=1e-8))
    assert (numpy.allclose(rdm2_sfree, rdm2_spinsum, atol=1e-8))

    # Fixed angles - check consistency of result
    ang = [-0.26284376686921973, -0.010829810670240182, 6.466541685258823]
    U_fixed = rdm_circuit(angles=ang)
    mol.compute_rdms(U=U_fixed, spin_free=True)
    rdm1_sfree, rdm2_sfree = mol.rdm1, mol.rdm2
    assert (numpy.allclose(rdm1_sfree, rdm1_ref, atol=1e-8))
    assert (numpy.allclose(rdm2_sfree, rdm2_ref, atol=1e-8))
コード例 #3
0
def obtain_PES(molecule, bond_lengths, basis, method, print_log=True):

    if method.lower() not in ['ccsd', 'cisd', 'fci', 'hf']:
        raise (ValueError(
            "Method not recognized, implemented methods are 'ccsd', 'cisd', 'fci', 'hf'."
        ))

    gridpoints = len(bond_lengths)

    energies = np.zeros(gridpoints)

    for i in range(gridpoints):

        obtained_e = False
        nudged_geo_tries = 0

        while obtained_e == False:

            try:
                mol_data = get_molecular_data(molecule,
                                              bond_lengths[i],
                                              xyz_format=True)
                mol_data = quantumchemistry.Molecule(mol_data, basis)

                if method == 'cisd':
                    result = mol_data.compute_energy(
                        'detci', options={"detci__ex_level": 2})
                else:
                    result = mol_data.compute_energy(method)

                if print_log:
                    print("E = {} Eh".format(result))

                energies[i] = result
                obtained_e = True

            except:
                #Nudge geometry, cross fingers
                bond_lengths[i] += 0.00000042
                nudged_geo_tries += 1

            if nudged_geo_tries > 9:
                obtained_e = True
                energies[i] = np.nan
                print("Could not converge")

    return energies