Skip to content

Commit

Permalink
Removing _name attribute from Parameter (Qiskit#12191)
Browse files Browse the repository at this point in the history
* removing _name attribute from Parameter

* lint fix for parameter update

* fix the __getstate__ structure
  • Loading branch information
joesho112358 authored May 29, 2024
1 parent 473c3c2 commit df37987
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions qiskit/circuit/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class Parameter(ParameterExpression):
bc.draw('mpl')
"""

__slots__ = ("_name", "_uuid", "_hash")
__slots__ = ("_uuid", "_hash")

# This `__init__` does not call the super init, because we can't construct the
# `_parameter_symbols` dictionary we need to pass to it before we're entirely initialised
Expand All @@ -79,7 +79,6 @@ def __init__(
field when creating two parameters to the same thing (along with the same name)
allows them to be equal. This is useful during serialization and deserialization.
"""
self._name = name
self._uuid = uuid4() if uuid is None else uuid
symbol = symengine.Symbol(name)

Expand Down Expand Up @@ -117,7 +116,7 @@ def subs(self, parameter_map: dict, allow_unknown_parameters: bool = False):
@property
def name(self):
"""Returns the name of the :class:`Parameter`."""
return self._name
return self._symbol_expr.name

@property
def uuid(self) -> UUID:
Expand All @@ -143,7 +142,7 @@ def __repr__(self):

def __eq__(self, other):
if isinstance(other, Parameter):
return (self._uuid, self._name) == (other._uuid, other._name)
return (self._uuid, self._symbol_expr) == (other._uuid, other._symbol_expr)
elif isinstance(other, ParameterExpression):
return super().__eq__(other)
else:
Expand All @@ -155,7 +154,7 @@ def _hash_key(self):
# expression, so its full hash key is split into `(parameter_keys, symbolic_expression)`.
# This method lets containing expressions get only the bits they need for equality checks in
# the first value, without wasting time re-hashing individual Sympy/Symengine symbols.
return (self._name, self._uuid)
return (self._symbol_expr, self._uuid)

def __hash__(self):
# This is precached for performance, since it's used a lot and we are immutable.
Expand All @@ -165,10 +164,10 @@ def __hash__(self):
# operation attempts to put this parameter into a hashmap.

def __getstate__(self):
return (self._name, self._uuid, self._symbol_expr)
return (self.name, self._uuid, self._symbol_expr)

def __setstate__(self, state):
self._name, self._uuid, self._symbol_expr = state
_, self._uuid, self._symbol_expr = state
self._parameter_keys = frozenset((self._hash_key(),))
self._hash = hash((self._parameter_keys, self._symbol_expr))
self._parameter_symbols = {self: self._symbol_expr}
Expand Down

0 comments on commit df37987

Please sign in to comment.