Esempio n. 1
0
 def test_equality_of_qbit(self):
     a, b, c = Qbit("a"), Qbit("b"), Qbit("a")
     self.assertTrue(a == c)
     self.assertFalse(a != c)
     self.assertTrue(hash(a) == hash(c))
     self.assertTrue(a != b)
     self.assertTrue(a != Spin("a"))
Esempio n. 2
0
    def test_compile_reduce_degree(self):
        a, b, c, d = Qbit("a"), Qbit("b"), Qbit("c"), Qbit("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)
Esempio n. 3
0
 def test_or(self):
     a, b = Qbit('a'), Qbit('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}, var_type='binary'))
         self.assertEqual(int(a + b > 0), e)
Esempio n. 4
0
 def test_and(self):
     a, b = Qbit('a'), Qbit('b')
     exp = And(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(a * b, e)
Esempio n. 5
0
 def test_to_qubo_with_index(self):
     a, b = Qbit("a"), Qbit("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)
Esempio n. 6
0
 def test_compile_div(self):
     a, b = Qbit("a"), Qbit("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)
Esempio n. 7
0
 def test_to_ising_with_index(self):
     a, b = Qbit("a"), Qbit("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)
Esempio n. 8
0
 def test_compile_expand(self):
     a, b = Qbit("a"), Qbit("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)
Esempio n. 9
0
 def test_to_ising(self):
     a, b = Qbit("a"), Qbit("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)
Esempio n. 10
0
 def test_compile_2nd_order(self):
     a, b = Qbit("a"), Qbit("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)
Esempio n. 11
0
 def test_compile_3rd_order(self):
     a, b = Qbit("a"), Qbit("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)
Esempio n. 12
0
 def test_compile(self):
     a, b = Qbit("a"), Qbit("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)
Esempio n. 13
0
 def test_not(self):
     a, b = Qbit("a"), Qbit("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)
Esempio n. 14
0
 def test_xor(self):
     a, b, c = Qbit("a"), Qbit("b"), Qbit("c")
     exp = XorConst(a, b, c, label="xor")
     model = exp.compile()
     self.assertTrue(model.energy({"a": 1, "b": 1, "c": 0, "aux_xor": 1}, vartype="BINARY") == 0)
     self.assertTrue(model.energy({"a": 1, "b": 0, "c": 1, "aux_xor": 0}, vartype="BINARY") == 0)
     self.assertTrue(model.energy({"a": 0, "b": 1, "c": 1, "aux_xor": 0}, vartype="BINARY") == 0)
     self.assertTrue(model.energy({"a": 1, "b": 1, "c": 0, "aux_xor": 1}, vartype="BINARY") == 0)
     self.assertTrue(model.energy({"a": 0, "b": 0, "c": 1, "aux_xor": 1}, vartype="BINARY") > 0)
     self.assertTrue(model.energy({"a": 1, "b": 1, "c": 1, "aux_xor": 1}, vartype="BINARY") > 0)
Esempio n. 15
0
 def test_to_qubo(self):
     a, b = Qbit("a"), Qbit("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
     })
     self.assertTrue(offset == -1)
Esempio n. 16
0
 def test_equality_of_const(self):
     c1 = Constraint(Qbit("a"), label="c1")
     c2 = Constraint(Qbit("b"), label="c1")
     c3 = Constraint(Qbit("a"), label="c3")
     c4 = Constraint(Qbit("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 != Qbit("a"))
Esempio n. 17
0
 def test_compile_const(self):
     a, b, w = Qbit("a"), Qbit("b"), Placeholder("w")
     exp = Constraint(Constraint(w * (a + b - 1), label="const1") +
                      Constraint((a + b - 1)**2, label="const2"),
                      label="const_all")
     expected_qubo = {('a', 'a'): 2.0, ('a', 'b'): 2.0, ('b', 'b'): 2.0}
     expected_offset = -2.0
     expected_structure = {'a': ('a', ), 'b': ('b', )}
     self.compile_check(exp,
                        expected_qubo,
                        expected_offset,
                        expected_structure,
                        feed_dict={"w": 3.0})
Esempio n. 18
0
 def test_compile_placeholder2(self):
     a, b, w, v = Qbit("a"), Qbit("b"), Placeholder("w"), Placeholder("v")
     exp = v * w * (a + b - 2) + v
     expected_qubo = {('a', 'a'): 15.0, ('b', 'b'): 15.0}
     expected_offset = -25
     expected_structure = {'a': ('a', ), 'b': ('b', )}
     self.compile_check(exp,
                        expected_qubo,
                        expected_offset,
                        expected_structure,
                        feed_dict={
                            "w": 3.0,
                            "v": 5.0
                        })
Esempio n. 19
0
 def test_equality_of_add(self):
     exp1 = Add(Qbit("a"), Qbit("b"))
     exp2 = Add(Qbit("b"), Qbit("a"))
     exp3 = Add(Qbit("a"), Qbit("a"))
     exp4 = Add(Qbit("a"), Qbit("b"))
     exp5 = Add(Qbit("a"), 1)
     self.assertTrue(exp1 == exp2)
     self.assertTrue(exp1 == exp4)
     self.assertTrue(hash(exp1) == hash(exp2))
     self.assertTrue(exp1 != exp3)
     self.assertFalse(exp1 == exp5)
     self.assertFalse(exp1 == Qbit("a"))
     self.assertFalse(exp1 == exp3)
     self.assertEqual(repr(exp1), "(Qbit(a)+Qbit(b))")
 def test_equality_of_express_with_const(self):
     a, b = Qbit("a"), Spin("b")
     exp = a + b - 1 + Constraint(a * b, label="const")
     expected_exp = AddList(
         [a, Num(-1.0), b,
          Constraint(Mul(a, b), label="const")])
     self.assertTrue(exp == expected_exp)
Esempio n. 21
0
 def test_equality_of_placeholder(self):
     p1 = Placeholder("p1")
     p2 = Placeholder("p2")
     p3 = Placeholder("p1")
     self.assertTrue(p1 == p3)
     self.assertTrue(hash(p1) == hash(p3))
     self.assertTrue(p1 != p2)
     self.assertTrue(p1 != Qbit("a"))
Esempio n. 22
0
 def test_equality_of_spin(self):
     a, b, c = Spin("a"), Spin("b"), Spin("a")
     self.assertTrue(a == c)
     self.assertFalse(a != c)
     self.assertTrue(hash(a) == hash(c))
     self.assertTrue(a != b)
     self.assertTrue(a != Qbit("a"))
     self.assertEqual(repr(a), "Spin(a)")
 def test_equality_of_param(self):
     p1 = Param("p1")
     p2 = Param("p2")
     p3 = Param("p1")
     self.assertTrue(p1 == p3)
     self.assertTrue(hash(p1) == hash(p3))
     self.assertTrue(p1 != p2)
     self.assertTrue(p1 != Qbit("a"))
Esempio n. 24
0
 def test_compile_spin(self):
     a, b = Qbit("a"), Spin("b")
     exp = a * b
     expected_qubo = {('a', 'a'): -1.0, ('a', 'b'): 2.0, ('b', 'b'): 0.0}
     expected_offset = 0.0
     expected_structure = {'a': ('a', ), 'b': ('b', )}
     self.compile_check(exp, expected_qubo, expected_offset,
                        expected_structure)
Esempio n. 25
0
    def test_not(self):
        a = Qbit('a')
        exp = Not(a)
        model = exp.compile()
        for a in [0, 1]:
            e = int(model.energy({'a': a}, var_type='binary'))
            self.assertEqual(1 - a, e)

        self.assertEqual(repr(exp), "Not(((Qbit(a)*Num(-1))+Num(1)))")
Esempio n. 26
0
 def test_equality_of_add_list(self):
     exp1 = AddList([Qbit("a"), Qbit("b")])
     exp2 = AddList([Qbit("b"), Qbit("a")])
     exp3 = AddList([Qbit("a"), Qbit("a")])
     self.assertTrue(exp1 == exp2)
     self.assertTrue(hash(exp1) == hash(exp2))
     self.assertFalse(exp1 == exp3)
     self.assertFalse(exp1 == Qbit("a"))
Esempio n. 27
0
 def test_equality_of_mul(self):
     exp1 = Mul(Qbit("a"), Qbit("b"))
     exp2 = Mul(Qbit("b"), Qbit("a"))
     exp3 = Mul(Qbit("a"), Qbit("a"))
     self.assertTrue(exp1 == exp2)
     self.assertTrue(hash(exp1) == hash(exp2))
     self.assertFalse(exp1 == exp3)
     self.assertTrue(exp1 != Qbit("a"))
Esempio n. 28
0
 def test_compile_placeholder3(self):
     a, v = Qbit("a"), Placeholder("v")
     exp = v * v * a + v
     expected_qubo = {('a', 'a'): 25.0}
     expected_offset = 5
     expected_structure = {'a': ('a', )}
     self.compile_check(exp,
                        expected_qubo,
                        expected_offset,
                        expected_structure,
                        feed_dict={"v": 5.0})
Esempio n. 29
0
 def test_placeholders(self):
     a, b, p = Qbit("a"), Qbit("b"), Placeholder("p")
     feed_dict = {'p': 2.0}
     exp = p * (1 + a * b + a)
     model = exp.compile()
     qubo, offset = model.to_qubo(index_label=False, feed_dict=feed_dict)
     dict_solution = {'a': 1, 'b': 0}
     dict_energy = model.energy(dict_solution,
                                vartype="BINARY",
                                feed_dict=feed_dict)
     list_solution = [1, 0]
     list_energy = model.energy(list_solution,
                                vartype="BINARY",
                                feed_dict=feed_dict)
     assert_qubo_equal(qubo, {
         ('a', 'b'): 2.0,
         ('a', 'a'): 2.0,
         ('b', 'b'): 0.0
     })
     self.assertEqual(offset, 2.0)
     self.assertTrue(dict_energy, 2.0)
     self.assertTrue(list_energy, 2.0)
Esempio n. 30
0
 def test_params(self):
     a, b, p = Qbit("a"), Qbit("b"), Param("p")
     params = {'p': 2.0}
     exp = p * (1 + a * b + a)
     model = exp.compile()
     qubo, offset = model.to_qubo(index_label=False, params=params)
     dict_solution = {'a': 1, 'b': 0}
     dict_energy = model.energy(dict_solution,
                                var_type="binary",
                                params=params)
     list_solution = [1, 0]
     list_energy = model.energy(list_solution,
                                var_type="binary",
                                params=params)
     assert_qubo_equal(qubo, {
         ('a', 'b'): 2.0,
         ('a', 'a'): 2.0,
         ('b', 'b'): 0.0
     })
     self.assertEqual(offset, 2.0)
     self.assertTrue(dict_energy, 2.0)
     self.assertTrue(list_energy, 2.0)