from minorminer import find_embedding
from dwave.system.samplers import DWaveSampler
from dwave.system.composites import FixedEmbeddingComposite

# Graph partitioning as full mesh
gamma = 3
linear = 3 - (3 * gamma)
quad = (2 * gamma) - 2

Q = {(0, 0): linear, (1, 1): linear, (2, 2): linear, (3, 3): linear, (0, 1): quad, (0, 2): quad, (0, 3): quad, (1, 2): quad, (1, 3): quad, (2, 3): quad}

chainstrength = 1.01
numruns = 100

dwave_sampler = DWaveSampler()
A = dwave_sampler.edgelist
embedding = find_embedding(Q, A)
print(embedding)

response = FixedEmbeddingComposite(DWaveSampler(), embedding).sample_qubo(Q, chain_strength=chainstrength, num_reads=numruns)

for sample, energy, num, cbf in response.data(['sample', 'energy', 'num_occurrences', 'chain_break_fraction']):
    print(sample, energy, num, cbf)
                                      embedding).sample_ising(h,
                                                              J,
                                                              num_reads=reads)
energies = fe_response.record.energy
print("QPU call complete using",
      fe_response.info['timing']['qpu_access_time'] / 1000000.0,
      "seconds of QPU time.")
print("Energy values in range", [energies.min(), energies.max()])

# Next we will visualize our results.
# `FixedEmbeddingComposite` and `VirtualGraphComposite` return information about broken chains by providing the `chain_break_fraction` in the response object.  Each sample has an associated chain break fraction that indicates the percentage of chains that broke in that sample.

fe_x_vals = range(reads)
fe_x_pos = np.arange(reads)
fe_y_vals = []
for datum in fe_response.data():
    for _ in range(datum[2]):
        fe_y_vals.append(datum[3])

plt.plot()
plt.scatter(fe_x_pos, fe_y_vals)
plt.ylabel('Chain Break Fraction')
plt.xlabel('Sample')
plt.ylim([0.0, 1.0])
plt.title('Chain Break Fractions')
fe_average = np.array(fe_y_vals).mean()
plt.plot([0, reads], [fe_average, fe_average], color='red')
print("Average chain break fraction", fe_average)

# Running with `VirtualGraphComposite`