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

p.inst(q) for alloc'd qubit gives bad error message #77

Closed ampolloreno closed 6 years ago

ampolloreno commented 7 years ago

Is this expected behavior?

from pyquil.quil import Program
p = Program()
q = p.alloc()
# The following works fine...
p.inst(('X', q))
# No dice
p.inst(['X', q])
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "/home/ampolloreno/repos/pyquil/pyquil/quilbase.py", line 283, in inst
    self.inst(*instruction)
  File "/home/ampolloreno/repos/pyquil/pyquil/quilbase.py", line 308, in inst
    raise TypeError("Invalid instruction: {}".format(instruction))
  File "/home/ampolloreno/repos/pyquil/pyquil/resource_manager.py", line 84, in __str__
    return str(self.index())
  File "/home/ampolloreno/repos/pyquil/pyquil/resource_manager.py", line 77, in index
    raise RuntimeError("Can't get the index of an unassigned qubit.")
RuntimeError: Can't get the index of an unassigned qubit.
stylewarning commented 7 years ago

A tuple can represent an instruction. For example, ('X', 5) represents the Quil instruction X 5. The method inst also takes lists of instructions. ['X', 5] is malformed since it's not a list of instructions. It doesn't error earlier, however, since pyQuil allows raw instructions insted as strings.

You can get this error more straightforwardly by doing:

>>> p = Program()
>>> q = p.alloc()
>>> p.inst(q)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/robert/Slash/anaconda/lib/python2.7/site-packages/pyquil/quilbase.py", line 309, in inst
    raise TypeError("Invalid instruction: {}".format(instruction))
  File "/Users/robert/Slash/anaconda/lib/python2.7/site-packages/pyquil/resource_manager.py", line 83, in __str__
    return str(self.index())
  File "/Users/robert/Slash/anaconda/lib/python2.7/site-packages/pyquil/resource_manager.py", line 76, in index
    raise RuntimeError("Can't get the index of an unassigned qubit.")
RuntimeError: Can't get the index of an unassigned qubit.

I would say this is an issue because of bad condition checking / bad error message.