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

Fix phase of pauli_list.insert(..., qubit=True) for length-1 pauli_list #13624

Merged
merged 8 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from 7 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
16 changes: 8 additions & 8 deletions qiskit/quantum_info/operators/symplectic/pauli_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,16 +451,14 @@ def insert(self, ind: int, value: PauliList, qubit: bool = False) -> PauliList:
f"Index {ind} is greater than number of qubits"
f" in the PauliList ({self.num_qubits})"
)
if len(value) == 1:
# Pad blocks to correct size
value_x = np.vstack(size * [value.x])
value_z = np.vstack(size * [value.z])
value_phase = np.vstack(size * [value.phase])
elif len(value) == size:
if len(value) == size:
# Blocks are already correct size
value_x = value.x
value_z = value.z
value_phase = value.phase
elif len(value) == 1:
# Pad blocks to correct size
value_x = np.vstack(size * [value.x])
value_z = np.vstack(size * [value.z])
else:
# Blocks are incorrect size
raise QiskitError(
Expand All @@ -471,7 +469,7 @@ def insert(self, ind: int, value: PauliList, qubit: bool = False) -> PauliList:
# Build new array by blocks
z = np.hstack([self.z[:, :ind], value_z, self.z[:, ind:]])
x = np.hstack([self.x[:, :ind], value_x, self.x[:, ind:]])
phase = self.phase + value_phase
phase = self.phase + value.phase

return PauliList.from_symplectic(z, x, phase)

Expand Down Expand Up @@ -1131,6 +1129,8 @@ def from_symplectic(
Returns:
PauliList: the constructed PauliList.
"""
if isinstance(phase, np.ndarray) and np.ndim(phase) > 1:
raise ValueError(f"phase should be at most 1D but has {np.ndim(phase)} dimensions.")
base_z, base_x, base_phase = cls._from_array(z, x, phase)
return cls(BasePauli(base_z, base_x, base_phase))

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
fixes:
- |
Fixed a bug that caused :meth:`.PauliList.insert(..., qubit=True)` to produce a `phase`
aeddins-ibm marked this conversation as resolved.
Show resolved Hide resolved
attribute with the wrong shape when the original object was length 1.
Fixed `#13623 <https://github.com/Qiskit/qiskit/issues/13623>`__.
Original file line number Diff line number Diff line change
Expand Up @@ -1560,6 +1560,15 @@ def test_insert(self):
value1 = pauli.insert(1, insert)
self.assertEqual(value1, target1)

# Insert single column to length-1 PauliList:
with self.subTest(msg="length-1, single-column, single-val"):
pauli = PauliList(["X"])
insert = PauliList(["Y"])
target0 = PauliList(["YX"])
value0 = pauli.insert(1, insert, qubit=True)
self.assertEqual(value0, target0)
self.assertEqual(value0.phase.shape, (1,))

# Insert single column
pauli = PauliList(["X", "Y", "Z", "-iI"])
for i in ["I", "X", "Y", "Z", "iY"]:
Expand Down
Loading