dimod.SampleSet.samples

SampleSet.samples(n=None, sorted_by='energy')[source]

Return an iterable over the samples.

Parameters:
  • n (int, optional, default=None) – Maximum number of samples to return in the view.
  • sorted_by (str/None, optional, default='energy') – Selects the record field used to sort the samples. If None, samples are returned in record order.
Returns:

A view object mapping variable labels to values.

Return type:

SamplesArray

Examples

>>> sampleset = dimod.ExactSolver().sample_ising({'a': 0.1, 'b': 0.0},
...                                              {('a', 'b'): 1})
>>> for sample in sampleset.samples():   # doctest: +SKIP
...     print(sample)
{'a': -1, 'b': 1}
{'a': 1, 'b': -1}
{'a': -1, 'b': -1}
{'a': 1, 'b': 1}
>>> sampleset = dimod.ExactSolver().sample_ising({'a': 0.1, 'b': 0.0},
...                                              {('a', 'b'): 1})
>>> samples = sampleset.samples()
>>> samples[0]
{'a': -1, 'b': 1}
>>> samples[0, 'a']
-1
>>> samples[0, ['b', 'a']]
array([ 1, -1], dtype=int8)
>>> samples[1:, ['a', 'b']]
array([[ 1, -1],
       [-1, -1],
       [ 1,  1]], dtype=int8)