Paulis & Observables

The conventions every other part of mpstab shares: what a Pauli string is, and the single normalization that turns any supported observable into Pauli strings with coefficients.

Pauli Strings

Pauli-string conventions shared by every part of mpstab.

A Pauli string is a plain str over "IXYZ", qubit-0-leftmost, one character per qubit. It never carries a sign: signs travel next to the string as a separate +1/-1 factor, so callers always handle them explicitly.

mpstab.pauli.PAULI_ARRAY = array([[[ 1.+0.j,  0.+0.j],         [ 0.+0.j,  1.+0.j]],         [[ 0.+0.j,  1.+0.j],         [ 1.+0.j,  0.+0.j]],         [[ 0.+0.j, -0.-1.j],         [ 0.+1.j,  0.+0.j]],         [[ 1.+0.j,  0.+0.j],         [ 0.+0.j, -1.+0.j]]])

The same matrices stacked in PAULI_LABELS order, shape (4, 2, 2).

mpstab.pauli.PAULI_LABELS = 'IXYZ'

Pauli labels, in the order used as the physical index of Pauli-basis tensors.

mpstab.pauli.PAULI_MATRICES = {'I': array([[1.+0.j, 0.+0.j],        [0.+0.j, 1.+0.j]]), 'X': array([[0.+0.j, 1.+0.j],        [1.+0.j, 0.+0.j]]), 'Y': array([[ 0.+0.j, -0.-1.j],        [ 0.+1.j,  0.+0.j]]), 'Z': array([[ 1.+0.j,  0.+0.j],        [ 0.+0.j, -1.+0.j]])}

Single-qubit Pauli matrices, keyed by label.

mpstab.pauli.anticommutation(left: Sequence[str], right: Sequence[str], nqubits: int)[source]

Which pairs of Pauli strings anticommute, via the symplectic product.

Parameters:
  • left – full-width Pauli strings.

  • right – full-width Pauli strings.

  • nqubits – their width, needed when either sequence is empty.

Returns:

Boolean (len(left), len(right)) matrix, True where the strings anticommute.

Example:

from mpstab.pauli import anticommutation

print(anticommutation(["XI", "ZZ"], ["ZI", "XX"], 2))
# [[ True False]
#  [False False]]
mpstab.pauli.conjugate(pauli: str, tableau: Tableau, sign: float = 1.0) → tuple[str, float][source]

Conjugate a Pauli string by a Clifford tableau: U P U^dag.

Parameters:
  • pauli – the Pauli string to conjugate.

  • tableau – the Clifford U. Pass tableau.inverse() to get U^dag P U instead, which is what folding an operator applied after the state into the observable needs.

  • sign – prefactor carried on the input Pauli.

Returns:

(pauli, sign) for the conjugated operator.

mpstab.pauli.from_stim(pauli: PauliString) → tuple[str, int][source]

Split a stim.PauliString into an unsigned label and its +/-1 sign.

mpstab.pauli.pauli_light_cone(generators: Sequence[str], observables: Sequence[str], nqubits: int) → ndarray[source]

The rotations in a chain that can affect the expectation of any of observables.

For <psi| R_1^dag ... R_n^dag  O  R_n ... R_1 |psi> with Pauli rotations R_j = exp(-i theta_j/2 G_j), a rotation whose generator commutes with O and with every later rotation still present commutes through to R_j^dag R_j = 1. Walking the chain backwards and dropping such rotations is exact, whatever the angles, and the result holds term by term for an observable that is a sum of the given strings.

Parameters:
  • generators – generator of each rotation, in the order they are applied.

  • observables – Pauli strings of the observable’s terms.

  • nqubits – width of every string.

Returns:

Increasing indices of the rotations to keep.

Example:

from mpstab.pauli import pauli_light_cone

# IX commutes with everything after it and drops out; ZI commutes
# with the observable but not with the kept XI, so it stays.
print(pauli_light_cone(["ZI", "IX", "XI"], ["ZI"], 2))
# [0 2]
mpstab.pauli.pauli_matrix(pauli: str) → ndarray[source]

Dense matrix of a Pauli string, qubit 0 being the most significant.

Parameters:

pauli – the Pauli string; the empty string gives the 1 x 1 identity.

Returns:

The 2**n x 2**n complex matrix P_0 (x) P_1 (x) ... (x) P_{n-1}.

mpstab.pauli.validate_pauli_string(pauli: str, nqubits: int | None = None) → None[source]

Check that pauli is a well-formed Pauli string of nqubits characters.

Parameters:
  • pauli – the string to check.

  • nqubits – expected length; None skips the length check.

Raises:

ValueError – on characters outside IXYZ or on a length mismatch.

mpstab.pauli.weight(pauli: str) → int[source]

Number of non-identity sites in pauli.

Observables

Observables, normalised into the one form mpstab works with.

Everything downstream – MPO construction, Clifford backpropagation, measurement planning – consumes a constant offset plus a {pauli_string: coefficient} map. pauli_terms() is the single place that produces it, from any of the observable formats the public API accepts.

mpstab.hamiltonians.pauli_string_to_hamiltonian(pauli: str, backend: Backend | None = None) → SymbolicHamiltonian[source]

The qibo SymbolicHamiltonian for a single Pauli string, e.g. "XZIY".

mpstab.hamiltonians.pauli_terms(observable: str | list | dict | SymbolicHamiltonian, nqubits: int) → tuple[float, dict][source]

Normalise an observable into (constant, {pauli_string: coefficient}).

Accepted formats:

  • a Pauli string, e.g. "XZIZ", taken with coefficient 1;

  • a list of Pauli strings, each taken with coefficient 1;

  • a {pauli_string: coefficient} mapping, returned as given;

  • a qibo SymbolicHamiltonian, whose terms are padded with identities to the full width and summed when two terms share a string.

The constant offset is returned separately rather than as an all-identity term, so callers add it once instead of measuring it.