diff --git a/pyquil/quil.py b/pyquil/quil.py index 21f849542..14cc9abfc 100644 --- a/pyquil/quil.py +++ b/pyquil/quil.py @@ -186,7 +186,6 @@ def copy_everything_except_instructions(self) -> "Program": """ new_prog = Program() new_prog._calibrations = self.calibrations.copy() - new_prog._declarations = self._declarations.copy() new_prog._waveforms = self.waveforms.copy() new_prog._defined_gates = self._defined_gates.copy() new_prog._frames = self.frames.copy() @@ -206,8 +205,9 @@ def copy(self) -> "Program": :return: a new Program """ - new_prog = self.copy_everything_except_instructions() + new_prog = self.copy_everything_except_instructions() # and declarations, which is a view new_prog._instructions = self._instructions.copy() + new_prog._declarations = self._declarations.copy() return new_prog @property diff --git a/test/unit/test_quil.py b/test/unit/test_quil.py index dd7cabfda..31ee3b7c4 100644 --- a/test/unit/test_quil.py +++ b/test/unit/test_quil.py @@ -1409,3 +1409,20 @@ def test_params_pi_and_precedence(): prog = Program(f"RX({more_less_trivial_pi}) 0") exp = str(prog[0].params[0]) assert _eval_as_np_pi(more_less_trivial_pi) == _eval_as_np_pi(exp) + + +def test_copy_everything_except_instructions(): + """Test for https://github.com/rigetti/pyquil/issues/1613""" + program = Program( + """ +DECLARE beta REAL[1] +RZ(0.5) 0 +CPHASE(pi) 0 1 +DECLARE ro BIT[2] +MEASURE 0 ro[0] +MEASURE 1 ro[1] +""" + ) + program = program.copy_everything_except_instructions() + assert len(program.instructions) == 0 # the purpose of copy_everything_except_instructions() + assert len(program.declarations) == 0 # this is a view on the instructions member; must be consistent