From e1f84c636b2def74adaf5196e44f3f5a9934ae8d Mon Sep 17 00:00:00 2001 From: Marquess Valdez Date: Mon, 15 Apr 2024 17:24:36 -0700 Subject: [PATCH] Add test --- pyquil/quil.py | 9 ++++++++- test/unit/test_quil.py | 44 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/pyquil/quil.py b/pyquil/quil.py index ae41ce981..050d7eb79 100644 --- a/pyquil/quil.py +++ b/pyquil/quil.py @@ -1014,7 +1014,7 @@ def __iter__(self) -> Iterator[AbstractInstruction]: def __eq__(self, other: object) -> bool: if isinstance(other, Program): - return self._program.to_instructions() == other._program.to_instructions() + return self._program == other._program return False def __len__(self) -> int: @@ -1035,6 +1035,13 @@ def __str__(self) -> str: """ return self._program.to_quil_or_debug() + def get_all_instructions(self) -> List[AbstractInstruction]: + """ + Get _all_ instructions that makeup the program. + """ + return _convert_to_py_instructions(self._program.to_instructions()) + + def merge_with_pauli_noise( prog_list: Iterable[Program], probabilities: Sequence[float], qubits: Sequence[int] diff --git a/test/unit/test_quil.py b/test/unit/test_quil.py index a4ae6c502..b9300c47a 100644 --- a/test/unit/test_quil.py +++ b/test/unit/test_quil.py @@ -80,6 +80,7 @@ ) from pyquil.quilatom import Frame, MemoryReference, Parameter, QubitPlaceholder, Sub, quil_cos, quil_sin from pyquil.quilbase import ( + AbstractInstruction, DefGate, DefFrame, Gate, @@ -89,6 +90,7 @@ DefCalibration, DefMeasureCalibration, DefPermutationGate, + DefWaveform, ) @@ -1194,3 +1196,45 @@ def test_out_without_calibrations(): combined_program = quilt_program + quil_program assert combined_program.out(calibrations=False) == quil_program.out() + + +def test_program_equality(): + program = Program("""DECLARE foo REAL[1] +DEFFRAME 1 "rx": + HARDWARE-OBJECT: "hardware" +DEFCAL I 0: + DELAY 0 1 +DEFCAL I 1: + DELAY 0 1 +DEFCAL I 2: + DELAY 0 1 +DEFCAL MEASURE 0 addr: + CAPTURE 0 "ro_rx" custom addr +DEFCAL MEASURE 1 addr: + CAPTURE 1 "ro_rx" custom addr +DEFWAVEFORM custom: + 1,2 +DEFWAVEFORM custom2: + 3,4 +DEFWAVEFORM another1: + 4,5 +DEFGATE BAR AS MATRIX: + 0, 1 + 1, 0 +DEFGATE FOO AS MATRIX: + 0, 1 + 1, 0 +H 1 +CNOT 2 3""") + + # Definitions in Quil are global in the sense that where they are defined in the program does not matter. + def is_global_state_instruction(i: AbstractInstruction) -> bool: + return isinstance(i, (DefFrame, DefCalibration, DefMeasureCalibration, DefWaveform, DefGate)) + + # Construct a copy of the program, inserting global instructions in reverse order. Since their order does not matter + # the new program should be equal to the original program. + new_program = program.filter_instructions(lambda i: not is_global_state_instruction(i)) + global_instructions = program.filter_instructions(lambda i: is_global_state_instruction(i)).get_all_instructions() + new_program += global_instructions[::-1] + + assert new_program == program