From a23583449adb07d105ded7b0e73459943dda4370 Mon Sep 17 00:00:00 2001 From: Marquess Valdez Date: Thu, 14 Dec 2023 09:54:42 -0800 Subject: [PATCH 1/2] fix: Gate instructions specified as tuples no longer error when using a list of parameters. --- pyquil/quil.py | 18 +++++++++++++++++- test/unit/__snapshots__/test_quil.ambr | 12 ++++++++++++ test/unit/test_quil.py | 14 ++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/pyquil/quil.py b/pyquil/quil.py index 25a108829..3951498ad 100644 --- a/pyquil/quil.py +++ b/pyquil/quil.py @@ -277,8 +277,24 @@ def inst(self, *instructions: Union[InstructionDesignator, RSProgram]) -> "Progr elif isinstance(instruction, tuple): 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]) + 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) elif isinstance(instruction, str): self.inst(RSProgram.parse(instruction.strip())) elif isinstance(instruction, Program): diff --git a/test/unit/__snapshots__/test_quil.ambr b/test/unit/__snapshots__/test_quil.ambr index 19706693b..a0ea5ad94 100644 --- a/test/unit/__snapshots__/test_quil.ambr +++ b/test/unit/__snapshots__/test_quil.ambr @@ -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 diff --git a/test/unit/test_quil.py b/test/unit/test_quil.py index f73efa6c9..9487c9809 100644 --- a/test/unit/test_quil.py +++ b/test/unit/test_quil.py @@ -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) From f1bf468e5ac102470f1ef2d73cbe82bc599baef7 Mon Sep 17 00:00:00 2001 From: Marquess Valdez Date: Thu, 14 Dec 2023 12:44:21 -0800 Subject: [PATCH 2/2] Add deprecation warning for tuple based initialization of instructions --- pyquil/quil.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/pyquil/quil.py b/pyquil/quil.py index 3951498ad..5d54b7440 100644 --- a/pyquil/quil.py +++ b/pyquil/quil.py @@ -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() @@ -275,6 +273,13 @@ 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: