dimod icon indicating copy to clipboard operation
dimod copied to clipboard

BinaryPolynomial.normalize should be defined for range that includes zero

Open JoelPasvolsky opened this issue 3 years ago • 0 comments

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.

JoelPasvolsky avatar Feb 22 '22 19:02 JoelPasvolsky