Classical Solvers#

You might use a classical solver while developing your code or on a small version of your problem to verify your code. To solve a problem classically on your local machine, you configure a classical solver, either one of those included in the Ocean tools or your own.

Examples#

Among several samplers provided in the dimod tool for testing your code locally, is the ExactSolver that calculates the energy of all possible samples for a given problem. Such a sampler can solve a small three-variable problem such as a BQM representing a Boolean AND gate (see also the Example: BQM for a Boolean Circuit section) as follows:

>>> from dimod.generators import and_gate
>>> from dimod import ExactSolver
>>> bqm = and_gate('in1', 'in2', 'out')
>>> sampler = ExactSolver()
>>> sampleset = sampler.sample(bqm)
>>> print(sampleset)       
  in1 in2 out energy num_oc.
0   0   0   0    0.0       1
1   1   0   0    0.0       1
3   0   1   0    0.0       1
5   1   1   1    0.0       1
2   1   1   0    2.0       1
4   0   1   1    2.0       1
6   1   0   1    2.0       1
7   0   0   1    6.0       1
['BINARY', 8 rows, 8 samples, 3 variables]

Note that the first four samples are the valid states of the AND gate and have lower values than the second four, which represent invalid states.

If you use a classical solver running locally on your CPU, a single sample might provide the optimal solution.

This example solves a two-variable problem using the dwave-samplers simulated annealing sampler. For such a small problem, num_reads=10 most likely finds the optimal solution.

>>> from dwave.samplers import SimulatedAnnealingSampler
>>> solver = SimulatedAnnealingSampler()
>>> sampleset = solver.sample_ising({'a': -0.5, 'b': 1.0}, {('a', 'b'): -1}, num_reads=10)
>>> sampleset.first.sample["a"] == sampleset.first.sample["b"] == -1
True