示例#1
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)
示例#2
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)
示例#3
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
示例#4
0
    def test_superset(self):
        a = Integer('a')
        b = Binary('b')

        qm = a + a * b + 1.5

        self.assertEqual(qm.energy({'a': 1, 'b': 1, 'c': 1}), 3.5)
        self.assertEqual(qm.energy({'a': 1, 'b': 0, 'c': 1}), 2.5)
示例#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_div_number(self):
        i, j = dimod.Integers('ij')
        x = Binary('x')

        qm = 6 * i * j + 2 * i * x + 4 * x + 12
        ref = qm
        qm /= 2
        self.assertIs(qm, ref)
        self.assertTrue(qm.is_equal(3 * i * j + i * x + 2 * x + 6))
示例#7
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)
示例#8
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, {})
示例#9
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)
示例#10
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))
示例#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_no_constraints(self):
     cqm = CQM.from_bqm(-Binary('a') + Binary('a')*Binary('b') + 1.5)
     self.assertEqual(cqm.violations({'a': 1, 'b': 1}), {})
示例#13
0
    def test_copy(self):
        x = Binary('x')

        newx = quicksum([x])

        self.assertIsNot(newx, x)