Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pauli sentence bug #5407

Merged
merged 6 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading