Repository: PennyLaneAI/pennylane
Pull request #5959 (2024-07-05 20:06:50)

Fixing bug QSVT wire order
**Context:** 
The order of the wires in QSVT is not maintained as they are used as a set internally.

**issue**
Solves issue #5965



github-actions[bot]: Hello. You may have forgotten to update the changelog!
Please edit [`doc/releases/changelog-dev.md`](/PennyLaneAI/pennylane/blob/master/doc/releases/changelog-dev.md) with:
* A one-to-two sentence description of the change. You may include a small working example for new features.
* A link back to this PR.
* Your name (or GitHub username) in the contributors section.
codecov[bot]: ## [Codecov](https://app.codecov.io/gh/PennyLaneAI/pennylane/pull/5959?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 99.66%. Comparing base [(`66421f1`)](https://app.codecov.io/gh/PennyLaneAI/pennylane/commit/66421f133f5e05b5ceb6e6e089dc648189dd56bf?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) to head [(`77f94c4`)](https://app.codecov.io/gh/PennyLaneAI/pennylane/commit/77f94c47a2e8f62fd41f714e18be9ad543b10985?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI).
> Report is 22 commits behind head on master.

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


```diff
@@            Coverage Diff             @@
##           master    #5959      +/-   ##
==========================================
- Coverage   99.67%   99.66%   -0.01%     
==========================================
  Files         422      426       +4     
  Lines       40658    40577      -81     
==========================================
- Hits        40525    40443      -82     
- Misses        133      134       +1     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/PennyLaneAI/pennylane/pull/5959?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI).   
: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=PennyLaneAI).

KetpuntoG: > Looks good to me!
> 
> I don't really think this is a bug? Since when we decompose the operations will act on the wires in the correct fashion, and matrix_expand will compute the matrix for each operation before multiplying them according to required wire_order. So its mostly just a visual artifact, the wires look like they are in a certain order, but actually it doesn't matter.

I detected the issue because I was not getting the correct results. This solves the issue 😄

Patch:
diff --git a/doc/releases/changelog-dev.md b/doc/releases/changelog-dev.md
index 82a142fc2..5f4aea481 100644
--- a/doc/releases/changelog-dev.md
+++ b/doc/releases/changelog-dev.md
@@ -56,6 +56,9 @@
 
 <h3>Bug fixes 🐛</h3>
 
+* `qml.QSVT` is updated to store wire order correctly.
+  [(#5959)](https://github.com/PennyLaneAI/pennylane/pull/5959)
+
 * `qml.devices.qubit.measure_with_samples` now returns the correct result if the provided measurements
   contain sum of operators acting on the same wire.
   [(#5978)](https://github.com/PennyLaneAI/pennylane/pull/5978)
@@ -66,7 +69,7 @@
 <h3>Contributors ✍️</h3>
 
 This release contains contributions from (in alphabetical order):
-
+Guillermo Alonso,
 Ahmed Darwish,
 Astral Cai,
 Yushao Chen,
diff --git a/pennylane/templates/subroutines/qsvt.py b/pennylane/templates/subroutines/qsvt.py
index f23cce6c5..72d07ef3c 100644
--- a/pennylane/templates/subroutines/qsvt.py
+++ b/pennylane/templates/subroutines/qsvt.py
@@ -285,10 +285,10 @@ class QSVT(Operation):
             "projectors": projectors,
         }
 
-        ua_wires = UA.wires.toset()
-        proj_wires = set.union(*(proj.wires.toset() for proj in projectors))
+        total_wires = qml.wires.Wires(UA.wires) + qml.wires.Wires.all_wires(
+            [proj.wires for proj in projectors]
+        )
 
-        total_wires = ua_wires.union(proj_wires)
         super().__init__(wires=total_wires, id=id)
 
     def map_wires(self, wire_map: dict):


Test patch:
diff --git a/tests/templates/test_subroutines/test_qsvt.py b/tests/templates/test_subroutines/test_qsvt.py
index 02b842a61..4da269ec5 100644
--- a/tests/templates/test_subroutines/test_qsvt.py
+++ b/tests/templates/test_subroutines/test_qsvt.py
@@ -169,6 +169,13 @@ class TestQSVT:
         for op1, op2 in zip(ops, decomp):
             qml.assert_equal(op1, op2)
 
+    def test_wire_order(self):
+        """Test that the wire order is preserved."""
+
+        op = qml.QFT(wires=[2, 1])
+        qsvt_wires = qml.QSVT(op, [op]).wires
+        assert qsvt_wires == op.wires
+
     @pytest.mark.parametrize(
         ("quantum_function", "phi_func", "A", "phis", "results"),
         [