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.
- dwave/__init__.py +16 -0
- dwave/preprocessing/__init__.py +42 -0
- dwave/preprocessing/composites/__init__.py +19 -0
- dwave/preprocessing/composites/clip.py +114 -0
- dwave/preprocessing/composites/connected_components.py +113 -0
- dwave/preprocessing/composites/fix_variables.py +153 -0
- dwave/preprocessing/composites/scale.py +144 -0
- dwave/preprocessing/composites/spin_reversal_transform.py +221 -0
- dwave/preprocessing/cyfix_variables.cp313-win_amd64.pyd +0 -0
- dwave/preprocessing/cyfix_variables.cpp +29498 -0
- dwave/preprocessing/cyfix_variables.pyx +58 -0
- dwave/preprocessing/include/dwave/exceptions.hpp +28 -0
- dwave/preprocessing/include/dwave/flags.hpp +63 -0
- dwave/preprocessing/include/dwave/presolve.hpp +91 -0
- dwave/preprocessing/include/dwave-preprocessing/fix_variables.hpp +119 -0
- dwave/preprocessing/include/dwave-preprocessing/helper_data_structures.hpp +152 -0
- dwave/preprocessing/include/dwave-preprocessing/helper_graph_algorithms.hpp +476 -0
- dwave/preprocessing/include/dwave-preprocessing/implication_network.hpp +690 -0
- dwave/preprocessing/include/dwave-preprocessing/mapping_policy.hpp +124 -0
- dwave/preprocessing/include/dwave-preprocessing/posiform_info.hpp +384 -0
- dwave/preprocessing/include/dwave-preprocessing/push_relabel.hpp +775 -0
- dwave/preprocessing/libcpp.pxd +59 -0
- dwave/preprocessing/lower_bounds.py +94 -0
- dwave/preprocessing/presolve/__init__.pxd +13 -0
- dwave/preprocessing/presolve/__init__.py +16 -0
- dwave/preprocessing/presolve/cypresolve.cp313-win_amd64.pyd +0 -0
- dwave/preprocessing/presolve/cypresolve.cpp +39255 -0
- dwave/preprocessing/presolve/cypresolve.pxd +45 -0
- dwave/preprocessing/presolve/cypresolve.pyi +50 -0
- dwave/preprocessing/presolve/cypresolve.pyx +340 -0
- dwave/preprocessing/presolve/exceptions.py +21 -0
- dwave/preprocessing/presolve/pypresolve.py +173 -0
- dwave/preprocessing/src/exceptions.cpp +21 -0
- dwave/preprocessing/src/presolve.cpp +105 -0
- dwave/preprocessing/src/presolveimpl.hpp +1060 -0
- dwave_preprocessing-0.6.7.dist-info/DELVEWHEEL +2 -0
- dwave_preprocessing-0.6.7.dist-info/LICENSE +203 -0
- dwave_preprocessing-0.6.7.dist-info/METADATA +114 -0
- dwave_preprocessing-0.6.7.dist-info/RECORD +42 -0
- dwave_preprocessing-0.6.7.dist-info/WHEEL +5 -0
- dwave_preprocessing-0.6.7.dist-info/top_level.txt +1 -0
- dwave_preprocessing.libs/msvcp140-c5f1b59771da57851103ca2011763553.dll +0 -0
|
@@ -0,0 +1,221 @@
|
|
|
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 typing
|
|
16
|
+
|
|
17
|
+
import dimod
|
|
18
|
+
import numpy as np
|
|
19
|
+
|
|
20
|
+
from dimod import Vartype
|
|
21
|
+
|
|
22
|
+
__all__ = ['SpinReversalTransformComposite']
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class SpinReversalTransformComposite(dimod.core.Sampler, dimod.core.Composite):
|
|
26
|
+
"""Composite for applying spin reversal transform preprocessing.
|
|
27
|
+
|
|
28
|
+
A spin-reversal transform can improve sample statistics when the
|
|
29
|
+
sampler is a physical object with asymmetries such as a QPU.
|
|
30
|
+
The technique works as follows: given an :math:`n`-variable Ising
|
|
31
|
+
problem, the composite selects a random :math:`g\in\{\pm1\}^n` and
|
|
32
|
+
transforms the problem via :math:`h_i\mapsto h_ig_i` and
|
|
33
|
+
:math:`J_{ij}\mapsto J_{ij}g_ig_j`. Solutions :math:`s` of the
|
|
34
|
+
original problem and :math:`s^\prime` of the transformed problem
|
|
35
|
+
are related by :math:`s^\prime_i=s_ig_i` and have identical energies.
|
|
36
|
+
[#km]_
|
|
37
|
+
|
|
38
|
+
.. note::
|
|
39
|
+
If you are configuring an anneal schedule, be mindful that this
|
|
40
|
+
composite does not recognize the ``initial_state`` parameter
|
|
41
|
+
used by dimod's :class:`~dwave.system.samplers.DWaveSampler` for
|
|
42
|
+
reverse annealing (composites do not generally process all keywords
|
|
43
|
+
of child samplers) and does not flip any of the configured initial
|
|
44
|
+
states.
|
|
45
|
+
|
|
46
|
+
Args:
|
|
47
|
+
sampler: A `dimod` sampler object.
|
|
48
|
+
|
|
49
|
+
seed: As passed to :func:`numpy.random.default_rng`.
|
|
50
|
+
|
|
51
|
+
Examples:
|
|
52
|
+
This example composes a dimod ExactSolver sampler with spin transforms then
|
|
53
|
+
uses it to sample an Ising problem.
|
|
54
|
+
|
|
55
|
+
>>> from dimod import ExactSolver
|
|
56
|
+
>>> from dwave.preprocessing.composites import SpinReversalTransformComposite
|
|
57
|
+
>>> base_sampler = ExactSolver()
|
|
58
|
+
>>> composed_sampler = SpinReversalTransformComposite(base_sampler)
|
|
59
|
+
... # Sample an Ising problem
|
|
60
|
+
>>> response = composed_sampler.sample_ising({'a': -0.5, 'b': 1.0}, {('a', 'b'): -1})
|
|
61
|
+
>>> response.first.sample
|
|
62
|
+
{'a': -1, 'b': -1}
|
|
63
|
+
|
|
64
|
+
References
|
|
65
|
+
----------
|
|
66
|
+
.. [#km] Andrew D. King and Catherine C. McGeoch. Algorithm engineering
|
|
67
|
+
for a quantum annealing platform. https://arxiv.org/abs/1410.2628,
|
|
68
|
+
2014.
|
|
69
|
+
|
|
70
|
+
"""
|
|
71
|
+
_children: typing.List[dimod.core.Sampler]
|
|
72
|
+
_parameters: typing.Dict[str, typing.Sequence[str]]
|
|
73
|
+
_properties: typing.Dict[str, typing.Any]
|
|
74
|
+
|
|
75
|
+
def __init__(self, child: dimod.core.Sampler, *, seed=None):
|
|
76
|
+
self._child = child
|
|
77
|
+
self.rng = np.random.default_rng(seed)
|
|
78
|
+
|
|
79
|
+
@property
|
|
80
|
+
def children(self) -> typing.List[dimod.core.Sampler]:
|
|
81
|
+
try:
|
|
82
|
+
return self._children
|
|
83
|
+
except AttributeError:
|
|
84
|
+
pass
|
|
85
|
+
|
|
86
|
+
self._children = children = [self._child]
|
|
87
|
+
return children
|
|
88
|
+
|
|
89
|
+
@property
|
|
90
|
+
def parameters(self) -> typing.Dict[str, typing.Sequence[str]]:
|
|
91
|
+
try:
|
|
92
|
+
return self._parameters
|
|
93
|
+
except AttributeError:
|
|
94
|
+
pass
|
|
95
|
+
|
|
96
|
+
self._parameters = parameters = dict(spin_reversal_variables=tuple())
|
|
97
|
+
parameters.update(self._child.parameters)
|
|
98
|
+
return parameters
|
|
99
|
+
|
|
100
|
+
@property
|
|
101
|
+
def properties(self) -> typing.Dict[str, typing.Any]:
|
|
102
|
+
try:
|
|
103
|
+
return self._properties
|
|
104
|
+
except AttributeError:
|
|
105
|
+
pass
|
|
106
|
+
|
|
107
|
+
self._properties = dict(child_properties=self._child.properties)
|
|
108
|
+
return self._properties
|
|
109
|
+
|
|
110
|
+
class _SampleSets:
|
|
111
|
+
def __init__(self, samplesets: typing.List[dimod.SampleSet]):
|
|
112
|
+
self.samplesets = samplesets
|
|
113
|
+
|
|
114
|
+
def done(self) -> bool:
|
|
115
|
+
return all(ss.done() for ss in self.samplesets)
|
|
116
|
+
|
|
117
|
+
@staticmethod
|
|
118
|
+
def _reorder_variables(sampleset: dimod.SampleSet,
|
|
119
|
+
order: dimod.variables.Variables) -> dimod.SampleSet:
|
|
120
|
+
"""Return a sampleset with the given variable order."""
|
|
121
|
+
if sampleset.variables == order:
|
|
122
|
+
return sampleset
|
|
123
|
+
|
|
124
|
+
# .index(...) is O(1) for dimod's Variables objects so this isn't too bad
|
|
125
|
+
sampleset_order = sampleset.variables
|
|
126
|
+
reorder = [sampleset_order.index(v) for v in order]
|
|
127
|
+
|
|
128
|
+
return dimod.SampleSet.from_samples(
|
|
129
|
+
(sampleset.record.sample[:, reorder], order),
|
|
130
|
+
sort_labels=False,
|
|
131
|
+
vartype=sampleset.vartype,
|
|
132
|
+
**sampleset.data_vectors,
|
|
133
|
+
)
|
|
134
|
+
|
|
135
|
+
@dimod.decorators.nonblocking_sample_method
|
|
136
|
+
def sample(self, bqm: dimod.BinaryQuadraticModel, *,
|
|
137
|
+
num_spin_reversal_transforms: int = 1,
|
|
138
|
+
**kwargs,
|
|
139
|
+
):
|
|
140
|
+
"""Sample from the binary quadratic model.
|
|
141
|
+
|
|
142
|
+
Args:
|
|
143
|
+
bqm: Binary quadratic model to be sampled from.
|
|
144
|
+
|
|
145
|
+
num_spin_reversal_transforms:
|
|
146
|
+
Number of spin reversal transform runs.
|
|
147
|
+
A value of ``0`` will not transform the problem.
|
|
148
|
+
If you specify a nonzero value, each spin reversal transform
|
|
149
|
+
will result in an independent run of the child sampler.
|
|
150
|
+
|
|
151
|
+
Returns:
|
|
152
|
+
A sample set. Note that for a sampler that returns ``num_reads`` samples,
|
|
153
|
+
the sample set will contain ``num_reads*num_spin_reversal_transforms`` samples.
|
|
154
|
+
|
|
155
|
+
Examples:
|
|
156
|
+
This example runs 100 spin reversals applied to one variable of a QUBO problem.
|
|
157
|
+
|
|
158
|
+
>>> from dimod import ExactSolver
|
|
159
|
+
>>> from dwave.preprocessing.composites import SpinReversalTransformComposite
|
|
160
|
+
>>> base_sampler = ExactSolver()
|
|
161
|
+
>>> composed_sampler = SpinReversalTransformComposite(base_sampler)
|
|
162
|
+
...
|
|
163
|
+
>>> Q = {('a', 'a'): -1, ('b', 'b'): -1, ('a', 'b'): 2}
|
|
164
|
+
>>> response = composed_sampler.sample_qubo(Q,
|
|
165
|
+
... num_spin_reversal_transforms=100)
|
|
166
|
+
>>> len(response)
|
|
167
|
+
400
|
|
168
|
+
"""
|
|
169
|
+
sampler = self._child
|
|
170
|
+
|
|
171
|
+
# No SRTs, so just pass the problem through
|
|
172
|
+
if not num_spin_reversal_transforms or not bqm.num_variables:
|
|
173
|
+
sampleset = sampler.sample(bqm, **kwargs)
|
|
174
|
+
# yield twice because we're using the @nonblocking_sample_method
|
|
175
|
+
yield sampleset # this one signals done()-ness
|
|
176
|
+
yield sampleset # this is the one actually used by the user
|
|
177
|
+
return
|
|
178
|
+
|
|
179
|
+
# we'll be modifying the BQM, so make a copy
|
|
180
|
+
bqm = bqm.copy()
|
|
181
|
+
|
|
182
|
+
# We maintain the Leap behavior that num_spin_reversal_transforms == 1
|
|
183
|
+
# corresponds to a single problem with randomly flipped variables.
|
|
184
|
+
|
|
185
|
+
# Get the SRT matrix
|
|
186
|
+
SRT = self.rng.random((num_spin_reversal_transforms, bqm.num_variables)) > .5
|
|
187
|
+
|
|
188
|
+
# Submit the problems
|
|
189
|
+
samplesets: typing.List[dimod.SampleSet] = []
|
|
190
|
+
flipped = np.zeros(bqm.num_variables, dtype=bool) # what variables are currently flipped
|
|
191
|
+
for i in range(num_spin_reversal_transforms):
|
|
192
|
+
# determine what needs to be flipped
|
|
193
|
+
transform = flipped != SRT[i, :]
|
|
194
|
+
|
|
195
|
+
# apply the transform
|
|
196
|
+
for v, flip in zip(bqm.variables, transform):
|
|
197
|
+
if flip:
|
|
198
|
+
bqm.flip_variable(v)
|
|
199
|
+
flipped[transform] = ~flipped[transform]
|
|
200
|
+
|
|
201
|
+
samplesets.append(sampler.sample(bqm, **kwargs))
|
|
202
|
+
|
|
203
|
+
# Yield a view of the samplesets that reports done()-ness
|
|
204
|
+
yield self._SampleSets(samplesets)
|
|
205
|
+
|
|
206
|
+
# Reorder the variables of all the returned samplesets to match our
|
|
207
|
+
# original BQM
|
|
208
|
+
samplesets = [self._reorder_variables(ss, bqm.variables) for ss in samplesets]
|
|
209
|
+
|
|
210
|
+
# Undo the SRTs according to vartype
|
|
211
|
+
if bqm.vartype is Vartype.BINARY:
|
|
212
|
+
for i, sampleset in enumerate(samplesets):
|
|
213
|
+
sampleset.record.sample[:, SRT[i, :]] = 1 - sampleset.record.sample[:, SRT[i, :]]
|
|
214
|
+
elif bqm.vartype is Vartype.SPIN:
|
|
215
|
+
for i, sampleset in enumerate(samplesets):
|
|
216
|
+
sampleset.record.sample[:, SRT[i, :]] *= -1
|
|
217
|
+
else:
|
|
218
|
+
raise RuntimeError("unexpected vartype")
|
|
219
|
+
|
|
220
|
+
# finally combine all samplesets together
|
|
221
|
+
yield dimod.concatenate(samplesets)
|
|
Binary file
|