Python API

Pauli

class fast_pauli.Pauli(*args, **kwargs)

A class for efficient representation of a \(2 \times 2\) Pauli Matrix \(\sigma_i \in \{ I, X, Y, Z \}\)

__init__

Overloaded function.

  1. __init__(self) -> None

Default constructor to initialize with identity matrix.

  1. __init__(self, code: int) -> None

Constructor given a numeric code.

Parameters:

code (int) – Numerical label of type int for corresponding Pauli matrix \(0: I, 1: X, 2: Y, 3: Z\)

  1. __init__(self, symbol: str) -> None

Constructor given Pauli matrix symbol.

Parameters:

symbol (str) – Character label of type str corresponding to one of the Pauli Matrix symbols \(I, X, Y, Z\)

__matmul__

Returns matrix product of two Paulis as a tuple of phase and new Pauli object.

Parameters:

rhs (Pauli) – Right hand side Pauli object

Returns:

Phase and resulting Pauli object

Return type:

tuple[complex, Pauli]

__new__(**kwargs)
__str__

Returns a string representation of Pauli matrix.

Returns:

One of \(I, X, Y, Z\), a single character string representing a Pauli Matrix

Return type:

str

to_tensor

Returns a dense representation of Pauli object as a \(2 \times 2\) matrix.

Returns:

2D numpy array of complex numbers

Return type:

np.ndarray

PauliString

class fast_pauli.PauliString(*args, **kwargs)

A class representation of a Pauli String \(\mathcal{\hat{P}}\) (i.e. a tensor product of Pauli matrices)

\[ \begin{align}\begin{aligned}\mathcal{\hat{P}} = \bigotimes_i \sigma_i\\\sigma_i \in \{ I,X,Y,Z \}\end{aligned}\end{align} \]
__add__

Returns the sum of two Pauli strings in a form of PauliOp object.

Parameters:

rhs (PauliString) – The other PauliString object to add

Returns:

A linear combination of the PauliString objects as a PauliOp.

Return type:

PauliOp

__init__

Overloaded function.

  1. __init__(self) -> None

Default constructor to initialize with empty string.

  1. __init__(self, string: str) -> None

Constructs a PauliString from a string and calculates the weight. This is often the most compact way to initialize a PauliString.

Parameters:

string (str) – Pauli String representation. Each character should be one of \(I, X, Y, Z\)

  1. __init__(self, paulis: collections.abc.Sequence[fast_pauli._fast_pauli.Pauli]) -> None

Constructs a PauliString from a list of Pauli objects and calculates the weight.

Parameters:

paulis (list[Pauli]) – List of ordered Pauli objects

__matmul__

Returns matrix product of two pauli strings and their phase as a pair.

Parameters:

rhs (PauliString) – Right hand side PauliString object

Returns:

Phase and resulting PauliString object

Return type:

tuple[complex, PauliString]

__new__(**kwargs)
__str__

Returns a string representation of PauliString object.

Returns:

string representation of PauliString object

Return type:

str

__sub__

Returns the difference of two Pauli strings in a form of PauliOp object.

Parameters:

rhs (PauliString) – The other PauliString object to subtract

Returns:

A linear combination of the PauliString objects as a PauliOp.

Return type:

PauliOp

apply

Apply a Pauli string to a single dimensional state vector or a batch of states.

\[c \mathcal{\hat{P}} \ket{\psi_t}\]

Note

For batch mode it applies the PauliString to each individual state separately. In this case, the input array is expected to have the shape of (n_dims, n_states) with states stored as columns.

Parameters:
  • states (np.ndarray) – The original state(s) represented as 1D (n_dims,) or 2D numpy array (n_dims, n_states) for batched calculation. Outer dimension must match the dimensionality of Pauli string.

  • coeff (complex) – Scalar multiplication factor (\(c\)) to scale the PauliString before applying to states

Returns:

New state(s) in a form of 1D (n_dims,) or 2D numpy array (n_dims, n_states) according to the shape of input states

Return type:

np.ndarray

property dim

The dimension of PauliString \(2^n, n\) - number of qubits

Type:

int

expectation_value

Calculate expectation value(s) for a given single dimensional state vector or a batch of states.

