Beispiel #1
0
    def test_energy_equivalence(self):
        binpoly = BinaryPolynomial({'abc': 1, 'bc': 1, 'ab': -1, '': 0}, 'BINARY')
        spipoly = binpoly.to_spin()

        variables = list(binpoly.variables)
        for config in itertools.product((0, 1), repeat=len(variables)):
            binary_sample = dict(zip(variables, config))
            spin_sample = {v: 2*x - 1 for v, x in binary_sample.items()}
            self.assertAlmostEqual(spipoly.energy(spin_sample), binpoly.energy(binary_sample))
 def test_aggregation(self):
     poly = BinaryPolynomial(
         {
             'ab': 1,
             'ba': 1,
             ('a', 'b'): 1,
             ('b', 'a'): 1
         }, 'SPIN')
     self.assertEqual(poly, BinaryPolynomial({'ab': 4}, 'SPIN'))
Beispiel #3
0
    def test_energy_equivalence_only_linear(self):
        spipoly = BinaryPolynomial({'a': 5, 'b': -3}, 'SPIN')
        binpoly = spipoly.to_binary()

        variables = list(binpoly.variables)
        for config in itertools.product((0, 1), repeat=len(variables)):
            binary_sample = dict(zip(variables, config))
            spin_sample = {v: 2*x - 1 for v, x in binary_sample.items()}
            self.assertAlmostEqual(spipoly.energy(spin_sample), binpoly.energy(binary_sample))
 def test_from_dict(self):
     BinaryPolynomial({
         'a': -1,
         tuple(): 1.3,
         'bc': -1,
         ('a', 'b'): 1
     }, 'SPIN')
 def test_repeated_term(self):
     poly = BinaryPolynomial(
         {
             'ab': 1,
             'ba': 1,
             ('a', 'b'): 1,
             ('b', 'a'): 1
         }, 'BINARY')
     self.assertEqual(poly['ab'], 4)
 def test_spin(self):
     poly = BinaryPolynomial({'abc': 1, 'bc': 1, 'ab': -1}, 'SPIN')
     h, J, off = poly.to_hising()
     new = BinaryPolynomial.from_hising(h, J, off)
     self.assertEqual(poly, new)
 def test_from_iterator(self):
     BinaryPolynomial(
         ((term, -1) for term in itertools.combinations(range(100), 2)),
         'SPIN')
 def test_repeated_term(self):
     poly = BinaryPolynomial({('a', 'b'): 1, 'ba': 1}, 'BINARY')
     self.assertEqual(len(poly), 1)
 def test_ignore_terms(self):
     poly = BinaryPolynomial({'a': 1, 'ab': 1, '': 1}, 'BINARY')
     poly.scale(2, ignored_terms=['', 'ba'])
     self.assertEqual(poly['a'], 2)
     self.assertEqual(poly['ba'], 1)
     self.assertEqual(poly[tuple()], 1)
 def test_single_term(self):
     poly = BinaryPolynomial({('a', 'b'): 1}, 'SPIN')
     self.assertIn('ab', poly)
     self.assertIn('ba', poly)
     self.assertIn(('a', 'b'), poly)
     self.assertIn(('b', 'a'), poly)
 def test_unlike_types(self):
     polydict = {'ab': -1, 'a': 1, 'b': 1}
     self.assertEqual(BinaryPolynomial(polydict, 'SPIN'), polydict)
     self.assertNotEqual(BinaryPolynomial(polydict, 'SPIN'), 1)
 def test_swap_partial(self):
     poly = BinaryPolynomial({'abc': 1, 'bc': 1, 'ab': -1}, 'BINARY')
     poly.relabel_variables({'b': 'c', 'c': 'b'})
     self.assertEqual(poly, {'abc': 1, 'bc': 1, 'ac': -1})
 def test_squares_spin(self):
     poly = BinaryPolynomial({'aa': -1}, dimod.SPIN)
     self.assertEqual(poly[()], -1)
 def test_int_division(self):
     # we use division which previous caused issues in python2
     poly = BinaryPolynomial({'a': 4}, 'SPIN')
     poly.normalize(bias_range=1, poly_range=None, ignored_terms=[])
     self.assertEqual(poly['a'], 1)
 def test_ignored(self):
     poly = BinaryPolynomial({'a': 4, 'b': 2}, 'SPIN')
     poly.normalize(bias_range=1, poly_range=None, ignored_terms=['a'])
     self.assertEqual(poly['a'], 4)  # should not be scaled or used in calcs
     self.assertEqual(poly['b'], 1)  # should be normalized correctly
 def test_typical(self):
     poly = BinaryPolynomial({'a': 1, 'ab': 1, '': 1}, 'BINARY')
     poly.normalize(.5)
     self.assertEqual(poly['a'], .5)
     self.assertEqual(poly['ba'], .5)
     self.assertEqual(poly[tuple()], .5)
 def test_empty(self):
     poly = BinaryPolynomial({}, 'SPIN')
     poly.normalize()
 def test_scale_by_float(self):
     poly = BinaryPolynomial({'a': 4}, 'SPIN')
     poly.scale(.25)
     self.assertEqual(poly['a'], 1)
 def test_empty(self):
     poly = BinaryPolynomial([], 'BINARY')
     self.assertEqual(poly.degree, 0)
 def test_copy(self):
     poly = BinaryPolynomial({'abc': 1, 'bc': 1, 'ab': -1}, 'BINARY')
     new = poly.relabel_variables({'a': 'z'}, inplace=False)
     self.assertEqual(new, {'zbc': 1, 'bc': 1, 'zb': -1})
     self.assertEqual(poly, {'abc': 1, 'bc': 1, 'ab': -1})
 def test_squares_binary(self):
     poly = BinaryPolynomial({'aa': -1}, dimod.BINARY)
     self.assertEqual(poly['a'], -1)
 def test_degree3(self):
     poly = BinaryPolynomial.from_hubo({'abc': -1}, 0)
     self.assertEqual(poly.degree, 3)
 def test_cubes_spin(self):
     poly = BinaryPolynomial({'aaa': -1}, dimod.SPIN)
     self.assertEqual(poly['a'], -1)
 def test_degree0(self):
     poly = BinaryPolynomial.from_hising({}, {}, 0)
     self.assertEqual(poly.degree, 0)
 def test_typical(self):
     poly = BinaryPolynomial({'ab': -1, 'a': 1, 'b': 1}, 'BINARY')
     self.assertEqual(len(poly), 3)
     del poly[('a', 'b')]
     self.assertEqual(len(poly), 2)
 def test_spin(self):
     poly = BinaryPolynomial({'abc': 1, 'bc': 1, 'ab': -1, '': 0}, 'SPIN')
     H, off = poly.to_hubo()
     new = BinaryPolynomial.from_hubo(H, off)
     self.assertEqual(poly, new.to_spin())
 def test_single_term(self):
     poly = BinaryPolynomial({('a', 'b'): 1}, 'SPIN')
     self.assertEqual(len(poly), 1)
 def test_binary(self):
     poly = BinaryPolynomial({'abc': 1, 'bc': 1, 'ab': -1}, 'BINARY')
     h, J, off = poly.to_hising()
     new = BinaryPolynomial.from_hising(h, J, off)
     self.assertEqual(poly, new.to_binary())
 def test_binary(self):
     poly = BinaryPolynomial({'abc': 1, 'bc': 1, 'ab': -1, '': 0}, 'BINARY')
     H, off = poly.to_hubo()
     new = BinaryPolynomial.from_hubo(H, off)
     self.assertEqual(poly, new)
 def test_single_variable(self):
     poly = BinaryPolynomial({'a': -1}, 'SPIN')
     poly.scale(.5)
     self.assertEqual(poly['a'], -.5)