rigetti / pyquil

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

program.inst() using a tuple no longer works for parametric gates #1700

Closed bramathon closed 7 months ago

bramathon commented 7 months ago

In pyquil 3, this worked as expected, but in pyquil 4.1.1,

from numpy import pi
from pyquil.quil import Program

program = Program()
program.inst(("RX", [pi/2], 0))

fails with the following error

---------------------------------------------------------------------------
PyProgramError                            Traceback (most recent call last)
Cell In[8], line 5
      2 from pyquil.quil import Program
      4 program = Program()
----> 5 program.inst(("RX", [pi/2], 0))
      6 # program.inst(("RX", [pi / 2], 0))
      8 print(program)

File ~/.cache/pypoetry/virtualenvs/qpu-hybrid-benchmark-trueq-_oUyiKIy-py3.10/lib/python3.10/site-packages/pyquil/quil.py:116, in _invalidates_cached_properties.<locals>.wrapper(self, *args, **kwargs)
    114 @functools.wraps(func)
    115 def wrapper(self: "Program", *args: Any, **kwargs: Any) -> RetType:
--> 116     result = func(self, *args, **kwargs)
    117     cls = type(self)
    118     cached = {
    119         attr
    120         for attr in list(self.__dict__.keys())
    121         if (descriptor := getattr(cls, attr, None))
    122         if isinstance(descriptor, functools.cached_property)
    123     }

File ~/.cache/pypoetry/virtualenvs/qpu-hybrid-benchmark-trueq-_oUyiKIy-py3.10/lib/python3.10/site-packages/pyquil/quil.py:281, in Program.inst(self, *instructions)
    279         raise ValueError("tuple should have at least one element")
    280     else:
--> 281         self.inst(" ".join(map(str, instruction)))
    282 elif isinstance(instruction, str):
    283     self.inst(RSProgram.parse(instruction.strip()))

File ~/.cache/pypoetry/virtualenvs/qpu-hybrid-benchmark-trueq-_oUyiKIy-py3.10/lib/python3.10/site-packages/pyquil/quil.py:116, in _invalidates_cached_properties.<locals>.wrapper(self, *args, **kwargs)
    114 @functools.wraps(func)
    115 def wrapper(self: "Program", *args: Any, **kwargs: Any) -> RetType:
--> 116     result = func(self, *args, **kwargs)
    117     cls = type(self)
    118     cached = {
    119         attr
    120         for attr in list(self.__dict__.keys())
    121         if (descriptor := getattr(cls, attr, None))
    122         if isinstance(descriptor, functools.cached_property)
    123     }

File ~/.cache/pypoetry/virtualenvs/qpu-hybrid-benchmark-trueq-_oUyiKIy-py3.10/lib/python3.10/site-packages/pyquil/quil.py:283, in Program.inst(self, *instructions)
    281         self.inst(" ".join(map(str, instruction)))
    282 elif isinstance(instruction, str):
--> 283     self.inst(RSProgram.parse(instruction.strip()))
    284 elif isinstance(instruction, Program):
    285     self.inst(instruction._program)

PyProgramError: error while parsing: at line 1, column 4 (LBRACKET): expected a command or a gate
MarquessV commented 7 months ago

Thanks for the report and the script to reproduce.

MarquessV commented 7 months ago

@bramathon How useful is adding an instruction to a program by specifying it as a tuple? The way this needs to be implemented is fragile, and for stability/maintainability I think it'd be much better to deprecate this feature in favor of making users construct the instruction class they need.

bramathon commented 7 months ago

I was able to move past it by using the Gate constructor. So I think it's fine.

On Thu, Dec 14, 2023, 18:00 Marquess Valdez @.***> wrote:

@bramathon https://github.com/bramathon How useful is adding an instruction to a program by specifying it as a tuple? The way this needs to be implemented is fragile, and for stability/maintainability I think it'd be much better to deprecate this feature in favor of making users construct the instruction class they need.

— Reply to this email directly, view it on GitHub https://github.com/rigetti/pyquil/issues/1700#issuecomment-1856334046, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAEWA7WJGGL7W6KTG3TYWSDYJM5DLAVCNFSM6AAAAAA7ZURTGSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQNJWGMZTIMBUGY . You are receiving this because you were mentioned.Message ID: @.***>