Skip to content

PauliWithQbit ​

PauliWithQbit represents a single Pauli matrix (I, X, Y, or Z) associated with a specific qubit index. It is the most granular building block in the Hamiltonian module -- each PauliTerm is composed of a collection of PauliWithQbit instances, one per qubit.

Methods ​

qbit ​

python
PauliWithQbit.qbit() -> int

Returns the qubit index that this Pauli matrix acts on.

Returns: An integer qubit index.

is_I ​

python
PauliWithQbit.is_I() -> bool

Returns True if this Pauli is the identity matrix I.

Returns: A boolean.

is_X ​

python
PauliWithQbit.is_X() -> bool

Returns True if this Pauli is the X (bit-flip) matrix.

Returns: A boolean.

is_Y ​

python
PauliWithQbit.is_Y() -> bool

Returns True if this Pauli is the Y matrix.

Returns: A boolean.

is_Z ​

python
PauliWithQbit.is_Z() -> bool

Returns True if this Pauli is the Z (phase-flip) matrix.

Returns: A boolean.

to_qgate ​

python
PauliWithQbit.to_qgate() -> QGate

Converts this Pauli into the corresponding quantum gate object. The mapping is:

PauliGate
IIdentity (no-op)
XX gate
YY gate
ZZ gate

Returns: A QGate applied to the qubit at index qbit().

pauli_char ​

python
PauliWithQbit.pauli_char() -> str

Returns a single-character string identifying the Pauli type: "I", "X", "Y", or "Z".

Returns: A one-character string.

Examples ​

Inspect Paulis in a term ​

python
from pyqpanda3.hamiltonian import PauliOperator

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

for pwq in term.paulis():
    print(f"Qubit {pwq.qbit()}: {pwq.pauli_char()}")
    print(f"  is_X: {pwq.is_X()}, is_Y: {pwq.is_Y()}, is_Z: {pwq.is_Z()}, is_I: {pwq.is_I()}")

运行结果:

Qubit 0: X
  is_X: True, is_Y: False, is_Z: False, is_I: False
Qubit 1: Y
  is_X: False, is_Y: True, is_Z: False, is_I: False
Qubit 2: Z
  is_X: False, is_Y: False, is_Z: True, is_I: False

Convert a Pauli to a quantum gate ​

python
from pyqpanda3.hamiltonian import PauliOperator

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

for pwq in term.paulis():
    if not pwq.is_I():
        gate = pwq.to_qgate()
        print(f"Gate on qubit {pwq.qbit()}: {gate}")

运行结果:

Gate on qubit 0: <pyqpanda3.core.core.QGate object at 0x...>
Gate on qubit 1: <pyqpanda3.core.core.QGate object at 0x...>

See Also ​

Released under the MIT License.