rigetti / pyquil

A Python library for quantum programming using Quil.
http://docs.rigetti.com
Apache License 2.0
1.4k stars 341 forks source link

Cannot pickle 'DefFrame' object #1791

Closed bramathon closed 2 weeks ago

bramathon commented 1 month ago

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
MarquessV commented 1 month ago

@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 commented 1 month ago

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 commented 1 month ago

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 commented 2 weeks ago

This is available as of pyQuil 4.14.1