Skip to content

Commit

Permalink
fix: Program declarations should be empty after copy_everything_excep…
Browse files Browse the repository at this point in the history
…t_instructions (#1614)

* Backported test which fails, as expected.

* Fix: Don't copy declarations member when copying program.

Migrated the v4 test to a simpler, more direct test of the Program class.

* Fix: the shallow copy() method needs to copy the declarations
  • Loading branch information
mhodson-rigetti committed Jul 7, 2023
1 parent 673928a commit 839663e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pyquil/quil.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down
17 changes: 17 additions & 0 deletions test/unit/test_quil.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 839663e

Please sign in to comment.