Skip to content

PauliOperator

PauliOperator represents a weighted sum of Pauli strings acting on qubits. It is the primary class for defining quantum observables and problem Hamiltonians in pyqpanda3. Each term in the operator is a tensor product of Pauli matrices (I, X, Y, Z) on specific qubits, multiplied by a complex coefficient:

O=icijσij

where σij{I,X,Y,Z} and ciC.

The four Pauli matrices are:

I=(1001),X=(0110),Y=(0ii0),Z=(1001)

Construction

Default constructor

python
PauliOperator() -> PauliOperator

Constructs an empty (null) PauliOperator with no terms.

Copy constructor

python
PauliOperator(pauli_operator: PauliOperator) -> PauliOperator
ParameterTypeDescription
pauli_operatorPauliOperatorAn existing PauliOperator to deep-copy.

From string

python
PauliOperator(pauli: str) -> PauliOperator

Constructs a PauliOperator from a string representation of a single Pauli string.

ParameterTypeDescription
paulistrA string like "X0 Z1" or "Z0 X1 Y2". Each token is a Pauli character followed by a qubit index.

From dictionary

python
PauliOperator(pauli_with_coef_s: dict[str, complex]) -> PauliOperator

Constructs a PauliOperator from a dictionary mapping Pauli strings to complex coefficients.

ParameterTypeDescription
pauli_with_coef_sdict[str, complex]A dict such as {"X0 Z1": 1.1, "Y2 I1": 2.1+0j}.

From parallel lists (sparse notation)

python
PauliOperator(paulis: list[str], coefs: list[complex]) -> PauliOperator

Constructs from two parallel lists -- Pauli strings and their coefficients.

ParameterTypeDescription
paulislist[str]A list of Pauli strings such as ["X0 Z1", "Y2 I1"].
coefslist[complex]A list of coefficients such as [1.1+0j, 2.1+0j]. Must be the same length as paulis.

From parallel lists (compact notation)

python
PauliOperator(paulis: list[str], coefs: list[complex], AB_is_A1_B0: bool) -> PauliOperator

Constructs from parallel lists using compact Pauli notation, where each string character maps positionally to a qubit index.

ParameterTypeDescription
paulislist[str]A list of compact Pauli strings such as ["XZI", "IZX"]. Character position maps to qubit index.
coefslist[complex]A list of coefficients such as [1.1+0j, 2.1+0j]. Must be the same length as paulis.
AB_is_A1_B0boolIf True, character index 0 in the string corresponds to qubit 0. If False, character index 0 corresponds to the highest qubit index.

From list of tuples

python
PauliOperator(paulis_qbits_coef__s: list[tuple[str, list[int], complex]]) -> PauliOperator

Constructs from a list of tuples, each specifying a Pauli string, the qubit indices, and a coefficient.

ParameterTypeDescription
paulis_qbits_coef__slist[tuple[str, list[int], complex]]For example [("XZ", [0, 4], 1.1+0j), ("YX", [1, 2], 2.1+0j)]. Each tuple is (pauli_string, qubit_indices, coefficient).

From matrix

python
PauliOperator(mat: numpy.ndarray) -> PauliOperator

Constructs a PauliOperator by decomposing a real-valued matrix into a sum of Pauli strings.

ParameterTypeDescription
matnumpy.ndarrayA real-valued square matrix (2D, double-precision). The matrix dimension must be a power of 2.

Operators

Addition

python
PauliOperator.__add__(other: PauliOperator) -> PauliOperator
PauliOperator.__add__(num: complex) -> PauliOperator
PauliOperator.__radd__(num: complex) -> PauliOperator
PauliOperator.__iadd__(other: PauliOperator) -> PauliOperator
PauliOperator.__iadd__(num: complex) -> PauliOperator

Adds two PauliOperators or adds a scalar (treated as a coefficient on the identity). When two terms have identical Pauli strings, their coefficients are summed. Returns a new PauliOperator.

Subtraction

python
PauliOperator.__sub__(other: PauliOperator) -> PauliOperator
PauliOperator.__sub__(num: complex) -> PauliOperator
PauliOperator.__rsub__(num: complex) -> PauliOperator
PauliOperator.__isub__(other: PauliOperator) -> PauliOperator
PauliOperator.__isub__(num: complex) -> PauliOperator

