예제 #1
0
    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)
예제 #2
0
    def test_kwargs(self):
        bqm = dimod.BinaryQuadraticModel({}, {}, 0.0, dimod.SPIN)
        with self.assertWarns(SamplerUnknownArgWarning):
            sampleset = dimod.ExactSolver().sample(bqm, a=1, b="abc")

        dqm = dimod.DiscreteQuadraticModel()
        with self.assertWarns(SamplerUnknownArgWarning):
            sampleset = dimod.ExactDQMSolver().sample_dqm(dqm, a=1, b="abc")

        cqm = dimod.ConstrainedQuadraticModel()
        with self.assertWarns(SamplerUnknownArgWarning):
            sampleset = dimod.ExactCQMSolver().sample_cqm(cqm, a=1, b="abc")
예제 #3
0
    def test_arbitrary_labels(self):
        bqm = dimod.BQM.from_ising({}, {'ab': -1})
        sampleset = dimod.ExactSolver().sample(bqm)
        self.assertEqual(set(sampleset.variables), set(bqm.variables))

        dqm = dimod.DQM.from_numpy_vectors([0], [-1], ([], [], []),
                                           labels={'ab'})
        sampleset = dimod.ExactDQMSolver().sample_dqm(dqm)
        self.assertEqual(set(sampleset.variables), set(dqm.variables))

        cqm = dimod.CQM.from_dqm(dqm)
        sampleset = dimod.ExactCQMSolver().sample_cqm(cqm)
        self.assertEqual(set(sampleset.variables), set(cqm.variables))
예제 #4
0
    def test_sample_CONSTRAINED_SPIN(self):
        # using Spin variables, with inequality constraint:
        cqm = dimod.ConstrainedQuadraticModel()
        x, y, z = dimod.Spin('x'), dimod.Spin('y'), dimod.Spin('z')
        cqm.set_objective(x * y + 2 * y * z)
        cqm.add_constraint(x * y <= 0)

        response = dimod.ExactCQMSolver().sample_cqm(cqm)

        # every possible combination should be present
        self.assertEqual(len(response), 8)
        self.assertEqual(response.record.sample.shape, (8, len(cqm.variables)))

        # four samples should be feasible
        feasible_responses = response.filter(lambda d: d.is_feasible)
        self.assertEqual(len(feasible_responses), 4)

        dimod.testing.assert_sampleset_energies_cqm(response, cqm)
예제 #5
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)
예제 #6
0
    def test_sample_CONSTRAINED_empty(self):
        cqm = dimod.ConstrainedQuadraticModel()
        response = dimod.ExactCQMSolver().sample_cqm(cqm)

        self.assertEqual(response.record.sample.shape, (0, 0))