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_with_offset(self, name, BQM):
Q = {(0, 0): -1, (0, 1): -1, (0, 2): -1, (1, 2): 1}
bqm = BQM.from_qubo(Q, 1.6)
assert_consistent_bqm(bqm)
self.assertEqual(bqm.linear, {0: -1, 1: 0, 2: 0})
self.assertEqual(bqm.adj, {0: {1: -1, 2: -1},
1: {0: -1, 2: 1},
2: {0: -1, 1: 1}})
self.assertEqual(bqm.offset, 1.6)
def test_simple_spin(self, name, BQM):
bqm = BQM({'a': 1, 'b': -3, 'c': 2}, {'ab': -5, 'bc': 6}, 16, 'BINARY')
assert_consistent_bqm(bqm.spin)
self.assertIs(bqm.spin.vartype, dimod.SPIN)
spin = bqm.change_vartype(dimod.SPIN, inplace=False)
self.assertEqual(spin, bqm.spin)
self.assertNotEqual(spin, bqm)
self.assertIs(bqm.spin.binary, bqm)
self.assertIs(bqm.spin.spin, bqm.spin) # and so on
def test_quadratic_only_spin(self, name, BQM):
M = {(0, 1): -1, (0, 0): 1, (1, 2): 1.5, (2, 2): 4, (4, 5): 7}
bqm = BQM(M, 'SPIN')
assert_consistent_bqm(bqm)
self.assertEqual(bqm.linear, {0: 0, 1: 0, 2: 0, 4: 0, 5: 0})
self.assertEqual(bqm.quadratic, {(0, 1): -1, (1, 2): 1.5, (4, 5): 7})
self.assertEqual(bqm.offset, 5)
def test_normalize(self, name, BQM):
bqm = BQM({0: -2, 1: 2}, {(0, 1): -1}, 1., dimod.SPIN)
bqm.normalize(.5)
self.assertAlmostEqual(bqm.linear, {0: -.5, 1: .5})
self.assertAlmostEqual(bqm.quadratic, {(0, 1): -.25})
self.assertAlmostEqual(bqm.offset, .25)
assert_consistent_bqm(bqm)
def test_bqm_spin(self):
linear = {'a': -1, 'b': 1, 0: 1.5}
quadratic = {(0, 1): -1, (0, 0): 1, (1, 2): 1.5, (2, 2): 4, (4, 5): 7}
offset = 0
vartype = dimod.SPIN
for source, target in itertools.product(BQM_SUBCLASSES, repeat=2):
with self.subTest(source=source, target=target):
bqm = source(linear, quadratic, offset, vartype)
new = target(bqm)
self.assertIsInstance(new, target)
assert_consistent_bqm(new)
self.assertEqual(bqm.adj, new.adj)
self.assertEqual(bqm.offset, new.offset)
self.assertEqual(bqm.vartype, new.vartype)
assert_consistent_bqm(bqm)
bqm = BQM(vartype='SPIN')
self.assertEqual(bqm.vartype, dimod.SPIN)
self.assertEqual(bqm.shape, (0, 0))
assert_consistent_bqm(bqm)
bqm = BQM('BINARY')
self.assertEqual(bqm.vartype, dimod.BINARY)
self.assertEqual(bqm.shape, (0, 0))
assert_consistent_bqm(bqm)
bqm = BQM(vartype='BINARY')
self.assertEqual(bqm.vartype, dimod.BINARY)
self.assertEqual(bqm.shape, (0, 0))
assert_consistent_bqm(bqm)
def test_provided(self, name, BQM):
if not BQM.shapeable():
raise unittest.SkipTest
bqm = BQM('SPIN')
bqm.add_variable('a')
bqm.add_variable('b')
bqm.add_variable('c')
bqm.remove_variable('b')
assert_consistent_bqm(bqm)
# maintained order
self.assertEqual(list(bqm.iter_variables()), ['a', 'c'])
with self.assertRaises(ValueError):
bqm.remove_variable('b')
def test_integer(self, name, BQM):
bqm = BQM(0, 'SPIN')
self.assertEqual(bqm.vartype, dimod.SPIN)
self.assertEqual(bqm.shape, (0, 0))
assert_consistent_bqm(bqm)
bqm = BQM(5, 'SPIN')
self.assertEqual(bqm.vartype, dimod.SPIN)
self.assertEqual(bqm.shape, (5, 0))
assert_consistent_bqm(bqm)
self.assertEqual(bqm.linear, {v: 0 for v in range(5)})
def test_array_like_1var(self, name, BQM):
D = [[1]]
bqm = BQM(D, 'BINARY')
assert_consistent_bqm(bqm)
self.assertEqual(bqm.shape, (1, 0))
self.assertEqual(bqm.linear[0], 1)
def test_quadratic_delitem(self, name, BQM):
if not BQM.shapeable():
raise unittest.SkipTest
bqm = BQM([[0, 1, 2, 3, 4],
[0, 6, 7, 8, 9],
[0, 0, 10, 11, 12],
[0, 0, 0, 13, 14],
[0, 0, 0, 0, 15]], 'SPIN')
del bqm.quadratic[0, 1]
self.assertEqual(set(bqm.iter_neighbors(0)), set([2, 3, 4]))
assert_consistent_bqm(bqm)
with self.assertRaises(KeyError):
del bqm.quadratic[0, 1]