Subtracts one PauliOperator from another, or subtracts a scalar. For num - self, the result is equivalent to PauliOperator("I") * num - self.

Unary negation

python
PauliOperator.__neg__() -> PauliOperator

Returns the negation of the operator, equivalent to self * (-1).

Multiplication

python
PauliOperator.__mul__(other: PauliOperator) -> PauliOperator
PauliOperator.__mul__(scalar: complex) -> PauliOperator
PauliOperator.__rmul__(scalar: complex) -> PauliOperator

Multiplies two PauliOperators (following Pauli algebra rules: X2=I, XY=iZ, etc.) or scales all coefficients by a scalar. Returns a new PauliOperator.

In-place multiplication

python
PauliOperator.__imul__(other: PauliOperator) -> PauliOperator

Multiplies the current PauliOperator by another PauliOperator in place, following Pauli algebra rules. The current object is updated to be the product.

Matrix multiplication (@)

python
PauliOperator.__matmul__(other: PauliOperator) -> PauliOperator

Equivalent to __mul__. Computes the product of two PauliOperators using Pauli algebra.

Tensor product

python
PauliOperator.tensor(other: PauliOperator) -> PauliOperator
PauliOperator.tensor(n: int) -> PauliOperator

Computes the tensor product (Kronecker product) of this operator with another, or with itself n-1 times. In the result, qubit indices of the left operand remain unchanged, while indices of the right operand are shifted by the number of qubits in the left operand.

Equality

python
PauliOperator.__eq__(other: PauliOperator) -> bool

Two PauliOperators are equal if they have the same terms with the same coefficients.

Methods

str_with_I

python
PauliOperator.str_with_I(AB_is_A1_B0: bool = True) -> str

Returns a string representation including identity (I) characters. If AB_is_A1_B0 is True, the first character in the string corresponds to the highest qubit index.

ParameterTypeDescription
AB_is_A1_B0boolControls qubit-to-character index ordering. Default True.

Returns: A string like "IXZ" representing the operator on all qubits.

str_no_I / str_without_I

python
PauliOperator.str_no_I() -> str
PauliOperator.str_without_I() -> str

Returns a string representation excluding identity terms. The format uses "X0", "Y1", "Z2" notation.

Returns: A string like "X0 Z1".

matrix

python
PauliOperator.matrix() -> numpy.ndarray

Converts the PauliOperator to its dense matrix representation.

Returns: A complex-valued NumPy array of shape (2^n, 2^n) where n is the number of qubits.

group_commuting

python
PauliOperator.group_commuting(qubit_wise: bool = False) -> list[list[PauliOperator]]

Groups the terms into commuting subsets.

ParameterTypeDescription
qubit_wiseboolIf True, use qubit-wise commutation (Paulis on the same qubit must commute). If False, use full commutation (commutator equals zero). Default False.

Returns: A list of lists of PauliOperator, where each inner list contains mutually commuting terms.

terms

python
PauliOperator.terms() -> list[PauliTerm]

Returns the individual Pauli terms that make up this operator.

Returns: A list of PauliTerm objects.

to_qcircuits

python
PauliOperator.to_qcircuits() -> list[tuple[QCircuit, complex]]

Converts each term into a quantum circuit paired with its coefficient.

Returns: A list of (QCircuit, complex) tuples.

qubits

python
PauliOperator.qubits() -> tuple[list[int], list[int]]

Returns the qubits involved in the operator, partitioned into those acted on only by identity and those acted on by non-identity Paulis.

Returns: A tuple (identity_only_qubits, non_identity_qubits).

max_qbit_idx

python
PauliOperator.max_qbit_idx() -> int

Returns the maximum qubit index referenced by any term in the operator.

Returns: An integer qubit index.

data_3tuple_list_complex_coeff

python
PauliOperator.data_3tuple_list_complex_coeff() -> list[tuple[str, list[int], complex]]

Returns the internal data as a list of (pauli_string, qubit_indices, complex_coefficient) tuples.

Returns: For example [("XXZ", [0, 1, 4], 1+2j), ("ZZ", [1, 2], -1+1j)].

data_3tuple_list_float_coeff