\[\bra{\psi_t} \mathcal{\hat{P}} \ket{\psi_t}\]

Note

For batch mode it computes the expectation value for each individual state separately. In this case, the input array is expected to have the shape of (n_dims, n_states) with states stored as columns.

Parameters:
  • states (np.ndarray) – The original state(s) represented as 1D (n_dims,) or 2D numpy array (n_dims, n_states) for batched calculation. Outer dimension must match the dimensionality of Pauli string.

  • coeff (complex) – Multiplication factor to scale the PauliString before calculating the expectation value

Returns:

Expectation value(s) in the form of a 1D numpy array with a shape of (n_states,)

Return type:

np.ndarray

property n_qubits

The number of qubits in PauliString (i.e. number of Pauli Matrices in tensor product)

Type:

int

to_tensor

Returns a dense representation of PauliString.

Returns:

2D numpy array of complex numbers

Return type:

np.ndarray

property weight

The weight of PauliString (i.e. number of non-identity Pauli matrices in it)

Type:

int

PauliOp

class fast_pauli.PauliOp(*args, **kwargs)

A class representation for a Pauli Operator \(A\) (i.e. a weighted sum of Pauli Strings)

\[ \begin{align}\begin{aligned}A = \sum_j h_j \mathcal{\hat{P}}_j\\\mathcal{\hat{P}} = \bigotimes_i \sigma_i \quad h_j \in \mathbb{C}\end{aligned}\end{align} \]
__add__

Overloaded function.

  1. __add__(self, arg: fast_pauli._fast_pauli.PauliOp, /) -> fast_pauli._fast_pauli.PauliOp

Returns the sum of two Pauli Operators.

Parameters:

rhs (PauliOp) – The other PauliOp object to add

Returns:

New PauliOp instance holding the sum.

Return type:

PauliOp

  1. __add__(self, arg: fast_pauli._fast_pauli.PauliString, /) -> fast_pauli._fast_pauli.PauliOp

Returns the sum of Pauli Operator with Pauli String.

Parameters:

rhs (PauliString) – Right hand side PauliString object to add

Returns:

New PauliOp instance holding the sum.

Return type:

PauliOp

__iadd__

Overloaded function.

  1. __iadd__(self, arg: fast_pauli._fast_pauli.PauliOp, /) -> fast_pauli._fast_pauli.PauliOp

Performs inplace addition with other Pauli Operator.

Parameters:

other (PauliOp) – Pauli operator object to add

Returns:

Current PauliOp instance after addition

Return type:

PauliOp

  1. __iadd__(self, arg: fast_pauli._fast_pauli.PauliString, /) -> fast_pauli._fast_pauli.PauliOp

Performs inplace addition with Pauli String.

Parameters:

other (PauliString) – Pauli string object to add

Returns:

Current PauliOp instance after addition

Return type:

PauliOp

__imul__

Scale Pauli Operator inplace by a scalar value.

Parameters:

other (complex or float) – Scalar multiplier

Returns:

Current PauliOp instance after scaling

Return type:

PauliOp

__init__

Overloaded function.

  1. __init__(self) -> None

Default constructor to initialize strings and coefficients with empty arrays.

  1. __init__(self, pauli_strings: collections.abc.Sequence[str]) -> None

Construct a PauliOp from a list of strings and default corresponding coefficients to ones.

Parameters:

pauli_strings (List[str]) – List of Pauli Strings as simple str. Each string should be composed of characters \(I, X, Y, Z\) and should have the same size

  1. __init__(self, arg: collections.abc.Sequence[fast_pauli._fast_pauli.PauliString], /) -> None

Construct a PauliOp from a list of PauliString objects and default corresponding coefficients to ones.

Parameters:

pauli_strings (List[PauliString]) – List of PauliString objects.

  1. __init__(self, coefficients: ndarray[dtype=complex128], pauli_strings: collections.abc.Sequence[fast_pauli._fast_pauli.PauliString]) -> None

Construct a PauliOp from a list of PauliString objects and corresponding coefficients.

