Response

dimod samplers respond with a consistent Response object that is Iterable (over samples, from lowest energy to highest) and Sized (number of samples).

Examples

This example shows the response of the dimod ExactSolver sampler.

>>> import dimod
>>> response = dimod.ExactSolver().sample_ising({'a': -0.5}, {})
>>> len(response)
2
>>> for sample in response:
...     print(sample)
{'a': 1}
{'a': -1}

Class

class Response(**kwargs)[source]

A container for samples and any other data returned by dimod samplers.

Parameters:
  • samples_matrix (numpy.matrix) – Samples as a NumPy matrix where each row is a sample.
  • data_vectors (dict[field, numpy.array/list]) – Additional per-sample data as a dict of vectors. Each vector is of the same length as samples_matrix. The key ‘energy’ and its vector are required.
  • vartype (Vartype) – Vartype of the samples.
  • info (dict, optional, default=None) – Information about the response as a whole formatted as a dict.
  • variable_labels (list, optional, default=None) – Variable labels mappped by index to columns of the samples matrix.
vartype

Vartype – Vartype of the samples.

info

dict – Information about the response as a whole formatted as a dict.

variable_labels

list/None – Variable labels. Each column in the samples matrix is the values returned for one variable. If None, column indices are the labels.

label_to_idx

dict – Map of variable labels to columns in samples matrix.

Examples

This example shows some attributes of the response for the sampler of dimod package’s random_sampler.py reference example.

>>> from dimod.reference.samplers.random_sampler import RandomSampler
>>> sampler = RandomSampler()
>>> bqm = dimod.BinaryQuadraticModel({0: 0.0, 1: 1.0}, {(0, 1): 0.5}, -0.5, dimod.SPIN)
>>> response = sampler.sample(bqm)
>>> response.vartype  
<Vartype.SPIN: frozenset([1, -1])>  
>>> response.variable_labels
[0, 1]

Properties

Response.samples_matrix numpy.matrix – Samples as a NumPy matrix of data type int8.
Response.data_vectors dict[field, numpy.array/list] – Per-sample data as a dict, where keys are the data labels and values are each a vector of the same length as sample_matrix.

Methods

Viewing a Response

Response.samples([n, sorted_by]) Iterate over the samples in the response.
Response.data([fields, sorted_by, name]) Iterate over the data in the response.

Constructing or Updating a Response

Response.from_dicts(samples, data_vectors[, …]) Build a response from an iterable of dicts.
Response.from_futures(futures, vartype, …) Build a response from Future-like objects.
Response.from_matrix(samples, data_vectors) Build a response from a NumPy array-like object.
Response.from_pandas(samples_df, data_vectors) Build a response from a pandas DataFrame.
Response.update(*other_responses) Add values of other responses to the response.

Transformations

Response.change_vartype(**kwargs) Create a new response with the given vartype.
Response.relabel_variables(mapping[, inplace]) Relabel a response’s variables as per a given mapping.

Copy

Response.copy() Creates a shallow copy of a response.

Done

Response.done() True if all loaded futures are done or if there are no futures.