Exemplo n.º 1
0
 def test_equality_of_qbit(self):
     a, b, c = Binary("a"), Binary("b"), Binary("a")
     self.assertTrue(a == c)
     self.assertFalse(a != c)
     self.assertTrue(hash(a) == hash(c))
     self.assertTrue(a != b)
     self.assertTrue(a != Spin("a"))
Exemplo n.º 2
0
 def test_compile_div(self):
     a, b = Binary("a"), Binary("b")
     exp = a * b / 2 + 1
     expected_qubo = {('a', 'a'): 0.0, ('a', 'b'): 0.5, ('b', 'b'): 0.0}
     expected_offset = 1.0
     q, offset = exp.compile().to_qubo()
     self.compile_check(exp, expected_qubo, expected_offset)
Exemplo n.º 3
0
 def test_compile_power(self):
     a, b = Binary("a"), Binary("b")
     exp = (a + b)**3
     expected_qubo = {('a', 'a'): 1.0, ('a', 'b'): 6.0, ('b', 'b'): 1.0}
     expected_offset = 0.0
     q, offset = exp.compile().to_qubo()
     self.compile_check(exp, expected_qubo, expected_offset)
Exemplo n.º 4
0
 def test_compile_expand_add(self):
     a, b = Binary("a"), Binary("b")
     exp = (a + b) * (a - b)
     # expected_qubo = {('a', 'a'): 1.0, ('a', 'b'): 0.0, ('b', 'b'): -1.0}
     expected_qubo = {('a', 'a'): 1.0, ('b', 'b'): -1.0}
     expected_offset = 0.0
     self.compile_check(exp, expected_qubo, expected_offset)
Exemplo n.º 5
0
 def test_compile_binary(self):
     a, b = Binary("a"), Binary("b")
     exp = 1 + a * b + a - 2
     # expected_qubo = {('a', 'a'): 1.0, ('a', 'b'): 1.0, ('b', 'b'): 0.0}
     expected_qubo = {('a', 'a'): 1.0, ('a', 'b'): 1.0}
     expected_offset = -1
     self.compile_check(exp, expected_qubo, expected_offset)
Exemplo n.º 6
0
    def test_compile_reduce_degree(self):
        a, b, c, d = Binary("a"), Binary("b"), Binary("c"), Binary("d")
        exp = a * b * c + b * c * d
        expected_qubo = {
            ('a', 'a'): 0.0,
            ('a', 'b*c'): 1.0,
            ('b', 'b'): 0.0,
            ('b', 'b*c'): -10.0,
            ('b', 'c'): 5.0,
            ('c', 'c'): 0.0,
            ('b*c', 'c'): -10.0,
            ('b*c', 'b*c'): 15.0,
            ('b*c', 'd'): 1.0,
            ('d', 'd'): 0.0
        }
        expected_offset = 0.0
        expected_structure = {
            'a': ('a', ),
            'b': ('b', ),
            'c': ('c', ),
            'd': ('d', )
        }

        self.compile_check(exp, expected_qubo, expected_offset,
                           expected_structure)
Exemplo n.º 7
0
 def test_or(self):
     a, b = Binary('a'), Binary('b')
     exp = Or(a, b)
     model = exp.compile()
     for a, b in itertools.product(*[(0, 1)] * 2):
         e = int(model.energy({'a': a, 'b': b}, vartype='BINARY'))
         self.assertEqual(int(a + b > 0), e)
Exemplo n.º 8
0
 def test_to_qubo_with_index(self):
     a, b = Binary("a"), Binary("b")
     exp = 1 + a * b + a - 2
     model = exp.compile()
     qubo, offset = model.to_qubo(index_label=True)
     assert_qubo_equal(qubo, {(0, 0): 1.0, (0, 1): 1.0, (1, 1): 0.0})
     self.assertTrue(offset == -1)
Exemplo n.º 9
0
 def test_compile_3rd_order(self):
     a, b = Binary("a"), Binary("b")
     exp = (a + b - 2)**3
     expected_qubo = {('a', 'a'): 7.0, ('a', 'b'): -6.0, ('b', 'b'): 7.0}
     expected_offset = -8.0
     expected_structure = {'a': ('a', ), 'b': ('b', )}
     self.compile_check(exp, expected_qubo, expected_offset,
                        expected_structure)
