Higher-Order Composites#
The dimod package includes several example higher-order composed samplers.
HigherOrderComposite#
- class HigherOrderComposite(child_sampler)[source]#
Convert a binary quadratic model sampler to a binary polynomial sampler.
Energies of the returned samples do not include the penalties.
- Parameters:
sampler (
dimod.Sampler
) – A dimod sampler
Example
This example uses
HigherOrderComposite
to instantiate a composed sampler that submits a simple Ising problem to a sampler. The composed sampler creates a binary quadratic model (BQM) from a higher order problem.>>> sampler = dimod.HigherOrderComposite(dimod.ExactSolver()) >>> h = {0: -0.5, 1: -0.3, 2: -0.8} >>> J = {(0, 1, 2): -1.7} >>> sampleset = sampler.sample_hising(h, J, discard_unsatisfied=True) >>> set(sampleset.first.sample.values()) == {1} True
Properties#
The child sampler. |
|
A list containing the wrapped sampler. |
|
A dict where keys are the keyword parameters accepted by the sampler methods and values are lists of the properties relevant to each parameter. |
|
A dict containing any additional information about the sampler. |
Methods#
|
Sample from the given binary polynomial. |
|
Sample from a higher-order Ising model. |
|
Sample from a higher-order unconstrained binary optimization problem. |
PolyFixedVariableComposite#
- class PolyFixedVariableComposite(child_sampler)[source]#
Composite that fixes variables of a problem.
Fixes variables of a binary polynomial and modifies linear and k-local terms accordingly. Returned samples include the fixed variable.
- Parameters:
sampler (
dimod.PolySampler
) – A dimod polynomial sampler.
Examples
This example uses
PolyFixedVariableComposite
to instantiate a composed sampler that submits a simple high-order Ising problem to a sampler. The composed sampler fixes a variable and modifies linear and k-local terms biases.>>> h = {1: -1.3, 2: 1.2, 3: -3.4, 4: -0.5} >>> J = {(1, 4): -0.6, (1, 2, 3): 0.2, (1, 2, 3, 4): -0.1} >>> poly = dimod.BinaryPolynomial.from_hising(h, J, offset=0) >>> sampler = dimod.PolyFixedVariableComposite(dimod.ExactPolySolver()) >>> sampleset = sampler.sample_poly(poly, fixed_variables={3: -1, 4: 1})
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 relevant to each parameter. |
|
A dict containing any additional information about the sampler. |
Methods#
Sample from the provided binary quadratic model. |
|
Sample from a higher-order Ising model. |
|
Sample from a higher-order unconstrained binary optimization problem. |
PolyScaleComposite#
- class PolyScaleComposite(child)[source]#
Composite to scale biases of a binary polynomial.
- Parameters:
child (
PolySampler
) – A binary polynomial sampler.
Examples
>>> linear = {'a': -4.0, 'b': -4.0} >>> quadratic = {('a', 'b'): 3.2, ('a', 'b', 'c'): 1} >>> sampler = dimod.PolyScaleComposite(dimod.HigherOrderComposite(dimod.ExactSolver())) >>> response = sampler.sample_hising(linear, quadratic, scalar=0.5, ... ignored_terms=[('a','b')])
Properties#
The child sampler. |
|
The child sampler in a list |
|
A dict where keys are the keyword parameters accepted by the sampler methods and values are lists of the properties relevant to each parameter. |
|
A dict containing any additional information about the sampler. |
Methods#
|
Scale and sample from the given binary polynomial. |
|
Sample from a higher-order Ising model. |
|
Sample from a higher-order unconstrained binary optimization problem. |
PolyTruncateComposite#
- class PolyTruncateComposite(child_sampler, n, sorted_by='energy', aggregate=False)[source]#
Composite that truncates returned samples.
Post-processing is expensive and sometimes one might want to only treat the lowest-energy samples. This composite layer allows one to pre-select the samples within a multi-composite pipeline.
- Parameters:
child_sampler (
dimod.PolySampler
) – A dimod binary polynomial sampler.n (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 relevant to each parameter. |
|
A dict containing any additional information about the sampler. |
Methods#
|
Sample from the binary polynomial and truncate output. |
|
Sample from a higher-order Ising model. |
|
Sample from a higher-order unconstrained binary optimization problem. |