-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Fix pickle handling for SingletonGate class #10871
Changes from all commits
5c9f223
05b8cae
3ad38b4
c24edc1
79ab4fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,8 @@ | |
""" | ||
|
||
import copy | ||
import io | ||
import pickle | ||
|
||
from qiskit.circuit.library import HGate, SXGate | ||
from qiskit.circuit import Clbit, QuantumCircuit, QuantumRegister, ClassicalRegister | ||
|
@@ -251,3 +253,27 @@ def test_positional_label(self): | |
label_gate = SXGate("I am a little label") | ||
self.assertIsNot(gate, label_gate) | ||
self.assertEqual(label_gate.label, "I am a little label") | ||
|
||
def test_immutable_pickle(self): | ||
gate = SXGate() | ||
self.assertFalse(gate.mutable) | ||
with io.BytesIO() as fd: | ||
pickle.dump(gate, fd) | ||
fd.seek(0) | ||
copied = pickle.load(fd) | ||
self.assertFalse(copied.mutable) | ||
mtreinish marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.assertIs(copied, gate) | ||
|
||
def test_mutable_pickle(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One thing that surprised me with this issue is that it was not caught in this repo before the singleton gate PR was merged. I expect there is a good set of tests for transpiling conditional gates, but likely the tests all transpile a single circuit and don't hit the pickling issues that come up with parallel transpilation. I wonder if the tests added here are enough or if there should be more tests of pickling and unpickling circuits. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's actually not a vast amount of testing of conditional gates (with The bug getting through points to a deficiency in integration testing around conditional gates for sure, and we can add more tests along those lines until |
||
gate = SXGate() | ||
clbit = Clbit() | ||
condition_gate = gate.c_if(clbit, 0) | ||
self.assertIsNot(gate, condition_gate) | ||
self.assertEqual(condition_gate.condition, (clbit, 0)) | ||
self.assertTrue(condition_gate.mutable) | ||
with io.BytesIO() as fd: | ||
pickle.dump(condition_gate, fd) | ||
fd.seek(0) | ||
copied = pickle.load(fd) | ||
self.assertEqual(copied, condition_gate) | ||
self.assertTrue(copied.mutable) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Totally unimportant, but pickle allows serialisation to
bytes
directly, so you can do this as a one-liner if you prefer:copied = pickle.loads(pickle.dumps(gate))
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just don't like using
pickle.dumps
becauses
implies string to me and it's not a string :DThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rest in peace, Python 2 🙂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe it's an initial from a language that reads right-to-left: "DUMP byteS" instead of "DUMP String" lol
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's the Python 2 docstring for dumps 🙂 : "Return the pickled representation of the object as a string, instead of writing it to a file."