Exemplo n.º 10
0
 def test_compile_2nd_order(self):
     a, b = Binary("a"), Binary("b")
     exp = (Add(a, b) - 3)**2
     expected_qubo = {('a', 'a'): -5.0, ('a', 'b'): 2.0, ('b', 'b'): -5.0}
     expected_offset = 9.0
     expected_structure = {'a': ('a', ), 'b': ('b', )}
     self.compile_check(exp, expected_qubo, expected_offset,
                        expected_structure)
Exemplo n.º 11
0
 def test_compile_expand(self):
     a, b = Binary("a"), Binary("b")
     exp = (a + b) * (a - b)
     expected_qubo = {('a', 'a'): 1.0, ('a', 'b'): 0.0, ('b', 'b'): -1.0}
     expected_offset = 0.0
     expected_structure = {'a': ('a', ), 'b': ('b', )}
     self.compile_check(exp, expected_qubo, expected_offset,
                        expected_structure)
Exemplo n.º 12
0
 def test_to_qubo(self):
     a, b = Binary("a"), Binary("b")
     exp = 1 + a * b + a - 2
     model = exp.compile()
     qubo, offset = model.to_qubo()
     # assert_qubo_equal(qubo, {("a", "a"): 1.0, ("a", "b"): 1.0, ("b", "b"): 0.0})
     assert_qubo_equal(qubo, {("a", "a"): 1.0, ("a", "b"): 1.0})
     self.assertTrue(offset == -1)
Exemplo n.º 13
0
 def test_compile_div(self):
     a, b = Binary("a"), Binary("b")
     exp = (a + b - 2) / 2
     expected_qubo = {('a', 'a'): 0.5, ('b', 'b'): 0.5}
     expected_offset = -1
     expected_structure = {'a': ('a', ), 'b': ('b', )}
     self.compile_check(exp, expected_qubo, expected_offset,
                        expected_structure)
Exemplo n.º 14
0
 def test_compile_subh(self):
     a, b = Binary("a"), Binary("b")
     p = Placeholder("p")
     exp = p * SubH((a + b - 1)**2, label="subh") + a * b
     expected_qubo = {('a', 'a'): -3.0, ('a', 'b'): 7.0, ('b', 'b'): -3.0}
     expected_offset = 3
     feed_dict = {"p": 3}
     self.compile_check(exp, expected_qubo, expected_offset, feed_dict)
Exemplo n.º 15
0
 def test_to_ising_with_index(self):
     a, b = Binary("a"), Binary("b")
     exp = 1 + a * b + a - 2
     model = exp.compile()
     linear, quad, offset = model.to_ising(index_label=True)
     self.assertTrue(linear == {0: 0.75, 1: 0.25})
     assert_qubo_equal(quad, {(0, 1): 0.25})
     self.assertTrue(offset == -0.25)
Exemplo n.º 16
0
 def test_not(self):
     a, b = Binary("a"), Binary("b")
     exp = NotConst(a, b, label="not")
     model = exp.compile()
     self.assertTrue(model.energy({"a": 1, "b": 0}, vartype="BINARY") == 0)
     self.assertTrue(model.energy({"a": 0, "b": 1}, vartype="BINARY") == 0)
     self.assertTrue(model.energy({"a": 1, "b": 1}, vartype="BINARY") > 0)
     self.assertTrue(model.energy({"a": 0, "b": 0}, vartype="BINARY") > 0)
Exemplo n.º 17
0
 def test_to_ising(self):
     a, b = Binary("a"), Binary("b")
     exp = 1 + a * b + a - 2
     model = exp.compile()
     linear, quad, offset = model.to_ising()
     self.assertTrue(linear == {'a': 0.75, 'b': 0.25})
     assert_qubo_equal(quad, {('a', 'b'): 0.25})
     self.assertTrue(offset == -0.25)
