dwave-preprocessing 0.6.11__cp314-cp314t-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. dwave/preprocessing/__init__.py +41 -0
  2. dwave/preprocessing/composites/__init__.py +34 -0
  3. dwave/preprocessing/composites/clip.py +116 -0
  4. dwave/preprocessing/composites/connected_components.py +115 -0
  5. dwave/preprocessing/composites/fix_variables.py +155 -0
  6. dwave/preprocessing/composites/scale.py +146 -0
  7. dwave/preprocessing/composites/spin_reversal_transform.py +265 -0
  8. dwave/preprocessing/cyfix_variables.cp314t-win_amd64.pyd +0 -0
  9. dwave/preprocessing/cyfix_variables.cpp +29650 -0
  10. dwave/preprocessing/cyfix_variables.pyx +58 -0
  11. dwave/preprocessing/include/dwave/exceptions.hpp +28 -0
  12. dwave/preprocessing/include/dwave/flags.hpp +63 -0
  13. dwave/preprocessing/include/dwave/presolve.hpp +91 -0
  14. dwave/preprocessing/include/dwave-preprocessing/fix_variables.hpp +119 -0
  15. dwave/preprocessing/include/dwave-preprocessing/helper_data_structures.hpp +152 -0
  16. dwave/preprocessing/include/dwave-preprocessing/helper_graph_algorithms.hpp +476 -0
  17. dwave/preprocessing/include/dwave-preprocessing/implication_network.hpp +690 -0
  18. dwave/preprocessing/include/dwave-preprocessing/mapping_policy.hpp +124 -0
  19. dwave/preprocessing/include/dwave-preprocessing/posiform_info.hpp +384 -0
  20. dwave/preprocessing/include/dwave-preprocessing/push_relabel.hpp +775 -0
  21. dwave/preprocessing/libcpp.pxd +59 -0
  22. dwave/preprocessing/lower_bounds.py +94 -0
  23. dwave/preprocessing/presolve/__init__.pxd +13 -0
  24. dwave/preprocessing/presolve/__init__.py +16 -0
  25. dwave/preprocessing/presolve/cypresolve.cp314t-win_amd64.pyd +0 -0
  26. dwave/preprocessing/presolve/cypresolve.cpp +38640 -0
  27. dwave/preprocessing/presolve/cypresolve.pxd +45 -0
  28. dwave/preprocessing/presolve/cypresolve.pyi +50 -0
  29. dwave/preprocessing/presolve/cypresolve.pyx +340 -0
  30. dwave/preprocessing/presolve/exceptions.py +21 -0
  31. dwave/preprocessing/presolve/pypresolve.py +173 -0
  32. dwave/preprocessing/src/exceptions.cpp +21 -0
  33. dwave/preprocessing/src/presolve.cpp +105 -0
  34. dwave/preprocessing/src/presolveimpl.hpp +1060 -0
  35. dwave_preprocessing-0.6.11.dist-info/DELVEWHEEL +2 -0
  36. dwave_preprocessing-0.6.11.dist-info/METADATA +108 -0
  37. dwave_preprocessing-0.6.11.dist-info/RECORD +41 -0
  38. dwave_preprocessing-0.6.11.dist-info/WHEEL +5 -0
  39. dwave_preprocessing-0.6.11.dist-info/licenses/LICENSE +203 -0
  40. dwave_preprocessing-0.6.11.dist-info/top_level.txt +1 -0
  41. dwave_preprocessing.libs/msvcp140-0f885b509a685d2bbfa652fed26b5fb3.dll +0 -0
