Skip to content

Commit

Permalink
Pauli sentence bug (#5407)
Browse files Browse the repository at this point in the history
**Context:**
Earlier `PauliSentence.to_mat(wire_order)` failed with exception when it
had `only empty PauliWords`.
This has been fixed under this PR.

**Description of the Change:**
Earlier when `PauliWord` didn't have wires the function `_pw_wires`
defaulted to `PauliSentence` wires. Now rather than `PauliSentence`
itself, it defaults to `wire_order`

**Benefits:**

**Possible Drawbacks:**

**Related GitHub Issues:**
Fixes #5354

---------

Co-authored-by: Korbinian Kottmann <43949391+Qottmann@users.noreply.github.com>
Co-authored-by: Christina Lee <christina@xanadu.ai>
  • Loading branch information
3 people committed Mar 21, 2024
1 parent 45f8e75 commit 2faade8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
6 changes: 5 additions & 1 deletion doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,13 @@
* We no longer perform unwanted dtype promotion in the `pauli_rep` of `SProd` instances when using tensorflow.
[(#5246)](https://github.com/PennyLaneAI/pennylane/pull/5246)

* Fixed `TestQubitIntegration.test_counts` in `tests/interfaces/test_jax_qnode.py` to always produce counts for all outcomes.
* Fixed `TestQubitIntegration.test_counts` in `tests/interfaces/test_jax_qnode.py` to always produce counts for all
outcomes.
[(#5336)](https://github.com/PennyLaneAI/pennylane/pull/5336)

* Fixed `PauliSentence.to_mat(wire_order)` to support identities with wires.
[(#5407)](https://github.com/PennyLaneAI/pennylane/pull/5407)

<h3>Contributors ✍️</h3>

This release contains contributions from (in alphabetical order):
Expand Down
11 changes: 10 additions & 1 deletion pennylane/pauli/pauli_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,16 @@ def _pw_wires(w: Iterable) -> Wires:
the PauliWord is empty ({}), choose any arbitrary wire from the
PauliSentence it is composed in.
"""
return w or Wires(self.wires[0]) if self.wires else self.wires
if w:
# PauliWord is not empty, so we can use its wires
return Wires(w)

if wire_order:
# PauliWord is empty, treat it as Identity operator on any wire
# Pick any arbitrary wire from wire_order
return Wires(wire_order[0])

return wire_order

if len(self) == 0:
n = len(wire_order) if wire_order is not None else 0
Expand Down
6 changes: 5 additions & 1 deletion tests/pauli/test_pauli_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
from pennylane import numpy as np
from pennylane.pauli.pauli_arithmetic import PauliWord, PauliSentence, I, X, Y, Z


matI = np.eye(2)
matX = np.array([[0, 1], [1, 0]])
matY = np.array([[0, -1j], [1j, 0]])
Expand Down Expand Up @@ -877,6 +876,11 @@ def test_to_mat_empty(self, ps, true_res):
assert sparse.issparse(res_sparse)
assert qml.math.allclose(res_sparse.todense(), true_res)

def test_empty_pauli_to_mat_with_wire_order(self):
"""Test the to_mat method with an empty PauliSentence and PauliWord and an external wire order."""
actual = PauliSentence({PauliWord({}): 1.5}).to_mat([0, 1])
assert np.allclose(actual, 1.5 * np.eye(4))

ps_wire_order = ((ps1, []), (ps1, [0, 1, 2, "a", "b"]), (ps3, [0, 1, "c"]))

@pytest.mark.parametrize("ps, wire_order", ps_wire_order)
Expand Down

0 comments on commit 2faade8

Please sign in to comment.