python
PauliOperator.data_3tuple_list_float_coeff() -> list[tuple[str, list[int], float]]

Returns the internal data as a list of (pauli_string, qubit_indices, float_coefficient) tuples. Raises an error if any coefficient has a non-zero imaginary part.

Returns: For example [("XXZ", [0, 1, 4], 1.0), ("ZZ", [1, 2], -1.0)].

data_dict_float_coeff

python
PauliOperator.data_dict_float_coeff() -> dict[str, float]

Returns the operator data as a dictionary mapping Pauli strings to float coefficients.

Returns: For example {"Z0": 0.395, "Z1": -0.395}.

data_dict_complex_coeff

python
PauliOperator.data_dict_complex_coeff() -> dict[str, complex]

Returns the operator data as a dictionary mapping Pauli strings to complex coefficients.

Returns: For example {"Z0": 0.395+0j, "Z1": -0.395+0j}.

data_dict_str

python
PauliOperator.data_dict_str(is_coeff_complex: bool = True, double_quote: bool = True) -> str

Serializes the operator to a string format suitable for later deserialization.

ParameterTypeDescription
is_coeff_complexboolWhether to serialize coefficients as complex numbers. Default True.
double_quoteboolWhether to use double quotes for keys. Default True.

Returns: A serialized string like '"Z0": 0.395, "Z1": -0.395'.

from_dict_str (static)

python
PauliOperator.from_dict_str(dict_str: str) -> PauliOperator

Deserializes a string produced by data_dict_str() back into a PauliOperator.

ParameterTypeDescription
dict_strstrA serialized string like '"Z0": 0.395, "Z1": -0.395'.

Returns: A new PauliOperator.

Examples

Construct from a dictionary

A common pattern for defining Hamiltonians is to pass a dictionary of Pauli strings and coefficients.

python
from pyqpanda3.hamiltonian import PauliOperator

# Define a 2-qubit Hamiltonian:
# H = 0.5 * Z0 Z1 + 0.3 * X0 + 0.1 * X1
h = PauliOperator({"Z0 Z1": 0.5, "X0": 0.3, "X1": 0.1})
print(h)

Construct from compact notation with flag

python
from pyqpanda3.hamiltonian import PauliOperator

# Compact strings: character position maps to qubit index
# "XZ" with qubit indices [0, 1] means X on qubit 0, Z on qubit 1
h = PauliOperator(["XZ", "ZX"], [1.0+0j, -1.0+0j], True)
print(h)

Arithmetic operations

python
from pyqpanda3.hamiltonian import PauliOperator

a = PauliOperator({"X0": 1.0})
b = PauliOperator({"Z0": 1.0})

# Addition
s = a + b
print("a + b =", s)

# Multiplication (Pauli algebra: X * Z = -iY)
p = a * b
print("a * b =", p)

# Scalar multiplication
scaled = 2.0 * a
print("2 * a =", scaled)

# Negation
neg = -a
print("-a =", neg)

Tensor product

python
from pyqpanda3.hamiltonian import PauliOperator

a = PauliOperator({"X0": 1.0})
b = PauliOperator({"Z0": 1.0})

# Tensor product: b's qubits are shifted
t = a.tensor(b)
print("a tensor b =", t)
# Result: X0 Z1 with coefficient 1.0

Matrix representation

python
from pyqpanda3.hamiltonian import PauliOperator

h = PauliOperator({"Z0": 1.0, "X0": 0.5})
mat = h.matrix()
print(mat)

Group commuting terms

python
from pyqpanda3.hamiltonian import PauliOperator

h = PauliOperator({"X0": 1.0, "Z0": 1.0, "X1": 0.5})
groups = h.group_commuting(qubit_wise=True)
for i, group in enumerate(groups):
    print(f"Group {i}:")
    for op in group:
        print(f"  {op}")

Serialize and deserialize

python
from pyqpanda3.hamiltonian import PauliOperator

h = PauliOperator({"Z0": 0.5, "X1": -0.3})
s = h.data_dict_str()
print("Serialized:", s)

h2 = PauliOperator.from_dict_str(s)
print("Deserialized:", h2)
print("Equal:", h == h2)

See Also

Released under the MIT License.