Skip to content

Commit

Permalink
fix: ResetQubit instructions will not be returned as Reset after bein…
Browse files Browse the repository at this point in the history
…g inserted into a Program
  • Loading branch information
MarquessV committed Jan 22, 2024
1 parent 8767bdd commit ad2ffea
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
9 changes: 8 additions & 1 deletion pyquil/quilbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,10 @@ def _convert_to_py_instruction(instr: Any) -> AbstractInstruction:
if isinstance(instr, quil_rs.RawCapture):
return RawCapture._from_rs_raw_capture(instr)
if isinstance(instr, quil_rs.Reset):
return Reset._from_rs_reset(instr)
if instr.qubit is None:
return Reset._from_rs_reset(instr)
else:
return ResetQubit._from_rs_reset(instr)
if isinstance(instr, quil_rs.CircuitDefinition):
return DefCircuit._from_rs_circuit_definition(instr)
if isinstance(instr, quil_rs.GateDefinition):
Expand Down Expand Up @@ -619,6 +622,10 @@ def __new__(cls, qubit: Union[Qubit, QubitPlaceholder, FormalArgument]) -> Self:
raise TypeError("qubit should not be None")
return super().__new__(cls, qubit)

@classmethod
def _from_rs_reset(cls, reset: quil_rs.Reset) -> "Reset":
return super().__new__(cls, reset.qubit)


class DefGate(quil_rs.GateDefinition, AbstractInstruction):
"""
Expand Down
15 changes: 15 additions & 0 deletions test/unit/test_program.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from pyquil import Program
from pyquil.quil import AbstractInstruction, Declare, Measurement, MemoryReference
from pyquil.quilbase import Reset, ResetQubit
from pyquil.experiment._program import (
measure_qubits,
parameterized_single_qubit_measurement_basis,
Expand Down Expand Up @@ -139,3 +140,17 @@ def test_filter_quil_t():
)
full_program = non_quil_t_program + quil_t_program
assert full_program.remove_quil_t_instructions() == non_quil_t_program


def test_compatibility_layer():
"""
Test that the compatibility layer that transforms pyQuil instructions to quil instructions works as intended.
"""
# Note: `quil` re-orders some instructions in a program (e.g. by shuffling DECLAREs to the top). This isn't a
# problem for the current set of instructions we're testing, but it's something to keep in mind if we add more.
instructions = [ResetQubit(0), Reset()]
program = Program(instructions)
for (original, transformed) in zip(instructions, program):
assert isinstance(transformed, AbstractInstruction)
assert isinstance(transformed, type(original))
assert transformed == original

0 comments on commit ad2ffea

Please sign in to comment.