Primitives

Basic building-block classes and superclasses for hybrid workflows.

Classes

class Present(result=None, exception=None)[source]

Already resolved Future object.

Users should treat this class as just another Future, the difference being an implementation detail: Present is “resolved” at construction time.

See the example of the run() method.

class Runnable(**runopts)[source]

Components such as samplers and branches that can be run for an iteration.

Parameters:**runopts (dict) – Keyword arguments passed down to each Runnable.run call.

Note

The base class Runnable does not enforce traits validation. To enable validation, derive your subclass from one of the state structure, I/O dimensionality, or I/O validation mixins in traits.

Examples

This example runs a tabu search on a binary quadratic model. An initial state is manually set to \(x=y=0, z=1; a=b=1, c=0\) and an updated state is created by running the sampler for one iteration.

>>> import dimod           # Create a binary quadratic model
>>> bqm = dimod.BinaryQuadraticModel({'x': 0.0, 'y': 0.0, 'z': 8.0, 'a': 2.0, 'b': 0.0, 'c': 6.0},
...                                  {('y', 'x'): 2.0, ('z', 'x'): -4.0, ('z', 'y'): -4.0,
...                                  ('b', 'a'): 2.0, ('c', 'a'): -4.0, ('c', 'b'): -4.0, ('a', 'z'): -4.0},
...                                  -1.0, 'BINARY')
>>> # Set up the sampler runnable
>>> sampler = TabuProblemSampler(tenure=2, timeout=5)
>>> # Run one iteration of the sampler
>>> new_state = sampler.next(State.from_sample({'x': 0, 'y': 0, 'z': 1, 'a': 1, 'b': 1, 'c': 0}, bqm))
>>> print(new_state.samples)      # doctest: +SKIP
      a  b  c  x  y  z  energy  num_occ.
   0  1  1  1  1  1  1    -1.0         1
   [ 1 rows, 6 variables ]
class State(*args, **kwargs)[source]

Computation state passed along a branch between connected components.

State is a dict subclass and usually contains at least two keys: samples and problem.

Examples

>>> import dimod           # Create a binary quadratic model
>>> bqm = dimod.BinaryQuadraticModel({0: -1, 1: -1}, {(0, 1): 2}, 0.0, dimod.BINARY)
>>> hybrid.core.State.from_sample(hybrid.utils.min_sample(bqm), bqm)   # doctest: +SKIP
{'problem': BinaryQuadraticModel({0: -1, 1: -1}, {(0, 1): 2}, 0.0, Vartype.BINARY),
 'samples': SampleSet(rec.array([([0, 0], 0., 1)],
    dtype=[('sample', 'i1', (2,)), ('energy', '<f8'), ('num_occurrences', '<i4')]), [0, 1], {}, 'BINARY')}
class States(*args)[source]

List of states.

Properties

Runnable.name Return the Runnable class name.
SampleSet.first Sample with the lowest-energy.

Methods

Runnable.dispatch(future, **kwargs) Dispatch state from resolving future to either next or error methods.
Runnable.error(exc) Execute one blocking iteration of an instantiated Runnable with an exception as input.
Runnable.init(state, **runopts) Run prior to the first next/run, with the first state received.
Runnable.halt() Called by stop().
Runnable.next(state, **runopts) Execute one blocking iteration of an instantiated Runnable with a valid state as input.
Runnable.run(state, **kwargs) Execute the next step/iteration of an instantiated Runnable.
Runnable.stop() Terminate an iteration of an instantiated Runnable.
State.updated(**kwargs) Return a (deep) copy of the state, updated from kwargs.
State.from_sample(sample, bqm, **kwargs) Convenience method for constructing a state from a raw (dict) sample.
State.from_samples(samples, bqm, **kwargs) Convenience method for constructing a state from raw (dict) samples.