model = H_obj.compile()

# --- Solve QUBO and unembed samplesn ---

# Get the QUBO matrix from the model
feed_dict = {'lam': 5.0}  # the coefficient of constraints
qubo, offset = model.to_qubo(feed_dict=feed_dict)

adjacency = {key: 1.0 for key in qubo.keys() if key[0] != key[1]}

# Run QUBO on DW_2000Q_5
sampler = DWaveSampler(solver='DW_2000Q_5')
embedding = find_embedding(adjacency, sampler.edgelist)
qubo_emb = embed_qubo(qubo, embedding, sampler.adjacency)
response = sampler.sample_qubo(qubo_emb,
                               num_reads=1000,
                               postprocess='optimization')

# Unembed samples by specifying chain_break_method argument of unembed_sampleset
bqm = dimod.BinaryQuadraticModel.from_qubo(qubo)
# majority_vote (default)
sample_majority_vote = unembed_sampleset(response,
                                         embedding,
                                         bqm,
                                         chain_break_method=majority_vote)
# discard
sample_discard = unembed_sampleset(response,
                                   embedding,
                                   bqm,
                                   chain_break_method=discard)
# weighted_random
Esempio n. 2
0
from dwave.system import DWaveSampler
import dwave.inspector

sampler = DWaveSampler(solver='DW_2000Q_6',
                       #token='',
                       )

# 0 = Lauch
# 4 = Sellerie
# 7 = Erbsen
# 3 = Mais

# ISING:
# E = s0 - s0 s4 + s0 s7 + s3 s4 - s3 s7
# QUBO: (si = 2*xi - 1)
# E = 2 x0 - 4 x0 x4 + 4 x0 x7 + 4 x3 x4 - 4 x3 x7 + 1

Q = {
    (0, 0): +2,
    (0, 4): -4,
    (0, 7): +4,
    (3, 4): +4,
    (3, 7): -4,
}
response = sampler.sample_qubo(
    Q,
    num_reads=100,
)
print(response)
dwave.inspector.show(response)
Esempio n. 3
0
from dwave.system import DWaveSampler, EmbeddingComposite
sampler_manual = DWaveSampler(solver={'topology__type': 'chimera'})

# Check if the qubits and couplers are available given the topology of the architecture
qubit_list = [0, 1, 4, 5]
print(
    f'The intended quibits are available: {all(qubit in sampler_manual.nodelist for qubit in qubit_list) }'
)
qubit_edges = [(0, 4), (0, 5), (1, 4), (1, 5)]
print(
    f'The intended couplers are available: {all(coupler in sampler_manual.edgelist for coupler in qubit_edges) }'
)

manual = False
if manual:
    # Define the problem a + b + c = 1 using the manual embedding
    qubit_biases = {(0, 0): 1, (1, 1): -1, (4, 4): -1, (5, 5): 1}
    coupler_strengths = {(0, 4): 2, (0, 5): -3, (1, 4): 2, (1, 5): 2}
    solver = DWaveSampler(solver={'topology__type': 'chimera'})
else:
    # Define the same problem using the automatic embedding
    qubit_biases = {('a', 'a'): -1, ('b', 'b'): -1, ('c', 'c'): -1}
    coupler_strengths = {('a', 'b'): 2, ('b', 'c'): 2, ('a', 'c'): 2}
    solver = EmbeddingComposite(
        DWaveSampler(solver={'topology__type': 'chimera'}))

Q = {**qubit_biases, **coupler_strengths}
iterations = 5000
sampleset = solver.sample_qubo(Q, num_reads=iterations)

print(sampleset)