示例#1
0
    def test_one_hot_enc_integer(self):
        a = OneHotEncInteger("a", (0, 4), strength=Placeholder("s"))
        H = (a - 3) ** 2
        model = H.compile()
        q, offset = model.to_qubo(feed_dict={"s": 10.0})
        sampleset = dimod.ExactSolver().sample_qubo(q)
        decoded = model.decode_sampleset(
            sampleset, feed_dict={"s": 10.0})
        best = min(decoded, key=lambda x: x.energy)
        self.assertTrue(best.subh["a"]==3)
        self.assertTrue(a.value_range == (0, 4))

        expected_q = {('a[0]', 'a[1]'): 20.0,
             ('a[0]', 'a[2]'): 20.0,
             ('a[0]', 'a[3]'): 20.0,
             ('a[0]', 'a[4]'): 20.0,
             ('a[1]', 'a[2]'): 24.0,
             ('a[1]', 'a[3]'): 26.0,
             ('a[1]', 'a[4]'): 28.0,
             ('a[2]', 'a[3]'): 32.0,
             ('a[2]', 'a[4]'): 36.0,
             ('a[3]', 'a[4]'): 44.0,
             ('a[0]', 'a[0]'): -10.0,
             ('a[1]', 'a[1]'): -15.0,
             ('a[2]', 'a[2]'): -18.0,
             ('a[3]', 'a[3]'): -19.0,
             ('a[4]', 'a[4]'): -18.0}
        expected_offset = 19
        assert_qubo_equal(q, expected_q)
        self.assertTrue(offset == expected_offset)        
示例#2
0
    def test_one_hot_enc_integer(self):
        a = OneHotEncInteger("a", 0, 4, strength=Placeholder("s"))
        H = (a - 3) ** 2
        model = H.compile()
        q, offset = model.to_qubo(feed_dict={"s": 10.0})
        sampleset = dimod.ExactSolver().sample_qubo(q)
        response, broken, e = model.decode_dimod_response(
            sampleset, topk=1, feed_dict={"s": 10.0})[0]
        self.assertTrue(response["a"][0] == 0)
        self.assertTrue(response["a"][1] == 0)
        self.assertTrue(response["a"][2] == 0)
        self.assertTrue(response["a"][3] == 1)
        self.assertTrue(response["a"][4] == 0)
        self.assertTrue(a.interval == (0, 4))

        expected_q = {('a[0]', 'a[1]'): 40.0,
             ('a[0]', 'a[2]'): 40.0,
             ('a[0]', 'a[3]'): 40.0,
             ('a[0]', 'a[4]'): 40.0,
             ('a[1]', 'a[2]'): 44.0,
             ('a[1]', 'a[3]'): 46.0,
             ('a[1]', 'a[4]'): 48.0,
             ('a[2]', 'a[3]'): 52.0,
             ('a[2]', 'a[4]'): 56.0,
             ('a[3]', 'a[4]'): 64.0,
             ('a[0]', 'a[0]'): -20.0,
             ('a[1]', 'a[1]'): -25.0,
             ('a[2]', 'a[2]'): -28.0,
             ('a[3]', 'a[3]'): -29.0,
             ('a[4]', 'a[4]'): -28.0}
        assert_qubo_equal(q, expected_q)
示例#3
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)
示例#4
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)
示例#5
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)
 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)
示例#7
0
 def compile_check(self,
                   exp,
                   expected_qubo,
                   expected_offset,
                   expected_structure,
                   feed_dict=None):
     model = exp.compile(strength=5)
     qubo, offset = model.to_qubo(feed_dict=feed_dict)
     assert_qubo_equal(qubo, expected_qubo)
     self.assertEqual(qubo, expected_qubo)
     self.assertEqual(offset, expected_offset)
     self.assertEqual(model.structure, expected_structure)
示例#8
0
    def test_qubo_equal(self):
        qubo1 = {('a', 'b'): 1.0}
        qubo2 = {('b', 'a'): 1.0}
        qubo3 = {('a', 'b'): 2.0}
        qubo4 = {('b', 'a'): 2.0}
        qubo5 = {('c', 'a'): 1.0}

        assert_qubo_equal(qubo1, qubo2)
        self.assertRaises(AssertionError,
                          lambda: assert_qubo_equal(qubo1, qubo3))
        self.assertRaises(AssertionError,
                          lambda: assert_qubo_equal(qubo1, qubo4))
        self.assertRaises(AssertionError,
                          lambda: assert_qubo_equal(qubo1, qubo5))
示例#9
0
    def test_placeholders(self):
        a, b, p = Binary("a"), Binary("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)

        # Test that `decode_dimod_response` accepts the `feed_dict` arg
        import numpy as np
        import dimod
        n = 10
        p = Placeholder("p")
        feed_dict = {'p': 2}
        A = [np.random.randint(-50, 50) for _ in range(n)]
        S = Array.create('S', n, "BINARY")
        H = sum(p * A[i] * S[i] for i in range(n))**2
        model = H.compile()
        binary_bqm = model.to_dimod_bqm(feed_dict=feed_dict)
        sa = dimod.reference.SimulatedAnnealingSampler()
        response = sa.sample(binary_bqm, num_reads=10, num_sweeps=5)
        # Confirm that decode_dimod_response() accecpts a feed_dict. Test of accuracy delegated to decode_solution()
        decoded_solutions = model.decode_dimod_response(response,
                                                        topk=2,
                                                        feed_dict=feed_dict)
        for (decoded_solution, broken, energy) in decoded_solutions:
            assert isinstance(decoded_solution, dict)
            assert isinstance(broken, dict)
            assert isinstance(energy, float)
示例#10
0
 def test_order_enc_integer(self):
     a = OrderEncInteger("a", (0, 4), strength=Placeholder("s"))
     model = ((a - 3)**2).compile()
     q, offset = model.to_qubo(feed_dict={"s": 10.0})
     expected_q = {
         ('a[3]', 'a[2]'): -8.0,
         ('a[0]', 'a[1]'): -8.0,
         ('a[3]', 'a[0]'): 2.0,
         ('a[2]', 'a[0]'): 2.0,
         ('a[1]', 'a[1]'): 5.0,
         ('a[3]', 'a[1]'): 2.0,
         ('a[2]', 'a[1]'): -8.0,
         ('a[3]', 'a[3]'): 5.0,
         ('a[0]', 'a[0]'): -5.0,
         ('a[2]', 'a[2]'): 5.0
     }
     response = dimod.ExactSolver().sample_qubo(q)
     decoded = model.decode_sampleset(response, feed_dict={"s": 10.0})[0]
     self.assertTrue(decoded.subh["a"] == 3)
     self.assertTrue(a.value_range == (0, 4))
     assert_qubo_equal(q, expected_q)
示例#11
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)
示例#12
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)
示例#13
0
    def test_unary_enc_integer(self):
        a = UnaryEncInteger("a", (0, 3))
        b = UnaryEncInteger("b", (0, 3))
        M = 2.0
        H = (2 * a - b - 1)**2 + M * (a + b - 3)**2
        model = H.compile()
        q, offset = model.to_qubo()
        sampleset = dimod.ExactSolver().sample_qubo(q)
        decoded = model.decode_sampleset(sampleset)[0]
        self.assertTrue(decoded.subh["a"] == 1)
        self.assertTrue(decoded.subh["b"] == 2)
        self.assertTrue(a.value_range == (0, 3))
        self.assertTrue(b.value_range == (0, 3))

        expected_q = {
            ('a[0]', 'a[1]'): 12.0,
            ('a[0]', 'a[2]'): 12.0,
            ('a[0]', 'b[0]'): 0.0,
            ('a[0]', 'b[1]'): 0.0,
            ('a[0]', 'b[2]'): 0.0,
            ('a[1]', 'a[2]'): 12.0,
            ('a[1]', 'b[0]'): 0.0,
            ('a[1]', 'b[1]'): 0.0,
            ('a[1]', 'b[2]'): 0.0,
            ('a[2]', 'b[0]'): 0.0,
            ('a[2]', 'b[1]'): 0.0,
            ('a[2]', 'b[2]'): 0.0,
            ('b[0]', 'b[1]'): 6.0,
            ('b[0]', 'b[2]'): 6.0,
            ('b[1]', 'b[2]'): 6.0,
            ('a[0]', 'a[0]'): -10.0,
            ('a[1]', 'a[1]'): -10.0,
            ('a[2]', 'a[2]'): -10.0,
            ('b[0]', 'b[0]'): -7.0,
            ('b[1]', 'b[1]'): -7.0,
            ('b[2]', 'b[2]'): -7.0
        }
        assert_qubo_equal(q, expected_q)
示例#14
0
    def test_log_enc_integer(self):
        a = LogEncInteger("a", 0, 4)
        b = LogEncInteger("b", 0, 4)
        M = 2.0
        H = (2 * a - b - 1) ** 2 + M * (a + b - 5) ** 2
        model = H.compile()
        q, offset = model.to_qubo()
        sampleset = dimod.ExactSolver().sample_qubo(q)
        response, broken, e = model.decode_dimod_response(sampleset, topk=1)[0]
        sol_a = sum(2 ** k * v for k, v in response["a"].items())
        sol_b = sum(2 ** k * v for k, v in response["b"].items())
        self.assertTrue(sol_a == 2)
        self.assertTrue(sol_b == 3)
        self.assertTrue(a.interval == (0, 4))
        self.assertTrue(b.interval == (0, 4))

        expected_q = {('a[0]', 'a[1]'): 24.0,
             ('a[0]', 'a[2]'): 48.0,
             ('a[0]', 'b[0]'): 0.0,
             ('a[0]', 'b[1]'): 0.0,
             ('a[0]', 'b[2]'): 0.0,
             ('a[1]', 'a[2]'): 96.0,
             ('a[1]', 'b[0]'): 0.0,
             ('a[1]', 'b[1]'): 0.0,
             ('a[1]', 'b[2]'): 0.0,
             ('a[2]', 'b[0]'): 0.0,
             ('a[2]', 'b[1]'): 0.0,
             ('a[2]', 'b[2]'): 0.0,
             ('b[0]', 'b[1]'): 12.0,
             ('b[0]', 'b[2]'): 24.0,
             ('b[1]', 'b[2]'): 48.0,
             ('a[0]', 'a[0]'): -18.0,
             ('a[1]', 'a[1]'): -24.0,
             ('a[2]', 'a[2]'): 0.0,
             ('b[0]', 'b[0]'): -15.0,
             ('b[1]', 'b[1]'): -24.0,
             ('b[2]', 'b[2]'): -24.0}
        assert_qubo_equal(q, expected_q)
示例#15
0
 def test_order_enc_integer(self):
     a = OrderEncInteger("a", 0, 4, strength=Placeholder("s"))
     model = ((a - 3) ** 2).compile()
     q, offset = model.to_qubo(feed_dict={"s": 10.0})
     expected_q = {('a[0]', 'a[1]'): -18.0,
                 ('a[0]', 'a[2]'): 2.0,
                 ('a[0]', 'a[3]'): 2.0,
                 ('a[1]', 'a[2]'): -18.0,
                 ('a[1]', 'a[3]'): 2.0,
                 ('a[2]', 'a[3]'): -18.0,
                 ('a[0]', 'a[0]'): -5.0,
                 ('a[1]', 'a[1]'): 15.0,
                 ('a[2]', 'a[2]'): 15.0,
                 ('a[3]', 'a[3]'): 15.0}
     response = dimod.ExactSolver().sample_qubo(q)
     response, broken, e = model.decode_dimod_response(
         response, topk=1, feed_dict={"s": 10.0})[0]
     self.assertTrue(response["a"][0] == 1)
     self.assertTrue(response["a"][1] == 1)
     self.assertTrue(response["a"][2] == 1)
     self.assertTrue(response["a"][3] == 0)
     self.assertTrue(a.interval == (0, 4))
     assert_qubo_equal(q, expected_q)
示例#16
0
    def test_unary_enc_integer(self):
        a = UnaryEncInteger("a", 0, 3)
        b = UnaryEncInteger("b", 0, 3)
        M = 2.0
        H = (2 * a - b - 1) ** 2 + M * (a + b - 3) ** 2
        model = H.compile()
        q, offset = model.to_qubo()
        sampleset = dimod.ExactSolver().sample_qubo(q)
        response, broken, e = model.decode_dimod_response(sampleset, topk=1)[0]
        self.assertTrue(sum(response["a"].values()) == 1)
        self.assertTrue(sum(response["b"].values()) == 2)
        self.assertTrue(a.interval == (0, 3))
        self.assertTrue(b.interval == (0, 3))

        expected_q = {('a[0]', 'a[1]'): 12.0,
             ('a[0]', 'a[2]'): 12.0,
             ('a[0]', 'b[0]'): 0.0,
             ('a[0]', 'b[1]'): 0.0,
             ('a[0]', 'b[2]'): 0.0,
             ('a[1]', 'a[2]'): 12.0,
             ('a[1]', 'b[0]'): 0.0,
             ('a[1]', 'b[1]'): 0.0,
             ('a[1]', 'b[2]'): 0.0,
             ('a[2]', 'b[0]'): 0.0,
             ('a[2]', 'b[1]'): 0.0,
             ('a[2]', 'b[2]'): 0.0,
             ('b[0]', 'b[1]'): 6.0,
             ('b[0]', 'b[2]'): 6.0,
             ('b[1]', 'b[2]'): 6.0,
             ('a[0]', 'a[0]'): -10.0,
             ('a[1]', 'a[1]'): -10.0,
             ('a[2]', 'a[2]'): -10.0,
             ('b[0]', 'b[0]'): -7.0,
             ('b[1]', 'b[1]'): -7.0,
             ('b[2]', 'b[2]'): -7.0}
        assert_qubo_equal(q, expected_q)