From 7e22ad3d4b8644b6f3bce442d9ea20a1765c9989 Mon Sep 17 00:00:00 2001 From: Mark Hodson <58451176+mhodson-rigetti@users.noreply.github.com> Date: Fri, 7 Jul 2023 08:24:14 +0930 Subject: [PATCH] fix:`Program.copy_everything_except_instructions()` no longer adds declarations to the instructions property (#1612) * Reverse RC23 change set (code only, not the test). * Fix: Program.declarations is a view on Program.instructions and should not be copied. This alternate solution makes the test that was developed for #1596 pass, where previously it failed. This should resolve both #1596 and #1611. * Fix: the shallow copy() method needs to copy the declarations --- pyquil/api/_rewrite_arithmetic.py | 3 +-- pyquil/quil.py | 6 ++---- test/unit/test_quantum_computer.py | 4 +++- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/pyquil/api/_rewrite_arithmetic.py b/pyquil/api/_rewrite_arithmetic.py index fe2cea3b8..fc674dc1f 100644 --- a/pyquil/api/_rewrite_arithmetic.py +++ b/pyquil/api/_rewrite_arithmetic.py @@ -138,8 +138,7 @@ def expr_mref(expr: object) -> MemoryReference: # so we divide by 8... expr = str(Div(inst.scale, 8)) updated.inst(SetScale(inst.frame, expr_mref(expr))) - # Program.copy_everything_except_instructions persists DECLARE statements - elif not isinstance(inst, Declare): + else: updated.inst(inst) if mref_idx > 0: diff --git a/pyquil/quil.py b/pyquil/quil.py index ab3df733d..e11c89634 100644 --- a/pyquil/quil.py +++ b/pyquil/quil.py @@ -183,9 +183,6 @@ def copy_everything_except_instructions(self) -> "Program": """ new_prog = Program() new_prog._calibrations = self.calibrations.copy() - for declaration in self.declarations.values(): - new_prog.inst(declaration) - 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() @@ -203,8 +200,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_quantum_computer.py b/test/unit/test_quantum_computer.py index dfb09275e..1d78a7a57 100644 --- a/test/unit/test_quantum_computer.py +++ b/test/unit/test_quantum_computer.py @@ -834,7 +834,7 @@ def test_qc_expectation_on_qvm(client_configuration: QCSClient, dummy_compiler: def test_undeclared_memory_region(client_configuration: QCSClient, dummy_compiler: DummyCompiler): """ - Fix for https://github.com/rigetti/pyquil/issues/1596 + Test for https://github.com/rigetti/pyquil/issues/1596 """ program = Program( """ @@ -847,6 +847,8 @@ def test_undeclared_memory_region(client_configuration: QCSClient, dummy_compile """ ) 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 qc = QuantumComputer(name="testy!", qam=QVM(client_configuration=client_configuration), compiler=dummy_compiler) executable = qc.compiler.native_quil_to_executable(program) qc.run(executable)