Parameters:
  • coefficients (np.ndarray) – Array of coefficients corresponding to Pauli strings.

  • pauli_strings (List[PauliString]) – List of PauliString objects.

  1. __init__(self, coefficients: collections.abc.Sequence[complex], pauli_strings: collections.abc.Sequence[fast_pauli._fast_pauli.PauliString]) -> None

Construct a PauliOp from a list of PauliString objects and corresponding coefficients.

Parameters:
  • coefficients (List[complex]) – List of coefficients corresponding to Pauli strings.

  • pauli_strings (List[PauliString]) – List of PauliString objects.

  1. __init__(self, coefficients: collections.abc.Sequence[complex], pauli_strings: collections.abc.Sequence[str]) -> None

Construct a PauliOp from a list of strings and corresponding coefficients.

Parameters:
  • coefficients (np.ndarray) – Array of coefficients corresponding to Pauli strings.

  • pauli_strings (List[str]) – List of Pauli Strings as simple str. Each string should be composed of characters \(I, X, Y, Z\) and should have the same size

__isub__

Overloaded function.

  1. __isub__(self, arg: fast_pauli._fast_pauli.PauliOp, /) -> fast_pauli._fast_pauli.PauliOp

Performs inplace subtraction with other Pauli Operator.

Parameters:

other (PauliOp) – Pauli operator object to subtract

Returns:

Current PauliOp instance after subtraction

Return type:

PauliOp

  1. __isub__(self, arg: fast_pauli._fast_pauli.PauliString, /) -> fast_pauli._fast_pauli.PauliOp

Performs inplace subtraction with Pauli String.

Parameters:

other (PauliString) – Pauli string object to subtract

Returns:

Current PauliOp instance after subtraction

Return type:

PauliOp

__matmul__

Overloaded function.

  1. __matmul__(self, arg: fast_pauli._fast_pauli.PauliOp, /) -> fast_pauli._fast_pauli.PauliOp

Efficient matrix multiplication of two Pauli Operators, leveraging their sparse structure.

Parameters:

rhs (PauliOp) – Right hand side PauliOp object

Returns:

New PauliOp instance containing the product

Return type:

PauliOp

  1. __matmul__(self, arg: fast_pauli._fast_pauli.PauliString, /) -> fast_pauli._fast_pauli.PauliOp

Efficient matrix multiplication of PauliOp with a PauliString on the right, leveraging their sparse structure.

Parameters:

rhs (PauliString) – Right hand side PauliString object

Returns:

New PauliOp instance containing the product

Return type:

PauliOp

__mul__

Scale Pauli Operator by a scalar value.

Parameters:

rhs (complex or float) – Right hand side scalar multiplier

Returns:

New PauliOp instance containing the product

Return type:

PauliOp

__new__(**kwargs)
__radd__

Returns the sum of Pauli Operators with Pauli String.

Parameters:

lhs (PauliString) – Left hand side PauliString object to add

Returns:

New PauliOp instance holding the sum.

Return type:

PauliOp

__rmatmul__

Efficient matrix multiplication of PauliOp with a PauliString on the left, leveraging their sparse structure.

Parameters:

rhs (PauliOp) – Left hand side PauliOp object

Returns:

New PauliOp instance containing the product

Return type:

PauliOp

__rmul__

Scale Pauli Operator by a scalar value.

Parameters:

lhs (complex or float) – Left hand side scalar multiplier

Returns:

New PauliOp instance containing the product

Return type:

PauliOp

__rsub__

Returns the difference of Pauli Operators with Pauli String.

Parameters:

lhs (PauliString) – Left hand side PauliString object to subtract

Returns:

New PauliOp instance holding the difference.

Return type:

PauliOp

__sub__

Overloaded function.

  1. __sub__(self, arg: fast_pauli._fast_pauli.PauliOp, /) -> fast_pauli._fast_pauli.PauliOp

Returns the difference of two Pauli Operators.

Parameters:

rhs (PauliOp) – The other PauliOp object to subtract

Returns:

New PauliOp instance holding the difference.

Return type:

PauliOp

  1. __sub__(self, arg: fast_pauli._fast_pauli.PauliString, /) -> fast_pauli._fast_pauli.PauliOp

Returns the difference of Pauli Operator with Pauli String.

Parameters:

rhs (PauliString) – Right hand side PauliString object to subtract

