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 (
where
The four Pauli matrices are:
Construction
Default constructor
PauliOperator() -> PauliOperatorConstructs an empty (null) PauliOperator with no terms.
Copy constructor
PauliOperator(pauli_operator: PauliOperator) -> PauliOperator| Parameter | Type | Description |
|---|---|---|
pauli_operator | PauliOperator | An existing PauliOperator to deep-copy. |
From string
PauliOperator(pauli: str) -> PauliOperatorConstructs a PauliOperator from a string representation of a single Pauli string.
| Parameter | Type | Description |
|---|---|---|
pauli | str | A string like "X0 Z1" or "Z0 X1 Y2". Each token is a Pauli character followed by a qubit index. |
From dictionary
PauliOperator(pauli_with_coef_s: dict[str, complex]) -> PauliOperatorConstructs a PauliOperator from a dictionary mapping Pauli strings to complex coefficients.
| Parameter | Type | Description |
|---|---|---|
pauli_with_coef_s | dict[str, complex] | A dict such as {"X0 Z1": 1.1, "Y2 I1": 2.1+0j}. |
From parallel lists (sparse notation)
PauliOperator(paulis: list[str], coefs: list[complex]) -> PauliOperatorConstructs from two parallel lists -- Pauli strings and their coefficients.
| Parameter | Type | Description |
|---|---|---|
paulis | list[str] | A list of Pauli strings such as ["X0 Z1", "Y2 I1"]. |
coefs | list[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)
PauliOperator(paulis: list[str], coefs: list[complex], AB_is_A1_B0: bool) -> PauliOperatorConstructs from parallel lists using compact Pauli notation, where each string character maps positionally to a qubit index.
| Parameter | Type | Description |
|---|---|---|
paulis | list[str] | A list of compact Pauli strings such as ["XZI", "IZX"]. Character position maps to qubit index. |
coefs | list[complex] | A list of coefficients such as [1.1+0j, 2.1+0j]. Must be the same length as paulis. |
AB_is_A1_B0 | bool | If 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
PauliOperator(paulis_qbits_coef__s: list[tuple[str, list[int], complex]]) -> PauliOperatorConstructs from a list of tuples, each specifying a Pauli string, the qubit indices, and a coefficient.
| Parameter | Type | Description |
|---|---|---|
paulis_qbits_coef__s | list[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
PauliOperator(mat: numpy.ndarray) -> PauliOperatorConstructs a PauliOperator by decomposing a real-valued matrix into a sum of Pauli strings.
| Parameter | Type | Description |
|---|---|---|
mat | numpy.ndarray | A real-valued square matrix (2D, double-precision). The matrix dimension must be a power of 2. |
Operators
Addition
PauliOperator.__add__(other: PauliOperator) -> PauliOperator
PauliOperator.__add__(num: complex) -> PauliOperator
PauliOperator.__radd__(num: complex) -> PauliOperator
PauliOperator.__iadd__(other: PauliOperator) -> PauliOperator
PauliOperator.__iadd__(num: complex) -> PauliOperatorAdds 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
PauliOperator.__sub__(other: PauliOperator) -> PauliOperator
PauliOperator.__sub__(num: complex) -> PauliOperator
PauliOperator.__rsub__(num: complex) -> PauliOperator
PauliOperator.__isub__(other: PauliOperator) -> PauliOperator
PauliOperator.__isub__(num: complex) -> PauliOperatorSubtracts one PauliOperator from another, or subtracts a scalar. For num - self, the result is equivalent to PauliOperator("I") * num - self.
Unary negation
PauliOperator.__neg__() -> PauliOperatorReturns the negation of the operator, equivalent to self * (-1).
Multiplication
PauliOperator.__mul__(other: PauliOperator) -> PauliOperator
PauliOperator.__mul__(scalar: complex) -> PauliOperator
PauliOperator.__rmul__(scalar: complex) -> PauliOperatorMultiplies two PauliOperators (following Pauli algebra rules: PauliOperator.
In-place multiplication
PauliOperator.__imul__(other: PauliOperator) -> PauliOperatorMultiplies the current PauliOperator by another PauliOperator in place, following Pauli algebra rules. The current object is updated to be the product.
Matrix multiplication (@)
PauliOperator.__matmul__(other: PauliOperator) -> PauliOperatorEquivalent to __mul__. Computes the product of two PauliOperators using Pauli algebra.
Tensor product
PauliOperator.tensor(other: PauliOperator) -> PauliOperator
PauliOperator.tensor(n: int) -> PauliOperatorComputes 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
PauliOperator.__eq__(other: PauliOperator) -> boolTwo PauliOperators are equal if they have the same terms with the same coefficients.
Methods
str_with_I
PauliOperator.str_with_I(AB_is_A1_B0: bool = True) -> strReturns 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.
| Parameter | Type | Description |
|---|---|---|
AB_is_A1_B0 | bool | Controls qubit-to-character index ordering. Default True. |
Returns: A string like "IXZ" representing the operator on all qubits.
str_no_I / str_without_I
PauliOperator.str_no_I() -> str
PauliOperator.str_without_I() -> strReturns a string representation excluding identity terms. The format uses "X0", "Y1", "Z2" notation.
Returns: A string like "X0 Z1".
matrix
PauliOperator.matrix() -> numpy.ndarrayConverts 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
PauliOperator.group_commuting(qubit_wise: bool = False) -> list[list[PauliOperator]]Groups the terms into commuting subsets.
| Parameter | Type | Description |
|---|---|---|
qubit_wise | bool | If 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
PauliOperator.terms() -> list[PauliTerm]Returns the individual Pauli terms that make up this operator.
Returns: A list of PauliTerm objects.
to_qcircuits
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
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
PauliOperator.max_qbit_idx() -> intReturns the maximum qubit index referenced by any term in the operator.
Returns: An integer qubit index.
data_3tuple_list_complex_coeff
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
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
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
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
PauliOperator.data_dict_str(is_coeff_complex: bool = True, double_quote: bool = True) -> strSerializes the operator to a string format suitable for later deserialization.
| Parameter | Type | Description |
|---|---|---|
is_coeff_complex | bool | Whether to serialize coefficients as complex numbers. Default True. |
double_quote | bool | Whether to use double quotes for keys. Default True. |
Returns: A serialized string like '"Z0": 0.395, "Z1": -0.395'.
from_dict_str (static)
PauliOperator.from_dict_str(dict_str: str) -> PauliOperatorDeserializes a string produced by data_dict_str() back into a PauliOperator.
| Parameter | Type | Description |
|---|---|---|
dict_str | str | A 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.
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
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
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
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.0Matrix representation
from pyqpanda3.hamiltonian import PauliOperator
h = PauliOperator({"Z0": 1.0, "X0": 0.5})
mat = h.matrix()
print(mat)Group commuting terms
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
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
- PauliTerm -- Individual term within a PauliOperator
- PauliWithQbit -- Single Pauli matrix on a qubit
- Hamiltonian -- Higher-level Hamiltonian wrapper