dimod.Response.from_futures

classmethod Response.from_futures(futures, vartype, num_variables, samples_key='samples', data_vector_keys=None, info_keys=None, variable_labels=None, active_variables=None, ignore_extra_keys=True)[source]

Build a response from Future-like objects.

Parameters:
  • futures (iterable) – Iterable Future or Future-like objects (Python objects with similar structure). result() returns a dict.
  • vartype (Vartype) – Vartype of the response.
  • num_variables (int) – Number of variables for each sample.
  • samples_key (hashable, optional, default='samples') – Key of the result dict containing the samples. Samples are array-like.
  • data_vector_keys (iterable/mapping, optional, default=None) – A mapping from the keys of the result dict to Response.data_vectors. If None, [‘energy’] is assumed to be a key in the result dict and the ‘energy’ data vector is mapped.
  • info_keys (iterable/mapping, optional, default=None) – A mapping from the keys of the result dict to Response.info. If None, info is empty.
  • variable_labels (list, optional, default=None) – Maps (by index) variable labels to columns of the samples matrix.
  • active_variables (array-like, optional, default=None) – Selects which columns of the result’s samples are used. If variable_labels is not provided, variable_labels is set to match active_variables.
  • ignore_extra_keys (bool, optional, default=True) – If True, keys given in data_vector_keys and info_keys but that are not in result() are ignored. If False, extra keys cause a ValueError.
Returns:

A dimod Response object based on the input Future-like objects.

Return type:

Response

Notes

Future objects are read on the first read of Response.samples_matrix or Response.data_vectors.

Examples

These example code snippets build responses from Future objects.

from concurrent.futures import Future

future = Future()

# load the future into response
response = dimod.Response.from_futures((future,), dimod.BINARY, 3)

future.set_result({'samples': [0, 1, 0], 'energy': [1]})

# now read from the response
matrix = response.samples_matrix
from concurrent.futures import Future

future = Future()

# load the future into response
response = dimod.Response.from_futures((future,), dimod.BINARY, 3,
                                       active_variables=[0, 1, 3])

future.set_result({'samples': [0, 1, 3, 0], 'energy': [1]})

# now read from the response, this matrix
matrix = response.samples_matrix
from concurrent.futures import Future

future = Future()

# load the future into response
response = dimod.Response.from_futures((future,), dimod.BINARY, 3,
                                       data_vector_keys={'en': 'energy'})

future.set_result({'samples': [0, 1, 0], 'en': [1]})

# now read from the response
matrix = response.samples_matrix