Пример #1
0
 def opdm_generator(self, parameters):
     opdm, var_dict = self.blackbox.calculate_rdm(parameters)
     self.noisy_opdms.append(opdm)
     self.variance_dicts.append(var_dict)
     if self.purification:
         opdm = cca.mcweeny_purification(opdm)
     return opdm
Пример #2
0
def test_mcweeny():
    np.random.seed(82)
    opdm = np.array([[
        0.766034130, -0.27166330, -0.30936072, -0.08471057, -0.04878244,
        -0.01285432
    ],
                     [
                         -0.27166330, 0.67657015, -0.37519640, -0.02101843,
                         -0.03568214, -0.05034585
                     ],
                     [
                         -0.30936072, -0.37519640, 0.55896791, 0.04267370,
                         -0.02258184, -0.08783738
                     ],
                     [
                         -0.08471057, -0.02101843, 0.04267370, 0.05450848,
                         0.11291253, 0.17131658
                     ],
                     [
                         -0.04878244, -0.03568214, -0.02258184, 0.11291253,
                         0.26821219, 0.42351185
                     ],
                     [
                         -0.01285432, -0.05034585, -0.08783738, 0.17131658,
                         0.42351185, 0.67570713
                     ]])
    for i, j in product(range(6), repeat=2):
        opdm[i, j] += np.random.randn() * 1.0E-3
    opdm = 0.5 * (opdm + opdm.T)
    pure_opdm = mcweeny_purification(opdm)
    w, _ = np.linalg.eigh(pure_opdm)
    assert len(np.where(w < -1.0E-9)[0]) == 0
Пример #3
0
    def calculate_rdm(self, parameters):
        data_dict = self.calculate_data(parameters)
        opdm, opdm_var_dict = cca.compute_opdm(data_dict, return_variance=True)
        if self.purification:
            self._last_noisy_opdm = opdm
            opdm = cca.mcweeny_purification(opdm)

        return opdm, opdm_var_dict
Пример #4
0
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