FutureResponse

class FutureResponse(info=None, vartype=None)[source]

A response object for async samples added to it.

Parameters:
  • info (dict) – Information about the response as a whole.
  • vartype (dimod.Vartype) – The values that the variables in each sample can take. See dimod.Vartype.
add_samples_future(future)[source]

Add samples from a micro client Future.

Parameters:future (dwave_micro_client.Future) – A Future from the dwave_micro_client.
datalist

list – The data in order of insertion. Each datum in data is a dict containing ‘sample’, ‘energy’, and ‘num_occurences’ keys as well an any other information added on insert. This attribute should be treated as read-only, as changing it can break the response’s internal logic.

add_data_from(data)

Loads data into the response.

Parameters:data (iterable[dict]) – An iterable of datum. Each datum is a dict. The datum must contain ‘sample’ and ‘energy’ keys with dict and number values respectively.

Examples

>>> response = dimod.TemplateResponse
>>> response.add_data_from([{'energy': -1, 'num_occurences': 1, 'sample': {0: 1}},
                            {'energy': 1, 'num_occurences': 1, 'sample': {0: -1}}])
add_sample(sample, energy, num_occurences=1, **kwargs)

Loads a sample and associated energy into the response.

Parameters:
  • sample (dict) – A sample as would be returned by a discrete model solver. Should be a dict of the form {var: value, …}.
  • energy (number) – The energy associated with the given sample.
  • num_occurences (int) – The number of times the sample occurred.
  • **kwargs

Examples

>>> response = dimod.TemplateResponse()
>>> response.add_sample({0: -1}, 1)
>>> response.add_sample({0: 1}, -1, sample_idx=1)
>>> list(response.data())
[{'energy': -1, 'num_occurences': 1, 'sample': {0: 1}, 'sample_idx': 1},
 {'energy': 1, 'num_occurences': 1, 'sample': {0: -1}}]
add_samples_from(samples, energies, num_occurences=1, **kwargs)

Loads samples and associated energies from iterators.

Parameters:
  • samples (iterator) – An iterable object that yields samples. Each sample should be a dict of the form {var: value, …}.
  • energies (iterator) – An iterable object that yields energies associated with each sample.
  • num_occurences (int) – Default 1. The number of times the sample occurred. This is applied to each sample.
  • **kwargs

Examples

>>> response = dimod.TemplateResponse()
>>> response.add_samples_from([{0: -1}, {0: 1}], [1, -1], dataval='test')
>>> list(response.data())
[{'dataval': 'test', 'energy': -1, 'num_occurences': 1, 'sample': {0: 1}},
 {'dataval': 'test', 'energy': 1, 'num_occurences': 1, 'sample': {0: -1}}]
cast(response_class, varmap=None, offset=0.0)

Casts the response to a different type of dimod response.

Parameters:
  • response_class (type) – A dimod response class.
  • varmap (dict, optional) – A dict mapping a change in sample values. If not provided samples are not changed.
  • offset (number, optional) – Default 0.0. The energy offset to apply to all of the energies in the response.
Returns:

A dimod response.

Return type:

response_class

data(keys=None, ordered_by_energy=True)

An iterator over the data.

Parameters:
  • keys (list, optional) – are provided, data yields a tuple of the values associated with each key in each datum.
  • ordered_by_energy (bool, optional) – Default True. If True, the datum (or tuples) are yielded in order energy, low-to-high. If False, they are yielded in order of insertion.
Yields:

dict – The datum stored in response.

If keys are provided, returns a tuple (see parameter description above and example below).

Examples

>>> response = dimod.TemplateResponse()
>>> response.add_samples_from([{0: -1}, {0: 1}], [1, -1])
>>> list(response.data())
[{'energy': -1, 'num_occurences': 1, 'sample': {0: 1}},
 {'energy': 1, 'num_occurences': 1, 'sample': {0: -1}}]
>>> for sample in response.data(keys=['sample']):
...     pass
>>> for sample, num_occurences in response.data(keys=['sample', 'num_occurences']):
...     pass
done()[source]

True if all of the futures added to the response have arrived.

energies(data=False)

An iterator over the energies.

Parameters:

data (bool, optional) – Default False. If True, returns an iterator of 2-tuples, (sample, datum).

Yields:

number – The energies, from low-to-high.

If data=True, yields 2-tuple (energy, datum). Where datum is the data associated with the given energy.

items(data=False)

Iterator over the samples and energies.

Parameters:data (bool, optional) – If True, return an iterator of 3-tuples (sample, energy, data). If False return an iterator of 2-tuples (sample, energy) over all of the samples and energies. Default False.
Returns:If data is False, return an iterator of 2-tuples (sample, energy) over all samples and energies in response in order of increasing energy. If data is True, return an iterator of 3-tuples (sample, energy, data) in order of increasing energy.
Return type:iterator

Examples

>>> response = TemplateResponse()
>>> response.add_sample({0: -1}, 1, data={'n': 5})
>>> response.add_sample({0: 1}, -1, data={'n': 1})
>>> list(response.items())
[({0: 1}, -1), ({0: -1}, 1)]
>>> list(response.items(data=True))
[({0: 1}, -1, {'n': 1}), ({0: -1}, 1, {'n': 5})]

Note

Depreciation Warning: This method of access is being depreciated. it can be replaced by response.data(keys=[‘sample’, ‘energy’])

relabel_samples(mapping)

Return a new response object with the samples relabeled.

Parameters:mapping (dict[hashable, hashable]) – A dictionary with the old labels as keys and the new labels as values. A partial mapping is allowed.

Examples

>>> response = TemplateResponse()
>>> response.add_sample({'a': -1, 'b': 1}, 1)
>>> response.add_sample({'a': 1, 'b': -1}, -1)
>>> mapping = {'a': 1, 'b': 0}
>>> new_response = response.relabel_samples(mapping)
>>> list(new_response.samples())
[{0: -1, 1: 1}, {0: 1, 1: -1}]
samples(data=False)

An iterator over the samples.

Parameters:

data (bool, optional) – Default False. If True, returns an iterator of 2-tuples, (sample, datum).

Yields:

dict – The samples, in order of energy low-to-high.

If data=True, yields 2-tuple (sample, datum). Where datum is the data associated with the given sample.

Examples

>>> response = TemplateResponse()
>>> response.add_samples_from([{0: -1}, {0: 1}], [1, -1])
>>> list(response.samples())
[{0: 1}, {0: -1}]
sorted_datalist

list[dict] – The data in order of energy, low-to-high. The datum stored in sorted_datalist are the same as in datalist. This list is generated on the first read after an insertion to the response.