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_ignored_offset(self):
bqm = dimod.BQM.from_ising({'a': -4.0, 'b': -4.0},
{('a', 'b'): 3.2}, 1.5)
sampler = ScaleComposite(dimod.TrackingComposite(dimod.ExactSolver()))
sampleset = sampler.sample(bqm, scalar=.5, ignore_offset=True)
# check that everything was restored properly
dimod.testing.assert_sampleset_energies(sampleset, bqm)
self.assertEqual(sampler.child.input['bqm'],
dimod.BQM.from_ising({'a': -2.0, 'b': -2.0},
{('a', 'b'): 1.6}, 1.5))
def test_empty(self):
"At least one input sample is required."
bqm = dimod.BQM.from_ising({}, {'ab': 1})
inp = State(problem=bqm, samples=None)
with self.assertRaises(ValueError):
ExplodeSamples().run(inp).result()
inp = State(problem=bqm, samples=SampleSet.empty())
with self.assertRaises(ValueError):
ExplodeSamples().run(inp).result()
def test_ignored_interactions(self):
bqm = dimod.BQM.from_ising({'a': -4.0, 'b': -4.0},
{('a', 'b'): 3.2, ('b', 'c'): 1}, 1.5)
sampler = ScaleComposite(dimod.TrackingComposite(dimod.ExactSolver()))
sampleset = sampler.sample(bqm, scalar=.5,
ignored_interactions=[('b', 'c')])
# check that everything was restored properly
dimod.testing.assert_sampleset_energies(sampleset, bqm)
self.assertEqual(sampler.child.input['bqm'],
dimod.BQM.from_ising({'a': -2.0, 'b': -2.0},
{'ab': 1.6, 'bc': 1}, .75))
def test_x_vartype(self):
samples = {'a': -1, 'b': 1}
bqm = dimod.BQM.from_qubo({'ab': 1})
init = Initialized().parse_initial_states(
bqm=bqm, initial_states=samples, num_reads=10)
self.assertIs(init.initial_states.vartype, dimod.BINARY)
arr = init.initial_states.record.sample
self.assertTrue(((arr == 1) ^ (arr == 0)).all())
samples = {'a': 0, 'b': 1}
bqm = dimod.BQM.from_ising({}, {'ab': 1})
init = Initialized().parse_initial_states(
bqm=bqm, initial_states=samples, num_reads=10)
self.assertIs(init.initial_states.vartype, dimod.SPIN)
arr = init.initial_states.record.sample
self.assertTrue(((arr == 1) ^ (arr == -1)).all())
def test_scalar(self):
bqm = dimod.BQM.from_ising({'a': -4.0, 'b': -4.0},
{('a', 'b'): 3.2}, 1.5)
sampler = ScaleComposite(dimod.TrackingComposite(dimod.ExactSolver()))
sampleset = sampler.sample(bqm, scalar=.5)
# check that everything was restored properly
dimod.testing.assert_sampleset_energies(sampleset, bqm)
self.assertEqual(sampler.child.input['bqm'],
dimod.BQM.from_ising({'a': -2.0, 'b': -2.0},
{('a', 'b'): 1.6}, .75))
def test_bq_request_encoding_ising_bqm(self):
"""Simple Ising BQM properly encoded."""
bqm = dimod.BQM.from_ising({0: 1}, {(0, 1): 1})
req = encode_problem_as_bq(bqm)
self.assertEqual(req.get('format'), 'bq')
self.assertEqual(pluck(req, 'data.version.bqm_schema'), '3.0.0')
self.assertEqual(pluck(req, 'data.variable_type'), 'SPIN')
self.assertEqual(pluck(req, 'data.num_variables'), 2)
self.assertEqual(pluck(req, 'data.num_interactions'), 1)
def test_scalar(self):
bqm = dimod.BQM.from_ising({'a': -4.0, 'b': -4.0},
{('a', 'b'): 3.2}, 1.5)
sampler = ScaleComposite(dimod.TrackingComposite(dimod.ExactSolver()))
sampleset = sampler.sample(bqm, scalar=.5)
# check that everything was restored properly
dimod.testing.assert_sampleset_energies(sampleset, bqm)
self.assertEqual(sampler.child.input['bqm'],
dimod.BQM.from_ising({'a': -2.0, 'b': -2.0},
{('a', 'b'): 1.6}, .75))
def test_hstack_from_bqm(self):
bqm = dimod.BQM.from_ising({'a': 1}, {})
ss = dimod.SampleSet.from_samples({'a': 0}, vartype='BINARY', energy=0)
res = hstack_samplesets(ss, bqm=bqm)
self.assertEqual(res.vartype, dimod.SPIN)
numpy.testing.assert_array_equal(res.record.energy, numpy.array([-1]))
def test_bias_ranges(self):
bqm = dimod.BQM.from_ising({'a': -4.0, 'b': -4.0},
{('a', 'b'): 4}, 1.5)
sampler = ScaleComposite(dimod.TrackingComposite(dimod.ExactSolver()))
sampleset = sampler.sample(bqm, bias_range=[-3, 3],
quadratic_range=[-2, 2])
# check that everything was restored properly
dimod.testing.assert_sampleset_energies(sampleset, bqm)
self.assertEqual(sampler.child.input['bqm'],
dimod.BQM.from_ising({'a': -2.0, 'b': -2.0},
{('a', 'b'): 2}, .75))
def test_bias_range(self):
bqm = dimod.BQM.from_ising({'a': -4.0, 'b': -4.0},
{('a', 'b'): 3.2}, 1.5)
sampler = ScaleComposite(dimod.TrackingComposite(dimod.ExactSolver()))
sampleset = sampler.sample(bqm, bias_range=[-2, 2])
# check that everything was restored properly
dimod.testing.assert_sampleset_energies(sampleset, bqm)
self.assertEqual(sampler.child.input['bqm'],
dimod.BQM.from_ising({'a': -2.0, 'b': -2.0},
{('a', 'b'): 1.6}, .75))