def test_bqm_Ge_constraint(self): x, y, z = dimod.Binaries('xyz') cqm = CQM() cqm.add_constraint(x + y + z >= 2) bqm, inverter = dimod.cqm_to_bqm(cqm, lagrange_multiplier=1) configs = set() for sample in dimod.ExactSolver().sample(bqm).lowest().samples(): self.assertGreaterEqual(sample['x'] + sample['y'] + sample['z'], 2) configs.add((sample['x'], sample['y'], sample['z'])) self.assertEqual(len(configs), 4)
def test_binary(self): a, b, c = dimod.Binaries('abc') cqm = CQM() cqm.add_constraint(a + b + c == 1, label='onehot') cqm.add_constraint(a*b <= 0, label='ab LE') cqm.add_constraint(c >= 1, label='c GE') sample = {'a': 0, 'b': 0, 'c': 1} # satisfying sample self.assertEqual(cqm.violations(sample), {'ab LE': 0.0, 'c GE': 0.0, 'onehot': 0.0}) self.assertEqual(cqm.violations(sample, skip_satisfied=True), {}) sample = {'a': 1, 'b': 0, 'c': 0} # violates one self.assertEqual(cqm.violations(sample), {'ab LE': 0.0, 'c GE': 1.0, 'onehot': 0.0}) self.assertEqual(cqm.violations(sample, skip_satisfied=True), {'c GE': 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))