quri_parts.core.operator.operator module#

class 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, coef)#

Method for adding single-term operator.

Parameters:
Return type:

None

copy()#

Returns the copy of itself.

Return type:

Operator

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()#
Return type:

Operator

zero()#

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

Return type:

Operator

commutator(op1, op2)#

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

text{op2}]`.

Parameters:
Return type:

Operator

is_ops_close(op1, op2, rtol=1e-09, atol=0.0)#

Returns True if two operators are close to each other.

Parameters:
Return type:

bool

truncate(op, atol=1e-08)#

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

Parameters:
Return type:

Operator

is_hermitian(op, atol=1e-08)#

Returns True if given operator is hermitian.

Parameters:
Return type:

bool