Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_squares_binary(self):
poly = BinaryPolynomial({'aa': -1}, dimod.BINARY)
self.assertEqual(poly['a'], -1)
def test_squares_spin(self):
poly = BinaryPolynomial({'aa': -1}, dimod.SPIN)
self.assertEqual(poly[()], -1)
def test_empty(self):
poly = BinaryPolynomial([], 'BINARY')
self.assertEqual(poly.degree, 0)
def test_energy_equivalence_only_linear(self):
binpoly = BinaryPolynomial({'a': 5, 'b': -3}, '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_repeated_term(self):
poly = BinaryPolynomial({'ab': 1, 'ba': 1, ('a', 'b'): 1, ('b', 'a'): 1}, 'BINARY')
self.assertEqual(poly['ab'], 4)
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_energy_equivalence_only_offset(self):
spipoly = BinaryPolynomial({'': 5}, '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_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_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)
def test_from_dict(self):
BinaryPolynomial({'a': -1, tuple(): 1.3, 'bc': -1, ('a', 'b'): 1}, 'SPIN')