Exemplo n.º 18
0
 def test_compile(self):
     a, b = Binary("a"), Binary("b")
     exp = 1 + a * b + a - 2
     expected_qubo = {('a', 'a'): 1.0, ('a', 'b'): 1.0, ('b', 'b'): 0.0}
     expected_offset = -1
     expected_structure = {'a': ('a', ), 'b': ('b', )}
     self.compile_check(exp, expected_qubo, expected_offset,
                        expected_structure)
Exemplo n.º 19
0
    def test_array_error(self):
        # raise ValueError when the size of the sub lists is not same
        with self.assertRaises(ValueError):
            Array([[Binary('x0'), Binary('x1')], [Binary('x2')]])

        # raise TypeError when index is neither int nor tuple[int]
        with self.assertRaises(TypeError):
            array = Array([Binary('x0'), Binary('x1')])
            array[1.5]
Exemplo n.º 20
0
 def test_compile_placeholder(self):
     a, b = Binary("a"), Binary("b")
     p, q, r = Placeholder("p"), Placeholder("q"), Placeholder("r")
     exp = r * (q * p * (a + b)**2 + q)
     expected_qubo = {('a', 'a'): 12.0, ('a', 'b'): 24.0, ('b', 'b'): 12.0}
     expected_offset = 4
     feed_dict = {"p": 3, "q": 2, "r": 2}
     q, offset = exp.compile().to_qubo(feed_dict=feed_dict)
     self.compile_check(exp, expected_qubo, expected_offset, feed_dict)
Exemplo n.º 21
0
 def test_xor(self):
     a, b = Binary('a'), Binary('b')
     exp = Xor(a, b)
     model = exp.compile()
     for a, b in itertools.product(*[(0, 1)] * 2):
         e = int(model.energy({'a': a, 'b': b}, vartype='BINARY'))
         if (a == 1 and b == 0) or (a == 0 and b == 1):
             self.assertTrue(e == 1)
         else:
             self.assertTrue(e == 0)
Exemplo n.º 22
0
    def test_array_division(self):
        array1 = Array.create('x', (2, 2), 'BINARY')
        expected = Array([[Binary('x[0][0]') / 2.0,
                           Binary('x[0][1]') / 2.0],
                          [Binary('x[1][0]') / 2.0,
                           Binary('x[1][1]') / 2.0]])
        self.assertTrue(array1 / 2.0 == expected)

        self.assertRaises(ValueError, lambda: 1 / array1)
        self.assertRaises(ValueError, lambda: array1 / array1)
Exemplo n.º 23
0
 def test_sub_qubo(self):
     a, b, c = Binary("a"), Binary("b"), Placeholder("c")
     exp = 1 + a * b + c * (a - 2)
     model = exp.compile()
     var_set = VarSetFromVarLabels([b])
     sample_sol = {'a': 0, 'b': 1}
     sub_qubo, offset = model.sub_qubo(var_set,
                                       sample_sol,
                                       feed_dict={'c': 3.0})
     self.assertTrue(sub_qubo == {("b", "b"): 0.0})
     self.assertTrue(offset == -5.0)
Exemplo n.º 24
0
 def test_equality_of_const(self):
     c1 = Constraint(Binary("a"), label="c1")
     c2 = Constraint(Binary("b"), label="c1")
     c3 = Constraint(Binary("a"), label="c3")
     c4 = Constraint(Binary("a"), label="c1")
     self.assertTrue(c1 == c4)
     self.assertFalse(c1 != c4)
     self.assertTrue(hash(c1) == hash(c4))
     self.assertTrue(c1 != c2)
     self.assertTrue(c1 != c3)
     self.assertTrue(c1 != Binary("a"))
Exemplo n.º 25
0
    def test_and_from_model(self):
        a, b, c = Binary("a"), Binary("b"), Binary("c")
        exp = (SubH(a + b, 'n1') + SubH(b + c, 'n2'))**2
        model = exp.compile()

        set_x = VarSetFromSubH('n1')
        set_y = VarSetFromSubH('n2')
        set_z = AndVars(set_x, set_y)

        self.assertTrue(set_x.var_names(model) == {'a', 'b'})
        self.assertTrue(set_y.var_names(model) == {'b', 'c'})
        self.assertTrue(set_z.var_names(model) == {'b'})
