dwavesystems / dwavesystems/dimod
BinaryPolynomial.normalize should be defined for range that includes zero
- Dominant language
- Python
- Stars
- 143
- Forks
- 91
- Avg merge
- 1h 24m
- Merged PRs (30d)
- 3
Description
**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.
Contributor guide
Assessment
This issue has not been assessed yet.