Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_aggregation(self):
bqm = dimod.BinaryQuadraticModel({}, {'ab': 1}, 0, dimod.SPIN)
states = States(State.from_sample({'a': 1, 'b': -1}, bqm),
State.from_sample({'a': 1, 'b': -1}, bqm))
expected = State(
problem=bqm,
samples=dimod.SampleSet.from_samples_bqm(
{'a': 1, 'b': -1}, bqm, num_occurrences=[2]))
state = MergeSamples(aggregate=True).run(states).result()
self.assertEqual(state, expected)
def test_typical(self):
bqm = dimod.BinaryQuadraticModel.from_ising({v: -1 for v in range(100)}, {})
samples = dimod.SampleSet.from_samples_bqm(np.tril(np.ones(100)), bqm.binary)
# by default should be in reverse order
new = samples.truncate(10)
self.assertEqual(len(new), 10)
for n, sample in enumerate(new.samples()):
for v, val in sample.items():
if v > 100 - n - 1:
self.assertEqual(val, 0)
else:
self.assertEqual(val, 1)
def test_order_preservation(self):
bqm = dimod.BinaryQuadraticModel({}, {'ab': 1}, 0, dimod.SPIN)
ss1 = dimod.SampleSet.from_samples_bqm([{'a': 1, 'b': -1},
{'a': -1, 'b': 1},
{'a': 1, 'b': -1}],
bqm)
ss2 = ss1.aggregate()
target = dimod.SampleSet.from_samples_bqm([{'a': 1, 'b': -1},
{'a': -1, 'b': 1}],
bqm,
num_occurrences=[2, 1])
self.assertEqual(target, ss2)
def test_unordered(self):
bqm = dimod.BinaryQuadraticModel.from_ising({v: -1 for v in range(100)}, {})
sampleset = dimod.SampleSet.from_samples_bqm(np.triu(np.ones(100)), bqm.binary)
# `:10` but for the unordered case
self.assertEqual(sampleset.slice(10, sorted_by=None), sampleset.truncate(10, sorted_by=None))
def sample_qubo(self, Q):
bqm = dimod.BinaryQuadraticModel.from_qubo(Q)
samples = [1]*len(bqm)
return dimod.SampleSet.from_samples_bqm(samples, bqm)
def test_all_three_functional(self):
builtin = [0, 'a', [0, 'a']]
num_variables = 100
num_samples = 100
samples = 2*np.triu(np.ones((num_samples, num_variables)), -4) - 1
bqm = dimod.BinaryQuadraticModel.from_ising({v: .1*v for v in range(num_variables)}, {})
sampleset = dimod.SampleSet.from_samples_bqm(samples, bqm)
linear = {'a': -1, 4: 1, ('a', "complex key"): 3}
quadratic = {('a', 'c'): 3, ('b', 'c'): -3., ('a', 3): -1}
bqm = dimod.BinaryQuadraticModel(linear, quadratic, 3, dimod.SPIN)
obj = [builtin, sampleset, bqm]
# no encoder, uses ._asdict
new = simplejson.loads(simplejson.dumps(obj), object_hook=dimod_object_hook)
self.assertEqual(obj, new)
def setUp(self):
self.bqm = dimod.BQM.from_ising({}, {'ab': -1, 'bc': 1, 'ac': 1})
self.initial_states = [{'a': 1, 'b': 1, 'c': 1},
{'a': -1, 'b': -1, 'c': -1}]
self.initial_sampleset = dimod.SampleSet.from_samples_bqm(
self.initial_states, self.bqm)
def test_iterator(self):
# deprecated feature
bqm = dimod.BinaryQuadraticModel.from_ising({}, {'ab': -1})
sampleset = dimod.SampleSet.from_samples_bqm([{'a': -1, 'b': 1}, {'a': 1, 'b': 1}], bqm)
self.assertIsInstance(sampleset.samples(), abc.Iterator)
self.assertIsInstance(sampleset.samples(n=2), abc.Iterator)
spl = next(sampleset.samples())
self.assertEqual(spl, {'a': 1, 'b': 1})
def test_soft_num_reads(self):
"""Number of reads adapts to initial_states size, if provided."""
bqm = dimod.BinaryQuadraticModel.from_ising({}, {'ab': -1, 'bc': 1, 'ac': 1})
init = dimod.SampleSet.from_samples_bqm([{'a': 1, 'b': 1, 'c': 1},
{'a': -1, 'b': -1, 'c': -1}], bqm)
sampler = Neal()
# default num_reads == 1
self.assertEqual(len(sampler.sample(bqm)), 1)
self.assertEqual(len(sampler.sample(bqm, initial_states_generator="random")), 1)
# with initial_states, num_reads == len(initial_states)
self.assertEqual(len(sampler.sample(bqm, initial_states=init)), 2)
# ... but explicit truncation works too
self.assertEqual(len(sampler.sample(bqm, initial_states=init, num_reads=1)), 1)
# if num_reads explicitly given together with initial_states, they are expanded
self.assertEqual(len(sampler.sample(bqm, initial_states=init, num_reads=3)), 3)