Samples

dimod provides a SampleSet class that contains, and enables you to manipulate, samples.

For an introduction to returned solutions and samples, see Solutions.

For all supported sampleset methods, see Samples.

This example creates a sample set and then demonstrates some SampleSet properties and methods.

>>> bqm = dimod.generators.random.ran_r(1, 7)
>>> sampler = dimod.ExactSolver()
>>> sampleset = sampler.sample(bqm)

Print the best solution’s energy.

>>> print(sampleset.first.energy)      
-9.0

Print the best solutions.

>>> print(sampleset.lowest())           
   0  1  2  3  4  5  6 energy num_oc.
0 +1 -1 +1 -1 +1 -1 -1   -9.0       1
1 +1 +1 +1 -1 +1 -1 -1   -9.0       1
...
7 -1 +1 -1 +1 -1 -1 +1   -9.0       1
['SPIN', 8 rows, 8 samples, 7 variables]

Convert to a third-party format (pandas).

>>> sampleset.to_pandas_dataframe()       
     0  1  2  3  4  5  6  energy  num_occurrences
0   -1 -1 -1 -1 -1 -1 -1     3.0                1
1    1 -1 -1 -1 -1 -1 -1     7.0                1
2    1  1 -1 -1 -1 -1 -1     7.0                1
...