parameter¶
Symbolic expressions built from real-valued, finite, atomic Parameter instances based on sympy
symbols and expressions.
Also defines Coefficient used e.g. in OperatorPolynomial, QuantumState or Parameterized
classes as union of symbolic expressions and Python built-in numeric types.
- class parityos.utils.parameter.Parameterized¶
Bases:
ABCInterface for any object that may contain symbolic parameters.
- abstractmethod 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.
- class parityos.utils.parameter.HasNoParameters¶
Bases:
ParameterizedImplementation of
Parameterizedinterface for classes that have no parameters.
- class parityos.utils.parameter.SupportsConjugate(*args, **kwargs)¶
Bases:
ProtocolA protocol representing objects that support the
conjugateoperation.
- class parityos.utils.parameter.BinaryOp(*args, **kwargs)¶
Bases:
ProtocolA protocol representing a binary operation.
This protocol defines a callable that takes two arguments and returns a
Exprresult. At least one of the arguments must be anExprwhile the other one can also be acomplex.- __call__(**kwds)¶
Helper for @overload to raise when called.
- class parityos.utils.parameter.Assumptions(*args, **kwargs)¶
Bases:
ProtocolA protocol for classes that hold assumption predicates for symbolic expressions.
- Not all predicates are independent. For example,
zero=Trueautomatically implies nonzero=Falsepositive=Falsenegative=False
Implementing classes must ensure that all attributes are consistent.
This protocol formalizes (most of) the [SymPy assumption predicates](https://docs.sympy.org/latest/guides/assumptions.html#predicates) into an object with named attributes.
Note
SymPy supports the following numerical tower
integer <: rational <: real <: complexwhere<:means “is a subtype of”. That means every well defined number is considered complex. Strictly complex symbols with a non-zero imaginary part are therefore marked withreal = False.- classmethod from_expr(expr: Expr) Self¶
Create
Assumptionsfrom a given SymPy expression.
- Not all predicates are independent. For example,
- class parityos.utils.parameter.ExpressionAssumptions(real: bool | None = None, finite: bool | None = None, zero: bool | None = None, nonzero: bool | None = None, positive: bool | None = None, negative: bool | None = None, rational: bool | None = None, irrational: bool | None = None, integer: bool | None = None, even: bool | None = None, odd: bool | None = None)¶
Bases:
AssumptionsAssumptions that are appropriate for a composite symbolic
ParameterExpression.- classmethod from_expr(expr: Expr) Self¶
Create assumptions from a given SymPy expression.
All further assumptions in
exprthat are not fields ofAssumptionsare disregarded.- Parameters:
expr – SymPy expression to fetch assumptions from.
- Returns:
Assumptions with values taken from
expr.
- class parityos.utils.parameter.ParameterAssumptions(zero: bool | None = None, nonzero: bool | None = None, positive: bool | None = None, negative: bool | None = None, rational: bool | None = None, irrational: bool | None = None, integer: bool | None = None, even: bool | None = None, odd: bool | None = None)¶
Bases:
ExpressionAssumptionsAssumptions that are appropriate for an atomic symbolic
Parameter.Parameterinstances are always assumed to be real and finite. This class thus auto-sets these attributes accordingly and removes them from the constructor.
- class parityos.utils.parameter.Symbolic(*args, **kwargs)¶
Bases:
SupportsConjugate,ProtocolInterface for symbolic objects based on SymPy expressions (
Expr) and symbols (Symbol).Symbolic expressions also have
Assumptionsthat hold any assumption predicates that are known for this expression.- property assumptions: Assumptions¶
The assumptions of this symbolic expression.
- property is_number: bool¶
Whether the expression is purely numeric (as opposed to containing symbols).
- parityos.utils.parameter.RealCoefficient: TypeAlias = parityos.utils.parameter.Symbolic | float¶
A numeric or symbolic real scalar coefficient.
- parityos.utils.parameter.Coefficient: TypeAlias = parityos.utils.parameter.Symbolic | complex¶
A numeric or symbolic scalar coefficient.
- class parityos.utils.parameter.Parameter(name: str, assumptions: ParameterAssumptions = NOTHING)¶
Bases:
SymbolicAn atomic, real-valued, finite symbolic parameter.
Possible additional assumptions (e.g.
positive,integer, …) can be given in form of anParameterAssumptionsinstance.Caution
Different Parameters with the same name but different assumptions are not assumed equal.
- assumptions: ParameterAssumptions¶
The assumptions for this parameter. Defaults to
ParameterAssumptionswith default parameters.
- expr: Symbol¶
The SymPy
Symbolrepresenting this parameter.Its assumptions are taken from
assumptions.
- classmethod from_symbol(symbol: Symbol) Self¶
Create a parameter from an existing SymPy
Symbol.Caution
Realness and finiteness of
symbolare disregarded, asParameterinstances are always real and positive.
- class parityos.utils.parameter.ParameterExpression(expr: Expr)¶
Bases:
SymbolicA symbolic expression containing one or more symbolic parameters.
Assumptionsare deduced from the contained parameter assumptions and their underlying SymPy expression.Examples
>>> # `a` could be zero, so `1/a` is undefined. >>> expr = 1./Parameter("a") >>> assert expr.assumptions.real is None >>> # `a` is explicitly nonzero, so `1/a` is real and finite. >>> expr = 1./Parameter("a", ParameterAssumptions(nonzero=True)) >>> assert expr.assumptions.real
- assumptions: Assumptions¶
The assumptions that are known for this expression.
The assumptions are automatically deduced from the input SymPy expression.
- classmethod from_string(expr: str) Self¶
Create a
ParameterExpressionfrom a string representation.- Parameters:
expr – String representation of the expression.
- Returns:
A
ParameterExpressioncreate from the string expressionexpr.
- property is_number: bool¶
Whether the expression is purely numeric (as opposed to containing symbols).
- conjugate() ParameterExpression¶
Return the complex conjugate of this object.
- parityos.utils.parameter.make_real_finite_symbol(name: str, zero: bool | None = None, positive: bool | None = None, rational: bool | None = None, integer: bool | None = None, even: bool | None = None) Symbol¶
Create a real-valued, finite Symbol with additional assumptions.
Assumptions that are set to
Noneare considered unset, i.e. the according property of the resulting symbol is unknown.- Parameters:
name – Name of the symbol.
zero – Whether the symbol is zero.
positive – Whether the symbol is positive,
rational – Whether the symbol is a rational number,
integer – Whether the symbol is an integer,
even – Whether the symbol is even,
- Returns:
A real-valued, finite symbol with
nameand given assumptions.
- parityos.utils.parameter.convert_to_real_finite_symbol(symbol: Symbol) Symbol¶
Convert a SymPy
Symbolto a real, finiteSymbol, preserving other assumptions.Preserved assumptions are whether the symbol is non-zero, positive, an integer and even/odd.
- Parameters:
symbol – The symbol to convert to a real, finite symbol.
- Returns:
The converted symbol representing a real, finite number.
- parityos.utils.parameter.get_symbols(expr: Basic) frozenset[Symbol]¶
Get free symbols from an expression that are
Symbolinstances.This function filters any results from
Basic.free_symbolsthat are notSymbolinstances.
- parityos.utils.parameter.desympify_numeric_expression(expr: Expr) complex¶
The inverse of
sympifyfor numeric types.- Parameters:
expr – The numeric expression to turn into a Python built-in numeric type.
- Returns:
A Python built-in numeric type (int, float, or complex) equivalent to the input expression.
- Raises:
TypeError – If
expris not a purely numeric expression.
- parityos.utils.parameter.to_sympy(expr: Symbolic | complex) Expr | complex¶
Convert a coefficient to a SymPy
Exprif the coefficient isSymbolic.If
expris numeric, it is returned unchanged.- Parameters:
expr – The
Coefficientto convert.- Returns:
The sympy expression equivalent to the input coefficient. If the input is already a sympy expression, it’s returned directly. Otherwise, the input is returned as is.
- parityos.utils.parameter.symbolic_from_sympy(expr: Expr) Symbolic¶
Convert any SymPy expression to a
Symbolicobject.Purely numeric expressions are not converted, use
coefficient_from_sympyfor that purpose.- Parameters:
expr – The sympy expression to convert.
- Returns:
A
Symbolicobject representing the input SymPy expression. If the input is aSymbol, an instance ofParameteris returned. If the input is aExpr, an instance ofParameterExpressionis returned.- Raises:
ParityOSTypeError – If
expris notSymbolorExpr.
- parityos.utils.parameter.symbolic_from_string(expr: str) Symbolic¶
Convert symbolic expression from string to a
Symbolicobject.Purely numeric expressions are not converted, use
coefficient_from_stringfor that purpose.- Parameters:
expr – The string representation of the symbolic expression.
- Returns:
A
Symbolicobject representing the parsed expression.- Raises:
ParityOSTypeError – If the parsed expression is not a
SymbolorExpr.
- parityos.utils.parameter.coefficient_from_sympy(expr: Expr) Symbolic | complex¶
Convert any SymPy expression to a
Symbolicobject.Purely numeric expressions are converted to Python built-in types.
- Parameters:
expr – The sympy expression to convert.
- Returns:
A
Coefficientobject representing the input sympy expression.- Raises:
ParityOSTypeError – If
expris not aSymbolorExpr.
- parityos.utils.parameter.coefficient_from_string(expr: str) Symbolic | complex¶
Parse symbolic expression from string to a
Symbolicobject.Purely numeric expressions are converted to Python built-in types.
- Parameters:
expr – The string expression to convert.
- Returns:
A
Coefficientobject representing the input string expression.- Raises:
ParityOSTypeError – If the parsed expression is not
SymbolorExpr.
- parityos.utils.parameter.is_real_coefficient(coefficient: Symbolic | complex) bool¶
Whether a coefficient is real, i.e. not complex at all or complex with zero imaginary part.
- Parameters:
coefficient – The coefficient to check.
- Returns:
Whether the coefficient is real.
- parityos.utils.parameter.convert_coefficient(coefficient: Symbolic | complex) Symbolic | complex¶
Convert purely numeric symbolic expressions to Python built-in numeric types and downcast if possible. Return symbolic coefficients unchanged.
- Parameters:
coefficient – The coefficient to convert.
- Returns:
The converted coefficient. If the input is a symbolic number, it’s converted to a Python numeric type. Otherwise, the input is returned unchanged.
- parityos.utils.parameter.substitute_parameters(number: Symbolic | complex, old_to_new: Mapping[Parameter, Symbolic | complex]) Symbolic | complex¶
If
numberis a symbolic expression, return a copy with all parameters replaced according to the mappingold_to_new. Ifnumberis numeric, return unchanged.Any parameters appearing in
old_to_newthat don’t appear innumberare disregarded.- Parameters:
number – The symbolic expression or number to substitute parameters in.
old_to_new – A mapping from old parameters to new coefficients.
- Returns:
A copy of the input
numberwith parameters substituted, or the originalnumberif it is numeric.
- parityos.utils.parameter.convert_to_unique_term_coefficient_frozenset(elements: Iterable[tuple[KeyT, Symbolic | complex]] | Mapping[KeyT, Symbolic | complex]) frozenset[tuple[KeyT, Symbolic | complex]]¶
Convert an iterable of (key, coefficient) pairs to a
frozensetof (key, coefficient) pairs.Pairs with the same key but different coefficients are collected into a single pair by summing the coefficients. Pairs with coefficient equal to zero are dropped.
- Parameters:
elements – Iterable or mapping of (key, coefficient) pairs.
- Returns:
frozensetof (key, coefficient) pairs with no duplicate keys.
- parityos.utils.parameter.coefficient_to_prefix_str(coefficient: Symbolic | complex) str¶
Construct a string prefix for an
OperatorProductstring representation containing the coefficient with a sign.- Parameters:
coefficient – Number for which a string prefix is constructed.
- Returns:
A string prefix for
coefficient.