Exemplo n.º 26
0
 def test_sub_ising(self):
     a, b, c = Binary("a"), Binary("b"), Placeholder("c")
     exp = 1 + a * b + c * (a - 2)
     model = exp.compile()
     var_set = VarSetFromVarLabels([b])
     sample_sol = {'a': 0, 'b': 1}
     linear, quad, offset = model.sub_ising(var_set,
                                            sample_sol,
                                            feed_dict={'c': 3.0})
     self.assertTrue(linear == {'b': 0.0})
     self.assertTrue(quad == {})
     self.assertTrue(offset == -5.0)
Exemplo n.º 27
0
    def test_or_from_vars(self):
        a, b, c = Binary("a"), Binary("b"), Binary("c")
        exp = (SubH(a + b, 'n1') + SubH(b + c, 'n2'))**2
        model = exp.compile()

        set_x = VarSetFromVarLabels([a, b])
        set_y = VarSetFromVarLabels([b, c])
        set_z = OrVars(set_x, set_y)

        self.assertTrue(set_x.var_names(model) == {'a', 'b'})
        self.assertTrue(set_y.var_names(model) == {'b', 'c'})
        self.assertTrue(set_z.var_names(model) == {'a', 'b', 'c'})
Exemplo n.º 28
0
 def test_or(self):
     a, b, c = Binary("a"), Binary("b"), Binary("c")
     exp = OrConst(a, b, c, label="or")
     model = exp.compile()
     self.assertTrue(
         model.energy({
             "a": 1,
             "b": 1,
             "c": 1
         }, vartype="BINARY") == 0)
     self.assertTrue(
         model.energy({
             "a": 1,
             "b": 0,
             "c": 1
         }, vartype="BINARY") == 0)
     self.assertTrue(
         model.energy({
             "a": 0,
             "b": 1,
             "c": 1
         }, vartype="BINARY") == 0)
     self.assertTrue(
         model.energy({
             "a": 0,
             "b": 0,
             "c": 0
         }, vartype="BINARY") == 0)
     self.assertTrue(
         model.energy({
             "a": 1,
             "b": 1,
             "c": 0
         }, vartype="BINARY") > 0)
     self.assertTrue(
         model.energy({
             "a": 1,
             "b": 0,
             "c": 0
         }, vartype="BINARY") > 0)
     self.assertTrue(
         model.energy({
             "a": 0,
             "b": 1,
             "c": 0
         }, vartype="BINARY") > 0)
     self.assertTrue(
         model.energy({
             "a": 0,
             "b": 0,
             "c": 1
         }, vartype="BINARY") > 0)
Exemplo n.º 29
0
 def test_compile_constraint(self):
     a, b, c = Binary("a"), Binary("b"), Binary("c")
     exp = Constraint(a * b * c, label="constraint")
     # expected_qubo = {('a', '0*1'): -10.0, ('b', '0*1'): -10.0, ('0*1', '0*1'): 15.0, ('a', 'a'): 0.0, ('a', 'b'): 5.0, ('c', '0*1'): 1.0, ('b', 'b'): 0.0, ('c', 'c'): 0.0}
     expected_qubo = {
         ('a', 'a * b'): -10.0,
         ('b', 'a * b'): -10.0,
         ('a * b', 'a * b'): 15.0,
         ('a', 'b'): 5.0,
         ('c', 'a * b'): 1.0
     }
     expected_offset = 0
     self.compile_check(exp, expected_qubo, expected_offset, feed_dict={})
Exemplo n.º 30
0
    def test_compile_with_penalty(self):
        class CustomPenalty(WithPenalty):
            def __init__(self, hamiltonian, penalty, label):
                super().__init__(hamiltonian, penalty, label)

        a, b = Binary("a"), Binary("b")
        p = Placeholder("p")
        custom_penalty = p * CustomPenalty(a + b, a * b, "label")
        expected_qubo = {('a', 'a'): 2.0, ('a', 'b'): 1.0, ('b', 'b'): 2.0}
        expected_offset = 0.0
        feed_dict = {"p": 2}
        self.compile_check(custom_penalty, expected_qubo, expected_offset,
                           feed_dict)