def test_qm_Ge_constraint(self): i = dimod.Integer('i', upper_bound=7) j = dimod.Integer('j', upper_bound=9) x = dimod.Binary('x') cqm = CQM() cqm.add_constraint(i + j + x >= 5) bqm, inverter = dimod.cqm_to_bqm(cqm, lagrange_multiplier=1) for bin_sample in dimod.ExactSolver().sample(bqm).lowest().samples(): int_sample = inverter(bin_sample) self.assertGreaterEqual(int_sample['i'] + int_sample['j'] + int_sample['x'], 5)
def test_qm_objective_only(self): i = dimod.Integer('i', upper_bound=7) j = dimod.Integer('j', upper_bound=9) x = dimod.Binary('x') qm = i*j + 5*j*x + 8*i + 3*x + 5 cqm = CQM.from_qm(qm) bqm, inverter = dimod.cqm_to_bqm(cqm) sampleset = dimod.ExactSolver().sample(bqm) for bin_sample, energy in sampleset.data(['sample', 'energy']): int_sample = inverter(bin_sample) self.assertEqual(qm.energy(int_sample), energy)
def test_serializable(self): i = dimod.Integer('i', upper_bound=7) j = dimod.Integer('j', upper_bound=9) x = dimod.Binary('x') cqm = CQM() cqm.add_constraint(i + j + x >= 5) bqm, inverter = dimod.cqm_to_bqm(cqm, lagrange_multiplier=1) newinverter = dimod.constrained.CQMToBQMInverter.from_dict( json.loads(json.dumps(inverter.to_dict()))) for bin_sample in dimod.ExactSolver().sample(bqm).lowest().samples(): int_sample = newinverter(bin_sample) self.assertGreaterEqual(int_sample['i'] + int_sample['j'] + int_sample['x'], 5)
def test_sample_CONSTRAINED_MIXED(self): # Using various Vartypes, including the `add_discrete` method cqm = dimod.ConstrainedQuadraticModel() x = {(u, v): dimod.Binary((u, v)) for u, v in product(['u1', 'u2'], range(3))} cqm.add_discrete([('u1', v) for v in range(3)], label='u1') cqm.add_discrete([('u2', v) for v in range(3)], label='u2') y, z = dimod.Spin('y'), dimod.Integer('z', lower_bound=1, upper_bound=3) obj1 = x[('u1', 0)] * y - x[('u2', 1)] * y obj2 = x[('u1', 0)] * z + 2 * x[('u1', 2)] * x[('u2', 2)] cqm.set_objective(obj1 + obj2) cqm.add_constraint(z == 2) response = dimod.ExactCQMSolver().sample_cqm(cqm) # every possible combination should be present, respecting the discrete constraints self.assertEqual(len(response), 54) self.assertEqual(response.record.sample.shape, (54, len(cqm.variables))) # only 18 samples should be feasible feasible_responses = response.filter(lambda d: d.is_feasible) self.assertEqual(len(feasible_responses), 18) dimod.testing.assert_sampleset_energies_cqm(response, cqm)
def test_qm(self): x = dimod.Binary('x') s = dimod.Spin('s') i = dimod.Integer('i') self.assertTrue((x + s + i).is_almost_equal(1.001 * (x + s + i), places=2)) self.assertFalse((x + s + i).is_almost_equal(1.1 * (x + s + i), places=2))
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_sample_CONSTRAINED_INTEGER(self): # using Integer variables, with inequality constraint: cqm = dimod.ConstrainedQuadraticModel() x, y, z = dimod.Integer( 'x', lower_bound=1, upper_bound=3), dimod.Integer( 'y', lower_bound=4, upper_bound=5), dimod.Integer('z', lower_bound=-2, upper_bound=1) cqm.set_objective(x * y + 2 * y * z) cqm.add_constraint(x * z >= 1) response = dimod.ExactCQMSolver().sample_cqm(cqm) # every possible combination should be present self.assertEqual(len(response), 24) self.assertEqual(response.record.sample.shape, (24, len(cqm.variables))) # only six samples should be feasible feasible_responses = response.filter(lambda d: d.is_feasible) self.assertEqual(len(feasible_responses), 6) dimod.testing.assert_sampleset_energies_cqm(response, cqm)