Skip to content

Commit

Permalink
Remove implicit broadcasting from Pauli/label construction of `Paul…
Browse files Browse the repository at this point in the history
…iList` (Qiskit#9779)

* Remove implicit broadcasting from `Pauli`/label construction of `PauliList`

* Update releasenotes/notes/paulilist-do-not-broadcast-from-paulis-96de3832fba21b94.yaml

Co-authored-by: Jake Lishman <jake@binhbar.com>

---------

Co-authored-by: Jake Lishman <jake@binhbar.com>
Co-authored-by: Jake Lishman <jake.lishman@ibm.com>
  • Loading branch information
3 people authored Mar 16, 2023
1 parent b0b5541 commit c37cec2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions qiskit/quantum_info/operators/symplectic/pauli_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,11 @@ def _from_paulis(data):
base_x = np.zeros((num_paulis, num_qubits), dtype=bool)
base_phase = np.zeros(num_paulis, dtype=int)
for i, pauli in enumerate(paulis):
if pauli.num_qubits != num_qubits:
raise ValueError(
f"The {i}th Pauli is defined over {pauli.num_qubits} qubits, "
f"but num_qubits == {num_qubits} was expected."
)
base_z[i] = pauli._z
base_x[i] = pauli._x
base_phase[i] = pauli._phase
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
fixes:
- |
A bug has been fixed which had allowed broadcasting when a
:class:`.PauliList` is initialized from :class:`.Pauli`\ s or labels. For
instance, the code ``PauliList(["XXX", "Z"])`` now raises a
``ValueError`` rather than constructing the equivalent of
``PauliList(["XXX", "ZZZ"])``.
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ def test_string_init(self):
np.testing.assert_equal(pauli_list.z, z)
np.testing.assert_equal(pauli_list.x, x)

with self.subTest(msg="str init prevent broadcasting"):
with self.assertRaises(ValueError):
PauliList(["XYZ", "I"])

def test_list_init(self):
"""Test list initialization."""
with self.subTest(msg="PauliList"):
Expand Down

0 comments on commit c37cec2

Please sign in to comment.