rigetti / pyquil

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

Cannot pickle Frame objects under certain circumstances #1747

Open steve-jeffrey opened 7 months ago

steve-jeffrey commented 7 months ago

Pre-Report Checklist

Issue Description

A Program containing Frames cannot be pickled if the frames member has been accessed.

For example, in the example below:

Code Snippet

import pickle
from typing import cast

from pyquil import get_qc
from pyquil.api import QPUCompiler

qc = get_qc("Ankaa-9Q-1")

compiler = cast(QPUCompiler, qc.compiler)

cals = compiler.get_calibration_program()

# Works
with open('/tmp/test', 'wb') as file:
    pickle.dump(cals, file)

# Works i.e. some individual members can be pickled
with open('/tmp/test', 'wb') as file:
    pickle.dump(cals.waveforms, file)

# Fails i.e. the frames member cannot be pickled
with open('/tmp/test', 'wb') as file:
    pickle.dump(cals.frames, file)

# Fails
tmp = cals.copy_everything_except_instructions()
with open('/tmp/test', 'wb') as file:
    pickle.dump(cals, file)

Error Output

Traceback (most recent call last):
  File "test.py", line 28, in <module>
    pickle.dump(cals, file)
TypeError: cannot pickle 'Frame' object

Environment Context

Operating System: Fedora 39 Python: 3.10 PyQuil: 4.7.0