Exemple #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))
Exemple #2
0
    def test_tolerance(self):
        x, y, z = dimod.Binaries('xyz')

        cqm = dimod.CQM()
        cqm.add_constraint((x + y + z) * 3 * .1 <= .9)

        sample = {'x': 1, 'y': 1, 'z': 1}

        self.assertTrue(cqm.check_feasible(sample))
Exemple #3
0
    def test_simple(self):
        x, y, z = dimod.Binaries('xyz')

        cqm = dimod.CQM()
        cqm.add_constraint((x + y + z) * 3 <= 3)

        self.assertTrue(cqm.check_feasible({'x': 1, 'y': 0, 'z': 0}))
        self.assertTrue(cqm.check_feasible({'x': 0, 'y': 0, 'z': 0}))
        self.assertFalse(cqm.check_feasible({'x': 1, 'y': 0, 'z': 1}))
    def test_sample_cqm_smoke_test(self):
        """Construction of and sampling from an unstructured CQM solver works."""

        # construct a small 3-variable CQM of mixed vartypes
        try:
            import dimod
            mixed = dimod.QM()
            mixed.add_variable('BINARY', 'a')
            mixed.add_variable('SPIN', 'b')
            mixed.add_variable('INTEGER', 'c')
            cqm = dimod.CQM()
            cqm.set_objective(mixed)
            cqm.add_constraint(mixed, rhs=1, sense='==')
        except:
            # dimod or dimod with CQM support not available, so just use a mock
            cqm = mock.Mock()
            cqm.to_file.return_value = io.BytesIO(b'123')

        problem_type = 'cqm'

        # use a global mocked session, so we can modify it on-fly
        session = mock.Mock()

        # upload is now part of submit, so we need to mock it
        mock_problem_id = 'mock-problem-id'

        def mock_upload(self, bqm):
            return Present(result=mock_problem_id)

        # construct a functional solver by mocking client and api response data
        with mock.patch.multiple(Client,
                                 create_session=lambda self: session,
                                 upload_problem_encoded=mock_upload):
            with Client('endpoint', 'token') as client:
                solver = CQMSolver(
                    client,
                    unstructured_solver_data(problem_type=problem_type))

                # use bqm for mock response (for now)
                ss = dimod.ExactSolver().sample(dimod.BQM.empty('SPIN'))
                ss.info.update(problem_id=mock_problem_id)
                session.post = lambda path, _: choose_reply(
                    path, {
                        'problems/':
                        complete_reply(
                            ss, id_=mock_problem_id, type_=problem_type)
                    })

                # verify decoding works
                fut = solver.sample_cqm(cqm)
                numpy.testing.assert_array_equal(fut.sampleset, ss)
                numpy.testing.assert_array_equal(fut.problem_type,
                                                 problem_type)
Exemple #5
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*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))
    def test_cqm(self):
        cqm = dimod.CQM()

        bqm = BinaryQuadraticModel({'a': -1}, {'ab': 1}, 1.5, 'SPIN')
        cqm.add_constraint(bqm, '<=')
        cqm.add_constraint(bqm, '>=')  # add it again

        new = load(cqm.to_file())

        self.assertEqual(cqm.objective, new.objective)
        self.assertEqual(set(cqm.constraints), set(new.constraints))
        for label, constraint in cqm.constraints.items():
            self.assertEqual(constraint.lhs, new.constraints[label].lhs)
            self.assertEqual(constraint.rhs, new.constraints[label].rhs)
            self.assertEqual(constraint.sense, new.constraints[label].sense)
Exemple #7
0
 def test_empty(self):
     bqm, inverter = dimod.cqm_to_bqm(dimod.CQM())
     self.assertEqual(bqm.shape, (0, 0))
     self.assertEqual(bqm.vartype, dimod.BINARY)