def helloh1(): rhf_objective, molecule, parameters, obi, tbi = make_h3_2_5() ansatz, energy, gradient = rhf_func_generator(rhf_objective) jsondict = {} jsondict["comment"] = "Done" jsondict["schema"] = "h3test-circuit-result" return jsondict
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 assiming 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)
def test_energy_from_opdm_odd_qubit(): """Build test assuming sampling functions work""" rhf_objective, molecule, parameters, obi, tbi = make_h3_2_5() unitary, energy, _ = rhf_func_generator(rhf_objective) parameters = np.array([0.1, 0.2]) initial_opdm = np.diag([1] * 1 + [0] * 2) print(initial_opdm) 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)
def helloh3(): # Generate the input files, set up quantum resources, and set up the OpdmFunctional to make measurements. rhf_objective, molecule, parameters, obi, tbi = make_h3_2_5() ansatz, energy, gradient = rhf_func_generator(rhf_objective) # settings for quantum resources qubits = [cirq.GridQubit(0, x) for x in range(molecule.n_orbitals)] sampler = cirq.Simulator( dtype=np.complex128) # this can be a QuantumEngine # OpdmFunctional contains an interface for running experiments opdm_func = OpdmFunctional( qubits=qubits, sampler=sampler, constant=molecule.nuclear_repulsion, one_body_integrals=obi, two_body_integrals=tbi, num_electrons=molecule.n_electrons // 2, # only simulate spin-up electrons clean_xxyy=True, purification=True) # Do measurements for a given set of parameters measurement_data = opdm_func.calculate_data(parameters) # Compute 1-RDM, variances, and purification opdm, var_dict = compute_opdm(measurement_data, return_variance=True) opdm_pure = mcweeny_purification(opdm) # Compute energy, fidelities, and errorbars raw_energies = [] raw_fidelity_witness = [] purified_energies = [] purified_fidelity_witness = [] purified_fidelity = [] true_unitary = ansatz(parameters) nocc = molecule.n_electrons // 2 nvirt = molecule.n_orbitals - nocc initial_fock_state = [1] * nocc + [0] * nvirt for _ in range(1000): # 1000 repetitions of the measurement new_opdm = resample_opdm(opdm, var_dict) raw_energies.append(opdm_func.energy_from_opdm(new_opdm)) raw_fidelity_witness.append( fidelity_witness(target_unitary=true_unitary, omega=initial_fock_state, measured_opdm=new_opdm)) # fix positivity and trace of sampled 1-RDM if strictly outside # feasible set w, v = np.linalg.eigh(new_opdm) if len(np.where(w < 0)[0]) > 0: new_opdm = fixed_trace_positive_projection(new_opdm, nocc) new_opdm_pure = mcweeny_purification(new_opdm) purified_energies.append(opdm_func.energy_from_opdm(new_opdm_pure)) purified_fidelity_witness.append( fidelity_witness(target_unitary=true_unitary, omega=initial_fock_state, measured_opdm=new_opdm_pure)) purified_fidelity.append( fidelity(target_unitary=true_unitary, measured_opdm=new_opdm_pure)) jsondict = {} jsondict["canonicalHFEnergy"] = str(molecule.hf_energy) jsondict["trueEnergy"] = str(energy(parameters)) jsondict["rawEnergy"] = str(opdm_func.energy_from_opdm(opdm)) jsondict["rawEnergySd"] = str(np.std(raw_energies)) jsondict["rawFidelityWitness"] = str(np.mean(raw_fidelity_witness)) jsondict["rawFidelityWitnessSd"] = str(np.std(raw_fidelity_witness)) jsondict["purifiedEnergy"] = str(opdm_func.energy_from_opdm(opdm_pure)) jsondict["purifiedEnergySd"] = str(np.std(purified_energies)) jsondict["purifiedFidelityWitness"] = str( np.mean(purified_fidelity_witness).real) jsondict["purifiedFidelityWitnessSd"] = str( np.std(purified_fidelity_witness)) jsondict["purifiedFidelity"] = str(np.mean(purified_fidelity).real) jsondict["purifiedFidelitySd"] = str(np.std(purified_fidelity)) jsondict["schema"] = "h3test-circuit-result" return jsondict
# # 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)