Skip to content

PauliTerm

PauliTerm represents a single term in a Pauli operator expansion -- that is, a tensor product of Pauli matrices acting on specific qubits, multiplied by a scalar coefficient:

T=cjσj

where σj{I,X,Y,Z} and cC.

A PauliTerm is the smallest independently addressable unit of a PauliOperator. You can inspect its coefficient, enumerate the individual Pauli-with-qubit pairs, convert it to a quantum circuit, or extract its raw data in several formats.

Methods

coef

python
PauliTerm.coef() -> complex

Returns the complex coefficient of this Pauli term.

Returns: A complex number.

to_qcircuit

python
PauliTerm.to_qcircuit() -> QCircuit

Converts this Pauli term into a quantum circuit. Each non-identity Pauli in the term is translated into the corresponding quantum gate (X, Y, or Z) acting on the appropriate qubit. Identity Paulis are skipped.

Returns: A QCircuit object representing the term.

paulis

python
PauliTerm.paulis() -> list[PauliWithQbit]

Returns the list of Pauli matrices with their associated qubit indices that make up this term.

Returns: A list of PauliWithQbit objects, one per qubit in the term.

qubits

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

Returns the qubits involved in this term, partitioned into those acted on only by identity and those acted on by a non-identity Pauli (X, Y, or Z).

Returns: A tuple (identity_only_qubits, non_identity_qubits) where each element is a list of integer qubit indices.

max_qbit_idx

python
PauliTerm.max_qbit_idx() -> int

Returns the maximum qubit index referenced by this term.

Returns: An integer.

data_3tuple_complex_coeff

python
PauliTerm.data_3tuple_complex_coeff() -> tuple[str, list[int], complex]

Returns the internal data as a single tuple containing the Pauli string, the qubit indices, and the complex coefficient.

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

data_2tuple_complex_coeff

python
PauliTerm.data_2tuple_complex_coeff() -> tuple[str, complex]

Returns the internal data as a (pauli_string, complex_coefficient) tuple using sparse notation (only non-identity Paulis are included in the string).

Returns: For example ("Z0", 0.395+0j).

data_3tuple_float_coeff

python
PauliTerm.data_3tuple_float_coeff() -> tuple[str, list[int], float]

Returns the internal data as a single tuple with a float coefficient. Raises an error if the coefficient has a non-zero imaginary part.

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

data_2tuple_float_coeff

python
PauliTerm.data_2tuple_float_coeff() -> tuple[str, float]

Returns the internal data as a (pauli_string, float_coefficient) tuple using sparse notation.

Returns: For example ("Z0", 0.395).

Examples

Inspect terms of a PauliOperator

python
from pyqpanda3.hamiltonian import PauliOperator

# Create a PauliOperator and inspect its individual terms
h = PauliOperator({"Z0 Z1": 0.5, "X0": 0.3})
for term in h.terms():
    print(f"Coefficient: {term.coef()}")
    for pwq in term.paulis():
        print(f"  Qubit {pwq.qbit()}: {pwq.pauli_char()}")
    print()

Extract data in different formats

python
from pyqpanda3.hamiltonian import PauliOperator

h = PauliOperator({"X0 Y1 Z2": 1.5})
term = h.terms()[0]

# 3-tuple with complex coefficient
print(term.data_3tuple_complex_coeff())
# Output: ('XYZ', [0, 1, 2], (1.5+0j))

# 2-tuple with complex coefficient (sparse string)
print(term.data_2tuple_complex_coeff())
# Output: ('X0 Y1 Z2', (1.5+0j))

# Float variants
print(term.data_3tuple_float_coeff())
print(term.data_2tuple_float_coeff())

Check qubit involvement

python
from pyqpanda3.hamiltonian import PauliOperator

h = PauliOperator({"X0 I1 Z2": 1.0})
term = h.terms()[0]

identity_qubits, pauli_qubits = term.qubits()
print("Identity-only qubits:", identity_qubits)   # [1]
print("Non-identity qubits:", pauli_qubits)        # [0, 2]

See Also

Released under the MIT License.