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

1613 Program declarations should be empty after copy #1614

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
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