operator_polynomial

Representation of linear combinations of tensor products of operators (OperatorProduct instances).

Operator polynomials (OperatorPolynomial) arise from summing different operators, possibly weighted by a Coefficient. For instance, an operator polynomial is the natural representation of a Hamiltonian or a SumConstraint.

They support basic arithmetic like addition and multiplication, where multiplication is to be interpreted as tensor product. Operator polynomials can thus easily be constructed piece by piece from elementary algebra.

Example

>>> operator_polynomial = Z(get_q(1)) - 1.5 * Z(get_q(2)) * Z(get_q(3))
final class parityos.operators.operator_polynomial.OperatorPolynomial(term_coefficient_pairs: Iterable[tuple[OperatorProduct[OperatorT_co], Symbolic | complex]] | Mapping[OperatorProduct[OperatorT_co], Symbolic | complex])

Bases: OperatorCompound[OperatorT_co], Sized, Parameterized

A linear combination of tensor products of operators (OperatorProduct instances).

Constituent operator tensor products are called terms and are stored together with their coefficients as a frozenset of (term, coefficient) pairs.

Implements basic arithmetic like addition and multiplication with other Operator, OperatorProduct and OperatorPolynomial instances.

term_coefficient_pairs: frozenset[tuple[OperatorProduct[OperatorT_co], Symbolic | complex]]

The set of (term, coefficient) pairs

term_to_coefficient: MappingProxyType

A mapping of each term to its coefficient.

property n_terms: int

The number of terms in this polynomial.

__len__() int

The number of terms in this polynomial.

property terms: frozenset[OperatorProduct[OperatorT_co]]

The set of terms without their coefficients.

is_all(cls: type[OtherOperatorT]) TypeGuard[OperatorPolynomial[OtherOperatorT]]

Check whether all contained operators are instances of cls.

property is_mixed: bool

Check whether all contained operators are of the same type.

property name: str

Dynamically generated name for this operator.

Where applicable the name must coincide with the OpenQASM standard.

qubits: frozenset[Qubit]
get_hermitian_conjugate() Self

Return the hermitian conjugate (\(\dagger\)) of this operator.

is_hermitian: bool

Checks whether this operator polynomial is hermitian.

Despite individual terms being non-hermitian (either the term itself, or a complex coefficient) the overall polynomial can be hermitian, if all non-hermitian terms appear in hermitian conjugate pairs.

This method keeps track of all non-hermitian terms and pairs them off with hermitian conjugate partners. If there are any unpaired non-hermitian terms at the end, the method returns False.

Returns:

Whether this operator polynomial is hermitian.

parameters: frozenset[Parameter]
substitute_parameters(old_to_new: Mapping[Parameter, Symbolic | complex]) Self

Substitute symbolic parameters with new symbolic expressions or numeric values.

Parameters:

old_to_new – Mapping from existing parameter symbols to new values (numeric or symbolic).

Returns:

Copy of self with replaced parameters.

__neg__() OperatorPolynomial[OperatorT_co]

The negation operator -.

Returns:

A copy of self with the sign of all coefficients flipped.

__mul__(other: Symbolic | complex) Self
__mul__(other: OperatorPolynomial[OtherOperatorT]) OperatorPolynomial[OperatorT_co | OtherOperatorT]

The multiplication operator *.

Multiplication with other operators is to be interpreted as tensor product.

Caution

If other is another operator, it may not share any qubit with self.

Parameters:

other – A Coefficient or another OperatorPolynomial.

Returns:

If other is a Coefficient, returns a copy of self with all coefficients multiplied with other. If other is another OperatorPolynomial, returns the sum of the products of all possible (term, coefficient) pairs of self and other.

Raises:

ParityOSUniquenessError – If qubits of self and other overlap in case other is another OperatorPolynomial.

__truediv__(other: Symbolic | complex) Self

The division operator / with a Coefficient.

Args: The coefficient to divide by.

Returns:

A copy of self with all term coefficients divided by other.

__add__(other: int | OperatorPolynomial[OtherOperatorT]) OperatorPolynomial[OperatorT_co | OtherOperatorT]

The addition operator +.

Parameters:

other – An int or another OperatorPolynomial.

Returns:

If other is another OperatorPolynomial, returns a new OperatorPolynomial with the union of all terms in self and other and sum up their coefficients. If other is the integer 0, return self. This option is to enable sum of OperatorPolynomial instances.

__sub__(other: OperatorPolynomial[OtherOperatorT]) OperatorPolynomial[OperatorT_co | OtherOperatorT]

The subtraction operator -.

Parameters:

other – Another OperatorPolynomial.

Returns:

self + (-other), see __add__ and __neg__.

parityos.operators.operator_polynomial.sum_terms(terms: Iterable[OperatorPolynomial[OperatorT] | OperatorProduct[OperatorT] | OperatorT]) OperatorPolynomial[OperatorT]

Sums iterable of terms together.

Terms can be Operator, OperatorProduct or OperatorPolynomial instances.

Wraps sum with correct defaults and typing.

Parameters:

terms – Iterable of terms to add up.

Returns:

A sum of all terms as an OperatorPolynomial.

Raises:

ParityOSException – If terms is empty.