From f1b5e31110a58c38712f8f3251d6bb6a76ea5cba Mon Sep 17 00:00:00 2001 From: Guillermo Alonso-Linaje <65235481+KetpuntoG@users.noreply.github.com> Date: Fri, 12 Jul 2024 13:37:16 -0400 Subject: [PATCH] Fixing bug QSVT wire order (#5959) **Context:** The order of the wires in QSVT is not maintained as they are used as a set internally. **issue** Solves issue #5965 --------- Co-authored-by: soranjh <40344468+soranjh@users.noreply.github.com> --- doc/releases/changelog-dev.md | 5 ++++- pennylane/templates/subroutines/qsvt.py | 6 +++--- tests/templates/test_subroutines/test_qsvt.py | 7 +++++++ 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/doc/releases/changelog-dev.md b/doc/releases/changelog-dev.md index 82a142fc208..5f4aea48141 100644 --- a/doc/releases/changelog-dev.md +++ b/doc/releases/changelog-dev.md @@ -56,6 +56,9 @@

Bug fixes 🐛

+* `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 @@

Contributors ✍️

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 f23cce6c5ef..72d07ef3c10 100644 --- a/pennylane/templates/subroutines/qsvt.py +++ b/pennylane/templates/subroutines/qsvt.py @@ -285,10 +285,10 @@ def __init__(self, UA, projectors, id=None): "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): diff --git a/tests/templates/test_subroutines/test_qsvt.py b/tests/templates/test_subroutines/test_qsvt.py index 02b842a6144..4da269ec589 100644 --- a/tests/templates/test_subroutines/test_qsvt.py +++ b/tests/templates/test_subroutines/test_qsvt.py @@ -169,6 +169,13 @@ def test_decomposition_queues_its_contents(self): 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"), [