Returns:

New PauliOp instance holding the difference.

Return type:

PauliOp

apply

Apply a Pauli Operator to a single dimensional state vector or a batch of states.

\[\big( \sum_j h_j \mathcal{\hat{P}}_j \big) \ket{\psi_t}\]

Note

For batch mode it applies the PauliOp to each individual state separately. In this case, the input array is expected to have the shape of (n_dims, n_states) with states stored as columns.

Parameters:

states (np.ndarray) – The original state(s) represented as 1D (n_dims,) or 2D numpy array (n_dims, n_states) for batched calculation. Outer dimension must match the dimensionality of Pauli Operator.

Returns:

New state(s) in a form of 1D (n_dims,) or 2D numpy array (n_dims, n_states) according to the shape of input states

Return type:

np.ndarray

property coeffs

Ordered list of coefficients corresponding to Pauli strings

Type:

List[complex]

property dim

The dimension of PauliStrings used to compose PauliOp \(2^n, n\) - number of qubits

Type:

int

expectation_value

Calculate expectation value(s) for a given single dimensional state vector or a batch of states.

\[\bra{\psi_t} \big( \sum_j h_j \mathcal{\hat{P}}_j \big) \ket{\psi_t}\]

Note

For batch mode it computes the expectation value for each individual state separately. In this case, the input array is expected to have the shape of (n_dims, n_states) with states stored as columns.

Parameters:

states (np.ndarray) – The original state(s) represented as 1D (n_dims,) or 2D numpy array (n_dims, n_states) for batched calculation. Outer dimension must match the dimensionality of Pauli Operator.

Returns:

Expectation value(s) in the form of a 1D numpy array with a shape of (n_states,)

Return type:

np.ndarray

extend

Overloaded function.

  1. extend(self, other: fast_pauli._fast_pauli.PauliOp) -> None

Add another PauliOp to the current one by extending the internal summation with new terms.

Parameters:

other (PauliOp) – PauliOp object to extend the current one with

  1. extend(self, other: fast_pauli._fast_pauli.PauliString, multiplier: complex, dedupe: bool = True) -> None

Add a Pauli String term with a corresponding coefficient to the summation inside PauliOp.

Parameters:
  • other (PauliString) – PauliString object to add to the summation

  • multiplier (complex) – Coefficient to apply to the PauliString

  • dedupe (bool) – Whether to deduplicate the set of PauliStrings

property n_pauli_strings

The number of PauliString terms in PauliOp

Type:

int

property n_qubits

The number of qubits in PauliOp

Type:

int

property pauli_strings

Ordered list of PauliString objects in PauliOp

Type:

List[PauliString]

property pauli_strings_as_str

Ordered list of Pauli Strings representations from PauliOp

Type:

List[str]

scale

Overloaded function.

  1. scale(self, factor: complex) -> None

Scale each individual term of Pauli Operator by a scalar value.

Parameters:

factor (complex or float) – Scalar multiplier

  1. scale(self, factors: ndarray[dtype=complex128]) -> None

Scale each individual term of Pauli Operator by a scalar value.

Parameters:

factors (np.ndarray) – Array of factors to scale each term with. The length of the array should match the number of Pauli strings in PauliOp

to_tensor

Returns a dense representation of PauliOp.

Returns:

2D numpy array of complex numbers with a shape of \(2^n \times 2^n, n\) - number of qubits

Return type:

np.ndarray

SummedPauliOp

class fast_pauli.SummedPauliOp(*args, **kwargs)
__init__

Overloaded function.

  1. __init__(self, pauli_strings: collections.abc.Sequence[fast_pauli._fast_pauli.PauliString], coeffs: ndarray[dtype=complex128]) -> None

Initialize SummedPauliOp from PauliStrings and coefficients.

Parameters:
  • pauli_strings (List[PauliString]) – List of PauliStrings to use in the SummedPauliOp (n_pauli_strings,)

  • coeffs (np.ndarray) – Array of coefficients corresponding to the PauliStrings (n_pauli_strings, n_operators)

Returns:

New SummedPauliOp instance

Return type:

SummedPauliOp

  1. __init__(self, pauli_strings: collections.abc.Sequence[str], coeffs: ndarray[dtype=complex128]) -> None

