Composites¶
The dimod package includes several example composed samplers:
The dwavesystem package provides additional composites for DWave systems such as those used for minorembedding.
Structured Composite¶
A composite that structures a sampler.
Class¶
 class StructureComposite(sampler, nodelist, edgelist)[source]¶
Creates a structured composed sampler from an unstructured sampler.
 Parameters
Examples
This example creates a composed sampler from the unstructure dimod ExactSolver sampler. The target structure is a square graph.
>>> base_sampler = dimod.ExactSolver() >>> node_list = [0, 1, 2, 3] >>> edge_list = [(0, 1), (1, 2), (2, 3), (0, 3)] >>> structured_sampler = dimod.StructureComposite(base_sampler, node_list, edge_list) ... >>> linear = {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0} >>> quadratic = {(0, 1): 1.0, (1, 2): 1.0, (0, 3): 1.0, (2, 3): 1.0} >>> bqm = dimod.BinaryQuadraticModel(linear, quadratic, 1.0, dimod.Vartype.SPIN) ... >>> response = structured_sampler.sample(bqm) >>> response.first.energy 1.0
The next part of the example tries giving the composed sampler a nonsquare model:
>>> del quadratic[(0, 1)] >>> quadratic[(0, 2)] = 1.0 >>> bqm = dimod.BinaryQuadraticModel(linear, quadratic, 1.0, dimod.Vartype.SPIN) ... >>> try: response = structured_sampler.sample(bqm) ... except dimod.BinaryQuadraticModelStructureError as details: ... print(details) ... given bqm does not match the sampler's structure
Properties¶
The child sampler. 

A dict where keys are the keyword parameters accepted by the sampler methods and values are lists of the properties relevent to each parameter. 

A dict containing any additional information about the sampler. 
Methods¶

Sample from the binary quadratic model. 

Sample from an Ising model using the implemented sample method. 

Sample from a QUBO using the implemented sample method. 
Tracking Composite¶
A composite that tracks inputs and outputs.
Class¶
 class TrackingComposite(child, copy=False)[source]¶
Composite that tracks inputs and outputs for debugging and testing.
 Parameters
child (
dimod.Sampler
) – A dimod sampler.copy (bool, optional, default=False) – If True, the inputs/outputs are copied (with
copy.deepcopy()
) before they are stored. This is useful if the child sampler mutates the values.
Examples
>>> sampler = dimod.TrackingComposite(dimod.RandomSampler()) >>> sampleset = sampler.sample_ising({'a': 1}, {('a', 'b'): 1}, ... num_reads=5) >>> sampler.input OrderedDict([('h', {'a': 1}), ('J', {('a', 'b'): 1}), ('num_reads', 5)]) >>> sampleset == sampler.output True
If we make additional calls to the sampler, the most recent input/output are stored in
input
andoutput
respectively. However, all are tracked ininputs
andoutputs
.>>> sampleset = sampler.sample_qubo({('a', 'b'): 1}) >>> sampler.input OrderedDict([('Q', {('a', 'b'): 1})]) >>> sampler.inputs [OrderedDict([('h', {'a': 1}), ('J', {('a', 'b'): 1}), ('num_reads', 5)]), OrderedDict([('Q', {('a', 'b'): 1})])]
In the case that you want to nest the tracking composite, there are two patterns for retrieving the data
>>> from dimod import ScaleComposite, TrackingComposite, ExactSolver ... >>> sampler = ScaleComposite(TrackingComposite(ExactSolver())) >>> sampler.child.inputs # empty because we haven't called sample []
>>> intermediate_sampler = TrackingComposite(ExactSolver()) >>> sampler = ScaleComposite(intermediate_sampler) >>> intermediate_sampler.inputs []
Properties¶
The most recent input to any sampling method. 

All of the inputs to any sampling methods. 

The most recent output of any sampling method. 

All of the outputs from any sampling methods. 

A dict where keys are the keyword parameters accepted by the sampler methods and values are lists of the properties relevent to each parameter. 

A dict containing any additional information about the sampler. 
Methods¶
Clear all the inputs/outputs. 


Sample from the child sampler and store the given inputs/outputs. 

Sample from the child sampler and store the given inputs/outputs. 

Sample from the child sampler and store the given inputs/outputs. 
Truncate Composite¶
A composite that truncates the response based on options provided by the user.
Class¶
 class TruncateComposite(child_sampler, n, sorted_by='energy', aggregate=False)[source]¶
Composite to truncate the returned samples
Inherits from
dimod.ComposedSampler
.Postprocessing is expensive and sometimes one might want to only treat the lowest energy samples. This composite layer allows one to preselect the samples within a multicomposite pipeline
 Parameters
child_sampler (
dimod.Sampler
) – A dimod samplern (int) – Maximum number of rows in the returned sample set.
sorted_by (str/None, optional, default='energy') – Selects the record field used to sort the samples before truncating. Note that sample order is maintained in the underlying array.
aggregate (bool, optional, default=False) – If True, aggregate the samples before truncating.
Note
If aggregate is True
SampleSet.record.num_occurrences
are accumulated but no other fields are.
Properties¶
The child sampler. 

List of child samplers that that are used by this composite. 

A dict where keys are the keyword parameters accepted by the sampler methods and values are lists of the properties relevent to each parameter. 

A dict containing any additional information about the sampler. 
Methods¶

Sample from the problem provided by BQM and truncate output. 

Sample from an Ising model using the implemented sample method. 

Sample from a QUBO using the implemented sample method. 