Skip to content

Commit 532bb61

Browse files
committed
Fix Variables._relabel() with supersets
1 parent 0acb9fb commit 532bb61

3 files changed

Lines changed: 16 additions & 0 deletions

File tree

dimod/utilities.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,10 @@ def iter_safe_relabels(mapping: collections.abc.Mapping[Variable, Variable],
366366
A "safe" relabelling.
367367
368368
"""
369+
# first if the mapping is a superset of existing, we can go ahead and drop
370+
# the stuff we don't care about
371+
mapping = {old: new for old, new in mapping.items() if old in existing}
372+
369373
# put the new labels into a set for fast lookup, also ensures that the
370374
# values are valid labels
371375
# We could use a set, but using a dict makes for nicer error messages later
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
fixes:
3+
- |
4+
Fix ``Variables._relabel()`` method. Previously when given a superset of
5+
labels that happen to correspond to valid indices the wrong relabelling
6+
would sometimes be applied.
7+
See also `#1408 <https://github.com/dwavesystems/dimod/issues/1408>`_.

tests/test_variables.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,11 @@ def test_permissive(self):
210210

211211
self.assertEqual(variables, Variables('ab'))
212212

213+
def test_superset(self):
214+
variables = Variables([2, 0])
215+
variables._relabel({0: "a", 1: "b", 2: "c"})
216+
self.assertEqual(variables, ["c", "a"])
217+
213218
def test_swap(self):
214219
variables = Variables([1, 0, 3, 4, 5])
215220
variables._relabel({5: 3, 3: 5})

0 commit comments

Comments
 (0)