Skip to content

Parameter has inconsistent hash #9299

@kevinsung

Description

@kevinsung

Environment

  • Qiskit Terra version: dd7f939
  • Python version: 3.10.8
  • Operating system: Arch Linux

What is happening?

Objects which compare equal must have the same hash value; see https://docs.python.org/3/reference/datamodel.html#object.hash. This property is violated by Parameter.

How can we reproduce the issue?

from qiskit.circuit import Parameter

a = Parameter("a")
aa = a + 0

print(a == aa)
print(hash(a) == hash(aa))
True
False

Note that a has type Parameter but aa has type ParameterExpression

What should happen?

Any one of the following is acceptable in theory:

  1. a == aa and hash(a) == hash(aa) both evaluate to True.
  2. a == aa and hash(a) == hash(aa) both evaluate to False.
  3. hash(a) raises TypeError: unhashable type

Any suggestions?

Another option would be to deprecate Parameter and instead use sympy.Symbol directly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions