pauliarray.pauli package

Contents

pauliarray.pauli package#

Submodules#

pauliarray.pauli.operator module#

class pauliarray.pauli.operator.Operator(weighted_paulis: WeightedPauliArray)[source]#

Bases: object

Defines an operator as a linear combination of Pauli strings.

__add__(other: Operator | Number) Operator[source]#

Adds another Operator or a scalar to this Operator.

Parameters:

other (Union[Operator, Number]) – Another Operator or a scalar.

Returns:

The resulting Operator.

Return type:

Operator

__eq__(other: Operator) bool[source]#

Checks if equal to another Operator. Two Operators are equal if after simplification their underlying WeightedPauliArrays are equal.

Parameters:

other (Operator) – Another Operator.

Returns:

True if the Operators are equal.

Return type:

bool

__init__(weighted_paulis: WeightedPauliArray)[source]#

Initializes the Operator with a WeightedPauliArray.

Parameters:

weighted_paulis (WeightedPauliArray) – A WeightedPauliArray object.

__mul__(other: any)[source]#

Multiplies this Operator with another Operator or a scalar.

Parameters:

other (Any) – Another Operator or a scalar.

Returns:

The resulting Operator or NotImplemented.

Return type:

Any

__rmul__(other: any)[source]#

Multiplies a scalar with this Operator from the right.

Parameters:

other (Any) – A scalar.

Returns:

The resulting Operator or NotImplemented.

Return type:

Any

__str__()[source]#

Returns a string representation of the Operator.

Returns:

String representation of the Operator.

Return type:

str

add_operator(other: Operator) Operator[source]#

Adds another Operator to this Operator.

Parameters:

other (Operator) – Another Operator.

Returns:

The resulting Operator.

Return type:

Operator

add_scalar(scalar: Number) Operator[source]#

Adds a scalar to this Operator.

Parameters:

scalar (Number) – A scalar.

Returns:

The resulting Operator.

Return type:

Operator

adjoint() Operator[source]#

Returns the adjoint of the Operator.

Returns:

The adjoint Operator.

Return type:

Operator

clifford_conjugate(clifford: Operator, inplace: bool = True) Operator[source]#

Performs a Clifford conjugate transformation on the Operator.

Parameters:
  • clifford (Operator) – The Clifford operator.

  • inplace (bool) – If True, applies changes to self; otherwise, returns a modified copy.

Returns:

The transformed Operator.

Return type:

Operator

clifford_conjugate_pauli_array(other: PauliArray) Tuple[PauliArray, ndarray[Any, dtype[_ScalarType_co]]][source]#

Transform a PauliArray using self to perform a Clifford conjugate. (Faster prototype)

Parameters:

other (pa.PauliArray) – A PauliArray

Returns:

The transformed PauliArray “np.ndarray[np.complex]”: Residual coefficient

Return type:

pa.PauliArray

clifford_conjugate_pauli_array_old(other: PauliArray) Tuple[PauliArray, np.ndarray[np.complex]][source]#

Transform a PauliArray using self to perform a Clifford conjugate.

Parameters:

other (pa.PauliArray) – A PauliArray

Returns:

The transformed PauliArray “np.ndarray[np.complex]”: Residual coefficient

Return type:

pa.PauliArray

combine_repeated_terms(inplace=False) Operator[source]#

Combine repeated terms in the sum associated with equal Pauli strings. Inspired by : numpy/numpy#11136

Parameters:

inplace (bool, optional) – If True, modifies the present instance. Defaults to False.

Returns:

Operator where each Pauli strings appears only once in the sum.

Return type:

Operator

compose(other: Any) Any[source]#

Composes the Operator with another Operator.

Parameters:

other (Any) – Another Operator.

Returns:

The resulting Operator or NotImplemented.

Return type:

Any

compose_operator(other: Operator) Operator[source]#

Composes the Operator with another Operator.

Parameters:

other (Operator) – Another Operator.

Returns:

The resulting Operator.

Return type:

Operator

compress_qubits(condition: np.ndarray[np.bool]) Operator[source]#

Compresses the qubits based on the given condition.

Parameters:

condition ("np.ndarray[np.bool]") – Condition for compressing the qubits.

Returns:

The resulting Operator with compressed qubits.

Return type:

Operator

copy() Operator[source]#

Returns a copy of the Operator.

Returns:

A copy of the Operator.

Return type:

Operator

covariances_from_paulis(paulis_covariances: ndarray[Any, dtype[float64]]) np.complex[source]#

Returns the PauliArray expectation value given the expectation values of the Paulis.

Parameters:

paulis_covariances (NDArray[float]) – _description_

Returns:

_description_

Return type:

NDArray

cx(control_qubits: int | List[int], target_qubits: int | List[int], inplace: bool = True) Operator[source]#

Applies CX (Controlled-X) transformations on specified control and target qubits of the Operator.

Parameters:
  • control_qubits (Union[int, List[int]]) – The qubits which control the CX operation.

  • target_qubits (Union[int, List[int]]) – The qubits targeted by the CX operation.

  • inplace (bool) – If True, applies changes to self; otherwise, returns a modified copy.

Returns:

The resulting Operator.

Return type:

Operator

cz(control_qubits: int | List[int], target_qubits: int | List[int], inplace: bool = True) Operator[source]#

Applies CZ (Controlled-Z) transformations on specified control and target qubits of the Operator.

Parameters:
  • control_qubits (Union[int, List[int]]) – The qubits which control the CZ operation.

  • target_qubits (Union[int, List[int]]) – The qubits targeted by the CZ operation.

  • inplace (bool) – If True, applies changes to self; otherwise, returns a modified copy.

Returns:

The resulting Operator.

Return type:

Operator

classmethod empty(num_qubits) Operator[source]#

Creates an empty Operator with a specified number of qubits.

Parameters:

num_qubits (int) – The number of qubits.

Returns:

The empty Operator.

Return type:

Operator

expectation_values_from_paulis(paulis_expectation_values: ndarray[Any, dtype[float64]]) np.complex[source]#

Returns the PauliArray expectation value given the expectation values of the Paulis. More useful for other classes, but still here for uniformity.

Parameters:

paulis_expectation_values (NDArray[float]) – _description_

Returns:

_description_

Return type:

NDArray

filter_weights(filter_function: callable) Operator[source]#

Removes Pauli strings from the Operator object based on the filter_function applied on weights.

Parameters:

filter_function (callable) – The filter_function to apply on weights.

Returns:

The filtered Operator.

Return type:

Operator

classmethod from_labels_and_weights(labels, weights) Operator[source]#

Creates an Operator from labels and weights.

Parameters:
  • labels – The labels of the Operator.

  • weights – The weights associated with the labels.

Returns:

The constructed Operator.

Return type:

Operator

classmethod from_matrix(matrix: ndarray[Any, dtype[_ScalarType_co]], threshold=1e-09)[source]#

Creates an Operator from a matrix representation.

Parameters:
  • matrix (NDArray) – The matrix representation of the Operator.

  • threshold (float) – Threshold for determining small weights.

Returns:

The constructed Operator.

Return type:

Operator

classmethod from_npz(filename) Operator[source]#

Creates an Operator from a .npz file.

Parameters:

filename (str) – The name of the .npz file.

Returns:

The constructed Operator.

Return type:

Operator

classmethod from_paulis(paulis) Operator[source]#

Creates an Operator from Pauli strings.

Parameters:

paulis – The Pauli strings.

Returns:

The constructed Operator.

Return type:

Operator

classmethod from_paulis_and_weights(paulis, weights) Operator[source]#

Creates an Operator from Pauli strings and corresponding weights.

Parameters:
  • paulis – The Pauli strings.

  • weights – The weights associated with the Pauli strings.

Returns:

The constructed Operator.

Return type:

Operator

h(qubits: int | List[int], inplace: bool = True) Operator[source]#

Applies H (Hadamard) transformations on specified qubits of the Operator.

Parameters:
  • qubits (Union[int, List[int]]) – The qubits on which to apply the H transformation.

  • inplace (bool) – If True, applies changes to self; otherwise, returns a modified copy.

Returns:

The resulting Operator.

Return type:

Operator

classmethod identity(num_qubits) Operator[source]#

Creates an identity Operator with a specified number of qubits.

Parameters:

num_qubits (int) – The number of qubits.

Returns:

The identity Operator.

Return type:

Operator

inspect() str[source]#

Generates a string representation of the Operator showing sum of weighted Pauli strings.

Returns:

A string representation of the Operator.

Return type:

str

is_clifford() bool[source]#

Check if the Operator is a Clifford operator. For an operator to be Clifford, it first needs to be unitary. # TODO : make sure this is robust

Returns:

True if the Operator is a Clifford operator.

Return type:

bool

is_scalar() bool[source]#

Check if the Operator is a scalar.

Returns:

True if the Operator is a scalar.

Return type:

bool

is_unitary() bool[source]#

Check if the Operator is unitary.

Returns:

True if the Operator is unitary.

Return type:

bool

mul_scalar(other: Number)[source]#

Multiplies the Operator by a scalar.

Parameters:

other (Number) – A scalar.

Returns:

The resulting Operator.

Return type:

Operator

property num_qubits: int#

Returns the number of qubits.

Returns:

Number of qubits.

Return type:

int

property num_terms: int#

Returns the number of terms.

Returns:

Number of terms.

Return type:

int

property paulis: PauliArray#

Returns the PauliArray.

Returns:

The PauliArray object.

Return type:

PauliArray

power(exponent: int, simplify: bool = False) Operator[source]#

Raises the Operator to the specified exponent.

Parameters:
  • exponent (int) – The exponent to raise the Pauli operator to.

  • simplify (bool) – If True, the Operator is simplified at every multiplication with itself. This can improve performance. Defaults to False.

Returns:

A new Pauli operator resulting from raising the original operator to the exponent.

Return type:

Operator

Raises:

ValueError – If the exponent is negative.

remove_small_weights(threshold: float = 1e-14) Operator[source]#

Remove small weights from the Operator.

Parameters:

threshold (float, optional) – The threshold below which weights are considered small. Defaults to 1e-14.

Returns:

The Operator with small weights removed.

Return type:

Operator

s(qubits: int | List[int], inplace: bool = True) Operator[source]#

Applies S (Phase) transformations on specified qubits of the Operator.

Parameters:
  • qubits (Union[int, List[int]]) – The qubits on which to apply the S transformation.

  • inplace (bool) – If True, applies changes to self; otherwise, returns a modified copy.

Returns:

The resulting Operator.

Return type:

Operator

simplify(threshold: float = 1e-14) Operator[source]#

Simplify the Operator by removing small weights, combining repeated terms, and again removing small weights.

Parameters:

threshold (float, optional) – The threshold below which weights are considered small. Defaults to 1e-14.

Returns:

The simplified Operator.

Return type:

Operator

sort_paulis()[source]#

Sorts the underlying WeightedPauliArray primarily for comparison purposes.

Returns:

None

take_qubits(indices: np.ndarray[np.int] | range | int) Operator[source]#

Takes a subset of qubits.

Parameters:

indices (Union["np.ndarray[np.int]", range, int]) – Indices of the qubits to take.

Returns:

The resulting Operator with the subset of qubits.

Return type:

Operator

tensor(other: Any) Any[source]#

Takes the tensor product of the Operator with another Operator.

Parameters:

other (Any) – Another Operator.

Returns:

The resulting Operator or NotImplemented.

Return type:

Any

tensor_operator(other: Operator) Operator[source]#

Takes the tensor product of the Operator with another Operator.

Parameters:

other (Operator) – Another Operator.

Returns:

The resulting Operator.

Return type:

Operator

to_matrix() ndarray[Any, dtype[_ScalarType_co]][source]#

Converts the Operator into a (n**2, n**2) matrix.

Returns:

The matrix representation of the Operator.

Return type:

NDArray

to_npz(filename)[source]#

Saves the Operator to a .npz file.

Parameters:

filename (str) – The name of the .npz file.

Returns:

None

trace() np.complex[source]#

Returns the trace of the Operator.

Returns:

The trace of the Operator.

Return type:

“np.complex”

update_weights(new_weights)[source]#

Updates the weights of the Operator with the provided new weights.

Parameters:

new_weights – The new weights to be assigned to the Operator.

Returns:

None

update_weights_from_other(other: Operator)[source]#

Updates the weights of the Operator from another Operator object.

Parameters:

other (Operator) – The Operator from which to update the weights.

Returns:

None

property weights: np.ndarray[np.complex]#

Returns the weights of the Pauli terms.

Returns:

The weights.

Return type:

“np.ndarray[np.complex]”

property wpaulis: WeightedPauliArray#

Returns the WeightedPauliArray.

Returns:

The WeightedPauliArray object.

Return type:

WeightedPauliArray

x(qubits: int | List[int], inplace: bool = True) Operator[source]#

Applies X transformations on specified qubits of the Operator.

Parameters:
  • qubits (Union[int, List[int]]) – The qubits on which to apply the X transformation.

  • inplace (bool) – If True, applies changes to self; otherwise, returns a modified copy.

Returns:

The resulting Operator.

Return type:

Operator

pauliarray.pauli.operator.anticommutator(operator_1: Operator, operator_2: Operator) Operator[source]#

Computes the anticommutator

\[{A, B} = AB + BA\]

of two Operators.

Parameters:
  • operator_1 (Operator) – The first Operator.

  • operator_2 (Operator) – The second Operator.

Returns:

The anticommutator of the two Operators.

Return type:

Operator

pauliarray.pauli.operator.commutator(operator_1: Operator, operator_2: Operator) Operator[source]#

Computes the commutator

\[[A, B] = AB - BA\]

of two Operators.

Parameters:
  • operator_1 (Operator) – The first Operator.

  • operator_2 (Operator) – The second Operator.

Returns:

The commutator of the two Operators.

Return type:

Operator

pauliarray.pauli.operator_array_type_1 module#

class pauliarray.pauli.operator_array_type_1.OperatorArrayType1(wpaulis: WeightedPauliArray)[source]#

Bases: object

Defines an OperatorArray that contains an array of Operator. Each op.Operator has the same number of Pauli strings. Based on the wpa.WeightedPauliArray where the last dimension is used for summation.

__eq__(other: Self) np.ndarray[np.bool][source]#

Checks element-wise if the operators in the array are equal to the other.

Parameters:

other (OperatorArrayType1) – Another OperatorArrayType1. Must be broadcastable.

Returns:

An array indicating where the operators are equal.

Return type:

“np.ndarray[np.bool]”

__getitem__(key) Self[source]#

Gets an item from the operator array.

Parameters:

key – The index or slice to access the operator array.

Returns:

A new OperatorArrayType1 instance with the accessed item.

Return type:

OperatorArrayType1

__init__(wpaulis: WeightedPauliArray)[source]#

Initializes the OperatorArrayType1 with a WeightedPauliArray.

Parameters:

wpaulis (wpa.WeightedPauliArray) – The WeightedPauliArray to initialize with.

__mul__(other: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes]) Self#

Multiplies the operator array with another array-like object.

Parameters:

other (ArrayLike) – The array-like object to multiply with.

Returns:

A new OperatorArrayType1 instance with the result.

Return type:

OperatorArrayType1

__rmul__(other: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes]) Self#

Multiplies the operator array with another array-like object.

Parameters:

other (ArrayLike) – The array-like object to multiply with.

Returns:

A new OperatorArrayType1 instance with the result.

Return type:

OperatorArrayType1

__str__() str[source]#

String representation of current operator array

_mul(other: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes]) Self[source]#

Multiplies the operator array with another array-like object.

Parameters:

other (ArrayLike) – The array-like object to multiply with.

Returns:

A new OperatorArrayType1 instance with the result.

Return type:

OperatorArrayType1

static _operator_ndarray_to_wpaulis(operators) WeightedPauliArray[source]#

Converts an NDArray of operators into a WeightedPauliArray.

Parameters:

operators (NDArray) – The input array of operators to convert.

Returns:

A new instance of WeightedPauliArray according to given operators.

Return type:

wpa.WeightedPauliArray

add(other: Any) Any[source]#

Adds another operator array to the current operator array. Supports : OperatorArrayType1. Exists for coherence between all PauliArray data structures and raises NotImplemented if other type is inputed.

Parameters:

other (Any) – The other object to add.

Returns:

The resulting OperatorArrayType1 or NotImplemented Error.

Return type:

Any

add_operator_array_type_1(other: Self) Self[source]#

Adds another OperatorArrayType1 to the current operator array.

Parameters:

other (OperatorArrayType1) – The other OperatorArrayType1 to add.

Returns:

The resulting OperatorArrayType1.

Return type:

OperatorArrayType1

add_scalar(other: ndarray[Any, dtype[_ScalarType_co]]) Self[source]#

Adds scalar array to the operator array.

Parameters:

other (NDArray) – The scalar array to add.

Returns:

The resulting operator array.

Return type:

OperatorArrayType1

adjoint() Self[source]#

Computes the adjoint of the operator array.

Returns:

The adjoint of the operator array.

Return type:

OperatorArrayType1

clifford_conjugate(clifford: Operator, inplace: bool = True) Self[source]#

Performs a Clifford transformation.

Parameters:
  • clifford (Operator) – Must represent a Clifford transformation with the correct number of qubits.

  • inplace (bool, optional) – Applies the changes to self if True. Returns a modified copy if False.

  • True. (Defaults to)

Returns:

The transformed OperatorArrayType1.

Return type:

OperatorArrayType1

combine_repeated_terms(inplace=False) Self[source]#

Combines repeated terms within each operator in the array.

Parameters:
  • inplace (bool, optional) – Applies the changes to self if True. Returns a modified copy if False.

  • False (Defaults to)

Returns:

A modified self if inplace=True, else returns a new modified instance of OperatorArrayType1.

Return type:

OperatorArrayType1

compose(other: Any) Any[source]#

Composes the operator array with another operator array or object. Supports : OperatorArrayType1. Exists for coherence between all PauliArray data structures and raises NotImplemented if other type is inputed.

Parameters:

other (Any) – The other object to compose with.

Returns:

The resulting OperatorArrayType1 or NotImplemented Error.

Return type:

Any

compose_operator_array_type_1(other: Self) Self[source]#

Composes the operator array with another OperatorArrayType1. Both objects must be broadcastable following numpy’s broadcasting rules.

Parameters:

other (OperatorArrayType1) – The other OperatorArrayType1 to compose with.

Returns:

The composition result.

Return type:

OperatorArrayType1

copy() Self[source]#

Creates a copy of the operator array.

Returns:

A copy of the operator array.

Return type:

OperatorArrayType1

covariances_from_paulis(paulis_covariances: ndarray[Any, dtype[float64]]) np.ndarray[np.complex][source]#

Returns the Operator array covariances given the covariances of the Paulis.

Parameters:
  • paulis_covariances (NDArray[float]) – An array of covariances for each Paulis present in the

  • OperatorArray.

Returns:

An array of covariances for the Operator array, derived from the given Paulis covariances.

Return type:

NDArray

cx(control_qubits: int | List[int], target_qubits: int | List[int], inplace: bool = True) Self[source]#

Applies CX transformations on qubits of Operator. The order of the CX is set by the order of the qubits.

Parameters:
  • control_qubits (int or list[int]) – The qubits which controls the CZ.

  • target_qubits (int or list[int]) – The qubits target by CZ.

  • inplace (bool, optional) – Applies the changes to self if True. Returns a modified copy if False.

  • True. (Defaults to)

Returns:

A modified self if inplace=True, else returns a new modified instance of OperatorArrayType1.

Return type:

OperatorArrayType1

cz(control_qubits: int | List[int], target_qubits: int | List[int], inplace: bool = True) Self[source]#

Applies CZ transformations on qubits of Operator. The order of the CZ is set by the order of the qubits.

Parameters:
  • control_qubits (int or list[int]) – The qubits which controls the CZ.

  • target_qubits (int or list[int]) – The qubits target by CZ.

  • inplace (bool, optional) – Applies the changes to self if True. Returns a modified copy if False.

  • True. (Defaults to)

Returns:

A modified self if inplace=True, else returns a new modified instance of OperatorArrayType1.

Return type:

OperatorArrayType1

expectation_values_from_paulis(paulis_expectation_values: ndarray[Any, dtype[float64]]) np.ndarray[np.complex][source]#

Returns the Operator array expectation value given the expectation values of the Paulis.

Parameters:
  • paulis_expectation_values (NDArray[float]) – An array of expectation values for each Paulis present in the

  • OperatorArray.

Returns:

An array of complex expectation values for the Operator array, derived from the given Paulis expectation values.

Return type:

NDArray

filter_weights(filter_function: Callable) Self[source]#

Filters the weights of the Pauli terms using a provided filter function.

Parameters:
  • filter_function (Callable) – A function that takes an array of weights and returns a boolean mask array of

  • shape (the same)

  • keep. (indicating which weights to)

Returns:

A new instance of the class with the filtered weights applied to the Pauli terms.

Return type:

OperatorArrayType1

flatten() Self[source]#

Flattens the operator array into one dimension.

Returns:

A flattened copy of the operator array.

Return type:

OperatorArrayType1

classmethod from_operator_list(operators: List[Operator]) Self[source]#

Converts a list of Operator into an OperatorArrayType1.

Parameters:

operators (List[op.Operator]) – The input list of operators to convert.

Returns:

A new instance of OperatorArrayType1 according to given operators.

Return type:

OperatorArrayType1

classmethod from_operator_ndarray(operators: ndarray[Any, dtype[_ScalarType_co]])[source]#

Converts an NDArray of operators into an OperatorArrayType1.

Parameters:

operators (NDArray) – The input array of operators to convert.

Returns:

A new instance of OperatorArrayType1 according to given operators.

Return type:

OperatorArrayType1

classmethod from_pauli_array(paulis: PauliArray, summation_axis: Tuple[int, ...] | None = None) Self[source]#

Converts a PauliArray into an OperatorArrayType1.

Parameters:
  • paulis (pa.PauliArray) – The input PauliArray to convert.

  • summation_axis (int, optional) – Which axis of PauliArray to use for summation.

  • None (If)

  • None. (each Pauli string becomes an operator. Defaults to)

Returns:

A new instance of OperatorArrayType1 according to given Paulis.

Return type:

OperatorArrayType1

classmethod from_weighted_pauli_array(wpaulis: WeightedPauliArray, summation_axis: Tuple[int, ...] | None = None) Self[source]#

Converts a WeightedPauliArray into an OperatorArrayType1.

Parameters:
  • wpaulis (wpa.WeightedPauliArray) – The input WeightedPauliArray to convert.

  • summation_axis (int, optional) – Which axis of WeightedPauliArray to use for summation.

  • None (If)

  • None. (each Pauli string becomes an operator. Defaults to)

Returns:

A new instance of OperatorArrayType1 according to given Paulis.

Return type:

OperatorArrayType1

get_operator(*idx) Operator[source]#

Returns a single operator in operator array.

Parameters:

idx – Indices to access the operator.

Returns:

The accessed operator.

Return type:

op.Operator

h(qubits: int | List[int], inplace: bool = True) Self[source]#

Applies a H transformation on qubits of OperatorArray. This exchanges X matrices for Z matrices and vice-versa. It exchanges Y matrices into -Y matrices.

Parameters:
  • qubits (Union[int, List[int]]) – The qubits to apply the H transformation to.

  • inplace (bool, optional) – Applies the changes to self if True. Returns a modified copy if False.

  • True. (Defaults to)

Returns:

A modified self if inplace=True, else returns a new modified instance of OperatorArrayType1.

Return type:

OperatorArrayType1

inspect() str[source]#

Creates a string describing the operator array.

Returns:

String representation of the operator array details.

Return type:

str

mul_weights(other: ndarray[Any, dtype[_ScalarType_co]]) Self[source]#

Multiplies the weights of the operator array with another array of weights. Both objects must be broadcastable following numpy’s broadcasting rules.

Parameters:

other (NDArray) – The array to multiply weights with.

Returns:

The resulting operator array with multiplied weights.

Return type:

OperatorArrayType1

property ndim: int#

Number of dimensions in the operator array, excluding the last dimension.

property num_qubits: int#

Number of qubits in the operator array.

property num_terms: int#

Number of terms in the operator array.

property paulis: PauliArray#

PauliArray object representing the current operator array.

remove_small_weights(threshold: float = 1e-14) Self[source]#

Removes Pauli terms with weights smaller than a specified threshold.

Parameters:
  • threshold (float, optional) – The threshold below which Pauli term weights will be removed.

  • 1e-14. (Defaults to)

Returns:

A new instance of the class with the small weights removed.

Return type:

OperatorArrayType1

reshape(shape: Tuple[int, ...]) Self[source]#

Reshapes the operator array.

Parameters:

shape (Tuple[int, ...]) – New shape.

Returns:

Reshaped operator array.

Return type:

OperatorArrayType1

s(qubits: int | List[int], inplace: bool = True) Self[source]#

Applies S transformations on qubits of Operator. This exchanges X for Y and vice-versa with respective factors.

Parameters:
  • qubits (int or list[int]) – The qubits on which to apply the S.

  • inplace (bool, optional) – Applies the changes to self if True. Returns a modified copy if False.

  • True. (Defaults to)

Returns:

A modified self if inplace=True, else returns a new modified instance of OperatorArrayType1.

Return type:

OperatorArrayType1

property shape: Tuple[int, ...]#

Shape of the operator array, excluding the last dimension.

property size#

Total number of elements in the operator array.

sum(axes: Tuple[int, ...] | None = None) Operator | Self[source]#

Performs summation along the specified axes, combining these axes with the hidden summation axis. Returns an Operator if a single operator remains in the operator array. Else, return an OperatorArray.

Parameters:

axes (Union[Tuple[int, ...], None], optional) – Axes along which to sum. If None, the entire array is summed. Axes can be negative to count from the last to the first axis. Defaults to None.

Returns:

An instance of op.Operator if the result is a single operator, otherwise an instance of OperatorArrayType1 with the summed array.

Return type:

Union[op.Operator, OperatorArrayType1]

property weights: np.ndarray[np.complex]#

Weights of the Pauli terms in the operator array.

property wpaulis: WeightedPauliArray#

WeightedPauliArray object representing the current operator array.

x(qubits: int | List[int], inplace: bool = True) Self[source]#

Applies X transformations on qubits of Operators. This leaves the Pauli Strings unchanged but produce phase factors -1 when operators are Y or Z.

Parameters:
  • qubits (int or list[int]) – The qubits on which to apply the X.

  • inplace (bool, optional) – Applies the changes to self if True. Returns a modified copy if False.

  • True. (Defaults to)

Returns:

A modified self if inplace=True, else returns a new modified instance of OperatorArrayType1.

Return type:

OperatorArrayType1

pauliarray.pauli.operator_array_type_1.anticommutator(operators_1: OperatorArrayType1, operators_2: OperatorArrayType1, combine_repeated_terms=False, remove_small_weights=False) OperatorArrayType1[source]#

Computes the anticommutator

\[{A, B} = AB + BA\]

for pairs of operators from two operator arrays.

Parameters:
  • operators_1 (OperatorArrayType1) – The first array of operators.

  • operators_2 (OperatorArrayType1) – The second array of operators.

  • combine_repeated_terms (bool, optional) – If True, combines repeated terms in the resulting anticommutators. Defaults to False.

  • remove_small_weights (bool, optional) – If True, removes small weights from the resulting anticommutators. Defaults to False.

Returns:

An array of anticommutators of the input operator arrays.

Return type:

OperatorArrayType1

pauliarray.pauli.operator_array_type_1.commutator(operators_1: OperatorArrayType1, operators_2: OperatorArrayType1, combine_repeated_terms=False, remove_small_weights=False) OperatorArrayType1[source]#

Computes the commutator

\[[A, B] = AB - BA\]

for pairs of operators from two operator arrays.

Parameters:
  • operators_1 (OperatorArrayType1) – The first array of operators.

  • operators_2 (OperatorArrayType1) – The second array of operators.

  • combine_repeated_terms (bool, optional) – If True, combines repeated terms in the resulting commutators.

  • False. (Defaults to)

  • remove_small_weights (bool, optional) – If True, removes small weights from the resulting commutators.

  • False.

Returns:

An array of commutators of the input operator arrays.

Return type:

OperatorArrayType1

pauliarray.pauli.operator_array_type_1.concatenate(operators: Tuple[OperatorArrayType1, ...], axis: int) OperatorArrayType1[source]#

Concatenates multiple operator arrays along the specified axis.

Parameters:
  • operators (Tuple[OperatorArrayType1, ...]) – A tuple of operator arrays to concatenate.

  • axis (int) – The axis along which to concatenate the operator arrays.

Returns:

A new operator array resulting from the concatenation.

Return type:

OperatorArrayType1

pauliarray.pauli.operator_array_type_2 module#

class pauliarray.pauli.operator_array_type_2.OperatorArrayType2(basis_paulis: PauliArray, weights: np.ndarray[np.complex])[source]#

Bases: object

__getitem__(key) Self[source]#

Gets an item from the weights using the provided key.

Parameters:

key – The key to index the weights.

Returns:

A new OperatorArrayType2 object with the indexed weights.

Return type:

OperatorArrayType2

__init__(basis_paulis: PauliArray, weights: np.ndarray[np.complex])[source]#

Initializes an OperatorArrayType2 object.

Parameters:
  • basis_paulis (pa.PauliArray) – The basis Pauli arrays.

  • weights ("np.ndarray[np.complex]") – The weights associated with the Pauli arrays.

__mul__(other: Any) Self[source]#

Multiplies the weights by a number.

Parameters:

other (any) – The number to multiply the weights by.

Returns:

A new OperatorArrayType2 object with the multiplied weights.

Return type:

OperatorArrayType2

__rmul__(other: Any) Self[source]#

Multiplies the weights by a number.

Parameters:

other (any) – The number to multiply the weights by.

Returns:

A new OperatorArrayType2 object with the multiplied weights.

Return type:

OperatorArrayType2

_mul(other: Number | Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes]) Self[source]#

Multiplies the weights by a number or an array.

Parameters:

other (Union[Number, ArrayLike]) – The number or array to multiply the weights by.

Returns:

A new OperatorArrayType2 object with the multiplied weights.

Return type:

OperatorArrayType2

add(other: Any) Any[source]#

Adds the current operator with another operator.

Parameters:

other (Any) – The other operator to add.

Returns:

The summed operator.

Return type:

Any

add_operator_array_type_2(other: Self) Self[source]#

Adds the current operator with another OperatorArrayType2.

Parameters:

other (OperatorArrayType2) – The other OperatorArrayType2 to add.

Returns:

The summed OperatorArrayType2.

Return type:

OperatorArrayType2

property basis_paulis: PauliArray#

Gets the basis Pauli arrays.

Returns:

The basis Pauli arrays.

Return type:

pa.PauliArray

static build_basis_paulis(operators: ndarray[Any, dtype[_ScalarType_co]]) Tuple[PauliArray, np.ndarray[np.complex]][source]#

Builds the basis and the basis map. The basis is a PauliArray that contains each of the Pauli strings appearing in the operators. The basis map has the same shape as the OperatorArray and contains an array of indices that can be used to construct the operator with relevant Pauli strings from the basis.

Parameters:

operators (NDArray) – Array of operator objects.

Returns:

All the Pauli Strings present in the OperatorArray. Forms the basis. “np.ndarray[np.complex]”: An array definining the operators of the OperatorArray using the indices as reference to the basis.

Return type:

PauliArray

combine_basis_paulis() Self[source]#

Combines repeated Pauli operators in the basis and updates weights accordingly.

Returns:

A new OperatorArrayType2 instance with combined Pauli operators and updated weights.

Return type:

OperatorArrayType2

compose(other: Any) Any[source]#

Composes the current operator with another operator.

Parameters:

other (Any) – The other operator to compose with.

Returns:

The composed operator.

Return type:

Any

compose_operator_array_type_2(other: Self) Self[source]#

Composes the current operator with another OperatorArrayType2.

Parameters:

other (OperatorArrayType2) – The other OperatorArrayType2 to compose with.

Returns:

The composed OperatorArrayType2.

Return type:

OperatorArrayType2

covariances_from_paulis(paulis_covariances: ndarray[Any, dtype[float64]]) ndarray[Any, dtype[float64]][source]#

Calculates the covariances of the operators given the covariances of the Pauli operators.

Parameters:

paulis_covariances (NDArray[float]) – Covariances of the input Paulis.

Returns:

The covariances of the operators.

Return type:

NDArray[float]

expectation_values_from_paulis(paulis_expectation_values: ndarray[Any, dtype[float64]]) ndarray[Any, dtype[float64]][source]#

Calculates the expectation values of the operators given the expectation values of the Pauli operators.

Parameters:

paulis_expectation_values (NDArray[float]) – Expectation values of the input Paulis.

Returns:

The expectation values of the operators in operator array.

Return type:

NDArray[float]

filter_weights(filter_function: Callable) Self[source]#

Filters the weights using a filter function.

Parameters:

filter_function (Callable) – The function to filter the weights. Must return a mask appliable on weights.

Returns:

A new OperatorArrayType2 object with filtered weights.

Return type:

OperatorArrayType2

flatten() Self[source]#

Returns a flattened copy of self.

Returns:

A flattened copy of the current OperatorArray.

Return type:

OperatorArrayType2

classmethod from_operator(operator: Operator) Self[source]#

Constructs an OperatorArrayType2 instance from a single operator.

Parameters:

operator (op.Operator) – An operator object.

Returns:

A new OperatorArrayType2 instance.

Return type:

OperatorArrayType2

classmethod from_operator_list(operators: List[Operator]) Self[source]#

Constructs an OperatorArrayType2 instance from a list of operators.

Parameters:

operators (List[op.Operator]) – List of operator objects.

Returns:

A new OperatorArrayType2 instance.

Return type:

OperatorArrayType2

classmethod from_operator_ndarray(operators: ndarray[Any, dtype[_ScalarType_co]]) Self[source]#

Constructs an OperatorArrayType2 instance from an array of operators.

Parameters:

operators (NDArray) – Array of operator objects.

Returns:

A new OperatorArrayType2 instance.

Return type:

OperatorArrayType2

classmethod from_pauli_array(paulis: PauliArray) Self[source]#

Constructs an OperatorArrayType2 instance from a Pauli array.

Parameters:

paulis (pa.PauliArray) – A Pauli array object.

Returns:

A new OperatorArrayType2 instance.

Return type:

OperatorArrayType2

get_operator(*idx) Operator[source]#

Retrieves the operator at the specified index.

Parameters:

*idx – Indices specifying the position of the desired operator.

Returns:

The operator at the specified index.

Return type:

op.Operator

mul_weights(other: Number | ndarray[Any, dtype[_ScalarType_co]]) Self[source]#

Multiplies the weights by a number or an array.

Parameters:

other (Union[Number, NDArray]) – The number or array to multiply the weights by.

Returns:

A new OperatorArrayType2 object with the multiplied weights.

Return type:

OperatorArrayType2

property ndim: int#

Gets the number of dimensions of the weights excluding the last dimension.

Returns:

The number of dimensions.

Return type:

int

property num_qubits: int#

Gets the number of qubits.

Returns:

The number of qubits.

Return type:

int

property paulis: PauliArray#

Gets the basis Pauli arrays.

Returns:

The basis Pauli arrays.

Return type:

pa.PauliArray

remove_small_weights(threshold: float = 1e-12) Self[source]#

Removes weights that are smaller than a threshold.

Parameters:

threshold (float) – The threshold below which weights are removed. Defaults to 1e-12.

Returns:

A new OperatorArrayType2 object with small weights removed.

Return type:

OperatorArrayType2

remove_unused_basis_paulis() Self[source]#

Removes Paulis from the basis that are not used in any operator of the OperatorArray.

Returns:

A new OperatorArrayType2 instance with unused Pauli operators removed from the basis.

Return type:

OperatorArrayType2

reshape(shape: Tuple[int, ...]) Self[source]#

Returns a copy of self with a new shape.

Parameters:

shape (tuple[int]) – Tuple containing the new shape.

Returns:

The OperatorArray object with the new shape.

Return type:

OperatorArrayType2

property shape: Tuple[int, ...]#

Gets the shape of the OperatorArrayType2 excluding the last dimension (summation dimension).

Returns:

The shape of the weights excluding the last dimension.

Return type:

tuple

property size: int#

Gets the total number of elements in the OperatorArrayType2 excluding the last dimension (summation dimension).

Returns:

The total number of elements.

Return type:

int

squeeze() Self[source]#

Returns an OperatorArray with axes of length one removed.

Returns:

The squeezed OperatorArray.

Return type:

OperatorArrayType2

sum(axis: Tuple[int, ...] | None = None) Operator[source]#

Sums the operators along the specified axis.

Parameters:
  • axis (Union[Tuple[int, ...], None]) – Axis or axes along which to sum the operators. If None, sums over all

  • axes.

Returns:

The summed operator.

Return type:

op.Operator

property weights: np.ndarray[np.complex]#

Gets the weights associated with the basis Pauli arrays.

Returns:

The weights.

Return type:

“np.ndarray[np.complex]”

pauliarray.pauli.operator_array_type_2.commutator(operators_1: Self, operators_2: Self) Self[source]#

Computes the commutator

\[[A, B] = AB - BA\]

for pairs of operators from two operator arrays.

Parameters:
Returns:

An array of commutators of the input operator arrays.

Return type:

OperatorArrayType2

pauliarray.pauli.operator_array_type_2.concatenate(operatorss: Tuple[Self, ...], axis: int) Self[source]#

Concatenates multiple operator arrays along the specified axis.

Parameters:
  • operators (Tuple[OperatorArrayType2, ...]) – A tuple of operator arrays to concatenate.

  • axis (int) – The axis along which to concatenate the operator arrays.

Returns:

A new operator array resulting from the concatenation.

Return type:

OperatorArrayType2

pauliarray.pauli.pauli_array module#

class pauliarray.pauli.pauli_array.PauliArray(z_strings: np.ndarray[np.bool], x_strings: np.ndarray[np.bool])[source]#

Bases: object

Defines an array of Pauli strings.

__eq__(other: PauliArray) np.ndarray[np.bool][source]#

Checks element-wise if the other PauliArray is equal.

Parameters:

other (PauliArray) – An other PauliArray. Must be broadcastable

Returns:

_description_

Return type:

“np.ndarray[np.bool]”

add_pauli_array(other: PauliArray) OperatorArrayType1[source]#

Performs an element-wise addition with other Pauli Array to produce an array of operator.

Parameters:

other (PauliArray) – The PauliArray to add. Must be broadcastable.

Returns:

The result of the addition as an array of operators.

Return type:

OperatorArrayType1

bitwise_commute_with(other: PauliArray) np.ndarray[np.bool][source]#

Returns True if the elements of PauliArray commutes bitwise with the elements of PauliArray passed as parameter, returns False otherwise.

Parameters:

other (PauliArray) – The other PauliArray to verify bitwise commutation with.

Returns:

An array of bool set to true for bitwise commuting Pauli string, and false otherwise.

Return type:

“np.ndarray[np.bool]”

clifford_conjugate(clifford: Operator, inplace: bool = True) Tuple[PauliArray, np.ndarray[np.complex]][source]#

Performs a Clifford transformation.

Parameters:
  • clifford (Operator) – Must represent a Clifford transformation with the correct number of qubits.

  • inplace (bool) – Apply the changes to self if True. Return a modified copy if False.

Returns:

The transformed PauliArray “np.ndarray[np.complex]”: The factors resulting from the transformation

Return type:

PauliArray

commute_with(other: PauliArray) np.ndarray[np.bool][source]#

Returns True if the elements of PauliArray commutes with the elements of PauliArray passed as parameter, returns False otherwise.

Parameters:

other (PauliArray) – The PauliArray to check commutation with.

Returns:

An array of bool set to true for commuting Pauli string, and false otherwise.

Return type:

“np.ndarray[np.bool]”

compose(other: Any) Any[source]#
compose_pauli_array(other: PauliArray) Tuple[PauliArray, np.ndarray[np.complex]][source]#

Performs an element-wise composition with an other PauliArray.

Parameters:

other (PauliArray) – The PauliArray to compose with. Must be broadcastable.

Returns:

The result of the composition. “np.ndarray[np.complex]” : Phases resulting from the composition.

Return type:

PauliArray

compress_qubits(condition: np.ndarray[np.bool], inplace: bool = True) PauliArray[source]#

Return the Pauli strings for a subset of qubits, ignoring the other. Using a mask.

Parameters:
  • condition ("np.ndarray[np.bool]") – Array that selects which qubit to keep. Must be on length equal to the number of qubits.

  • inplace (bool) – Apply the changes to self if True. Return a modified copy if False.

Returns:

PauliArray with a reduced number of qubits.

Return type:

PauliArray

copy() PauliArray[source]#

Returns a copy of the PauliArray.

Returns:

Copied PauliArray.

Return type:

PauliArray

covariances_from_paulis(paulis_covariances: ndarray[Any, dtype[float64]]) ndarray[Any, dtype[float64]][source]#

Returns the PauliArray covariances given the covariances of the Paulis. More useful for other classes, but still here for uniformity.

Parameters:

paulis_covariances (NDArray[float]) – The covariance array of the underlying PauliArray. Must be of shape self.shape + self.shape

Returns:

The covariance array.

Return type:

NDArray

cx(control_qubits: int | List[int], target_qubits: int | List[int], inplace: bool = True) Tuple[PauliArray, np.ndarray[np.complex]][source]#

Performs a Clifford conjugaison by CX on given qubits. The order of the CX is set by the order of the qubits.

Parameters:
  • control_qubits (int or list[int]) – The qubits which controls the CX.

  • target_qubits (int or list[int]) – The qubits target by CX.

  • inplace (bool) – Apply the changes to self if True. Return a modified copy if False.

Returns:

The transformed PauliArray “np.ndarray[np.complex]”: The factors resulting from the transformation

Return type:

PauliArray

cz(control_qubits: int | List[int], target_qubits: int | List[int], inplace: bool = True) Tuple[PauliArray, np.ndarray[np.complex]][source]#

Performs a Clifford conjugaison by CZ on given qubits. The order of the CZ is set by the order of the qubits.

Parameters:
  • control_qubits (int or list[int]) – The qubits which controls the CZ.

  • target_qubits (int or list[int]) – The qubits target by CZ.

  • inplace (bool) – Apply the changes to self if True. Return a modified copy if False.

Returns:

The transformed PauliArray “np.ndarray[np.complex]”: The factors resulting from the transformation

Return type:

PauliArray

expectation_values_from_paulis(paulis_expectation_values: ndarray[Any, dtype[float64]]) ndarray[Any, dtype[float64]][source]#

Returns the PauliArray expectation value given the expectation values of the Paulis. More useful for other classes, but still here for uniformity.

Parameters:

paulis_expectation_values (NDArray[float]) – The expectation values of the underlying PauliArray. Must be of the same shape as self.

Returns:

The expectation values.

Return type:

NDArray

extract(condition: ndarray[Any, dtype[_ScalarType_co]] | list) PauliArray[source]#

Return the Pauli strings from the PauliArray object that satisfy some condition.

Parameters:

condition (Union[NDArray, list]) – An array whose nonzero or True entries indicate the Pauli strings of PauliArray to extract.

Returns:

A new PauliArray object containing the extracted Pauli strings.

Return type:

PauliArray

Raises:

ValueError – If the shape of the condition array is not equal to shape of the PauliArray.

flatten() PauliArray[source]#

Returns a copy of the PauliArray flattened into one dimension.

Returns:

A flattened copy of the current PauliArray.

Return type:

PauliArray

flip_zx() PauliArray[source]#

Returns a copy of the PauliArray with x strings as z strings, and vice versa.

Returns:

The PauliArray with flipped z and x strings.

Return type:

PauliArray

classmethod from_labels(labels: list[str] | np.ndarray[np.str]) PauliArray[source]#

Creates a PauliArray from a labels using IXYZ.

Parameters:

labels (Union[list[str], "np.ndarray[np.str]"]) – The list of labels.

Returns:

The PauliArray created from labels.

Return type:

new_pauli_array (PauliArray)

classmethod from_zx_strings(zx_strings: np.ndarray[np.bool]) PauliArray[source]#

Create a PauliArray from zx strings.

Parameters:

zx_strings ("np.ndarray[np.bool]") – Array where the last dimension size is an even integers (twice the number of qubits.)

Returns:

The created PauliArray .

Return type:

PauliArray

generators() PauliArray[source]#

Finds a set of linearly independant PauliString which can be multiplied together to generate every PauliStirng in self.

Returns:

The generators

Return type:

PauliArray

generators_with_map() Tuple[PauliArray, np.ndarray[np.bool]][source]#

Finds a set of linearly independant PauliString which can be multiplied together to generate every PauliStirng in self. Alse returns a matrix identifying which generators are involved in each PauliString in self.

Returns:

The generators NDArray: Element [idx,j] = True if generator j is used to construct self[idx]

Return type:

PauliArray

h(qubits: int | List[int], inplace: bool = True) Tuple[PauliArray, np.ndarray[np.complex]][source]#

Performs a Clifford conjugaison by H on given qubits. This exchanges X for Z and vice-versa and Y into -Y.

Parameters:
  • qubits (int or list[int]) – The qubits on which to apply H.

  • inplace (bool) – Apply the changes to self if True. Return a modified copy if False.

Returns:

The transformed PauliArray “np.ndarray[np.complex]”: The factors resulting from the transformation

Return type:

PauliArray

classmethod identities(shape: Tuple[int, ...], num_qubits: int) PauliArray[source]#

Creates a new PauliArray of a given shape and number of qubits filled with identities.

Parameters:
  • shape (_type_) – The shape of new the PauliArray.

  • num_qubits (_type_) – The number of qubits of the new PauliArray.

Returns:

The created PauliArray .

Return type:

PauliArray

inspect() str[source]#

Returns an inspection string showing all labels of the PauliArray.

Returns:

The inspection string.

Return type:

str

is_diagonal() np.ndarray[np.bool][source]#

Checks if the Pauli strings are diagonal i.e. if all Pauli strings are I or Z.

Returns:

True if the Pauli string is diagonal, False otherwise.

Return type:

NDArray[bool]

is_identity() np.ndarray[np.bool][source]#

Checks if the Pauli strings are identities i.e. if all Pauli strings are I.

Returns:

True if the Pauli string is identity, False otherwise.

Return type:

NDArray[bool]

static label_table_1d(labels) str[source]#
static label_table_2d(labels) str[source]#
static label_table_nd(labels) str[source]#
static label_to_z_string_x_string(label: str) Tuple[np.ndarray[np.bool], np.ndarray[np.bool]][source]#

Returns the z and x strings corresponding to the label passed as parameter.

Parameters:

label (str) – The label to convert to z_string and x_string.

Returns:

The z strings “np.ndarray[np.bool]” : The x strings

Return type:

“np.ndarray[np.bool]”

static labels_to_z_strings_x_strings(labels: list[str] | np.ndarray[np.str]) Tuple[np.ndarray[np.bool], np.ndarray[np.bool]][source]#

Returns z strings and x strings created from labels.

Parameters:

labels (Union[list[str], "np.ndarray[np.str]"]) – The list of labels.

Returns:

The z strings “np.ndarray[np.bool]” : The x strings

Return type:

“np.ndarray[np.bool]”

static matrix_from_zx_ints(z_int: int, x_int: int, num_qubits: int) ndarray[Any, dtype[_ScalarType_co]][source]#

Builds the matrix representing the Pauli String.

Parameters:
  • z_int (int) – Integer which binary representation defines the z part of a Pauli String.

  • x_int (int) – Integer which binary representation defines the x part of a Pauli String.

  • num_qubits (int) – Length of the Pauli String.

Returns:

The matrix reprensetating the Pauli String.

Return type:

NDArray

mul_weights(other: Number | ndarray[Any, dtype[_ScalarType_co]]) WeightedPauliArray[source]#

Apply a weight to each Pauli string to form a WeightedPauliArray

Parameters:

other (Union[Number, NDArray]) – A number or an array of number. Must be broadcastable.

Returns:

The result of the weight application.

Return type:

WeightedPauliArray

property ndim: int#

Returns the number of dimensions.

Returns:

The number of dimensions.

Return type:

int

classmethod new(shape: Tuple[int, ...], num_qubits: int) PauliArray[source]#
property num_ids: np.ndarray[np.int]#

Returns the number of identity operators.

Returns:

The number of identity operators.

Return type:

“np.ndarray[np.int]”

property num_non_ids: np.ndarray[np.int]#

Returns the number of non-identity operators.

Returns:

The number of non-identity operators.

Return type:

“np.ndarray[np.int]”

property num_qubits: int#

Returns the number of qubits.

Returns:

The number of qubits.

Return type:

int

property paulis: PauliArray#
classmethod random(shape: Tuple[int, ...], num_qubits: int) PauliArray[source]#

Creates a PauliArray of a given shape and number of qubits filled with random Pauli strings.

Parameters:
  • shape (_type_) – Shape of new PauliArray.

  • num_qubits (_type_) – Number of qubits of new PauliArray.

Returns:

The PauliArray created.

Return type:

new_PauliArray (PauliArray)

remove(index: int) PauliArray[source]#

Returns a PauliArray with removed item at given index.

Parameters:

index (int) – Index of element to remove.

Returns:

PauliArray with removed item at given index.

Return type:

PauliArray

reorder_qubits(qubit_order: List[int], inplace: bool = True) PauliArray[source]#

Reorder the qubits.

Parameters:
  • qubit_order – The new qubits order. Must contain each qubit index once.

  • inplace (bool) – Apply the changes to self if True. Return a modified copy if False.

Returns:

The transformed PauliArray

Return type:

PauliArray

reshape(shape: Tuple[int, ...]) PauliArray[source]#

Returns a PauliArray with a new shape.

Parameters:

shape (tuple[int]) – Tuple containing the new shape e.g. for 2x2 matrix: shape = (2,2)

Returns:

The PauliArray object with the new shape.

Return type:

PauliArray

s(qubits: int | List[int], inplace: bool = True) Tuple[PauliArray, np.ndarray[np.complex]][source]#

Performs a Clifford conjugaison by S on given qubits. This exchanges X for Y and vice-versa with respective factors.

Parameters:
  • qubits (int or list[int]) – The qubits on which to apply S.

  • inplace (bool) – Apply the changes to self if True. Return a modified copy if False.

Returns:

The transformed PauliArray “np.ndarray[np.complex]”: The factors resulting from the transformation

Return type:

PauliArray

property shape: Tuple[int, ...]#

Returns the shape of the object.

Returns:

The shape of the object.

Return type:

Tuple[int, …]

property size: int#

Returns the total number of elements in the PauliArray.

Returns:

The total number of elements in the PauliArray.

Return type:

int

static sparse_matrix_from_zx_ints(z_int: int, x_int: int, num_qubits: int) Tuple[ndarray[Any, dtype[_ScalarType_co]], ndarray[Any, dtype[_ScalarType_co]], ndarray[Any, dtype[_ScalarType_co]]][source]#

Builds the matrix representing the Pauli String encoded in a sparse notation.

Parameters:
  • z_int (int) – Integer which binary representation defines the z part of a Pauli String.

  • x_int (int) – Integer which binary representation defines the x part of a Pauli String.

  • num_qubits (int) – Length of the Pauli String.

Returns:

The row indices of returned matrix elements. col_ind (NDArray): The column indices of returned matrix elements. matrix_elements (NDArray): The matrix elements.

Return type:

row_ind (NDArray)

squeeze() PauliArray[source]#

Returns a PauliArray with axes of length one removed.

Returns:

The squeezed PauliArray.

Return type:

PauliArray

sum(axis: Tuple[int, ...] | None = None) OperatorArrayType1[source]#
take_qubits(indices: np.ndarray[np.int] | range | int, inplace: bool = True) PauliArray[source]#

Return the Pauli strings for a subset of qubits, ignoring the other. Using indices.

Parameters:
  • indices (Union["np.ndarray[np.int]", range, int]) – Index or array of indices of the qubits to return.

  • inplace (bool) – Apply the changes to self if True. Return a modified copy if False.

Returns:

PauliArray with a reduced number of qubits.

Return type:

PauliArray

tensor(other: Any) Any[source]#
tensor_pauli_array(other: PauliArray) PauliArray[source]#

Performs a tensor product, element-wise with an other PauliArray.

Parameters:

other (PauliArray) – The PauliArray to multiply with. Must be broadcastable.

Returns:

The result of the tensor product.

Return type:

PauliArray

to_labels() np.ndarray[np.str][source]#

Returns the labels of all zx strings.

Returns:

An array containing the labels of all Pauli strings.

Return type:

“np.ndarray[np.str]”

to_matrices() ndarray[Any, dtype[_ScalarType_co]][source]#

Returns the Pauli strings as a matrices.

Returns:

An ndarray of shape self.shape + (n**2, n**2).

Return type:

matrices (NDArray)

traces() ndarray[Any, dtype[_ScalarType_co]][source]#

Return the traces of the Pauli Strings which are 2^n if Identity and 0 otherwise.

Returns:

Traces of the Pauli Strings

Return type:

“np.ndarray[np.int]”

x(qubits: int | List[int], inplace: bool = True) Tuple[PauliArray, np.ndarray[np.complex]][source]#

Performs a Clifford conjugaison by X on given qubits. This leaves the PauliStrings unchanged but produce phase factors -1 when an operator is Y or Z.

Parameters:
  • qubits (int or list[int]) – The qubits on which to apply the X

  • inplace (bool) – Apply the changes to self if True. Return a modified copy if False.

Returns:

The transformed PauliArray “np.ndarray[np.complex]”: The factors resulting from the transformation

Return type:

PauliArray

property x_strings: np.ndarray[np.bool]#

Returns the X bits.

Returns:

The X bits.

Return type:

“np.ndarray[np.bool]”

property xz_strings: np.ndarray[np.bool]#

Returns the combined X and Z bits.

Returns:

The combined X and Z bits.

Return type:

“np.ndarray[np.bool]”

static z_string_x_string_to_label(z_string: np.ndarray[np.bool], x_string: np.ndarray[np.bool]) str[source]#

Converts a pair of z string and x string into a label (IXYZ).

Parameters:
  • z_string ("np.ndarray[np.bool]") – Single z string

  • x_string ("np.ndarray[np.bool]") – Single x string

Returns:

Label from the zx strings

Return type:

str

property z_strings: np.ndarray[np.bool]#

Returns the Z bits.

Returns:

The Z bits.

Return type:

“np.ndarray[np.bool]”

property zx_strings: np.ndarray[np.bool]#

Returns the combined Z and X bits.

Returns:

The combined Z and X bits.

Return type:

“np.ndarray[np.bool]”

pauliarray.pauli.pauli_array.anticommutator(paulis_1: PauliArray, paulis_2: PauliArray) Tuple[PauliArray, np.ndarray[np.complex]][source]#

