Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def sample_qubo(self, Q):
return dimod.SampleSet.from_samples([[1]], energy=[-3], vartype=dimod.BINARY)
def test_3path_with_bytes(self):
samples = [[-1, -1, -1, 1], [1, 1, 1, -1]]
sampleset = dimod.SampleSet.from_samples(samples, energy=0,
vartype=dimod.SPIN)
dct = sampleset.to_serializable(use_bytes=True)
new = dimod.SampleSet.from_serializable(dct)
self.assertEqual(sampleset, new)
def test_variables_order(self):
ss0 = dimod.SampleSet.from_samples(([-1, +1], 'ab'), dimod.SPIN, energy=-1)
ss1 = dimod.SampleSet.from_samples(([-1, +1], 'ba'), dimod.SPIN, energy=-1)
ss2 = dimod.SampleSet.from_samples(([[+1, +1], [-1, -1]], 'ab'), dimod.SPIN, energy=[1, 1])
comb = dimod.concatenate((ss0, ss1, ss2))
out = dimod.SampleSet.from_samples(([[-1, +1], [+1, -1], [+1, +1], [-1, -1]], 'ab'),
dimod.SPIN, energy=[-1, -1, 1, 1])
self.assertEqual(comb, out)
np.testing.assert_array_equal(comb.record.sample, out.record.sample)
def test_from_samples_empty(self):
self.assertEqual(len(dimod.SampleSet.from_samples([], dimod.SPIN, energy=[], a=1)), 0)
self.assertEqual(len(dimod.SampleSet.from_samples(np.empty((0, 0)), dimod.SPIN, energy=[], a=1)), 0)
def test_sampleset1_append1(self):
sampleset = dimod.SampleSet.from_samples({'a': -1, 'b': 1}, dimod.SPIN, energy=0)
new = sampleset.append_variables({'c': -1, 'd': -1})
target = dimod.SampleSet.from_samples({'a': -1, 'b': 1, 'c': -1, 'd': -1}, dimod.SPIN, energy=0)
self.assertEqual(new, target)
def test_overlapping_variables(self):
sampleset = dimod.SampleSet.from_samples([{'a': -1, 'b': 1}, {'a': -1, 'b': -1}],
dimod.SPIN, energy=0)
with self.assertRaises(ValueError):
sampleset.append_variables([{'c': -1, 'd': -1, 'a': -1}])
def test_non_blocking(self):
future = concurrent.futures.Future()
sampleset = dimod.SampleSet.from_future(future)
# shouldn't block or raise
new = sampleset.change_vartype(dimod.BINARY)
future.set_result(dimod.SampleSet.from_samples({'a': -1},
dimod.SPIN,
energy=1))
np.testing.assert_array_equal(new.record.sample, [[0]])
def test_bq_response_decoding(self):
"""Answer to simple problem properly decoded."""
ss = dimod.SampleSet.from_samples(
([[0, 1], [1, 0]], 'ab'), vartype='BINARY', energy=0)
msg = dict(answer=dict(format='bq', data=ss.to_serializable()))
res = decode_bq(msg)
self.assertEqual(res.get('problem_type'), 'bqm')
self.assertEqual(res.get('sampleset'), ss)
# prepare empty result sampleset
samples = numpy.empty((num_samples, len(variables)))
# copy over samplesets, one by one, from left to right
for ss in samplesets:
ss.change_vartype(vartype)
mask = [variables.index[v] for v in ss.variables]
samples[:, mask] = ss.record.sample[:num_samples]
if bqm is None:
energies = 0
else:
energies = bqm.energies((samples, variables))
return dimod.SampleSet.from_samples((samples, variables), energy=energies, vartype=vartype)