Esempio n. 1
0
    def __init__(self, raw_results, answer, problem, all_vars):
        # Store our arguments.
        self.raw_results = raw_results
        self.answer = answer
        self.problem = problem

        # Unembed the solutions.  Fix rather than discard invalid solutions.
        if self.problem.embedding == {}:
            fixed_answer = self.answer.copy()
        else:
            fixed_answer = unembed_sampleset(self.answer, self.problem.embedding,
                                             self.problem.logical.bqm,
                                             chain_break_method=chain_breaks.majority_vote)

        # Construct one Solution object per solution.
        self.solutions = []
        energies = fixed_answer.record.energy
        tallies = fixed_answer.record.num_occurrences
        fixed_solns = fixed_answer.record.sample
        raw_solns = self.answer.record.sample
        sym2col, num2col = self._map_to_column(fixed_answer,
                                               self.problem.embedding,
                                               self.problem.qmasm.sym_map)
        for i in range(len(fixed_solns)):
            sset = self.answer.slice(i, i + 1)
            self.solutions.append(Solution(self.problem, sym2col, num2col, sset,
                                           fixed_solns[i], tallies[i], energies[i],
                                           all_vars))

        # Store the frequency of chain breaks across the entire SampleSet.
        self.chain_breaks = chain_break_frequency(self.answer, self.problem.embedding)
                                     dwave_sampler.adjacency,
                                     chain_strength=2.0)
reads = 1000
fixed_response = dwave_sampler.sample_ising(embedded_h,
                                            embedded_J,
                                            num_reads=reads)
energies = fixed_response.record.energy
print("QPU call complete using",
      fixed_response.info['timing']['qpu_access_time'] / 1000000.0,
      "seconds of QPU time.\n")

# Next we will look at the frequency that chains broke in our samples.
# This will be our metric to explore the advantages of `VirtualGraphComposite`.

from dwave.embedding import chain_break_frequency
chain_break_frequency = chain_break_frequency(fixed_response, embedding)
for key, val in chain_break_frequency.items():
    print("Chain", key, "broke in", val * 100, "percent of samples.")

# Let's visualize these results by plotting each chain against the percentage of samples in which it broke.

get_ipython().run_line_magic('matplotlib', 'inline')
import matplotlib.pyplot as plt

x_vals = range(S_size)
x_pos = np.arange(len(chain_break_frequency.items()))
y_vals = [-1 for _ in x_vals]
for key, val in chain_break_frequency.items():
    y_vals[key] = val * 100

f = plt.figure(figsize=(10, 3))