Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that Parameters compare equal when they have the same hash (backport #11652) #11875

Merged
merged 1 commit into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion qiskit/circuit/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def __repr__(self):

def __eq__(self, other):
if isinstance(other, Parameter):
return self._uuid == other._uuid
return (self._uuid, self._name) == (other._uuid, other._name)
elif isinstance(other, ParameterExpression):
return super().__eq__(other)
else:
Expand Down
8 changes: 8 additions & 0 deletions releasenotes/notes/parameter-hash-eq-645f9de55aa78d02.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
fixes:
- |
:class:`.Parameter` was updated so that instances that compare equal always
have the same hash. Previously, only the :attr:`.Parameter.uuid` was
compared, so :class:`.Paramemter` instances with different names could
compare equal if they had been constructed using a common value for the
``uuid`` parameter (which is usually not passed explicitly).
4 changes: 4 additions & 0 deletions test/python/circuit/test_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,14 @@ def test_equality(self):
param = Parameter("a")
param_copy = Parameter(param.name, uuid=param.uuid)
param_different = Parameter("a")
param_same_uuid_diff_name = Parameter("b", uuid=param.uuid)

self.assertEqual(param, param, "Parameter does not equal itself")
self.assertEqual(param, param_copy, "Parameters with same data are not equal")
self.assertNotEqual(param, param_different, "Different Parameters are treated as equal")
self.assertNotEqual(
param, param_same_uuid_diff_name, "Parameters with different names are treated as equal"
)

def test_gate(self):
"""Test instantiating gate with variable parameters"""
Expand Down
Loading