/
test_sampler.py
342 lines (267 loc) · 11.9 KB
/
test_sampler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# Copyright 2018 D-Wave Systems Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# ================================================================================================
import concurrent.futures
import unittest
import numpy as np
import dimod
class TestSamplerClass(unittest.TestCase):
"""Tests for the template Sampler class"""
def test_instantiation_base_class(self):
with self.assertRaises(TypeError):
dimod.Sampler()
def test_instantiation_missing_properties(self):
# overwrite sample and parameters
class Dummy(dimod.Sampler):
def sample(self, bqm):
# just override
pass
@property
def parameters(self):
return {}
# @property
# def properties(self):
# return {}
with self.assertRaises(TypeError):
Dummy()
def test_instantiation_missing_parameters(self):
# overwrite sample and parameters
class Dummy(dimod.Sampler):
def sample(self, bqm):
# just override
pass
# @property
# def parameters(self):
# return {}
@property
def properties(self):
return {}
with self.assertRaises(TypeError):
Dummy()
def test_instantiation_missing_sample(self):
# overwrite sample and parameters
class Dummy(dimod.Sampler):
# def sample(self, bqm):
# # just override
# pass
@property
def parameters(self):
return {}
@property
def properties(self):
return {}
with self.assertRaises(TypeError):
Dummy()
def test_instantiation_overwrite_sample(self):
class Dummy(dimod.Sampler):
def sample(self, bqm):
# just override
pass
@property
def parameters(self):
return {}
@property
def properties(self):
return {}
sampler = Dummy()
self.assertTrue(hasattr(sampler, 'sample'),
"sampler must have a 'sample' method")
self.assertTrue(callable(sampler.sample),
"sampler must have a 'sample' method")
self.assertTrue(hasattr(sampler, 'sample_ising'),
"sampler must have a 'sample_ising' method")
self.assertTrue(callable(sampler.sample_ising),
"sampler must have a 'sample_ising' method")
self.assertTrue(hasattr(sampler, 'sample_qubo'),
"sampler must have a 'sample_qubo' method")
self.assertTrue(callable(sampler.sample_qubo),
"sampler must have a 'sample_qubo' method")
self.assertTrue(hasattr(sampler, 'parameters'),
"sampler must have a 'parameters' property")
self.assertFalse(callable(sampler.parameters),
"sampler must have a 'parameters' property")
self.assertTrue(hasattr(sampler, 'properties'),
"sampler must have a 'properties' property")
self.assertFalse(callable(sampler.properties),
"sampler must have a 'properties' property")
def test_instantiation_overwrite_sample_ising(self):
class Dummy(dimod.Sampler):
def sample_ising(self, h, J):
# just override
pass
@property
def parameters(self):
return {}
@property
def properties(self):
return {}
sampler = Dummy()
self.assertTrue(hasattr(sampler, 'sample'),
"sampler must have a 'sample' method")
self.assertTrue(callable(sampler.sample),
"sampler must have a 'sample' method")
self.assertTrue(hasattr(sampler, 'sample_ising'),
"sampler must have a 'sample_ising' method")
self.assertTrue(callable(sampler.sample_ising),
"sampler must have a 'sample_ising' method")
self.assertTrue(hasattr(sampler, 'sample_qubo'),
"sampler must have a 'sample_qubo' method")
self.assertTrue(callable(sampler.sample_qubo),
"sampler must have a 'sample_qubo' method")
self.assertTrue(hasattr(sampler, 'parameters'),
"sampler must have a 'parameters' property")
self.assertFalse(callable(sampler.parameters),
"sampler must have a 'parameters' property")
self.assertTrue(hasattr(sampler, 'properties'),
"sampler must have a 'properties' property")
self.assertFalse(callable(sampler.properties),
"sampler must have a 'properties' property")
def test_instantiation_overwrite_sample_qubo(self):
class Dummy(dimod.Sampler):
def sample_qubo(self, Q):
# just override
pass
@property
def parameters(self):
return {}
@property
def properties(self):
return {}
sampler = Dummy()
self.assertTrue(hasattr(sampler, 'sample'),
"sampler must have a 'sample' method")
self.assertTrue(callable(sampler.sample),
"sampler must have a 'sample' method")
self.assertTrue(hasattr(sampler, 'sample_ising'),
"sampler must have a 'sample_ising' method")
self.assertTrue(callable(sampler.sample_ising),
"sampler must have a 'sample_ising' method")
self.assertTrue(hasattr(sampler, 'sample_qubo'),
"sampler must have a 'sample_qubo' method")
self.assertTrue(callable(sampler.sample_qubo),
"sampler must have a 'sample_qubo' method")
self.assertTrue(hasattr(sampler, 'parameters'),
"sampler must have a 'parameters' property")
self.assertFalse(callable(sampler.parameters),
"sampler must have a 'parameters' property")
self.assertTrue(hasattr(sampler, 'properties'),
"sampler must have a 'properties' property")
self.assertFalse(callable(sampler.properties),
"sampler must have a 'properties' property")
def test_instantiation_overwrite_sample_ising_and_call_sample(self):
class Dummy(dimod.Sampler):
def sample_ising(self, h, J):
return dimod.SampleSet.from_samples([[-1, 1]], energy=[0.05], vartype=dimod.SPIN)
@property
def parameters(self):
return {}
@property
def properties(self):
return {}
sampler = Dummy()
bqm = dimod.BinaryQuadraticModel({0: 0.1, 1: -0.3}, {(0, 1): -1}, 0.0, dimod.BINARY)
resp = sampler.sample(bqm)
expected_resp = dimod.SampleSet.from_samples([[0, 1]], energy=[-0.3], vartype=dimod.BINARY)
np.testing.assert_almost_equal(resp.record.sample, expected_resp.record.sample)
np.testing.assert_almost_equal(resp.record.energy, expected_resp.record.energy)
def test_instantiation_overwrite_sample_qubo_and_call_sample(self):
class Dummy(dimod.Sampler):
def sample_qubo(self, Q):
return dimod.SampleSet.from_samples([[0, 1]], energy=[1.4], vartype=dimod.BINARY)
@property
def parameters(self):
return {}
@property
def properties(self):
return {}
sampler = Dummy()
bqm = dimod.BinaryQuadraticModel({0: 0.1, 1: -0.3}, {(0, 1): -1}, 0.1, dimod.SPIN)
resp = sampler.sample(bqm)
expected_resp = dimod.SampleSet.from_samples([[-1, 1]], energy=[.7], vartype=dimod.SPIN)
np.testing.assert_almost_equal(resp.record.sample, expected_resp.record.sample)
np.testing.assert_almost_equal(resp.record.energy, expected_resp.record.energy)
def test_sampler_can_return_integer_energy_values(self):
class Dummy(dimod.Sampler):
def sample_qubo(self, Q):
return dimod.SampleSet.from_samples([[1]], energy=[-3], vartype=dimod.BINARY)
@property
def parameters(self):
return {}
@property
def properties(self):
return {}
sampler = Dummy()
bqm = dimod.BinaryQuadraticModel({0: -3}, {}, 0, dimod.BINARY)
resp = sampler.sample(bqm)
expected_resp = dimod.SampleSet.from_samples([[1]], energy=[-3], vartype=dimod.BINARY)
np.testing.assert_almost_equal(resp.record.sample, expected_resp.record.sample)
np.testing.assert_almost_equal(resp.record.energy, expected_resp.record.energy)
def test_spin_bqm_to_sample_ising(self):
class CountBQM(dimod.BinaryQuadraticModel):
# should never have vartype changed
def change_vartype(self, *args, **kwargs):
raise RuntimeError
def to_qubo(self):
raise RuntimeError
class Ising(dimod.Sampler):
parameters = None
properties = None
def sample_ising(self, h, J):
bqm = dimod.BinaryQuadraticModel.from_ising(h, J)
samples = [1]*len(bqm)
return dimod.SampleSet.from_samples_bqm(samples, bqm)
sampler = Ising()
cbqm = CountBQM.from_ising({0: -3}, {(0, 1): -1.5}, offset=1.3)
sampleset = sampler.sample(cbqm)
dimod.testing.assert_response_energies(sampleset, cbqm)
def test_binary_bqm_to_sample_qubo(self):
class CountBQM(dimod.BinaryQuadraticModel):
# should never have vartype changed
def change_vartype(self, *args, **kwargs):
raise RuntimeError
def to_ising(self):
raise RuntimeError
class Qubo(dimod.Sampler):
parameters = None
properties = None
def sample_qubo(self, Q):
bqm = dimod.BinaryQuadraticModel.from_qubo(Q)
samples = [1]*len(bqm)
return dimod.SampleSet.from_samples_bqm(samples, bqm)
sampler = Qubo()
cbqm = CountBQM.from_qubo({(0, 0): -3, (1, 0): 1.5}, offset=.5)
sampleset = sampler.sample(cbqm)
dimod.testing.assert_response_energies(sampleset, cbqm)
def test_nonblocking_sample_ising(self):
class SignalException(Exception):
pass
class Mock(dimod.Sampler):
properties = {}
parameters = {}
def sample_ising(self, h, J):
future = concurrent.futures.Future()
future.set_exception(SignalException)
future.done = lambda: False # pretend to be running
return dimod.SampleSet.from_future(future)
# should not block
ss = Mock().sample_ising({}, {})
with self.assertRaises(SignalException):
ss.resolve()
ss = Mock().sample_qubo({})
with self.assertRaises(SignalException):
ss.resolve()
ss = Mock().sample(dimod.BinaryQuadraticModel({}, {}, 0.0, dimod.SPIN))
with self.assertRaises(SignalException):
ss.resolve()