dimod
dimod copied to clipboard
BinaryPolynomial.normalize should be defined for range that includes zero
Description
BinaryPolynomial.normalize enables asymmetric ranges, which is good, but silently ignores shifts left or right from the zero, and crashes with a ZeroDivisionError if the range includes zero.
Steps To Reproduce
>>> poly1 = dimod.BinaryPolynomial({'x': -2}, 'BINARY')
>>> poly1.normalize(bias_range=[-0.5, 1])
>>> poly1
BinaryPolynomial({frozenset({'x'}): -0.5}, 'BINARY')
>>> poly1 = dimod.BinaryPolynomial({'x': -2}, 'BINARY')
>>> poly1.normalize(bias_range=[0.5, 1])
>>> poly1
BinaryPolynomial({frozenset({'x'}): -2}, 'BINARY')
>>> poly1 = dimod.BinaryPolynomial({'x': -2}, 'BINARY')
>>> poly1.normalize(bias_range=[0, 1])
ZeroDivisionError: division by zero
Expected Behavior
If normalize should only support ranges [-eps1, +eps2], we should enforce and document that; if users can set ranges wholly above or wholly below zero, we should define behavior.