Skip to content

Commit

Permalink
BugFix: CompositeOp.overlapping_ops maintains the original order of o…
Browse files Browse the repository at this point in the history
…ps. (#6091)

Fixes a bug where `CompositeOp.overlapping_ops` changes the original
ordering of ops.

Fixes #6090
[sc-71201]
  • Loading branch information
astralcai authored Aug 13, 2024
1 parent afd8433 commit 9b403fa
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
3 changes: 3 additions & 0 deletions doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,9 @@
* Workflows that parameterize the coefficients of `qml.exp` are now jit-compatible.
[(#6082)](https://github.com/PennyLaneAI/pennylane/pull/6082)

* Fixes a bug where `CompositeOp.overlapping_ops` changes the original ordering of ops, causing incorrect matrix generated for `Prod` with `Sum` as operands.
[(#6091)](https://github.com/PennyLaneAI/pennylane/pull/6091)

<h3>Contributors ✍️</h3>

This release contains contributions from (in alphabetical order):
Expand Down
5 changes: 3 additions & 2 deletions pennylane/ops/op_math/composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,6 @@ def overlapping_ops(self) -> list[list[Operator]]:
while i < len(groups):
if first_group_idx is None and any(wire in op.wires for wire in groups[i][1]):
# Found the first group that has overlapping wires with this op
groups[i][0].append(op)
groups[i][1] = groups[i][1] + op.wires
first_group_idx = i # record the index of this group
i += 1
Expand All @@ -219,7 +218,9 @@ def overlapping_ops(self) -> list[list[Operator]]:
groups[first_group_idx][1] = groups[first_group_idx][1] + wires
else:
i += 1
if first_group_idx is None:
if first_group_idx is not None:
groups[first_group_idx][0].append(op)
else:
# Create new group
groups.append([[op], op.wires])

Expand Down
2 changes: 1 addition & 1 deletion tests/ops/op_math/test_composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,8 @@ def test_overlapping_ops_property(self):
[
qml.sum(qml.PauliX(0), qml.PauliY(5), qml.PauliZ(10)),
qml.prod(qml.PauliX(10), qml.PauliY(2)),
qml.Hamiltonian([1, 1], [qml.PauliX(2), qml.PauliZ(7)]),
qml.PauliY(7),
qml.Hamiltonian([1, 1], [qml.PauliX(2), qml.PauliZ(7)]),
],
[
qml.sum(qml.PauliX(1), qml.PauliY(4), qml.PauliZ(6)),
Expand Down

0 comments on commit 9b403fa

Please sign in to comment.