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

Cannot pickle 'DefFrame' object #1791

Closed
bramathon opened this issue Jul 18, 2024 · 4 comments
Closed

Cannot pickle 'DefFrame' object #1791

bramathon opened this issue Jul 18, 2024 · 4 comments
Labels
bug 🐛 An issue that needs fixing. urgent Priority assistance requested

Comments

@bramathon
Copy link
Collaborator

Code Snippet

import pickle
from pyquil.quilbase import DefFrame
from pyquil.quilatom import Frame

def_frame = DefFrame(
    frame=Frame([9], "Transmon-9_flux_tx_c"),
    direction="tx",
    initial_frequency=0,
    hardware_object="{\"instrument_name\":\"tsunami01\",\"card_index\":7,\"channel_type\":\"QDOFastFluxChannel\",\"channel_index\":1,\"sequencer_index\":1,\"nco_index\":0}",
    sample_rate=1000000000,
    center_frequency=0,
    channel_delay=0,
)


with open("blarg-frame.pickle", "wb") as f:
    pickle.dump(def_frame, f)```

### Error Output

TypeError Traceback (most recent call last)
Cell In[33], line 17
5 def_frame = DefFrame(
6 frame=Frame([9], "Transmon-9_flux_tx_c"),
7 direction="tx",
(...)
12 channel_delay=0,
13 )
16 with open("blarg-frame.pickle", "wb") as f:
---> 17 pickle.dump(def_frame, f)

TypeError: cannot pickle 'DefFrame' object


Environment Context
-------------------

pyquil 4.13


@bramathon bramathon added the bug 🐛 An issue that needs fixing. label Jul 18, 2024
@MarquessV MarquessV added the urgent Priority assistance requested label Jul 18, 2024
@MarquessV
Copy link
Contributor

@bramathon I've been looking into implementing this, and the more I fight against pickling, the more I dislike it for our use case. Essentially, the code flow Python enforces to implement it is inefficient and clunky to implement consistently across our instruction types.

I think it would be better to lean directly on serialization and deserialization from a Quil string. The API doesn't have a way to initialize a particular instruction from a string, but I could add that, and I think that would be a better solution to this problem. Would you agree?

@bramathon
Copy link
Collaborator Author

Not being able to picke pyquil objects prevents users from pickling any object which contains a pyquil programs or objects. Pickling is also required from some uses of multi-processing. Most pyquil users do not have a deep knowledge of object serialization and will expect to be pickle things, especially for experimental code that might be fairly ephemeral. So this is a common use case a regression from pyquil 3.

@MarquessV
Copy link
Contributor

To clarify, a pyQuil Program does support pickling, so it can be pickled as a member of any other pickleable object.

I do wonder how common the pickling use case is, given that this is the first time it's popping up for instructions. I am not a fan of Python's pickle module for a number of reasons (it's an vector for ACE, not platform-agnostic, slow). While I think it might be okay for simple use cases, if your Python object already supports its own serialization format, especially when it's backed by a fast parser, I don't think it's pros outweigh the cons. Relying on quil serialization is more direct and performant (and it's exactly what pickling would do behind the scenes, with wasted work).

For your use case specifically, would deserializing an instruction from a string work? I think it's a feature we should consider any way, and instruction pickling is an issue we can come back to later if necessary.

@MarquessV
Copy link
Contributor

This is available as of pyQuil 4.14.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 An issue that needs fixing. urgent Priority assistance requested
Projects
None yet
Development

No branches or pull requests

2 participants