Пример #1
0
 def test_order_enc_integer_more_than(self):
     a = OrderEncInteger("a", (0, 4), strength=5.0)
     b = OrderEncInteger("b", (0, 4), strength=5.0)
     model = ((a - b) ** 2 + (1 - a.more_than(1)) ** 2 + (1 - b.less_than(3)) ** 2).compile()
     q, offset = model.to_qubo()
     sampleset = dimod.ExactSolver().sample_qubo(q)
     decoded = model.decode_sampleset(sampleset)
     best = min(decoded, key=lambda x: x.energy)
     self.assertTrue(best.subh["a"]==2)
     self.assertTrue(best.subh["b"]==2)
Пример #2
0
 def test_order_enc_integer_more_than(self):
     a = OrderEncInteger("a", 0, 4, strength=5.0)
     b = OrderEncInteger("b", 0, 4, strength=5.0)
     model = ((a - b) ** 2 + (1 - a.more_than(1)) ** 2 + (1 - b.less_than(3)) ** 2).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(response["a"][0] == 1)
     self.assertTrue(response["a"][1] == 1)
     self.assertTrue(response["a"][2] == 0)
     self.assertTrue(response["a"][3] == 0)
     self.assertTrue(response["b"][0] == 1)
     self.assertTrue(response["b"][1] == 1)
     self.assertTrue(response["b"][2] == 0)
     self.assertTrue(response["b"][3] == 0)
Пример #3
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)
Пример #4
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})
     best = min(decoded, key=lambda x: x.energy)
     self.assertTrue(best.subh["a"]==3)
     self.assertTrue(a.value_range == (0, 4))
Пример #5
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)