@@ -0,0 +1,41 @@
1
+ # Copyright 2021 D-Wave Systems Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+
16
+ # start delvewheel patch
17
+ def _delvewheel_patch_1_11_2():
18
+ import os
19
+ if os.path.isdir(libs_dir := os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, 'dwave_preprocessing.libs'))):
20
+ os.add_dll_directory(libs_dir)
21
+
22
+
23
+ _delvewheel_patch_1_11_2()
24
+ del _delvewheel_patch_1_11_2
25
+ # end delvewheel patch
26
+
27
+ __version__ = '0.6.11'
28
+
29
+ from dwave.preprocessing import *
30
+ import dwave.preprocessing.composites
31
+ from dwave.preprocessing.composites import *
32
+ import dwave.preprocessing.lower_bounds
33
+ from dwave.preprocessing.lower_bounds import *
34
+
35
+ from dwave.preprocessing.presolve import *
36
+
37
+
38
+ def get_include() -> str:
39
+ """Return the directory with dwave-preprocessing's header files."""
40
+ import os.path
41
+ return os.path.join(os.path.dirname(__file__), 'include')
@@ -0,0 +1,34 @@
1
+ # Copyright 2021 D-Wave Systems Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ """
16
+ The *dwave-preprocessing* package includes several composites.
17
+
18
+ .. versionadded:: 0.6.8
19
+ Support for context manager protocol and
20
+ :meth:`~dimod.core.composite.Composite.close` method.
21
+
22
+ Context-manager usage pattern is recommended for samplers and composites
23
+ that allocate scope-bound resources, such as
24
+ :class:`~dwave.system.samplers.DWaveSampler`.
25
+
26
+ >>> with ClipComposite(DWaveSampler()) as sampler: # doctest: +SKIP
27
+ ... sampler.sample(...)
28
+ """
29
+
30
+ from dwave.preprocessing.composites.clip import *
31
+ from dwave.preprocessing.composites.connected_components import *
32
+ from dwave.preprocessing.composites.fix_variables import *
33
+ from dwave.preprocessing.composites.scale import *
34
+ from dwave.preprocessing.composites.spin_reversal_transform import *
@@ -0,0 +1,116 @@
1
+ # Copyright 2021 D-Wave Systems Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ from dimod.core.composite import ComposedSampler
16
+ from dimod.sampleset import SampleSet
17
+
18
+ __all__ = ['ClipComposite']
19
+
20
+ # versionadded (0.6.8) to the __init__ file for context manager
21
+
22
+ class ClipComposite(ComposedSampler):
23
+ """Composite to clip variables of a problem.
24
+
25
+ Clips the variables of a binary quadratic model (BQM) and modifies linear
26
+ and quadratic terms accordingly.
27
+
28
+ Args:
29
+ sampler (:class:`dimod.Sampler`):
30
+ A dimod sampler.
31
+
32
+ Examples:
33
+ This example uses :class:`.ClipComposite` to instantiate a
34
+ composed sampler that submits a simple Ising problem to a sampler.
35
+ The composed sampler clips linear and quadratic biases as
36
+ indicated by options.
37
+
38
+ >>> from dimod import ExactSolver
39
+ >>> from dwave.preprocessing.composites import ClipComposite
40
+ >>> h = {'a': -4.0, 'b': -4.0}
41
+ >>> J = {('a', 'b'): 3.2}
42
+ >>> sampler = ClipComposite(ExactSolver())
43
+ >>> response = sampler.sample_ising(h, J, lower_bound=-2.0, upper_bound=2.0)
44
+
45
+ """
46
+
47
+ def __init__(self, child_sampler):
48
+ self._children = [child_sampler]
49
+
50
+ @property
51
+ def children(self):
52
+ return self._children
53
+
54
+ @property
55
+ def parameters(self):
56
+ param = self.child.parameters.copy()
57
+ param.update({'lower_bound': [], 'upper_bound': []})
58
+ return param
59
+
60
+ @property
61
+ def properties(self):
62
+ return {'child_properties': self.child.properties.copy()}
63
+
64
+ def sample(self, bqm, *, lower_bound=None, upper_bound=None, **parameters):
65
+ """Clip and sample from the provided binary quadratic model.
66
+
67
+ If lower_bound and upper_bound are given variables with value above or below are clipped.
68
+
69
+ Args:
70
+ bqm (:class:`dimod.BinaryQuadraticModel`):
71
+ Binary quadratic model to be sampled from.
72
+
73
+ lower_bound (number):
74
+ Value by which to clip the variables from below.
75
+
76
+ upper_bound (number):
77
+ Value by which to clip the variables from above.
78
+
79
+ **parameters:
80
+ Parameters for the sampling method, specified by the child sampler.
81
+
82
+ Returns:
83
+ :class:`dimod.SampleSet`
84
+
85
+ """
86
+ child = self.child
87
+ bqm_copy = _clip_bqm(bqm, lower_bound, upper_bound)
88
+ response = child.sample(bqm_copy, **parameters)
89
+
90
+ return SampleSet.from_samples_bqm(response, bqm, info=response.info)
91
+
92
+
93
+ def _clip_bqm(bqm, lower_bound, upper_bound):
94
+ """Helper function for clipping a bqm."""
95
+
96
+ bqm_copy = bqm.copy()
97
+ if lower_bound is not None:
98
+ linear = bqm_copy.linear
99
+ for k, v in linear.items():
100
+ if v < lower_bound:
101
+ linear[k] = lower_bound
102
+ quadratic = bqm_copy.quadratic
103
+ for k, v in quadratic.items():
104
+ if v < lower_bound:
105
+ quadratic[k] = lower_bound
106
+
107
+ if upper_bound is not None:
108
+ linear = bqm_copy.linear
109
+ for k, v in linear.items():
110
+ if v > upper_bound:
111
+ linear[k] = upper_bound
112
+ quadratic = bqm_copy.quadratic
113
+ for k, v in quadratic.items():
114
+ if v > upper_bound:
115
+ quadratic[k] = upper_bound
116
+ return bqm_copy
@@ -0,0 +1,115 @@
1
+ # Copyright 2021 D-Wave Systems Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ from dimod.core.composite import ComposedSampler
16
+ from dimod.sampleset import SampleSet, append_variables
17
+ from dimod.traversal import connected_components
18
+
19
+ __all__ = ['ConnectedComponentsComposite']
20
+
21
+ # versionadded (0.6.8) to the __init__ file for context manager
22
+
23
+ class ConnectedComponentsComposite(ComposedSampler):
24
+ """Composite to decompose a problem to the connected components
25
+ and solve each.
26
+
27
+ Connected components of a binary quadratic model (BQM) graph are computed
28
+ (if not provided), and each subproblem is passed to the child sampler.
29
+ Returned samples from each child sampler are merged. Only the best solution
30
+ of each response is selected and merged with others (i.e. this composite
31
+ returns a single solution).
32
+
33
+ Args:
34
+ sampler (:class:`dimod.Sampler`):
35
+ A dimod sampler
36
+
37
+ Examples:
38
+ This example uses :class:`.ConnectedComponentsComposite` to solve a simple
39
+ Ising problem that can be separated into two components. This small example
40
+ uses :class:`dimod.ExactSolver` and is just illustrative.
41
+
42
+ >>> from dimod import ExactSolver
43
+ >>> from dwave.preprocessing.composites import ConnectedComponentsComposite
44
+ >>> h = {}
45
+ >>> J1 = {(1, 2): -1.0, (2, 3): 2.0, (3, 4): 3.0}
46
+ >>> J2 = {(12, 13): 6}
47
+ >>> sampler = ExactSolver()
48
+ >>> sampler_ccc = ConnectedComponentsComposite(sampler)
49
+ >>> e1 = sampler.sample_ising(h, J1).first.energy
50
+ >>> e2 = sampler.sample_ising(h, J2).first.energy
51
+ >>> e_ccc = sampler_ccc.sample_ising(h, {**J1, **J2}).first.energy
52
+ >>> e_ccc == e1 + e2
53
+ True
54
+
55
+ """
56
+
57
+ def __init__(self, child_sampler):
58
+ self._children = [child_sampler]
59
+
60
+ @property
61
+ def children(self):
62
+ return self._children
63
+
64
+ @property
65
+ def parameters(self):
66
+ params = self.child.parameters.copy()
67
+ return params
68
+
69
+ @property
70
+ def properties(self):
71
+ return {'child_properties': self.child.properties.copy()}
72
+
73
+ def sample(self, bqm, *, components=None, **parameters):
74
+ """Sample from the provided binary quadratic model.
75
+
76
+ Args:
77
+ bqm (:class:`dimod.BinaryQuadraticModel`):
78
+ Binary quadratic model to be sampled from.
79
+
80
+ components (list(set)):
81
+ A list of disjoint set of variables that fully partition the variables
82
+
83
+ **parameters:
84
+ Parameters for the sampling method, specified by the child sampler.
85
+
86
+ Returns:
87
+ :class:`dimod.SampleSet`
88
+
89
+ """
90
+
91
+ # solve the problem on the child system
92
+ child = self.child
93
+ variables = bqm.variables
94
+ if components is None:
95
+ components = list(connected_components(bqm))
96
+ if isinstance(components, set):
97
+ components = [components]
98
+ sampleset = None
99
+ fixed_value = min(bqm.vartype.value)
100
+ for component in components:
101
+ bqm_copy = bqm.copy()
102
+ bqm_copy.fix_variables({i: fixed_value for i in (variables - component)})
103
+ if sampleset is None:
104
+ # here .truncate(1) is used to pick the best solution only. The other options
105
+ # for future development is to combine all sample with all.
106
+ # This way you'd get the same behaviour as the ExactSolver
107
+ sampleset = child.sample(bqm_copy, **parameters).truncate(1)
108
+ else:
109
+ sampleset = append_variables(sampleset.truncate(1), child.sample(bqm_copy, **parameters).truncate(1))
110
+
111
+ if sampleset is None:
112
+ return SampleSet.from_samples_bqm({}, bqm)
113
+ else:
114
+ return SampleSet.from_samples_bqm(sampleset, bqm)
115
+
@@ -0,0 +1,155 @@
1
+ # Copyright 2021 D-Wave Systems Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ import numpy as np
16
+ import warnings
17
+
18
+ from dimod.binary.binary_quadratic_model import as_bqm
19
+ from dimod.core.composite import ComposedSampler
20
+ from dimod.sampleset import SampleSet, append_variables
21
+
22
+ from dwave.preprocessing.lower_bounds import roof_duality
23
+
24
+ __all__ = ['FixVariablesComposite']
25
+
26
+ # versionadded (0.6.8) to the __init__ file for context manager
27
+
28
+ class FixVariablesComposite(ComposedSampler):
29
+ """Composite to fix variables of a problem to provided.
30
+
31
+ Fixes variables of a binary quadratic model (BQM) and modifies linear and
32
+ quadratic terms accordingly. Returned samples include the fixed variable.
33
+
34
+ Args:
35
+ child_sampler (:class:`dimod.Sampler`):
36
+ A dimod sampler
37
+
38
+ algorithm (str, optional, default='explicit'):
39
+ Determines how ``fixed_variables`` are found.
40
+
41
+ 'explicit': ``fixed_variables`` should be passed in a call to
42
+ `.sample()`. If not, no fixing occurs and the problem is directly
43
+ passed to the child sampler.
44
+
45
+ 'roof_duality': Roof duality algorithm is used to find ``fixed_variables``.
46
+ ``strict`` may be passed in a call to `.sample()` to determine what
47
+ variables the algorithm will fix. For details, see
48
+ :func:`~dwave.preprocessing.lower_bounds.roof_duality`.
49
+
50
+ Examples:
51
+ This example uses the :class:`.FixVariablesComposite` to instantiate a
52
+ composed sampler that submits a simple Ising problem to a sampler.
53
+ The composed sampler fixes a variable and modifies linear and quadratic
54
+ biases accordingly.
55
+
56
+ >>> from dimod import ExactSolver
57
+ >>> from dwave.preprocessing.composites import FixVariablesComposite
58
+ >>> h = {1: -1.3, 4: -0.5}
59
+ >>> J = {(1, 4): -0.6}
60
+ >>> sampler = FixVariablesComposite(ExactSolver())
61
+ >>> sampleset = sampler.sample_ising(h, J, fixed_variables={1: -1})
62
+
63
+ This next example involves the same problem but calculates ``fixed_variables``
64
+ using the 'roof_duality' ``algorithm``.
65
+
66
+ >>> sampler = FixVariablesComposite(ExactSolver(), algorithm='roof_duality')
67
+ >>> sampleset = sampler.sample_ising(h, J, strict=False)
68
+
69
+ """
70
+
71
+ def __init__(self, child_sampler, *, algorithm='explicit'):
72
+ self._children = [child_sampler]
73
+ self.algorithm = algorithm
74
+
75
+ self._parameters = self.child.parameters.copy()
76
+
77
+ if self.algorithm == 'explicit':
78
+ self._parameters['fixed_variables'] = []
79
+ elif self.algorithm == 'roof_duality':
80
+ self._parameters['strict'] = []
81
+ else:
82
+ raise ValueError("Unknown algorithm: {}".format(algorithm))
83
+
84
+ @property
85
+ def children(self):
86
+ return self._children
87
+
88
+ @property
89
+ def parameters(self):
90
+ return self._parameters
91
+
92
+ @property
93
+ def properties(self):
94
+ return {'child_properties': self.child.properties.copy()}
95
+
96
+ def sample(self, bqm, **parameters):
97
+ """Sample from the provided binary quadratic model.
98
+
99
+ Args:
100
+ bqm (:class:`dimod.BinaryQuadraticModel`):
101
+ Binary quadratic model to be sampled from.
102
+
103
+ fixed_variables (dict, optional, default=None):
104
+ A dictionary of variable assignments used when ``self.algorithm``
105
+ is 'explicit'.
106
+
107
+ strict (bool, optional, default=True):
108
+ Only used if ``self.algorithm`` is 'roof_duality'. If True, only
109
+ fixes variables for which assignments are true for all minimizing
110
+ points (strong persistency). If False, also fixes variables for
111
+ which the assignments are true for some but not all minimizing
112
+ points (weak persistency).
113
+
114
+ **parameters:
115
+ Parameters for the sampling method, specified by the child sampler.
116
+
117
+ Returns:
118
+ :class:`dimod.SampleSet`
119
+
120
+ """
121
+
122
+ if self.algorithm == 'explicit':
123
+ fixed_variables = parameters.pop('fixed_variables', None)
124
+ if fixed_variables is None:
125
+ msg = ("No fixed_variables passed in when algorithm is 'explicit'. "
126
+ "Passing problem to child sampler without fixing.")
127
+ warnings.warn(msg)
128
+ return self.child.sample(bqm, **parameters)
129
+ elif self.algorithm == 'roof_duality':
130
+ _, fixed_variables = roof_duality(bqm, strict=parameters.pop('strict', True))
131
+
132
+ # make sure that we're shapeable and that we have a BQM we can mutate
133
+ bqm_copy = as_bqm(bqm, copy=True)
134
+
135
+ bqm_copy.fix_variables(fixed_variables)
136
+
137
+ sampleset = self.child.sample(bqm_copy, **parameters)
138
+
139
+ def _hook(sampleset):
140
+ # make RoofDualityComposite non-blocking
141
+
142
+ if sampleset.variables:
143
+ if len(sampleset):
144
+ return append_variables(sampleset, fixed_variables)
145
+ else:
146
+ return sampleset.from_samples_bqm((np.empty((0, len(bqm))),
147
+ bqm.variables), bqm=bqm)
148
+
149
+ # there are only fixed variables, make sure that the correct number
150
+ # of samples are returned
151
+ samples = [fixed_variables]*max(len(sampleset), 1)
152
+
153
+ return sampleset.from_samples_bqm(samples, bqm=bqm)
154
+
155
+ return SampleSet.from_future(sampleset, _hook)
@@ -0,0 +1,146 @@
1
+ # Copyright 2021 D-Wave Systems Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ from dimod.core.composite import ComposedSampler
16
+ from dimod.decorators import nonblocking_sample_method
17
+ from dimod.sampleset import SampleSet
18
+
19
+ __all__ = ['ScaleComposite']
20
+
21
+ # versionadded (0.6.8) to the __init__ file for context manager
22
+
23
+ class ScaleComposite(ComposedSampler):
24
+ """Composite that scales variables of a problem.
25
+
26
+ Scales the variables of a binary quadratic model (BQM) and modifies linear
27
+ and quadratic terms accordingly.
28
+
29
+ Args:
30
+ sampler (:class:`dimod.Sampler`):
31
+ A dimod sampler.
32
+
33
+ Examples:
34
+ This example uses :class:`.ScaleComposite` to instantiate a
35
+ composed sampler that submits a simple Ising problem to a sampler.
36
+ The composed sampler scales linear biases, quadratic biases, and
37
+ offset as indicated by options.
38
+
39
+ >>> from dimod import ExactSolver
40
+ >>> from dwave.preprocessing.composites import ScaleComposite
41
+ >>> h = {'a': -4.0, 'b': -4.0}
42
+ >>> J = {('a', 'b'): 3.2}
43
+ >>> sampler = ScaleComposite(ExactSolver())
44
+ >>> response = sampler.sample_ising(h, J, scalar=0.5,
45
+ ... ignored_interactions=[('a','b')])
46
+
47
+ """
48
+
49
+ def __init__(self, child_sampler):
50
+ self._children = [child_sampler]
51
+
52
+ @property
53
+ def children(self):
54
+ return self._children
55
+
56
+ @property
57
+ def parameters(self):
58
+ param = self.child.parameters.copy()
59
+ param.update({'scalar': [],
60
+ 'bias_range': [],
61
+ 'quadratic_range': [],
62
+ 'ignored_variables': [],
63
+ 'ignored_interactions': [],
64
+ 'ignore_offset': []})
65
+ return param
66
+
67
+ @property
68
+ def properties(self):
69
+ return {'child_properties': self.child.properties.copy()}
70
+
71
+ @nonblocking_sample_method
72
+ def sample(self, bqm, *, scalar=None, bias_range=1, quadratic_range=None,
73
+ ignored_variables=None, ignored_interactions=None,
74
+ ignore_offset=False, **parameters):
75
+ """Scale and sample from the provided binary quadratic model.
76
+
77
+ If ``scalar`` is not given, the problem is scaled based on bias and
78
+ quadratic ranges. See :meth:`.BinaryQuadraticModel.scale` and
79
+ :meth:`.BinaryQuadraticModel.normalize`
80
+
81
+ Args:
82
+ bqm (:class:`dimod.BinaryQuadraticModel`):
83
+ Binary quadratic model to be sampled from.
84
+
85
+ scalar (number):
86
+ Value by which to scale the energy range of the binary
87
+ quadratic model. Overrides `bias_range` and `quadratic_range`.
88
+
89
+ bias_range (number/pair, default=1):
90
+ Value/range by which to normalize the all the biases, or if
91
+ `quadratic_range` is provided, just the linear biases.
92
+ Overridden by `scalar`.
93
+
94
+ quadratic_range (number/pair):
95
+ Value/range by which to normalize the quadratic biases.
96
+ Overridden by `scalar`.
97
+
98
+ ignored_variables (iterable, optional):
99
+ Biases associated with these variables are not scaled.
100
+
101
+ ignored_interactions (iterable[tuple], optional):
102
+ As an iterable of 2-tuples. Biases associated with these
103
+ interactions are not scaled.
104
+
105
+ ignore_offset (bool, default=False):
106
+ If True, the offset is not scaled.
107
+
108
+ **parameters:
109
+ Parameters for the sampling method, specified by the child
110
+ sampler.
111
+
112
+ Returns:
113
+ :class:`dimod.SampleSet`
114
+
115
+ """
116
+ original_bqm = bqm
117
+ bqm = bqm.copy() # we're going to be scaling
118
+
119
+ if scalar is not None:
120
+ bqm.scale(scalar,
121
+ ignored_variables=ignored_variables,
122
+ ignored_interactions=ignored_interactions,
123
+ ignore_offset=ignore_offset)
124
+ else:
125
+ scalar = bqm.normalize(bias_range, quadratic_range,
126
+ ignored_variables=ignored_variables,
127
+ ignored_interactions=ignored_interactions,
128
+ ignore_offset=ignore_offset)
129
+
130
+ if scalar == 0:
131
+ raise ValueError('scalar must be non-zero')
132
+
133
+ sampleset = self.child.sample(bqm, **parameters)
134
+
135
+ yield sampleset # so that SampleSet.done() works
136
+
137
+ if not (ignored_variables or ignored_interactions or ignore_offset):
138
+ # we just need to scale back and don't need to worry about
139
+ # the stuff we ignored
140
+ sampleset.record.energy *= 1 / scalar
141
+ else:
142
+ sampleset.record.energy = original_bqm.energies(sampleset)
143
+
144
+ sampleset.info.update(scalar=scalar)
145
+
146
+ yield sampleset