Beispiel #1
0
    def test_from_configurations(self):
        const = dwavebinarycsp.Constraint.from_configurations([(-1, 1), (1, 1)], ['a', 'b'], dwavebinarycsp.SPIN)

        dcspt.assert_consistent_constraint(const)

        self.assertEqual(const.configurations, frozenset([(-1, 1), (1, 1)]))
        self.assertEqual(const.variables, ('a', 'b'))
    def test__or__(self):
        eq_a_b = dwavebinarycsp.Constraint.from_func(operator.eq, ['a', 'b'],
                                                     dwavebinarycsp.SPIN)
        ne_b_c = dwavebinarycsp.Constraint.from_func(operator.ne, ['c', 'b'],
                                                     dwavebinarycsp.SPIN)

        const = eq_a_b | ne_b_c

        dcspt.assert_consistent_constraint(const)

        self.assertEqual(const.variables, ('a', 'b', 'c'))

        self.assertTrue(const.check({
            'a': 0,
            'b': 0,
            'c': 0
        }))  # only eq_a_b is satisfied
        self.assertFalse(const.check({
            'a': 1,
            'b': 0,
            'c': 0
        }))  # neither satisfied
        self.assertTrue(const.check({
            'a': 0,
            'b': 1,
            'c': 0
        }))  # only ne_b_c is satisfied
Beispiel #3
0
    def test_XOR(self):
        or_ = constraint.xor_gate([0, 1, 2], vartype=dwavebinarycsp.BINARY)
        dcspt.assert_consistent_constraint(or_)

        self.assertEqual(or_.name, 'XOR')

        or_ = constraint.xor_gate([0, 1, 2], vartype=dwavebinarycsp.SPIN)
        dcspt.assert_consistent_constraint(or_)

        self.assertEqual(or_.name, 'XOR')
Beispiel #4
0
    def test_AND(self):
        and_ = constraint.and_gate([0, 1, 2], vartype=dwavebinarycsp.BINARY)
        dcspt.assert_consistent_constraint(and_)

        self.assertEqual(and_.name, 'AND')

        and_ = constraint.and_gate([0, 1, 2], vartype=dwavebinarycsp.SPIN)
        dcspt.assert_consistent_constraint(and_)

        self.assertEqual(and_.name, 'AND')
Beispiel #5
0
    def test__and__(self):
        eq_a_b = dwavebinarycsp.Constraint.from_func(operator.eq, ['a', 'b'], dwavebinarycsp.SPIN)
        ne_b_c = dwavebinarycsp.Constraint.from_func(operator.ne, ['c', 'b'], dwavebinarycsp.SPIN)

        const = eq_a_b & ne_b_c

        dcspt.assert_consistent_constraint(const)

        self.assertEqual(const.configurations, frozenset([(-1, -1, 1), (1, 1, -1)]))
        self.assertEqual(const.variables, ('a', 'b', 'c'))
Beispiel #6
0
    def test_FULL_ADDER(self):
        or_ = constraint.fulladder_gate([0, 1, 2, 'a', 'b'],
                                        vartype=dwavebinarycsp.BINARY)
        dcspt.assert_consistent_constraint(or_)

        self.assertEqual(or_.name, 'FULL_ADDER')

        or_ = constraint.fulladder_gate([0, 1, 2, 'a', 'b'],
                                        vartype=dwavebinarycsp.SPIN)
        dcspt.assert_consistent_constraint(or_)

        self.assertEqual(or_.name, 'FULL_ADDER')
Beispiel #7
0
    def test_HALF_ADDER(self):
        or_ = constraint.halfadder_gate([0, 1, 2, 3],
                                        vartype=dwavebinarycsp.BINARY)
        dcspt.assert_consistent_constraint(or_)

        self.assertEqual(or_.name, 'HALF_ADDER')

        or_ = constraint.halfadder_gate([0, 1, 2, 3],
                                        vartype=dwavebinarycsp.SPIN)
        dcspt.assert_consistent_constraint(or_)

        self.assertEqual(or_.name, 'HALF_ADDER')
Beispiel #8
0
    def test_sat2in4(self):
        const = constraint.sat2in4(['a', 'b', 'c', 'd'])
        dcspt.assert_consistent_constraint(const)

        valid = set()
        for u, v in itertools.combinations(range(4), 2):
            config = [0, 0, 0, 0]
            config[u], config[v] = 1, 1

            valid.add(tuple(config))

        for config in itertools.product([0, 1], repeat=4):
            if config in valid:
                self.assertTrue(const.func(*config))
            else:
                self.assertFalse(const.func(*config))
    def test_negate_variables_spi(self):
        const = dwavebinarycsp.Constraint.from_configurations(
            [(-1, 1), (1, -1)], ['a', 'b'], vartype=dwavebinarycsp.SPIN)

        const.flip_variable('a')

        dcspt.assert_consistent_constraint(const)

        self.assertEqual(const.configurations, frozenset([(1, 1), (-1, -1)]))

        #

        const = dwavebinarycsp.Constraint.from_configurations(
            [(-1, 1), (1, -1)], ['a', 'b'], vartype=dwavebinarycsp.SPIN)

        const.flip_variable('b')

        dcspt.assert_consistent_constraint(const)

        self.assertEqual(const.configurations, frozenset([(1, 1), (-1, -1)]))

        #

        const = dwavebinarycsp.Constraint.from_configurations(
            [(-1, 1, 1), (1, -1, -1)], ['a', 'b', 'c'],
            vartype=dwavebinarycsp.SPIN)

        const.flip_variable('b')

        dcspt.assert_consistent_constraint(const)

        self.assertEqual(const.configurations,
                         frozenset([(1, 1, -1), (-1, -1, 1)]))
    def test__len__(self):

        const = dwavebinarycsp.Constraint.from_func(operator.eq, ['a', 'b'],
                                                    dwavebinarycsp.SPIN)
        dcspt.assert_consistent_constraint(const)
        self.assertEqual(len(const), 2)
Beispiel #11
0
    def test_sat2in4_with_negation(self):
        const = constraint.sat2in4(pos=('a', 'd'), neg=('c', 'b'))
        dcspt.assert_consistent_constraint(const)

        self.assertTrue(const.check({'a': 1, 'b': 1, 'c': 1, 'd': 1}))