From 50049a9eeb28cfc92433d9f496d297a6f863e3a7 Mon Sep 17 00:00:00 2001 From: JakeKitchen <155792753+JakeKitchen@users.noreply.github.com> Date: Thu, 24 Oct 2024 10:53:49 -0400 Subject: [PATCH] Update qft.py (#6434) **Context:** The original implementation recalculated the length of the `shifts` list inside the loop during each iteration, which introduces unnecessary repetition. --- **Description of the Change:** moving the computation of `shift_len = len(shifts)` outside the loop in the `compute_decomposition` function., it is now computed once before the loop starts and reused. --- **Benefits:** - **Performance Improvement:** The length of the `shifts` list is now only computed once, reducing unnecessary calculations and slightly improving performance, especially for cases with a large number of wires or repeated calls. - **Readability:** The code is clearer as the intent to calculate the length only once is explicitly shown. - **Maintainability:** It simplifies the logic by ensuring that `shift_len` is calculated only once, reducing the risk of future errors or bugs related to recalculating the list length. --- **Possible drawbacks:** - None --- **Related GitHub Issues:** - N/A --------- Co-authored-by: Andrija Paurevic <46359773+andrijapau@users.noreply.github.com> --- doc/releases/changelog-dev.md | 4 ++++ pennylane/templates/subroutines/qft.py | 3 ++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/doc/releases/changelog-dev.md b/doc/releases/changelog-dev.md index 8708dac5538..8728c2de687 100644 --- a/doc/releases/changelog-dev.md +++ b/doc/releases/changelog-dev.md @@ -189,6 +189,9 @@ * A more sensible error message is raised from a `RecursionError` encountered when accessing properties and methods of a nested `CompositeOp` or `SProd`. [(#6375)](https://github.com/PennyLaneAI/pennylane/pull/6375) +* Moved the calculation of `shift_len = len(shifts)` outside of a loop in the `QFT.compute_decomposition` static method, reducing redundant recalculations and improving performance. + [(#6434)](https://github.com/PennyLaneAI/pennylane/pull/6434) +

Breaking changes 💔

* `AllWires` validation in `QNode.construct` has been removed. @@ -405,6 +408,7 @@ Lillian M. A. Frederiksen, Pietropaolo Frisoni, Emiliano Godinez, Austin Huang, +Jacob Kitchen, Korbinian Kottmann, Christina Lee, William Maxwell, diff --git a/pennylane/templates/subroutines/qft.py b/pennylane/templates/subroutines/qft.py index d43e69d31f8..be2a1057a1d 100644 --- a/pennylane/templates/subroutines/qft.py +++ b/pennylane/templates/subroutines/qft.py @@ -175,11 +175,12 @@ def compute_decomposition(wires, n_wires): # pylint: disable=arguments-differ,u """ shifts = [2 * np.pi * 2**-i for i in range(2, n_wires + 1)] + shift_len = len(shifts) decomp_ops = [] for i, wire in enumerate(wires): decomp_ops.append(qml.Hadamard(wire)) - for shift, control_wire in zip(shifts[: len(shifts) - i], wires[i + 1 :]): + for shift, control_wire in zip(shifts[: shift_len - i], wires[i + 1 :]): op = qml.ControlledPhaseShift(shift, wires=[control_wire, wire]) decomp_ops.append(op)