quri_parts.core.operator.operator module#

class quri_parts.core.operator.operator.Operator#

Bases: dict[PauliLabel, complex]

Operator represents the set of single-term operators as a dict[PauliLabel, coefficient].

Coefficients can not only be real values but also complex values since Operator represents a general operator including non-Hermitian one.

Example

>>> op = Operator({pauli_label("X0"): 0.1j})
>>> op
{PauliLabel({(0, <SinglePauli.X: 1>)}): 0.1j}
>>> op[pauli_label("X1 Y2")] = 0.2
>>> op
{PauliLabel({(0, <SinglePauli.X: 1>)}): 0.1j, PauliLabel({(1, <SinglePauli.X: 1>), (2, <SinglePauli.Y: 2>)}): 0.2}
>>> str(op)
'0.1j*X0 + 0.2*X1 Y2'
>>> for pauli, coef in op.items():
...     print(f"Pauli: {pauli}, coefficient: {coef}")
...
Pauli: X0, coefficient: 0.1j
Pauli: X1 Y2, coefficient: 0.2
>>> op.constant
0.0
>>> op[PAULI_IDENTITY] = 2.0
>>> op
{PauliLabel({(0, <SinglePauli.X: 1>)}): 0.1j, PauliLabel({(1, <SinglePauli.X: 1>), (2, <SinglePauli.Y: 2>)}): 0.2, PauliLabel(): 2.0}
>>> op.constant
2.0
>>> op.constant = 1.0
>>> op.constant
1.0
You can add a single-term operator by add_term() method.
>>> operator.add_term(PauliLabel({(1, 1)}), 1.0)
By accessing by key, the coefficient is replaced with a new value.
>>> operator = Operator({pauli_label("X0"): 0.1j})
>>> op
{PauliLabel({(0, <SinglePauli.X: 1>)}): 0.1j}
>>> operator[pauli_label("X0")] = 0.1
>>> op
{PauliLabel({(0, <SinglePauli.X: 1>)}): 0.1}
add_term(pauli_label: PauliLabel, coef: complex) None#

Method for adding single-term operator.

copy() Operator#

Returns the copy of itself.

property n_terms: int#

Number of all terms.

property constant: complex#

Constant value.

Note that the constant value is a coefficient of an identity pauli term (PAULI_IDENTITY).

hermitian_conjugated() Operator#
quri_parts.core.operator.operator.zero() Operator#

Returns zero (empty) operator, which always return zero expectation value for all states.

quri_parts.core.operator.operator.commutator(op1: Operator, op2: Operator) Operator#

Returns the commutator of op1 and op2 :math:`[text{op1},

text{op2}]`.

quri_parts.core.operator.operator.is_ops_close(op1: Operator, op2: Operator, rtol: float = 1e-09, atol: float = 0.0) bool#

Returns True if two operators are close to each other.

quri_parts.core.operator.operator.truncate(op: Operator, atol: float = 1e-08) Operator#

Returns truncated operator by eliminating terms whose coefficients are smaller than atol.

quri_parts.core.operator.operator.is_hermitian(op: Operator, atol: float = 1e-08) bool#

Returns True if given operator is hermitian.