Exemplo n.º 1
0
 def test_squares_spin(self):
     poly = BinaryPolynomial({'aa': -1}, dimod.SPIN)
     self.assertEqual(poly[()], -1)
Exemplo n.º 2
0
 def test_typical(self):
     poly = BinaryPolynomial({'a': 1, 'ab': 1, '': 1}, 'BINARY')
     poly.scale(2)
     self.assertEqual(poly['a'], 2)
     self.assertEqual(poly['ba'], 2)
     self.assertEqual(poly[tuple()], 2)
Exemplo n.º 3
0
 def test_scale_by_float(self):
     poly = BinaryPolynomial({'a': 4}, 'SPIN')
     poly.scale(.25)
     self.assertEqual(poly['a'], 1)
Exemplo n.º 4
0
 def test_repeated_term(self):
     poly = BinaryPolynomial({'ab': 1, 'ba': 1, ('a', 'b'): 1, ('b', 'a'): 1}, 'BINARY')
     self.assertEqual(poly['ab'], 4)
Exemplo n.º 5
0
 def test_empty(self):
     poly = BinaryPolynomial([], 'BINARY')
     self.assertEqual(poly.degree, 0)
Exemplo n.º 6
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)
Exemplo n.º 7
0
 def test_single_term(self):
     poly = BinaryPolynomial({('a', 'b'): 1}, 'SPIN')
     self.assertEqual(len(poly), 1)
Exemplo n.º 8
0
 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)
Exemplo n.º 9
0
 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())
Exemplo n.º 10
0
 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})
Exemplo n.º 11
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})
Exemplo n.º 12
0
 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
Exemplo n.º 13
0
 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)
Exemplo n.º 14
0
 def test_empty(self):
     poly = BinaryPolynomial({}, 'SPIN')
     poly.normalize()
Exemplo n.º 15
0
 def test_cubes_spin(self):
     poly = BinaryPolynomial({'aaa': -1}, dimod.SPIN)
     self.assertEqual(poly['a'], -1)
Exemplo n.º 16
0
 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())
Exemplo n.º 17
0
 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)
Exemplo n.º 18
0
 def test_from_dict(self):
     BinaryPolynomial({'a': -1, tuple(): 1.3, 'bc': -1, ('a', 'b'): 1}, 'SPIN')
Exemplo n.º 19
0
 def test_unlike_types(self):
     polydict = {'ab': -1, 'a': 1, 'b': 1}
     self.assertEqual(BinaryPolynomial(polydict, 'SPIN'), polydict)
     self.assertNotEqual(BinaryPolynomial(polydict, 'SPIN'), 1)
Exemplo n.º 20
0
 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)
Exemplo n.º 21
0
 def test_repeated_term(self):
     poly = BinaryPolynomial({('a', 'b'): 1, 'ba': 1}, 'BINARY')
     self.assertEqual(len(poly), 1)
Exemplo n.º 22
0
 def test_from_iterator(self):
     BinaryPolynomial(((term, -1) for term in itertools.combinations(range(100), 2)), 'SPIN')
Exemplo n.º 23
0
    def test_single_variable(self):
        poly = BinaryPolynomial({'a': -1}, 'SPIN')

        energies = poly.energies(([[-1], [1]], ['a']))
        np.testing.assert_array_equal(energies, [1, -1])
Exemplo n.º 24
0
 def test_aggregation(self):
     poly = BinaryPolynomial({'ab': 1, 'ba': 1, ('a', 'b'): 1, ('b', 'a'): 1}, 'SPIN')
     self.assertEqual(poly, BinaryPolynomial({'ab': 4}, 'SPIN'))
Exemplo n.º 25
0
 def test_single_variable(self):
     poly = BinaryPolynomial({'a': -1}, 'SPIN')
     poly.scale(.5)
     self.assertEqual(poly['a'], -.5)
Exemplo n.º 26
0
 def test_squares_binary(self):
     poly = BinaryPolynomial({'aa': -1}, dimod.BINARY)
     self.assertEqual(poly['a'], -1)
Exemplo n.º 27
0
 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_different_terms(self):
     poly1 = BinaryPolynomial({'a': 1, 'b': 1}, 'BINARY')
     poly2 = BinaryPolynomial({'ab': -1, 'a': 1, 'b': 1}, 'BINARY')
     self.assertNotEqual(poly1, poly2)