1
$\begingroup$

at 1, the following code example is available:

>>> from dwave.system.samplers import DWaveSampler
>>> from dwave.system.composites import EmbeddingComposite
>>> sampler = DWaveSampler(endpoint='https://URL_to_my_D-Wave_system/', token='ABC-123456789012345678901234567890', solver='My_D-Wave_Solver')
>>> sampler_embedded = EmbeddingComposite(sampler)
>>> Q = {('x1', 'x2'): 1, ('x1', 'z'): -2, ('x2', 'z'): -2, ('z', 'z'): 3}
>>> response = sampler_embedded.sample_qubo(Q, num_reads=5000)

When looking at the results, they are grouped by sample solution, with a mention of the number of occurrences:

    >>> for datum in response.data(['sample', 'energy', 'num_occurrences']):   
...    print(datum.sample, "Energy: ", datum.energy, "Occurrences: ", datum.num_occurrences)
...
{'x1': 1, 'x2': 0, 'z': 0} Energy:  0.0 Occurrences:  1009
{'x1': 1, 'x2': 1, 'z': 1} Energy:  0.0 Occurrences:  1452
{'x1': 0, 'x2': 0, 'z': 0} Energy:  0.0 Occurrences:  1292
{'x1': 0, 'x2': 1, 'z': 0} Energy:  0.0 Occurrences:  1246
{'x1': 0, 'x2': 1, 'z': 0} Energy:  0.0 Occurrences:  1

After looking at the available samplers at 2, I changed the code to simulate the process with D-Wave's Dimod, and repeated this D-Wave's neal:

from dwave.system.samplers import DWaveSampler
from dwave.system.composites import EmbeddingComposite
import dimod
import neal
#sampler = DWaveSampler(endpoint='https://URL_to_my_D-Wave_system/',  token='ABC-123456789012345678901234567890', solver='My_D-Wave_Solver')
sampler = dimod.SimulatedAnnealingSampler()
# Note: also tried with neal: 
# sampler = neal.SimulatedAnnealingSampler()
#sampler_embedded = EmbeddingComposite(sampler)
Q = {('x1', 'x2'): 1, ('x1', 'z'): -2, ('x2', 'z'): -2, ('z', 'z'): 3}
#response = sampler_embedded.sample_qubo(Q, num_reads=5000)
response = sampler.sample_qubo(Q, num_reads=500)

However, my results are not binned:

for datum in response.data(['sample', 'energy', 'num_occurrences']):   
    print(datum.sample, "Energy: ", datum.energy, "Occurrences: ", datum.num_occurrences)    
({'x2': 0, 'x1': 0, 'z': 0}, 'Energy: ', 0.0, 'Occurrences: ', 1)
({'x2': 1, 'x1': 1, 'z': 1}, 'Energy: ', 0.0, 'Occurrences: ', 1)
({'x2': 1, 'x1': 1, 'z': 1}, 'Energy: ', 0.0, 'Occurrences: ', 1)
[497 similar lines removed]

My question: how can I make sure my results are correctly binned*?
OR: how should I properly port the given code to be able to simulate it without access to the actual D-Wave hardware?

*: I am not asking for a generic algorithm to create bins/histograms, I have those already. I am looking for a function of set of parameters from the D-Wave libraries, as I'm surprised by the different outcomes

$\endgroup$
1
$\begingroup$

Old answer : Maybe the answer mode differs using a different sampler. I have not tried but I know there is an option "answer_mode" in the sample_qubo method. If answer_mode='raw', you will have all responses not binned. If answer_mode='histogram', you should get them as bins.

New answer : Dimod does not use the answer_mode argument. To work it in your way, you have to aggregate response:

Q = {('x1', 'x2'): 1, ('x1', 'z'): -2, ('x2', 'z'): -2, ('z', 'z'): 3}

response = sampler.sample_qubo(Q, num_reads=500)
response = response.aggregate()

Then you will have bins :

for datum in response.data(['sample', 'energy', 'num_occurrences']):   
    print(datum.sample, "Energy: ", datum.energy, "Occurrences: ", datum.num_occurrences)

This yields for me :

{'x1': 0, 'x2': 1, 'z': 0} Energy: 0.0 Occurrences: 134

{'x1': 1, 'x2': 1, 'z': 1} Energy: 0.0 Occurrences: 123

{'x1': 0, 'x2': 0, 'z': 0} Energy: 0.0 Occurrences: 115 ...

$\endgroup$
  • $\begingroup$ I think this is in the right direction, but not yet there. sampler = dimod.SimulatedAnnealingSampler(answer_mode='histogram') returned unexpected keyword argument, even though I can find it here: docs.dwavesys.com/docs/latest/c_solver_1.html. $\endgroup$ – Thomas Hubregtsen Jun 26 '19 at 11:50
  • $\begingroup$ @ThomasHubregtsen Try sampler.sample_qubo(Q, num_reads=500, answer_mode='histogram') $\endgroup$ – cnada Jun 26 '19 at 13:39
  • $\begingroup$ also tried this one, did not work either: TypeError: sample() got an unexpected keyword argument 'answer_mode' $\endgroup$ – Thomas Hubregtsen Jun 26 '19 at 13:56
  • $\begingroup$ Seems like in dimod, things are done differentl (see examples similar to yours docs.ocean.dwavesys.com/en/latest/overview/…) $\endgroup$ – cnada Jun 26 '19 at 14:17
  • $\begingroup$ @ThomasHubregtsen Found the trick by looking in the dimod repository $\endgroup$ – cnada Jun 26 '19 at 14:37
0
$\begingroup$

As cnada pointed out, the correct parameter for this is "answer_mode". This parameter is not available for the simulated_annealing sampler.

As per 1, samplers.DWaveSampler.sample_qubo() can take the set of parameters mentioned in 2, including "answer mode". As seen in 3, samplers.simulated_annealing.sample_qubo() can only take beta_range’, num_reads’, ‘num_sweeps’ 4. This does not include "answer_mode".

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.