diff --git a/doc/releases/changelog-dev.md b/doc/releases/changelog-dev.md index 08084f267eb..230463d5507 100644 --- a/doc/releases/changelog-dev.md +++ b/doc/releases/changelog-dev.md @@ -162,6 +162,9 @@

Bug fixes 🐛

+* Fix `skip_first` option in `expand_tape_state_prep`. + [(#4564)](https://github.com/PennyLaneAI/pennylane/pull/4564) + * `convert_to_numpy_parameters` now uses `qml.ops.functions.bind_new_parameters`. This reinitializes the operation and makes sure everything references the new numpy parameters. @@ -174,6 +177,7 @@ This release contains contributions from (in alphabetical order): Soran Jahangiri, Lillian M. A. Frederiksen, +Vincent Michaud-Rioux, Romain Moyard, Mudit Pandey, Matthew Silverman, diff --git a/pennylane/tape/tape.py b/pennylane/tape/tape.py index 226776bf4e0..d8e9cd4fbd8 100644 --- a/pennylane/tape/tape.py +++ b/pennylane/tape/tape.py @@ -263,7 +263,7 @@ def expand_tape_state_prep(tape, skip_first=True): first_op = tape.operations[0] new_ops = ( [first_op] - if isinstance(first_op, StatePrepBase) and skip_first + if not isinstance(first_op, StatePrepBase) or skip_first else first_op.decomposition() ) diff --git a/tests/tape/test_tape.py b/tests/tape/test_tape.py index ba535958095..bf873447dd6 100644 --- a/tests/tape/test_tape.py +++ b/tests/tape/test_tape.py @@ -1014,10 +1014,12 @@ def test_depth_expansion(self): [ qml.BasisState([1, 0], wires=[0, 1]), qml.StatePrep([0, 1, 0, 0], wires=[0, 1]), + qml.PauliZ(0), ], [ qml.BasisStatePreparation([1, 0], wires=[0, 1]), qml.MottonenStatePreparation([0, 1, 0, 0], wires=[0, 1]), + qml.PauliZ(0), ], ), ) @@ -1026,12 +1028,13 @@ def test_expansion_state_prep(self, skip_first, op, decomp): expanding other operations in the tape. """ ops = [ + op, qml.PauliZ(wires=0), qml.Rot(0.1, 0.2, 0.3, wires=0), qml.BasisState([0], wires=1), qml.StatePrep([0, 1], wires=0), ] - tape = QuantumTape(ops=ops, measurements=[], prep=[op]) + tape = QuantumTape(ops=ops, measurements=[]) new_tape = expand_tape_state_prep(tape, skip_first=skip_first) true_decomposition = []