Skip to content

Commit

Permalink
Removing ambiguity in PauliRot error message. (#6298)
Browse files Browse the repository at this point in the history
**Context:**
For the input of `PauliRot` the number of wires given must equal the
length of the given Pauli word. Otherwise a `ValueError` is raised. For
example, `op = qml.PauliRot(0.5, "XY", wires=[0,1,2])` results in
`ValueError: The given Pauli word has length 2, length 3 was expected
for wires [0,1,2]`.

However, this message can be ambiguous. `op = qml.PauliRot(0.5,
"XYZZXYI", wires=[0])` raises the exception `ValueError: The given Pauli
word has length 7, length 1 was expected for wires [0]`. It's unclear if
"length 1" refers to the length of the Pauli word or the length of the
wires.

**Description of the Change:**
This PR changes the error message to remove the ambiguity. Now it will
say `ValueError: The number of wires must be equal to the length of the
Pauli Word. The Pauli word XY has length 2, and 3 wires were given [0,
1, 2].`
  • Loading branch information
willjmax authored Oct 2, 2024
1 parent d892b10 commit 010f8fb
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 @@ -179,6 +179,9 @@
* Add `work_wires` parameter to `qml.MultiControlledX` docstring signature.
[(#6271)](https://github.com/PennyLaneAI/pennylane/pull/6271)

* Removed ambiguity in error raised by the `PauliRot` class.
[(#6298)](https://github.com/PennyLaneAI/pennylane/pull/6298)

<h3>Bug fixes 🐛</h3>

* `qml.map_wires` can now be applied to a batch of tapes.
Expand Down
5 changes: 3 additions & 2 deletions pennylane/ops/qubit/parametric_ops_multi_qubit.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,9 @@ def __init__(

if not len(pauli_word) == num_wires:
raise ValueError(
f"The given Pauli word has length {len(pauli_word)}, length "
f"{num_wires} was expected for wires {wires}"
f"The number of wires must be equal to the length of the Pauli word. "
f"The Pauli word {pauli_word} has length {len(pauli_word)}, and "
f"{num_wires} wires were given {wires}."
)

def __repr__(self) -> str:
Expand Down
2 changes: 1 addition & 1 deletion tests/ops/qubit/test_parametric_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -2995,7 +2995,7 @@ def test_init_incorrect_pauli_word_length_error(self, pauli_word, wires):

with pytest.raises(
ValueError,
match="The given Pauli word has length .*, length .* was expected for wires .*",
match=r"The number of wires must be equal to the length of the Pauli word\. The Pauli word .* has length .*, and .* wires were given .*\.",
):
qml.PauliRot(0.3, pauli_word, wires=wires)

Expand Down

0 comments on commit 010f8fb

Please sign in to comment.