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

Feature: Have semantics of neal match DWaveSampler #36

Open tsphillips opened 5 years ago

tsphillips commented 5 years ago

Current Problem When I write code for DWaveSampler, I have to modify the code to use neal instead. For example, no matter which of these I write, the code that follows should never have to change.

sampler = DWaveSampler()
sampler = neal.SimulatedAnnealingSampler()

Proposed Solution Have the inputs and outputs of neal match those of DWaveSampler. For example, the argument "anneal_schedule=" should be silently ignored. The output in response.data() should be aggregated like it is in DWaveSampler.

Alternatives Considered If I want the same code to work with both neal and DWaveSampler, then I would need to make my own wrapper around these. That is a layer of abstraction I would very much like to avoid.

tsphillips commented 5 years ago

Here is a sample that shows the difference in output:

$ python3 anneal-vs-neal.py 
D-Wave anneal results:
{0: 0, 4: 1} Energy:  -1.0 Occurrences:  5
{0: 1, 4: 0} Energy:  -1.0 Occurrences:  3
Simulated anneal results:
{0: 1, 4: 0} Energy:  -1.0 Occurrences:  1
{0: 0, 4: 1} Energy:  -1.0 Occurrences:  1
{0: 1, 4: 0} Energy:  -1.0 Occurrences:  1
{0: 1, 4: 0} Energy:  -1.0 Occurrences:  1
{0: 1, 4: 0} Energy:  -1.0 Occurrences:  1
{0: 1, 4: 1} Energy:  0.0 Occurrences:  1
{0: 0, 4: 0} Energy:  0.0 Occurrences:  1
{0: 0, 4: 0} Energy:  0.0 Occurrences:  1

$ cat anneal-vs-neal.py
from dwave.system.samplers import DWaveSampler
from neal import SimulatedAnnealingSampler

# This is a simple boolean NOT gate.
# true = (q0 NOT q4)
Q = {(0, 0): -1, (0, 4): 0, (4, 0): 2, (4, 4): -1}

# First we run a dwave anneal
dwave_sampler = DWaveSampler()
dwave_response = dwave_sampler.sample_qubo(Q, num_reads=8)
print('D-Wave anneal results:')
for sample, energy, num_occurrences in dwave_response.data():
    print(sample, "Energy: ", energy, "Occurrences: ", num_occurrences)

# Second we run a simulated anneal
neal_sampler = SimulatedAnnealingSampler()
neal_response = neal_sampler.sample_qubo(Q, num_reads=8)
print('Simulated anneal results:')
for sample, energy, num_occurrences in neal_response.data():
    print(sample, "Energy: ", energy, "Occurrences: ", num_occurrences)