def make_h3_2_5() -> Tuple[RestrictedHartreeFockObjective, of.MolecularData,
                           np.ndarray, np.ndarray, np.ndarray]:
    # load the molecule from moelcular data
    h3_2_5_path = os.path.join(
        hfvqe.__path__[0],
        'molecular_data/hydrogen_chains/h_3_p_sto-3g/bond_distance_2.5')

    molfile = os.path.join(h3_2_5_path,
                           'H3_plus_sto-3g_singlet_linear_r-2.5.hdf5')
    molecule = of.MolecularData(filename=molfile)
    molecule.load()

    S = np.load(os.path.join(h3_2_5_path, 'overlap.npy'))
    Hcore = np.load(os.path.join(h3_2_5_path, 'h_core.npy'))
    TEI = np.load(os.path.join(h3_2_5_path, 'tei.npy'))

    _, X = sp.linalg.eigh(Hcore, S)
    obi = of.general_basis_change(Hcore, X, (1, 0))
    tbi = np.einsum('psqr', of.general_basis_change(TEI, X, (1, 0, 1, 0)))
    molecular_hamiltonian = generate_hamiltonian(obi, tbi,
                                                 molecule.nuclear_repulsion)

    rhf_objective = RestrictedHartreeFockObjective(molecular_hamiltonian,
                                                   molecule.n_electrons)

    scipy_result = rhf_minimization(rhf_objective)
    return rhf_objective, molecule, scipy_result.x, obi, tbi
def make_h3_2_5(molecular_data_directory=None) \
        -> Tuple[RestrictedHartreeFockObjective, of.MolecularData,
                 np.ndarray, np.ndarray, np.ndarray]:
    if molecular_data_directory is None:
        molecular_data_directory = _MOLECULAR_DATA_DIRECTORY

    h3_2_5_path = f'{molecular_data_directory}/hydrogen_chains/h_3_p_sto-3g/bond_distance_2.5'
    molfile = f'{h3_2_5_path}/H3_plus_sto-3g_singlet_linear_r-2.5.hdf5'
    molecule = of.MolecularData(filename=molfile)
    molecule.load()

    S = np.load(os.path.join(h3_2_5_path, 'overlap.npy'))
    Hcore = np.load(os.path.join(h3_2_5_path, 'h_core.npy'))
    TEI = np.load(os.path.join(h3_2_5_path, 'tei.npy'))

    _, X = sp.linalg.eigh(Hcore, S)
    obi = of.general_basis_change(Hcore, X, (1, 0))
    tbi = np.einsum('psqr', of.general_basis_change(TEI, X, (1, 0, 1, 0)))
    molecular_hamiltonian = generate_hamiltonian(obi, tbi,
                                                 molecule.nuclear_repulsion)

    rhf_objective = RestrictedHartreeFockObjective(molecular_hamiltonian,
                                                   molecule.n_electrons)

    scipy_result = rhf_minimization(rhf_objective)
    return rhf_objective, molecule, scipy_result.x, obi, tbi
Exemple #3
0
def test_rhf_func_gen():
    rhf_objective, molecule, parameters, _, _ = make_h6_1_3()
    ansatz, energy, _ = rhf_func_generator(rhf_objective)
    assert np.isclose(molecule.hf_energy, energy(parameters))

    ansatz, energy, _, opdm_func = rhf_func_generator(rhf_objective,
                                                      initial_occ_vec=[1] * 3 +
                                                      [0] * 3,
                                                      get_opdm_func=True)
    assert np.isclose(molecule.hf_energy, energy(parameters))
    test_opdm = opdm_func(parameters)
    u = ansatz(parameters)
    initial_opdm = np.diag([1] * 3 + [0] * 3)
    final_odpm = u @ initial_opdm @ u.T
    assert np.allclose(test_opdm, final_odpm)

    result = rhf_minimization(rhf_objective, initial_guess=parameters)
    assert np.allclose(result.x, parameters)
        bond_distances = numpy.linspace(0.5, 2.5, 6)
        for bb in bond_distances:
            print(bb)
            local_dir = 'bond_distance_{:.1f}'.format(bb)
            os.mkdir(local_dir)
            os.chdir(local_dir)
            molecule = molecule_generator[n](bb)

            rhf_objective, S, HCore, TEI = make_rhf_objective(molecule)

            numpy.save("overlap.npy", S)
            numpy.save("h_core.npy", HCore)
            numpy.save("tei.npy", TEI)

            ansatz, energy, gradient = rhf_func_generator(rhf_objective)
            scipy_result = rhf_minimization(rhf_objective)
            print(molecule.hf_energy)
            print(scipy_result.fun)
            assert numpy.isclose(molecule.hf_energy, scipy_result.fun)

            numpy.save("parameters.npy", numpy.asarray(scipy_result.x))
            initial_opdm = numpy.diag([1] * rhf_objective.nocc +
                                      [0] * rhf_objective.nvirt)
            unitary = ansatz(scipy_result.x)
            final_opdm = unitary @ initial_opdm @ numpy.conjugate(unitary).T
            assert numpy.isclose(rhf_objective.energy_from_opdm(final_opdm),
                                 scipy_result.fun)
            numpy.save("true_opdm.npy", numpy.asarray(final_opdm))

            molecule.filename = os.path.join(os.getcwd(), molecule.name)
            molecule.save()