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

fix: Gate instructions specified as tuples no longer error when using a list of parameters. #1716

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions pyquil/quil.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,6 @@ def inst(self, *instructions: Union[InstructionDesignator, RSProgram]) -> "Progr
Program { ... }
>>> p.inst(H(i) for i in range(4)) # A generator of instructions
Program { ... }
>>> p.inst(("H", 1)) # A tuple representing an instruction
Program { ... }
>>> p.inst("H 0") # A string representing an instruction
Program { ... }
>>> q = Program()
Expand All @@ -275,10 +273,33 @@ def inst(self, *instructions: Union[InstructionDesignator, RSProgram]) -> "Progr
elif isinstance(instruction, types.GeneratorType):
self.inst(*instruction)
elif isinstance(instruction, tuple):
warnings.warn(
"Adding instructions to a program by specifying them as tuples is deprecated. Consider building "
"the instruction you need using classes from the `pyquil.gates` or `pyquil.quilbase` modules and "
"passing those to Program.inst() instead.",
DeprecationWarning,
stacklevel=2,
)
if len(instruction) == 0:
raise ValueError("tuple should have at least one element")
elif len(instruction) == 1:
self.inst(instruction[0])
else:
self.inst(" ".join(map(str, instruction)))
op = instruction[0]
if op == "MEASURE":
if len(instruction) == 2:
self.measure(instruction[1], None)
else:
self.measure(instruction[1], instruction[2])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this else check for len == 3 and raise an exception if len > 3?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, but this is how it was written in pyQuil 3, and since this code exists purely for backwards compatibility, I'm hesitant to add a new error case.

else:
params: List[ParameterDesignator] = []
possible_params = instruction[1]
rest: Sequence[Any] = instruction[2:]
if isinstance(possible_params, list):
params = possible_params
else:
rest = [possible_params] + list(rest)
self.gate(op, params, rest)
MarquessV marked this conversation as resolved.
Show resolved Hide resolved
elif isinstance(instruction, str):
self.inst(RSProgram.parse(instruction.strip()))
elif isinstance(instruction, Program):
Expand Down
12 changes: 12 additions & 0 deletions test/unit/__snapshots__/test_quil.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,18 @@

'''
# ---
# name: test_inst_tuple_measure
'''
MEASURE 0 ro[1]

'''
# ---
# name: test_inst_tuple_multiple_params
'''
RX(1.5707963267948966) 0

'''
# ---
# name: test_kraus
'''
X 0
Expand Down
14 changes: 14 additions & 0 deletions test/unit/test_quil.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,20 @@ def test_inst_tuple(snapshot):
assert p.out() == snapshot


def test_inst_tuple_measure(snapshot):
p = Program()
p.inst(("MEASURE", 0, ("ro", 1)))
assert len(p) == 1
assert p.out() == snapshot


def test_inst_tuple_multiple_params(snapshot):
p = Program()
p.inst(("RX", [pi / 2], 0))
assert len(p) == 1
assert p.out() == snapshot


def test_inst_rs_gate(snapshot):
p = Program()
q = quil_rs.Qubit.from_fixed(0)
Expand Down
Loading