Initialize SummedPauliOp from PauliStrings and coefficients.

Parameters:
  • pauli_strings (List[str]) – List of PauliStrings to use in the SummedPauliOp (n_pauli_strings,)

  • coeffs (np.ndarray) – Array of coefficients corresponding to the PauliStrings (n_pauli_strings, n_operators)

Returns:

New SummedPauliOp instance

Return type:

SummedPauliOp

__new__(**kwargs)
apply

Apply the SummedPauliOp to a batch of states.

\[\big(\sum_k \sum_i h_{ik} \mathcal{\hat{P}}_i \big) \ket{\psi_t}\]
Parameters:

states (np.ndarray) – The original state(s) represented as 2D numpy array (n_operators, n_states) for batched calculation.

Returns:

New state(s) in a form of 2D numpy array (n_operators, n_states) according to the shape of input states

Return type:

np.ndarray

apply_weighted

Apply the SummedPauliOp to a batch of states with corresponding weights.

\[\big(\sum_k x_{tk} \sum_i h_{ik} \mathcal{\hat{P}}_i \big) \ket{\psi_t}\]
Parameters:
  • states (np.ndarray) – The original state(s) represented as 2D numpy array (n_operators, n_states) for batched calculation.

  • data (np.ndarray) – The data to weight the operators corresponding to the states (n_operators, n_states)

Returns:

New state(s) in a form of 2D numpy array (n_operators, n_states) according to the shape of input states

Return type:

np.ndarray

property dim

Return the Hilbert space dimension of the SummedPauliOp.

Returns:

Hilbert space dimension

Return type:

int

expectation_value

Calculate expectation value(s) for a given batch of states.

\[\bra{\psi_t} \big(\sum_k \sum_i h_{ik} \mathcal{\hat{P}}_i \big) \ket{\psi_t}\]
Parameters:

states (np.ndarray) – The state(s) represented as 2D numpy array (n_operators, n_states) for batched calculation.

Returns:

Expectation value(s) in a form of 2D numpy array (n_operators, n_states) according to the shape of input states

Return type:

np.ndarray

property n_operators

Return the number of Pauli operators in the SummedPauliOp.

Returns:

Number of operators

Return type:

int

property n_pauli_strings

Return the number of PauliStrings in the SummedPauliOp.

Returns:

Number of PauliStrings

Return type:

int

square

Square the SummedPauliOp.

Returns:

New SummedPauliOp instance

Return type:

SummedPauliOp

to_tensor

Returns a dense representation of SummedPauliOp.

Returns:

3D numpy array of complex numbers with a shape of (n_operators, 2^n_qubits, 2^n_qubits)

Return type:

np.ndarray

Helpers

fast_pauli.helpers.calculate_pauli_strings(n_qubits: int, weight: int) list[fast_pauli._fast_pauli.PauliString]

Calculate all Pauli strings for a given weight.

Parameters:
  • n_qubits (int) – Number of qubits

  • weight (int) – Weight of Pauli strings to return

Returns:

List of PauliStrings

Return type:

List[PauliString]

fast_pauli.helpers.calculate_pauli_strings_max_weight(n_qubits: int, weight: int) list[fast_pauli._fast_pauli.PauliString]

Calculate all Pauli strings up to and including a given weight.

Parameters:
  • n_qubits (int) – Number of qubits

  • weight (int) – Maximum weight of Pauli strings to return

Returns:

List of PauliStrings

Return type:

List[PauliString]

fast_pauli.helpers.pauli_string_sparse_repr(paulis: collections.abc.Sequence[fast_pauli._fast_pauli.Pauli]) tuple[list[int], list[complex]]

Get a sparse representation of a list of Pauli strings.

Parameters:

paulis (List[PauliString]) – List of PauliStrings

Returns:

List of tuples representing the Pauli string in a sparse format

Return type:

List[Tuple[int, int]]

fast_pauli.helpers.get_nontrivial_paulis(weight: int) list[str]

Get all nontrivial Pauli strings up to a given weight.

Parameters:

weight (int) – Maximum weight of Pauli strings to return

Returns:

List of PauliStrings as strings

Return type:

List[str]