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,ParameterizedA linear combination of tensor products of operators (
OperatorProductinstances).Constituent operator tensor products are called
termsand are stored together with their coefficients as afrozensetof (term, coefficient) pairs.Implements basic arithmetic like addition and multiplication with other
Operator,OperatorProductandOperatorPolynomialinstances.- 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 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 name: str¶
Dynamically generated name for this operator.
Where applicable the name must coincide with the OpenQASM standard.
- 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.
- 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
selfwith replaced parameters.
- __neg__() OperatorPolynomial[OperatorT_co]¶
The negation operator
-.- Returns:
A copy of
selfwith 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
otheris another operator, it may not share any qubit with self.- Parameters:
other – A
Coefficientor anotherOperatorPolynomial.- Returns:
If
otheris aCoefficient, returns a copy ofselfwith all coefficients multiplied withother. Ifotheris anotherOperatorPolynomial, returns the sum of the products of all possible (term, coefficient) pairs ofselfandother.- Raises:
ParityOSUniquenessError – If qubits of
selfandotheroverlap in caseotheris anotherOperatorPolynomial.
- __truediv__(other: Symbolic | complex) Self¶
The division operator
/with aCoefficient.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
intor anotherOperatorPolynomial.- Returns:
If
otheris anotherOperatorPolynomial, returns a newOperatorPolynomialwith the union of all terms inselfandotherand sum up their coefficients. Ifotheris the integer0, returnself. This option is to enablesumofOperatorPolynomialinstances.
- __sub__(other: OperatorPolynomial[OtherOperatorT]) OperatorPolynomial[OperatorT_co | OtherOperatorT]¶
The subtraction operator
-.- Parameters:
other – Another
OperatorPolynomial.- Returns:
- parityos.operators.operator_polynomial.sum_terms(terms: Iterable[OperatorPolynomial[OperatorT] | OperatorProduct[OperatorT] | OperatorT]) OperatorPolynomial[OperatorT]¶
Sums iterable of terms together.
Terms can be
Operator,OperatorProductorOperatorPolynomialinstances.Wraps
sumwith correct defaults and typing.- Parameters:
terms – Iterable of terms to add up.
- Returns:
A sum of all terms as an
OperatorPolynomial.- Raises:
ParityOSException – If
termsis empty.