예제 #1
0
 def test_squares_spin(self):
     poly = BinaryPolynomial({'aa': -1}, dimod.SPIN)
     self.assertEqual(poly[()], -1)
예제 #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)
예제 #3
0
 def test_scale_by_float(self):
     poly = BinaryPolynomial({'a': 4}, 'SPIN')
     poly.scale(.25)
     self.assertEqual(poly['a'], 1)
예제 #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)
예제 #5
0
 def test_empty(self):
     poly = BinaryPolynomial([], 'BINARY')
     self.assertEqual(poly.degree, 0)
예제 #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)
예제 #7
0
 def test_single_term(self):
     poly = BinaryPolynomial({('a', 'b'): 1}, 'SPIN')
     self.assertEqual(len(poly), 1)
예제 #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)
예제 #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())
예제 #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})
예제 #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})
예제 #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
예제 #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)
예제 #14
0
 def test_empty(self):
     poly = BinaryPolynomial({}, 'SPIN')
     poly.normalize()
예제 #15
0
 def test_cubes_spin(self):
     poly = BinaryPolynomial({'aaa': -1}, dimod.SPIN)
     self.assertEqual(poly['a'], -1)
예제 #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())
예제 #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)
예제 #18
0
 def test_from_dict(self):
     BinaryPolynomial({'a': -1, tuple(): 1.3, 'bc': -1, ('a', 'b'): 1}, 'SPIN')
예제 #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)
예제 #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)
예제 #21
0
 def test_repeated_term(self):
     poly = BinaryPolynomial({('a', 'b'): 1, 'ba': 1}, 'BINARY')
     self.assertEqual(len(poly), 1)
예제 #22
0
 def test_from_iterator(self):
     BinaryPolynomial(((term, -1) for term in itertools.combinations(range(100), 2)), 'SPIN')
예제 #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])
예제 #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'))
예제 #25
0
 def test_single_variable(self):
     poly = BinaryPolynomial({'a': -1}, 'SPIN')
     poly.scale(.5)
     self.assertEqual(poly['a'], -.5)
예제 #26
0
 def test_squares_binary(self):
     poly = BinaryPolynomial({'aa': -1}, dimod.BINARY)
     self.assertEqual(poly['a'], -1)
예제 #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)