Samples#

Returned solutions and samples are described under Binary Quadratic Models.

sample_like Objects#

as_samples(-> tuple[np.ndarray, ...)

Convert a samples_like object to a NumPy array and list of labels.

SampleSet#

class SampleSet(record, variables, info, vartype)[source]#

Samples and any other data returned by dimod samplers.

Parameters:
  • record (numpy.recarray) – A NumPy record array. Must have ‘sample’, ‘energy’ and ‘num_occurrences’ as fields. The ‘sample’ field should be a 2D NumPy array where each row is a sample and each column represents the value of a variable.

  • variables (iterable) – An iterable of variable labels, corresponding to columns in record.samples.

  • info (dict) – Information about the SampleSet as a whole, formatted as a dict.

  • vartype (Vartype/str/set) –

    Variable type for the SampleSet. Accepted input values:

    • Vartype.SPIN, 'SPIN', {-1, 1}

    • Vartype.BINARY, 'BINARY', {0, 1}

    • ExtendedVartype.DISCRETE, 'DISCRETE'

Examples

This example creates a SampleSet out of a samples_like object (a NumPy array).

>>> import numpy as np
...
>>> sampleset =  dimod.SampleSet.from_samples(np.ones(5, dtype='int8'),
...                                           'BINARY', 0)
>>> sampleset.variables
Variables([0, 1, 2, 3, 4])

Properties#

SampleSet.first

Sample with the lowest-energy.

SampleSet.info

Dict of information about the SampleSet as a whole.

SampleSet.record

numpy.recarray containing the samples, energies, number of occurences, and other sample data.

SampleSet.variables

Variables of variable labels.

SampleSet.vartype

Vartype of the samples.

Methods#

SampleSet.aggregate()

Create a new SampleSet with repeated samples aggregated.

SampleSet.append_variables(samples_like[, ...])

Deprecated in favor of dimod.append_variables.

SampleSet.change_vartype(vartype[, ...])

Return the SampleSet with the given vartype.

SampleSet.copy()

Create a shallow copy.

SampleSet.data([fields, sorted_by, name, ...])

Iterate over the data in the SampleSet.

SampleSet.done()

Return True if a pending computation is done.

SampleSet.filter(pred)

Return a new sampleset with rows filtered by the given predicate.

SampleSet.from_future(future[, result_hook])

Construct a SampleSet referencing the result of a future computation.

SampleSet.from_samples(samples_like, ...[, ...])

Build a SampleSet from raw samples.

SampleSet.from_samples_bqm(samples_like, ...)

Build a sample set from raw samples and a binary quadratic model.

SampleSet.from_samples_cqm(samples_like, cqm)

Build a sample set from raw samples and a constrained quadratic model.

SampleSet.from_serializable(obj)

Deserialize a SampleSet.

SampleSet.lowest([rtol, atol])

Return a sample set containing the lowest-energy samples.

SampleSet.resolve()

Ensure that the sampleset is resolved if constructed from a future.

SampleSet.relabel_variables(mapping[, inplace])

Relabel the variables of a SampleSet according to the specified mapping.

SampleSet.samples([n, sorted_by])

Return an iterable over the samples.

SampleSet.slice(*slice_args, **kwargs)

Create a new sample set with rows sliced according to standard Python slicing syntax.

SampleSet.to_pandas_dataframe([sample_column])

Convert a sample set to a Pandas DataFrame.

SampleSet.to_serializable([use_bytes, ...])

Convert a SampleSet to a serializable object.

SampleSet.truncate(n[, sorted_by])

Create a new sample set with up to n rows.

Utility Functions#

append_data_vectors(sampleset, **vectors)

Create a new SampleSet with additional fields in SampleSet.record.

append_variables(sampleset, samples_like[, ...])

Create a new SampleSet with the given variables and values.

concatenate(samplesets[, defaults])

Combine sample sets.

drop_variables(sampleset, variables)

Return a new sample set with the given variables removed.

keep_variables(sampleset, variables)

Return a new sample set with only the given variables.

Printing#

Formatter(**kwargs)[source]#

Sets formatting for printing SampleSet objects.

Parameters:
  • width (int, optional, default=79) – Maximum number of characters to a single line.

  • depth (int, optional, default=None) – Maximum number of rows printed. Summation is used if exceeded. Default is unlimited.

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

Examples

>>> from dimod.serialization.format import Formatter
>>> sampleset = dimod.SampleSet.from_samples(([-1, 1], ['a', 'b']), dimod.SPIN, energy=1)
>>> Formatter(width=45).fprint(sampleset)
   a  b energy num_oc.
0 -1 +1      1       1
['SPIN', 1 rows, 1 samples, 2 variables]
>>> Formatter(width=30).fprint(sampleset)
   a  b energy num_oc.
0 -1 +1      1       1
['SPIN',
 1 rows,
 1 samples,
 2 variables]
set_printoptions(**kwargs)[source]#

Set print options globally.

Parameters:
  • width (int, optional, default=79) – Maximum number of characters to a single line.

  • depth (int, optional, default=None) – Maximum number of rows printed. Summation is used if exceeded. Default is unlimited.

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

Note

All arguments must be provided as keyword arguments.