def test_typical(self): i, j, k = dimod.Integers('ijk') cqm = CQM() cqm.set_objective(2 * i * i + i * k) label = cqm.add_constraint(-3 * i * i + 4 * j * j <= 5) mapping = cqm.substitute_self_loops() self.assertIn('i', mapping) self.assertIn('j', mapping) self.assertEqual(len(mapping), 2) self.assertEqual(cqm.objective.quadratic, { ('k', 'i'): 1, (mapping['i'], 'i'): 2 }) self.assertEqual(cqm.constraints[label].lhs.quadratic, { (mapping['i'], 'i'): -3.0, (mapping['j'], 'j'): 4.0 }) self.assertEqual(len(cqm.constraints), 3) for v, new in mapping.items(): self.assertIn(new, cqm.constraints) self.assertEqual(cqm.constraints[new].sense, Sense.Eq) self.assertEqual(cqm.constraints[new].lhs.linear, {v: 1, new: -1}) self.assertEqual(cqm.constraints[new].rhs, 0)
def test_multiple_unlabelled(self): i, j, k = dimod.Integers(3) self.assertNotEqual(i.variables[0], j.variables[0]) self.assertNotEqual(i.variables[0], k.variables[0]) self.assertIs(i.vartype(i.variables[0]), dimod.INTEGER) self.assertIs(j.vartype(j.variables[0]), dimod.INTEGER) self.assertIs(k.vartype(k.variables[0]), dimod.INTEGER)
def test_bug982(self): # https://github.com/dwavesystems/dimod/issues/982 i, j = dimod.Integers('ij') self.assertAlmostEqual((i * j).energy({ 'i': 4294967296, 'j': 4294967296 }), 1.8446744073709552e+19)
def test_div_number(self): i, j = dimod.Integers('ij') x = Binary('x') qm = 6 * i * j + 2 * i * x + 4 * x + 12 ref = qm qm /= 2 self.assertIs(qm, ref) self.assertTrue(qm.is_equal(3 * i * j + i * x + 2 * x + 6))
def test_several(self): i, j, k = dimod.Integers('ijk') qm = i * i + i * j + i * k + j * k + k + 1 self.assertEqual(qm.num_interactions, 4) qm.remove_interaction('i', 'i') self.assertEqual(qm.num_interactions, 3) qm.remove_interaction('i', 'k') self.assertEqual(qm.num_interactions, 2) self.assertEqual(qm.quadratic, {('j', 'i'): 1.0, ('k', 'j'): 1.0})
def test_multiple_labelled(self): i, j, k = dimod.Integers('ijk') self.assertEqual(i.variables[0], 'i') self.assertEqual(j.variables[0], 'j') self.assertEqual(k.variables[0], 'k') self.assertIs(i.vartype('i'), dimod.INTEGER) self.assertIs(j.vartype('j'), dimod.INTEGER) self.assertIs(k.vartype('k'), dimod.INTEGER)
def test_integer(self): i = dimod.Integer('i', lower_bound=-1000) j, k = dimod.Integers('jk') cqm = CQM() label_le = cqm.add_constraint(i + j*k <= 5) label_ge = cqm.add_constraint(i + j >= 1000) sample = {'i': 105, 'j': 4, 'k': 5} self.assertEqual(cqm.violations(sample), {label_le: 120.0, label_ge: 891.0}) sample = {'j': -1, 'i': 1004, 'k': 1000} self.assertEqual(cqm.violations(sample, clip=False), {label_ge: -3.0, label_le: -1.0})
def test_simple(self): i, j = dimod.Integers('ij') x, y = dimod.Binaries('xy') cqm0 = dimod.CQM() cqm0.set_objective(i + 2*j + x*i) cqm0.add_constraint(i <= 5, label='a') cqm0.add_constraint(y*j >= 4, label='b') cqm1 = dimod.CQM() cqm1.set_objective(i + 2.0001*j + x*i) cqm1.add_constraint(i <= 5, label='a') cqm1.add_constraint(1.001*y*j >= 4, label='b') self.assertTrue(cqm0.is_almost_equal(cqm1, places=2)) self.assertFalse(cqm0.is_almost_equal(cqm1, places=5))
def test_simple(self): i, j = dimod.Integers('ij') x, y = dimod.Binaries('xy') cqm0 = dimod.CQM() cqm0.set_objective(i + 2*j + x*i) cqm0.add_constraint(i <= 5, label='a') cqm0.add_constraint(y*j >= 4, label='b') cqm1 = dimod.CQM() cqm1.set_objective(i + 2*j + x*i) cqm1.add_constraint(i <= 5, label='a') cqm1.add_constraint(y*j >= 4, label='b') self.assertTrue(cqm0.is_equal(cqm1)) cqm1.set_objective(y) self.assertFalse(cqm0.is_equal(cqm1)) cqm1.set_objective(i + 2*j + x*i) cqm1.add_constraint(x*y == 1) self.assertFalse(cqm0.is_equal(cqm1))