Returns the anticommutator of the two PauliArray parameters.

Parameters:
  • paulis_1 (PauliArray) – PauliArray to calculate the anticommutator with.

  • paulis_2 (PauliArray) – Other PauliArray to calculate the anticommutator with.

Returns:

PauliArray containing the Pauli strings of the anticommutator. coefficients (“np.ndarray[np.complex]”) : Coefficients of Pauli strings in returned PauliArray.

Return type:

anticommutators_pauli_array (PauliArray)

pauliarray.pauli.pauli_array.argsort(paulis: PauliArray, axis: int = -1) np.ndarray[np.int][source]#

Return indices which sorts the Pauli Strings.

Returns:

Indices which sorts the Pauli Strings.

Return type:

NDArray

pauliarray.pauli.pauli_array.broadcast_to(paulis: PauliArray, shape: Tuple[int, ...]) PauliArray[source]#

Returns the given PauliArray broadcasted to a given shape.

Parameters:
  • paulis (PauliArray) – PauliArray to broadcast.

  • shape (Tuple[int, ...]) – Shape to broadcast to.

Returns:

The PauliArray with a new shape.

Return type:

new_pauli_array (PauliArray)

pauliarray.pauli.pauli_array.commutator(paulis_1: PauliArray, paulis_2: PauliArray) Tuple[PauliArray, np.ndarray[np.complex]][source]#

Returns the commutator of the two PauliArray parameters.

Parameters:
  • paulis_1 (PauliArray) – PauliArray to calculate commmutator with.

  • paulis_2 (PauliArray) – Other PauliArray to calculate commmutator with.

Returns:

PauliArray containing the Pauli strings of the commutators. “np.ndarray[np.complex]” : Coefficients of Pauli strings in returned PauliArray.

Return type:

PauliArray

pauliarray.pauli.pauli_array.commutator2(paulis_1: PauliArray, paulis_2: PauliArray) Tuple[PauliArray, np.ndarray[np.complex]][source]#

Returns the commutator of the two PauliArray parameters.

Parameters:
  • paulis_1 (PauliArray) – PauliArray to calculate commmutator with.

  • paulis_2 (PauliArray) – Other PauliArray to calculate commmutator with.

Returns:

PauliArray containing the Pauli strings of the commutator. coefficients (“np.ndarray[np.complex]”) : Coefficients of Pauli strings in returned PauliArray.

Return type:

commutator_pauli_array (PauliArray)

pauliarray.pauli.pauli_array.concatenate(paulis: Tuple[PauliArray, ...], axis: int) PauliArray[source]#

Concatenated multiple PauliArrays.

Parameters:
  • paulis (List[PauliArray]) – PauliArrays to concatenate.

  • axis (int) – The axis along which the arrays will be joined.

Returns:

The concatenated PauliArrays.

Return type:

PauliArray

pauliarray.pauli.pauli_array.expand_dims(paulis: PauliArray, axis: int | Tuple[int, ...]) PauliArray[source]#

Expands the shape of a PauliArray.

Inserts a new axis that will appear at the axis position in the expanded array shape.

Parameters:
  • paulis (PauliArray) – The PauliArray to expand.

  • axis (Union[int, Tuple[int, ...]]) – The axis upon which expand the PauliArray.

Returns:

The expanded PauliArray.

Return type:

expanded_pauli_array (PauliArray)

pauliarray.pauli.pauli_array.fast_flat_unique(paulis: PauliArray, return_index: bool = False, return_inverse: bool = False, return_counts: bool = False) PauliArray | Tuple[PauliArray, ndarray[Any, dtype[_ScalarType_co]]][source]#

Faster version of unique for PauliArray. Only works with flat PauliArray. Directly uses numpy.unique.

Parameters:
  • paulis (PauliArray) – The PauliArray to return. Must be flat.

  • return_index (bool, optional) – If True, also return the indices of PauliArray (along the specified axis, if provided, or in the flattened array) that result in the unique array. Defaults to False.

  • return_inverse (bool, optional) – If True, also return the indices of the unique array (for the specified axis, if provided) that can be used to reconstruct array. Defaults to False.

  • return_counts (bool, optional) – If True, also return the number of times each unique item appears in array. Defaults to False.

Returns:

The unique Pauli strings in a PauliArray NDArray, optional: Index to get unique from the orginal PauliArray NDArray, optional: Innverse to reconstrut the original PauliArray from unique NDArray, optional: The number of each unique in the original PauliArray

Return type:

PauliArray

pauliarray.pauli.pauli_array.moveaxis(paulis: PauliArray, source: int, destination: int) PauliArray[source]#

Move an axis of a PauliArray

Parameters:
  • paulis (PauliArray) – The PauliArray

  • source (int) – Original axis position

  • destination (int) – Target axis position

Returns:

The PauliArrays with axis moved.

Return type:

PauliArray

pauliarray.pauli.pauli_array.swapaxes(paulis: PauliArray, axis1: int, axis2: int) PauliArray[source]#

Swap axes of a PauliArray

Parameters:
  • paulis (PauliArray) – The PauliArray

  • axis1 (int) – Original axis position

  • axis2 (int) – Target axis position

Returns:

The PauliArrays with axes swaped.

Return type:

PauliArray

pauliarray.pauli.pauli_array.unique(paulis: PauliArray, axis: int | None = None, return_index: bool = False, return_inverse: bool = False, return_counts: bool = False) PauliArray | Tuple[PauliArray, ndarray[Any, dtype[_ScalarType_co]]][source]#

Finds unique elements in a PauliArray. Directly uses numpy.unique and has the same interface.

Parameters:
  • paulis (PauliArray) – The PauliArray to return.

  • axis (Optional[int], optional) – The axis to operate on. If None, the PauliArray will be flattened. If an integer, the subarrays indexed by the given axis will be flattened and treated as the elements of a 1-D array with the dimension of the given axis. Object arrays or structured arrays that contain objects are not supported if the axis kwarg is used. Defaults to None.

  • return_index (bool, optional) – If True, also return the indices of PauliArray (along the specified axis, if provided, or in the flattened array) that result in the unique array. Defaults to False.

  • return_inverse (bool, optional) – If True, also return the indices of the unique array (for the specified axis, if provided) that can be used to reconstruct array. Defaults to False.

  • return_counts (bool, optional) – If True, also return the number of times each unique item appears in array. Defaults to False.

Returns:

The unique Pauli strings (or PauliArray along an axis) in a PauliArray NDArray, optional: Index to get unique from the orginal PauliArray NDArray, optional: Innverse to reconstrut the original PauliArray from unique NDArray, optional: The number of each unique in the original PauliArray

Return type:

PauliArray

pauliarray.pauli.weighted_pauli_array module#

class pauliarray.pauli.weighted_pauli_array.WeightedPauliArray(paulis: PauliArray, weights: np.ndarray[np.complex] | Number)[source]#

Bases: object

__eq__(other: WeightedPauliArray) np.ndarray[np.bool][source]#

Checks element-wise if the other WeightedPauliArray is equal.

Parameters:

other (WeightedPauliArray) – An other WeightedPauliArray. Must be broadcastable

Returns:

_description_

Return type:

“np.ndarray[np.bool]”

add_weighted_pauli_array(other: WeightedPauliArray) OperatorArrayType1[source]#
adjoint() WeightedPauliArray[source]#
bitwise_commute_with(other: WeightedPauliArray) np.ndarray[np.bool][source]#
clifford_conjugate(clifford: Operator, inplace: bool = True) WeightedPauliArray[source]#

Performs a Clifford transformation.

Parameters:
  • clifford (Operator) – Must represent a Clifford transformation with the correct number of qubits.

  • inplace (bool) – Apply the changes to self if True. Return a modified copy if False.

Returns:

The transformed WeightedPauliArray

Return type:

WeightedPauliArray

commute_with(other: WeightedPauliArray) np.ndarray[np.bool][source]#
compose(other: Any) Any[source]#
compose_weighted_pauli_array(other: WeightedPauliArray) WeightedPauliArray[source]#
compress_qubits(condition: np.ndarray[np.bool]) WeightedPauliArray[source]#
copy() WeightedPauliArray[source]#

Returns a copy of the WeightedPauliArray.

Returns:

Copied WeightedPauliArray.

Return type:

WeightedPauliArray

covariances_from_paulis(paulis_covariances: ndarray[Any, dtype[float64]]) np.ndarray[np.complex][source]#

Returns the WeightedPauliArray covariances given the covariances of the Paulis.

Parameters:

paulis_covariances (NDArray[float]) – _description_

Returns:

_description_

Return type:

NDArray

cx(control_qubits: int | List[int], target_qubits: int | List[int], inplace: bool = True) WeightedPauliArray[source]#

Apply CX transformations on qubits of WeightedPauliStrings. The order of the CX is set by the order of the qubits.

Parameters:
  • control_qubits (int or list[int]) – The qubits which controls the CZ.

  • target_qubits (int or list[int]) – The qubits target by CZ.

  • inplace (bool) – Apply the changes to self if True. Return a modified copy if False.

Returns:

A modified copy of self, only if inplace=True

Return type:

self_copy (WeightedPauliArray)

cz(control_qubits: int | List[int], target_qubits: int | List[int], inplace: bool = True) WeightedPauliArray[source]#

