def test_copy(self): i = Integer('i', lower_bound=-10, upper_bound=10) j = i.copy() self.assertEqual(j.lower_bound('i'), -10) self.assertEqual(j.upper_bound('i'), 10)
def test_integer(self): i = Integer('i') for s in np.geomspace(1, i.upper_bound('i'), num=20, dtype=np.int64): sample = {'i': s} self.assertEqual(i.energy(sample), s) sample = {'i': -s} self.assertEqual(i.energy(sample), -s)
def test_expression_mixed_smoke(self): i = Integer('i') j = Integer('j') x = Binary('x') y = Binary('y') s = Spin('s') t = Spin('t') exp = i + j + x + y + s + t + i * j + s * i + x * j + (s + 1) * (1 - j)
def test_expressions(self): i = Integer('i') j = Integer('j') qm = (i - 1) * (j - 1) self.assertEqual(qm.linear, {'i': -1, 'j': -1}) self.assertEqual(qm.quadratic, {('i', 'j'): 1}) self.assertEqual(qm.offset, 1)
def test_later_defn(self): i0 = Integer('i') i1 = Integer('i', upper_bound=1) cqm = CQM() cqm.add_variable('i', 'INTEGER') cqm.set_objective(i0) with self.assertRaises(ValueError): cqm.add_constraint(i1 <= 1) cqm.add_variable('i', 'INTEGER')
def test_inconsistent(self): i0 = Integer('i', lower_bound=-7, upper_bound=14) i1 = Integer('i', lower_bound=-7, upper_bound=140) with self.assertRaises(ValueError): i0 + i1 with self.assertRaises(ValueError): i0 - i1 with self.assertRaises(ValueError): i0 * i1
def test_isub(self): qm = Integer('i') qm -= Integer('j') qm -= 5 self.assertTrue( qm.is_equal( QM({ 'i': 1, 'j': -1 }, {}, -5, { 'i': 'INTEGER', 'j': 'INTEGER' })))
def test_inconsistent(self): i0 = Integer('i') i1 = Integer('i', upper_bound=1) i2 = Integer('i', lower_bound=-2) cqm = CQM() cqm.set_objective(i0) with self.assertRaises(ValueError): cqm.add_constraint(i1 <= 1) cqm = CQM() cqm.add_constraint(i0 <= 1) with self.assertRaises(ValueError): cqm.set_objective(i2)
def test_sub_permutations(self): x = Binary('x') i = Integer('i') s = Spin('s') for t0, t1 in itertools.permutations([x, i, s, 1], 2): qm = t0 - t1
def test_typedvariables(self): cqm = CQM() x = Binary('x') s = Spin('s') i = Integer('i') cqm.set_objective(x + i) cqm.add_constraint(i + s <= 1) with self.assertWarns(DeprecationWarning): self.assertEqual(cqm.variables.lower_bounds, {'x': 0.0, 'i': 0.0, 's': -1.0}) with self.assertWarns(DeprecationWarning): self.assertEqual(cqm.variables.upper_bounds, {'x': 1.0, 'i': 9007199254740991.0, 's': 1}) with self.assertWarns(DeprecationWarning): self.assertEqual(len(cqm.variables.lower_bounds), 3) with self.assertWarns(DeprecationWarning): self.assertEqual(len(cqm.variables.upper_bounds), 3) with self.assertWarns(DeprecationWarning): self.assertEqual(list(cqm.variables.vartypes), [dimod.BINARY, dimod.INTEGER, dimod.SPIN]) with self.assertWarns(DeprecationWarning): self.assertIs(cqm.variables.vartype('x'), dimod.BINARY)
def test_superset(self): a = Integer('a') b = Binary('b') qm = a + a * b + 1.5 self.assertEqual(qm.energy({'a': 1, 'b': 1, 'c': 1}), 3.5) self.assertEqual(qm.energy({'a': 1, 'b': 0, 'c': 1}), 2.5)
def test_expressions(self): i = Integer('i') j = Integer('j') self.assertTrue((i * j).is_equal( QM({}, {'ij': 1}, 0, { 'i': 'INTEGER', 'j': 'INTEGER' }))) self.assertTrue((i * i).is_equal(QM({}, {'ii': 1}, 0, {'i': 'INTEGER'}))) self.assertTrue( ((2 * i) * (3 * i)).is_equal(QM({}, {'ii': 6}, 0, {'i': 'INTEGER'}))) self.assertTrue((i + j).is_equal( QM({ 'i': 1, 'j': 1 }, {}, 0, { 'i': 'INTEGER', 'j': 'INTEGER' }))) self.assertTrue((i + 2 * j).is_equal( QM({ 'i': 1, 'j': 2 }, {}, 0, { 'i': 'INTEGER', 'j': 'INTEGER' }))) self.assertTrue((i - 2 * j).is_equal( QM({ 'i': 1, 'j': -2 }, {}, 0, { 'i': 'INTEGER', 'j': 'INTEGER' }))) self.assertTrue((-i).is_equal(QM({'i': -1}, {}, 0, {'i': 'INTEGER'}))) self.assertTrue((1 - i).is_equal(QM({'i': -1}, {}, 1, {'i': 'INTEGER'}))) self.assertTrue((i - 1).is_equal(QM({'i': 1}, {}, -1, {'i': 'INTEGER'}))) self.assertTrue(((i - j)**2).is_equal((i - j) * (i - j))) self.assertTrue( ((2 * i + 4 * i * j + 6) / 2.).is_equal(i + 2 * i * j + 3))
def test_terms_integer_bounds(self): # bug report: https://github.com/dwavesystems/dimod/issues/943 cqm = CQM() i = Integer('i', lower_bound=-1, upper_bound=5) cqm.set_objective(i) label = cqm.add_constraint([('i', 1)], sense='<=') # failing in #943 self.assertEqual(cqm.constraints[label].lhs.lower_bound('i'), -1) self.assertEqual(cqm.constraints[label].lhs.upper_bound('i'), 5)
def test_functional_discrete(self): cqm = CQM() bqm = BQM({'a': -1}, {'ab': 1}, 1.5, 'SPIN') cqm.add_constraint(bqm, '<=') cqm.add_constraint(bqm, '>=') cqm.set_objective(Integer('c')) cqm.add_constraint(Spin('a')*Integer('d')*5 <= 3) cqm.add_discrete('efg') new = CQM.from_file(cqm.to_file()) self.assertTrue(cqm.objective.is_equal(new.objective)) self.assertEqual(set(cqm.constraints), set(new.constraints)) for label, constraint in cqm.constraints.items(): self.assertTrue(constraint.lhs.is_equal(new.constraints[label].lhs)) self.assertEqual(constraint.rhs, new.constraints[label].rhs) self.assertEqual(constraint.sense, new.constraints[label].sense) self.assertSetEqual(cqm.discrete, new.discrete)
def test_subset(self): a = Integer('a') b = Binary('b') c = Spin('c') qm = a + a * b + c + 1.5 samples = {'a': 1, 'c': 1} with self.assertRaises(ValueError): qm.energy(samples)
def test_add_permutations(self): x = Binary('x') i = Integer('i') s = Spin('s') for perm in itertools.permutations([x, i, s, 1]): qm = sum(perm) self.assertEqual(qm.linear, {'x': 1, 'i': 1, 's': 1}) self.assertEqual(qm.offset, 1) self.assertEqual(qm.quadratic, {})
def test_symbolic_mixed(self): cqm = CQM() x = Binary('x') s = Spin('s') i = Integer('i') cqm.add_constraint(2*i + s + x <= 2) self.assertIs(cqm.vartype('x'), dimod.BINARY) self.assertIs(cqm.vartype('s'), dimod.SPIN) self.assertIs(cqm.vartype('i'), dimod.INTEGER)
def test_deepcopy(self): from copy import deepcopy cqm = CQM() x = Binary('x') s = Spin('s') i = Integer('i') cqm.set_objective(i + s + x) constraint = cqm.add_constraint(i + s + x <= 1) new = deepcopy(cqm) self.assertTrue(new.objective.is_equal(cqm.objective)) self.assertTrue(new.constraints[constraint].lhs.is_equal(cqm.constraints[constraint].lhs))
def test_header(self): from dimod.serialization.fileview import read_header cqm = CQM() x = Binary('x') s = Spin('s') i = Integer('i') cqm.set_objective(x + 3*i + s*x) cqm.add_constraint(x*s + x <= 5) cqm.add_constraint(i*i + i*s <= 4) header_info = read_header(cqm.to_file(), b'DIMODCQM') self.assertEqual(header_info.data, {'num_biases': 11, 'num_constraints': 2, 'num_quadratic_variables': 4, 'num_variables': 3})
def test_symbolic_add(self): i = Integer('i', lower_bound=-10, upper_bound=10) new = i + i self.assertEqual(new.lower_bound('i'), -10) self.assertEqual(new.upper_bound('i'), 10)
def test_no_label_collision(self): qm_1 = Integer() qm_2 = Integer() self.assertNotEqual(qm_1.variables[0], qm_2.variables[0])
def test_init_no_label(self): integer_qm = Integer() self.assertIsInstance(integer_qm.variables[0], str)
def test_set(self): cqm = CQM() cqm.set_objective(Integer('a') * 5) self.assertTrue(cqm.objective.is_equal(Integer('a') * 5))
def test_from_qm(self): qm = BQM({'a': -1}, {'ab': 1}, 1.5, 'SPIN') + Integer('i') cqm = CQM.from_quadratic_model(qm) self.assertTrue(cqm.objective.is_equal(qm))