Skip to content

Fix Variables._relabel() with supersets#1409

Merged
arcondello merged 1 commit intodwavesystems:mainfrom
arcondello:fix/Variables-relabel-superset
Jun 23, 2025
Merged

Fix Variables._relabel() with supersets#1409
arcondello merged 1 commit intodwavesystems:mainfrom
arcondello:fix/Variables-relabel-superset

Conversation

@arcondello
Copy link
Copy Markdown
Member

Looking through the logic, I think it would be possible to avoid the copy with a more significant rework. But we already are doing many copies and traversals in that family of functions so IMO the simpler approach is best.

Closes #1408

@arcondello arcondello requested a review from randomir June 23, 2025 22:45
@arcondello arcondello added the bug label Jun 23, 2025
Comment thread dimod/utilities.py
"""
# first if the mapping is a superset of existing, we can go ahead and drop
# the stuff we don't care about
mapping = {old: new for old, new in mapping.items() if old in existing}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is exactly the workaround I implemented in dwavesystems/dwave-system#582. I was hoping it's feasible to fix it on a "lower level" (but I haven't checked where the actual bug is).

Copy link
Copy Markdown
Member Author

@arcondello arcondello Jun 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The bug is here:

idx = self._label_to_index.pop(old, old)

We pop 1, which results in a miss but then 1 gets assigned to idx. The first fix that I implemented was to ignore it here, but IMO the upstream fix that I went with ends up being better. At the cost of a copy (which as I said, is one of many).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix I went with also makes iter_safe_relabels() (a public utility function) more correct than it was before.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, makes sense.

@arcondello
Copy link
Copy Markdown
Member Author

The doctest failure is unrelated. I will also fix that in a followup PR.

@arcondello arcondello merged commit 30b7e16 into dwavesystems:main Jun 23, 2025
35 of 36 checks passed
@arcondello arcondello deleted the fix/Variables-relabel-superset branch June 23, 2025 23:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect BQM.relabel_variables() when mapping is a superset of BQM variables

2 participants