Remove equality between Param::Obj and Param::ParameterExpression#16084
Merged
Conversation
This commit fixes a mismatch in the Param::eq() method where a Python object param was potentially viewed as equal to a ParameterExpression param. When comparing a python object parameter with a ParameterExpression it would convert the ParameterExpression into a pyobject and then uses Python's == to evaluate the equality. Historically this was something that was done because the ParameterExpression object was also a Python object. However, since the function was first written the source of truth for the inner ParameterExpression has moved to Rust. We're never in a situation anymore when a ParameterExpression parameter on an instruction should be treated as a Python object. While this legacy equality may seem harmless because the equality should typically just evaluate to false (albeit slowly through Python's equality check), there are edge cases where this is doing the incorrect comparison. Primarily the bug that Qiskit#16076 was fixing was not caught initially because in tests this legacy logic was converting a ParameterExpression that is a constant int expression with no symbols to a Python int during the conversion of the expression to Python. That was then being compared against a Param::Obj() of a true Python int. The equality check would incorrectly assert the ParameterExpression was the same as the python int. This is what masked the bug in Qiskit#16076 because this differs from how the rust data model treats these parameters.
Collaborator
|
One or more of the following people are relevant to this code:
|
jakelishman
approved these changes
Apr 24, 2026
jakelishman
left a comment
Member
There was a problem hiding this comment.
tbf, the fact that ParameterExpression attempts to typecast itself on conversion to Python space was another reason the bug was masked as well - hard to pin down when type(qc.data[0].params[0]) is int but the OQ3 dumper is saying "failed to parse ParameterExpression"!
Member
Author
|
Thinking more about that we probably need to fix that too. We'll end in a really weird state where |
Member
|
Yeah - my initial patch for the QPY debugging included that change, it just needed to be stripped from the backport. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This commit fixes a mismatch in the Param::eq() method where a Python object param was potentially viewed as equal to a ParameterExpression param. When comparing a python object parameter with a ParameterExpression it would convert the ParameterExpression into a pyobject and then uses Python's == to evaluate the equality. Historically this was something that was done because the ParameterExpression object was also a Python object. However, since the function was first written the source of truth for the inner ParameterExpression has moved to Rust. We're never in a situation anymore when a ParameterExpression parameter on an instruction should be treated as a Python object.
While this legacy equality may seem harmless because the equality should typically just evaluate to false (albeit slowly through Python's equality check), there are edge cases where this is doing the incorrect comparison. Primarily the bug that #16076 was fixing was not caught initially because in tests this legacy logic was converting a ParameterExpression that is a constant int expression with no symbols to a Python int during the conversion of the expression to Python. That was then being compared against a Param::Obj() of a true Python int. The equality check would incorrectly assert the ParameterExpression was the same as the python int. This is what masked the bug in #16076 because this differs from how the rust data model treats these parameters.
AI/LLM disclosure