dwavesystems / dwave-neal

An implementation of a simulated annealing sampler for general Ising model graphs in C++ with a dimod Python wrapper.
https://docs.ocean.dwavesys.com/projects/neal/en/latest
Apache License 2.0
50 stars 44 forks source link

Added "answer_mode" #69

Closed rkxy01 closed 4 years ago

rkxy01 commented 4 years ago

Report and pull request

Description SimulatedAnnealingSampler.sample responses the dimod.SampleSet object. In samplers such as EmbeddingComposites, answer_mode is set 'histogram' as its default value. https://docs.dwavesys.com/docs/latest/c_solver_1.html#answer-format However, in this repository, sampler doesn't return answers following histogram format even if we set answer_mode='histogram'. I modified this and added some tests. (If you consider this implementation bad, please ignore or edit.)

To Reproduce

import neal
sampler = neal.SimulatedAnnealingSampler()
h = {0:1, 1:2}
J = {(0,1):1}
sampler.sample_ising(h, J)
sampler.sample_ising(h, J, answer_mode='histogram', num_reads=3)

This outputs

SampleSet(rec.array([([-1, -1], -2., 1), ([ 1, -1], -2., 1), ([-1, -1], -2., 1)],
          dtype=[('sample', 'i1', (2,)), ('energy', '<f8'), ('num_occurrences', '<i8')]), [0, 1], {'beta_range': [0.23104906018664842, 4.605170185988092], 'beta_schedule_type': 'geometric'}, 'SPIN')

Expected behavior

SampleSet(rec.array([([ 1, -1], -2., 3)],
          dtype=[('sample', '<i8', (2,)), ('energy', '<f8'), ('num_occurrences', '<i8')]), [0, 1], {'beta_range': [0.23104906018664842, 4.605170185988092], 'beta_schedule_type': 'geometric'}, 'SPIN')

Environment:

Additional context I set default value of answer_mode 'raw'. This is because to avoid unexpected behavior in surrounding repositories related to this. If it is OK, please change this.

arcondello commented 4 years ago

One thing that would be straightforward to add would be an AggregateComposite, the benefit of that would be to make a "general" solution that would also apply to other Ocean samplers, e.g. TabuSampler.

That would make the flow

sampler = AggregateComposite(SimulatedAnnealingSampler())
sampleset = sampler.sample(bqm, answer_mode='histogram')
sampleset = sampler.sample(bqm, answer_mode='raw')

would this satisfy the use case you have in mind?

rkxy01 commented 4 years ago

Oh, thank you for your quick and kind advice! I didn't know AggregateComposite, but this is what I'd like to do. I'd like to use this function from now.

arcondello commented 4 years ago

Hi @Xiangyue-San , that composite does not yet exist! We'd love to get a PR to dimod for it, or we can add it ourselves (probably sometime in the next week or so). Let us know!

rkxy01 commented 4 years ago

I'm sorry, I seem to have jumped to the conclusion :-o I'd like to ask you to add them into dimod because I'm not familiar with D-Wave Ocean's project... I'll add issue comment in dimod repository and study how to develop them by looking your source codes! Thank you very much.

randomir commented 4 years ago

@Xiangyue-San, @arcondello, you can also consider using D-Wave Hybrid, where the runnable (composite) for this already exists!

AggregatedSamples was released in 0.4.0 and it allows you to aggregate or spread the input samples.