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

Remove implicit broadcasting from Pauli/label construction of PauliList #9779

Merged
merged 4 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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
``PauliList`` is initialized from ``Pauli``s or labels. For
garrison marked this conversation as resolved.
Show resolved Hide resolved
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