Repository: TeamGraphix/graphix

Issue:
[Bug]: "list index out of range" in `pattern.get_measurement_commands`
**Describe the bug**
The method `pattern.get_measurement_commands` may raise `IndexError: list index out of range` if measure commands are the last commands in the pattern.

**To Reproduce**
The following code raises `IndexError`:
```python
import graphix
p = graphix.pattern.Pattern(input_nodes=[0])
p.add(["N", 1])
p.add(["M", 1, "XY", 0, [], []])
p.perform_pauli_measurements()
```

**Expected behavior**
No exception should be raised and pauli measurements should be done.

**Environment (please complete the following information):**
 - OS: macOS 14.4.1 (23E224)
 - Python version: 3.11.8
 - Related module versions if applicable:


Discussion:
codecov[bot]: ## [Codecov](https://app.codecov.io/gh/TeamGraphix/graphix/pull/154?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TeamGraphix) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 71.84%. Comparing base [(`dd15e5d`)](https://app.codecov.io/gh/TeamGraphix/graphix/commit/dd15e5d65fac5ba2a1fac7b4a5ac6ec943abc613?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TeamGraphix) to head [(`2c77573`)](https://app.codecov.io/gh/TeamGraphix/graphix/pull/154?dropdown=coverage&src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TeamGraphix).


<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##           master     #154      +/-   ##
==========================================
+ Coverage   71.75%   71.84%   +0.08%     
==========================================
  Files          30       30              
  Lines        5353     5359       +6     
==========================================
+ Hits         3841     3850       +9     
+ Misses       1512     1509       -3     
```



</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TeamGraphix/graphix/pull/154?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TeamGraphix).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TeamGraphix).


Patch:
diff --git a/graphix/pattern.py b/graphix/pattern.py
index 0c700c6..fc4efc4 100644
--- a/graphix/pattern.py
+++ b/graphix/pattern.py
@@ -954,8 +954,14 @@ class Pattern:
         ind = self._find_op_to_be_moved("M")
         if ind == "end":
             return []
-        while self.__seq[ind][0] == "M":
-            meas_cmds.append(self.__seq[ind])
+        while True:
+            try:
+                cmd = self.__seq[ind]
+            except IndexError:
+                break
+            if cmd[0] != "M":
+                break
+            meas_cmds.append(cmd)
             ind += 1
         return meas_cmds
 


Test patch:
diff --git a/tests/test_pattern.py b/tests/test_pattern.py
index dd9784d..067bd23 100644
--- a/tests/test_pattern.py
+++ b/tests/test_pattern.py
@@ -507,6 +507,13 @@ class TestLocalPattern:
         assert not result1
         assert result2
 
+    def test_pauli_measurement_end_with_measure(self) -> None:
+        # https://github.com/TeamGraphix/graphix/issues/153
+        p = Pattern(input_nodes=[0])
+        p.add(["N", 1])
+        p.add(["M", 1, "XY", 0, [], []])
+        p.perform_pauli_measurements()
+
 
 def assert_equal_edge(edge: Sequence[int], ref: Sequence[int]) -> bool:
     ans = True