예제 #1
0
    def test_symbolic(self):
        cqm = CQM()

        a = Spin('a')
        b = Spin('b')
        c = Spin('c')

        cqm.add_constraint(2*a*b + b*c - c + 1 <= 1)
예제 #2
0
    def test_expression_mixed_smoke(self):
        i = Integer('i')
        j = Integer('j')
        x = Binary('x')
        y = Binary('y')
        s = Spin('s')
        t = Spin('t')

        exp = i + j + x + y + s + t + i * j + s * i + x * j + (s + 1) * (1 - j)
예제 #3
0
    def test_typedvariables(self):
        cqm = CQM()

        x = Binary('x')
        s = Spin('s')
        i = Integer('i')

        cqm.set_objective(x + i)
        cqm.add_constraint(i + s <= 1)

        with self.assertWarns(DeprecationWarning):
            self.assertEqual(cqm.variables.lower_bounds, {'x': 0.0, 'i': 0.0, 's': -1.0})
        with self.assertWarns(DeprecationWarning):
            self.assertEqual(cqm.variables.upper_bounds, {'x': 1.0, 'i': 9007199254740991.0, 's': 1})
        with self.assertWarns(DeprecationWarning):
            self.assertEqual(len(cqm.variables.lower_bounds), 3)
        with self.assertWarns(DeprecationWarning):
            self.assertEqual(len(cqm.variables.upper_bounds), 3)

        with self.assertWarns(DeprecationWarning):
            self.assertEqual(list(cqm.variables.vartypes),
                             [dimod.BINARY, dimod.INTEGER, dimod.SPIN])

        with self.assertWarns(DeprecationWarning):
            self.assertIs(cqm.variables.vartype('x'), dimod.BINARY)
예제 #4
0
    def test_sub_permutations(self):
        x = Binary('x')
        i = Integer('i')
        s = Spin('s')

        for t0, t1 in itertools.permutations([x, i, s, 1], 2):
            qm = t0 - t1
예제 #5
0
    def test_spin_bin(self):
        x = Binary('x')
        s = Spin('s')

        self.assertEqual((2 * x * s).energy({'x': 1, 's': 1}), 2)
        self.assertEqual((2 * x * s).energy({'x': 1, 's': -1}), -2)
        self.assertEqual((2 * x * s).energy({'x': 0, 's': 1}), 0)
        self.assertEqual((2 * x * s + 1).energy({'x': 0, 's': -1}), 1)
예제 #6
0
    def test_subset(self):
        a = Integer('a')
        b = Binary('b')
        c = Spin('c')

        qm = a + a * b + c + 1.5

        samples = {'a': 1, 'c': 1}

        with self.assertRaises(ValueError):
            qm.energy(samples)
예제 #7
0
    def test_add_permutations(self):
        x = Binary('x')
        i = Integer('i')
        s = Spin('s')

        for perm in itertools.permutations([x, i, s, 1]):
            qm = sum(perm)

            self.assertEqual(qm.linear, {'x': 1, 'i': 1, 's': 1})
            self.assertEqual(qm.offset, 1)
            self.assertEqual(qm.quadratic, {})
예제 #8
0
    def test_symbolic_mixed(self):
        cqm = CQM()

        x = Binary('x')
        s = Spin('s')
        i = Integer('i')

        cqm.add_constraint(2*i + s + x <= 2)

        self.assertIs(cqm.vartype('x'), dimod.BINARY)
        self.assertIs(cqm.vartype('s'), dimod.SPIN)
        self.assertIs(cqm.vartype('i'), dimod.INTEGER)
예제 #9
0
    def test_deepcopy(self):
        from copy import deepcopy

        cqm = CQM()

        x = Binary('x')
        s = Spin('s')
        i = Integer('i')

        cqm.set_objective(i + s + x)
        constraint = cqm.add_constraint(i + s + x <= 1)

        new = deepcopy(cqm)

        self.assertTrue(new.objective.is_equal(cqm.objective))
        self.assertTrue(new.constraints[constraint].lhs.is_equal(cqm.constraints[constraint].lhs))
예제 #10
0
    def test_functional(self):
        cqm = CQM()

        bqm = BQM({'a': -1}, {'ab': 1}, 1.5, 'SPIN')
        cqm.add_constraint(bqm, '<=')
        cqm.add_constraint(bqm, '>=')
        cqm.set_objective(BQM({'c': -1}, {}, 'SPIN'))
        cqm.add_constraint(Spin('a')*Integer('d')*5 <= 3)

        new = CQM.from_file(cqm.to_file())

        self.assertTrue(cqm.objective.is_equal(new.objective))
        self.assertEqual(set(cqm.constraints), set(new.constraints))
        for label, constraint in cqm.constraints.items():
            self.assertTrue(constraint.lhs.is_equal(new.constraints[label].lhs))
            self.assertEqual(constraint.rhs, new.constraints[label].rhs)
            self.assertEqual(constraint.sense, new.constraints[label].sense)
예제 #11
0
    def test_header(self):
        from dimod.serialization.fileview import read_header

        cqm = CQM()

        x = Binary('x')
        s = Spin('s')
        i = Integer('i')

        cqm.set_objective(x + 3*i + s*x)
        cqm.add_constraint(x*s + x <= 5)
        cqm.add_constraint(i*i + i*s <= 4)

        header_info = read_header(cqm.to_file(), b'DIMODCQM')

        self.assertEqual(header_info.data,
                         {'num_biases': 11, 'num_constraints': 2,
                          'num_quadratic_variables': 4, 'num_variables': 3})
예제 #12
0
 def test_set(self):
     cqm = CQM()
     cqm.set_objective(Spin('a') * 5)
     self.assertEqual(cqm.objective, Spin('a') * 5)