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)
def test_energy_from_opdm(): """Build test assuming sampling functions work""" rhf_objective, molecule, parameters, obi, tbi = make_h6_1_3() unitary, energy, _ = rhf_func_generator(rhf_objective) parameters = np.array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]) initial_opdm = np.diag([1] * 3 + [0] * 3) final_opdm = unitary(parameters) @ initial_opdm @ unitary( parameters).conj().T test_energy = energy_from_opdm(final_opdm, constant=molecule.nuclear_repulsion, one_body_tensor=obi, two_body_tensor=tbi) true_energy = energy(parameters) assert np.allclose(test_energy, true_energy)
# # now make a separate folder for each of 50 points along a line 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)