Skip to content

Commit

Permalink
fix:Program.copy_everything_except_instructions() no longer adds de…
Browse files Browse the repository at this point in the history
…clarations 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
  • Loading branch information
mhodson-rigetti committed Jul 6, 2023
1 parent a1e9b0d commit 7e22ad3
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 7 deletions.
3 changes: 1 addition & 2 deletions pyquil/api/_rewrite_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 2 additions & 4 deletions pyquil/quil.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion test/unit/test_quantum_computer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
"""
Expand All @@ -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)
Expand Down

0 comments on commit 7e22ad3

Please sign in to comment.