Apply CZ transformations on qubits of WeightedPauliStrings. The order of the CZ is set by the order of the qubits.

Parameters:
  • control_qubits (int or list[int]) – The qubits which controls the CZ.

  • target_qubits (int or list[int]) – The qubits target by CZ.

  • inplace (bool) – Apply the changes to self if True. Return a modified copy if False.

Returns:

A modified copy of self, only if inplace=True

Return type:

self_copy (WeightedPauliArray)

classmethod empty(num_qubits: int) WeightedPauliArray[source]#

Returns an empty WeightedPauliArray with the number of qubits already set.

Parameters:

num_qubits (int) – The number of qubits.

Returns:

An empty WeightedPauliArray

Return type:

WeightedPauliArray

expectation_values_from_paulis(paulis_expectation_values: ndarray[Any, dtype[float64]]) np.ndarray[np.complex][source]#

Returns the WeightedPauliArray expectation value given the expectation values of the Paulis.

Parameters:

paulis_expectation_values (NDArray[float]) – _description_

Returns:

_description_

Return type:

NDArray

extract(condition: ndarray[Any, dtype[_ScalarType_co]] | list) WeightedPauliArray[source]#

Return the Pauli strings from the WeightedPauliArray object that satisfy some condition.

Parameters:

condition (Union[NDArray, list]) – An array whose nonzero or True entries indicate the Pauli strings of WeightedPauliArray to extract.

Returns:

A new WeightedPauliArray object containing the extracted Pauli strings.

Return type:

WeightedPauliArray

Raises:

ValueError – If the shape of the condition array is not equal to shape of the WeightedPauliArray.

flatten() WeightedPauliArray[source]#

Returns a copy of the WeightedPauliArray flattened into one dimension.

Returns:

A flattened copy of the current WeightedPauliArray.

Return type:

WeightedPauliArray

classmethod from_labels_and_weights(labels, weights) WeightedPauliArray[source]#
classmethod from_npz(filename) WeightedPauliArray[source]#
classmethod from_paulis(paulis: PauliArray) WeightedPauliArray[source]#
classmethod from_z_strings_and_x_strings_and_weights(z_strings: np.ndarray[np.bool], x_strings: np.ndarray[np.bool], weights: np.ndarray[np.complex]) WeightedPauliArray[source]#
h(qubits: int | List[int], inplace: bool = True) WeightedPauliArray[source]#

Apply H transformations on qubits of WeightedPauliStrings. This exchanges X for Z and vice-versa and Y into -Y.

Parameters:
  • qubits (int or list[int]) – The qubits on which to apply the H.

  • inplace (bool) – Apply the changes to self if True. Return a modified copy if False.

Returns:

A modified copy of self, only if inplace=True

Return type:

self_copy (WeightedPauliArray)

inspect() str[source]#
is_diagonal() np.ndarray[np.bool][source]#

Checks if the Pauli strings are diagonal i.e. if all Pauli strings are I or Z.

Returns:

True if the Pauli string is diagonal, False otherwise.

Return type:

NDArray[bool]

static label_table_1d(labels, weights) str[source]#
static label_table_2d(labels, weights) str[source]#
static label_table_nd(labels, weights) str[source]#
mul_weights(other: Number | ndarray[Any, dtype[_ScalarType_co]]) WeightedPauliArray[source]#
property ndim: int#
classmethod new(shape: Tuple[int, ...], num_qubits: int) WeightedPauliArray[source]#
property num_qubits: int#
property paulis: PauliArray#
classmethod random(shape: Tuple[int, ...], num_qubits: int) WeightedPauliArray[source]#

Creates a PauliArray of a given shape and number of qubits filled with random Pauli strings.

Parameters:
  • shape (_type_) – Shape of new PauliArray.

  • num_qubits (_type_) – Number of qubits of new PauliArray.

Returns:

The PauliArray created.

Return type:

new_PauliArray (PauliArray)

remove(index: int) WeightedPauliArray[source]#

Returns a WeightedPauliArray with removed item at given index.

Parameters:

index (int) – Index of element to remove.

Returns:

WeightedPauliArray with removed item at given index.

Return type:

WeightedPauliArray

reshape(shape: Tuple[int, ...]) WeightedPauliArray[source]#

Reshape the WeightedPauliArray

Parameters:

shape (tuple[int]) – New shape

Returns:

Reshaped WeightedPauliArray

Return type:

WeightedPauliArray

s(qubits: int | List[int], inplace: bool = True) WeightedPauliArray[source]#

Apply S transformations on qubits of WeightedPauliStrings. This exchanges X for Y and vice-versa with respective factors.

Parameters:
  • qubits (int or list[int]) – The qubits on which to apply the S.

  • inplace (bool) – Apply the changes to self if True. Return a modified copy if False.

Returns:

A modified copy of self, only if inplace=True

Return type:

self_copy (WeightedPauliArray)

property shape: Tuple[int, ...]#
property size: int#
squeeze() WeightedPauliArray[source]#

Returns a WeightedPauliArray with axes of length one removed.

Returns:

The squeezed WeightedPauliArray.

Return type:

WeightedPauliArray

take_qubits(indices: np.ndarray[np.int] | range | int) WeightedPauliArray[source]#
tensor(other: Any) Any[source]#
tensor_weighted_pauli_array(other: WeightedPauliArray) WeightedPauliArray[source]#
to_matrices() ndarray[Any, dtype[_ScalarType_co]][source]#

Returns the WeightedPauliArray as a numpy matrix.

Returns:

An ndarray of shape self.shape + (n**2, n**2).

Return type:

matrices (NDArray)

to_npz(filename)[source]#
update_weights(new_weights)[source]#
update_weights_from_other(other: WeightedPauliArray)[source]#
property weights: np.ndarray[np.complex]#
x(qubits: int | List[int], inplace: bool = True) WeightedPauliArray[source]#

Apply X transformations on qubits of WeightedPauliStrings. This leaves the PauliStrings unchanged but produce phase factors -1 when operators are Y or Z.

Parameters:
  • qubits (int or list[int]) – The qubits on which to apply the X.

  • inplace (bool) – Apply the changes to self if True. Return a modified copy if False.

Returns:

A modified copy of self, only if inplace=True

Return type:

self_copy (WeightedPauliArray)

pauliarray.pauli.weighted_pauli_array.anticommutator(wpaulis_1: WeightedPauliArray, wpaulis_2: WeightedPauliArray) WeightedPauliArray[source]#
pauliarray.pauli.weighted_pauli_array.broadcast_to(wpaulis: WeightedPauliArray, shape: Tuple[int, ...]) WeightedPauliArray[source]#

Returns the given WeightedPauliArray broadcasted to a given shape.

Parameters:
  • paulis (WeightedPauliArray) – WeightedPauliArray to broadcast.

  • shape (Tuple[int, ...]) – Shape to broadcast to.

Returns:

The WeightedPauliArray with a new shape.

Return type:

new_pauli_array (WeightedPauliArray)

pauliarray.pauli.weighted_pauli_array.commutator(wpaulis_1: WeightedPauliArray, wpaulis_2: WeightedPauliArray) WeightedPauliArray[source]#

Returns the commutator of the two WeightedPauliArray parameters.

Parameters:
Returns:

WeightedPauliArray containing the commutators.

Return type:

commutator_pauli_array (WeightedPauliArray)

pauliarray.pauli.weighted_pauli_array.concatenate(wpauli_arrays: Tuple[WeightedPauliArray, ...], axis: int) WeightedPauliArray[source]#

Concatenated multiple WeightedPauliArrays.

Parameters:
  • paulis (List[PauliArray]) – WeightedPauliArrays to concatenate.

  • axis (int) – The axis along which the arrays will be joined.

Returns:

The concatenated WeightedPauliArrays.

Return type:

WeightedPauliArray

pauliarray.pauli.weighted_pauli_array.expand_dims(wpaulis: WeightedPauliArray, axis=typing.Union[int, typing.Tuple[int, ...]]) WeightedPauliArray[source]#

Expands the shape of a WeightedPauliArray.

Inserts a new axis that will appear at the axis position in the expanded array shape.

Parameters:
  • paulis (WeightedPauliArray) – The WeightedPauliArray to expand.

  • axis (Union[int, Tuple[int, ...]]) – The axis upon which expand the WeightedPauliArray.

Returns:

The expanded WeightedPauliArray.

Return type:

expanded_pauli_array (WeightedPauliArray)

pauliarray.pauli.weighted_pauli_array.moveaxis(wpaulis: WeightedPauliArray, source: int, destination: int)[source]#

Move an axis of a WeightedPauliArray

Parameters:
  • paulis (WeightedPauliArray) – The WeightedPauliArray

  • axis1 (int) – Original axis position

  • axis2 (int) – Target axis position

Returns:

The WeightedPauliArrays with axis moved.

Return type:

WeightedPauliArray

pauliarray.pauli.weighted_pauli_array.swapaxes(wpaulis: WeightedPauliArray, axis1: int, axis2: int)[source]#

Swap axes of a WeightedPauliArray

Parameters:
  • paulis (WeightedPauliArray) – The WeightedPauliArray

  • axis1 (int) – Original axis position

  • axis2 (int) – Target axis position

Returns:

The WeightedPauliArrays with axes swaped.

Return type:

WeightedPauliArray

Module contents#

Pauli module. Implements the various data structures made available by the PauliArray library.