dimod.Response.data¶
-
Response.data(fields=None, sorted_by='energy', name='Sample')[source]¶ Iterate over the data in the response.
Parameters: - fields (list, optional, default=None) – If specified, only these fields’ values 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 data_vector used to sort the samples. If None, the samples are yielded in the order given by the samples matrix.
- name (str/None, optional, default='Sample') – Name of the yielded namedtuples or None to yield regular tuples.
Yields: namedtuple/tuple – The data in the response, in the order specified by the input fields.
Examples
This example iterates over the response data of the dimod ExactSolver sampler.
>>> import dimod >>> response = dimod.ExactSolver().sample_ising({'a': -0.5, 'b': 1.0}, {('a', 'b'): -1}) >>> for datum in response.data(): ... 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 response.data(fields=['energy'], sorted_by='energy'): ... print(energy) ... -1.5 -0.5 -0.5 2.5 >>> print(next(response.data(fields=['energy'], name='ExactSolverSample'))) ExactSolverSample(energy=-1.5)