Skip to content

Commit 1c177b4

Browse files
committed
Restrict NumberNode _from_zip return type
Previously accepted list of tuples and list of lists. Now simply list of tuples for consistency.
1 parent 71d5368 commit 1c177b4

2 files changed

Lines changed: 25 additions & 21 deletions

File tree

dwave/optimization/model.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -180,14 +180,14 @@ def binary(self, shape: None | _ShapeLike = None,
180180
each variable). Non-boolean values are rounded down to the domain
181181
[0,1]. If None, the default value of 1 is used.
182182
subject_to (optional): Axis-wise bounds for the symbol. Must be an
183-
array of tuples or lists. Each tuple/list is of the form:
184-
(axis, operator(s), bound(s)) where `axis` (int) is the axis in
185-
which to apply the bound, `operator(s)` (str | array[str]) is
186-
the operator(s) ("<=", "==", or ">=") defined per hyperslice or
187-
for all hyperslice along the bound axis, and `bound(s)` (float
188-
| array[float]) is the bound(s) defined per hyperslice or for
189-
all hyperslice along the bound axis. If provided, the sum of
190-
the values within each hyperslice along each bound axis will
183+
array of tuples. Each tuple is of the form: (axis, operator(s),
184+
bound(s)) where `axis` (int) is the axis to apply the bound(s),
185+
`operator(s)` (str | array[str]) is the operator(s) ("<=",
186+
"==", or ">=") defined for all hyperslice or per hyperslice
187+
along the bound axis, and `bound(s)` (float | array[float]) is
188+
the bound(s) defined for all hyperslice or per hyperslice
189+
hyperslice along the bound axis. If provided, the sum of the
190+
values within each hyperslice along each bound axis will
191191
satisfy the axis-wise bounds. Note: At most one axis-wise bound
192192
may be provided.
193193
@@ -530,14 +530,14 @@ def integer(
530530
each variable). Non-integer values are down up. If None, the
531531
default value is used.
532532
subject_to (optional): Axis-wise bounds for the symbol. Must be an
533-
array of tuples or lists. Each tuple/list is of the form:
534-
(axis, operator(s), bound(s)) where `axis` (int) is the axis in
535-
which to apply the bound, `operator(s)` (str | array[str]) is
536-
the operator(s) ("<=", "==", or ">=") defined per hyperslice or
537-
for all hyperslice along the bound axis, and `bound(s)` (float
538-
| array[float]) is the bound(s) defined per hyperslice or for
539-
all hyperslice along the bound axis. If provided, the sum of
540-
the values within each hyperslice along each bound axis will
533+
array of tuples. Each tuple is of the form: (axis, operator(s),
534+
bound(s)) where `axis` (int) is the axis to apply the bound(s),
535+
`operator(s)` (str | array[str]) is the operator(s) ("<=",
536+
"==", or ">=") defined for all hyperslice or per hyperslice
537+
along the bound axis, and `bound(s)` (float | array[float]) is
538+
the bound(s) defined for all hyperslice or per hyperslice
539+
hyperslice along the bound axis. If provided, the sum of the
540+
values within each hyperslice along each bound axis will
541541
satisfy the axis-wise bounds. Note: At most one axis-wise bound
542542
may be provided.
543543

dwave/optimization/symbols/numbers.pyx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,9 @@ cdef vector[NumberNode.BoundAxisInfo] _convert_python_bound_axes(
5858
cdef double[:] mem
5959

6060
for bound_axis_data in bound_axes_data:
61-
# We allow lists and tuples because the _from_zipfile method yields
62-
# a list of lists not a list of tuples.
63-
if not isinstance(bound_axis_data, (tuple, list)) or len(bound_axis_data) != 3:
64-
raise TypeError("Each bound axis entry must be a tuple or list with"
61+
if not isinstance(bound_axis_data, tuple) or len(bound_axis_data) != 3:
62+
print(bound_axis_data)
63+
raise TypeError("Each bound axis entry must be a tuple with"
6564
" three elements: axis, operator(s), bound(s)")
6665

6766
axis, py_ops, py_bounds = bound_axis_data
@@ -200,8 +199,10 @@ cdef class BinaryVariable(ArraySymbol):
200199
subject_to = None
201200
else:
202201
with zf.open(info, "r") as f:
203-
# Note that import is a list of lists, not a list of tuples
204202
subject_to = json.load(f)
203+
# Note that import is a list of lists, not a list of tuples,
204+
# hence we convert to tuple. We could also support lists.
205+
subject_to = [(axis, ops, bounds) for axis, ops, bounds in subject_to]
205206

206207
return BinaryVariable(model,
207208
shape=shape_info["shape"],
@@ -409,6 +410,9 @@ cdef class IntegerVariable(ArraySymbol):
409410
with zf.open(info, "r") as f:
410411
# Note that import is a list of lists, not a list of tuples
411412
subject_to = json.load(f)
413+
# Note that import is a list of lists, not a list of tuples,
414+
# hence we convert to tuple. We could also support lists.
415+
subject_to = [(axis, ops, bounds) for axis, ops, bounds in subject_to]
412416

413417
return IntegerVariable(model,
414418
shape=shape_info["shape"],

0 commit comments

Comments
 (0)