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