dimod.SampleSet.data#

SampleSet.data(fields=None, sorted_by='energy', name='Sample', reverse=False, sample_dict_cast=True, index=False)[source]#

Iterate over the data in the SampleSet.

Parameters:
  • fields (list, optional, default=None) – If specified, only these fields are included in the yielded tuples. The special field name ‘sample’ can be used to view the samples.

  • sorted_by (str/None, optional, default='energy') – Selects the record field used to sort the samples. If None, the samples are yielded in record order.

  • name (str/None, optional, default='Sample') – Name of the yielded namedtuples or None to yield regular tuples.

  • reverse (bool, optional, default=False) – If True, yield in reverse order.

  • sample_dict_cast (bool, optional, default=True) – Samples are returned as dicts rather than SampleView, which requires heavy memory usage. Set to False to reduce load on memory.

  • index (bool, optional, default=False) – If True, datum.idx gives the corresponding index of the SampleSet.record.

Yields:

namedtuple/tuple – The data in the SampleSet, in the order specified by the input fields.

Examples

>>> sampleset = dimod.ExactSolver().sample_ising({'a': -0.5, 'b': 1.0}, {('a', 'b'): -1})
>>> for datum in sampleset.data(fields=['sample', 'energy']):   
...     print(datum)
Sample(sample={'a': -1, 'b': -1}, energy=-1.5)
Sample(sample={'a': 1, 'b': -1}, energy=-0.5)
Sample(sample={'a': 1, 'b': 1}, energy=-0.5)
Sample(sample={'a': -1, 'b': 1}, energy=2.5)
>>> for energy, in sampleset.data(fields=['energy'], sorted_by='energy'):
...     print(energy)
...
-1.5
-0.5
-0.5
2.5
>>> print(next(sampleset.data(fields=['energy'], name='ExactSolverSample')))
ExactSolverSample(energy=-1.5)