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

Improve speed of Pauli.to_label #13379

Merged
merged 1 commit into from
Oct 30, 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
28 changes: 10 additions & 18 deletions qiskit/quantum_info/operators/symplectic/base_pauli.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@

# utility for _to_matrix
_PARITY = np.array([-1 if bin(i).count("1") % 2 else 1 for i in range(256)], dtype=complex)
# Utility for `_to_label`
_TO_LABEL_CHARS = np.array([ord("I"), ord("X"), ord("Z"), ord("Y")], dtype=np.uint8)


class BasePauli(BaseOperator, AdjointMixin, MultiplyMixin):
Expand Down Expand Up @@ -496,25 +498,15 @@ def _to_label(z, x, phase, group_phase=False, full_group=True, return_phase=Fals
the phase ``q`` for the coefficient :math:`(-i)^(q + x.z)`
for the label from the full Pauli group.
"""
num_qubits = z.size
phase = int(phase)
coeff_labels = {0: "", 1: "-i", 2: "-", 3: "i"}
label = ""
for i in range(num_qubits):
if not z[num_qubits - 1 - i]:
if not x[num_qubits - 1 - i]:
label += "I"
else:
label += "X"
elif not x[num_qubits - 1 - i]:
label += "Z"
else:
label += "Y"
if not group_phase:
phase -= 1
phase %= 4
# Map each qubit to the {I: 0, X: 1, Z: 2, Y: 3} integer form, then use Numpy advanced
# indexing to get a new data buffer which is compatible with an ASCII string label.
index = z << 1
index += x
ascii_label = _TO_LABEL_CHARS[index[::-1]].data.tobytes()
phase = (int(phase) if group_phase else int(phase) - ascii_label.count(b"Y")) % 4
label = ascii_label.decode("ascii")
if phase and full_group:
label = coeff_labels[phase] + label
label = ("", "-i", "-", "i")[phase] + label
if return_phase:
return label, phase
return label
Expand Down
4 changes: 4 additions & 0 deletions releasenotes/notes/pauli-label-perf-b704cbcc5ef92794.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
features_quantum_info:
- |
The performance of :meth:`.Pauli.to_label` has significantly improved for large Paulis.