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

Mutable SingletonGates are modified when passed through pickle serialization and deserialization #10915

Closed
wshanks opened this issue Sep 28, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@wshanks
Copy link
Contributor

wshanks commented Sep 28, 2023

Environment

  • Qiskit Terra version: e5420d2 (main branch with SingletonGate addition)
  • Python version: 3.10
  • Operating system: Fedora Linux 38

What is happening?

pickle.load's default procedure is to call cls.__new__(cls) to generate an object. Since SingletonGate overrides __new__ and behaves differently based on the arguments to __new__, pickle.load will generate an immutable object even when the pickled object was mutable (since SingletonGate(*args, **kwargs) pass the arguments to __new__ and __init__ but pickle.load does not by default).

How can we reproduce the issue?

from qiskit.circuit.library import SXGate
from pickle import loads, dumps
sx = SXGate()
sx50 = SXGate(duration=50)
new_sx50 = loads(dumps(sx))
print(f"sx with duration has same duration after pickle round trip: {sx50.duration == new_sx50.duration = }")
print(f"sx with duration is the immutable gate after pickle roundtrip: {sx is new_sx50 = }")

This gives:

sx with duration has same duration after pickle round trip: sx50.duration == new_sx50.duration = False
sx with duration is the immutable gate after pickle roundtrip: sx is new_sx50 = True

What should happen?

A mutable SingletonGate should pass through pickle and still have the same duration, condition, label, and unit.

Any suggestions?

This is fixed in #10871. I Just wanted to make an issue about it to refer to.

This issue was discovered by a test in qiskit-experiments that used a conditional gate. When the circuits using the conditonal gate were transpiled on Linux, the circuits were passed through pickle as part of Python's multiprocessing and the conditional gate lost its condition.

@wshanks wshanks added the bug Something isn't working label Sep 28, 2023
@wshanks
Copy link
Contributor Author

wshanks commented Sep 28, 2023

Closed by #10871

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant