qilisdk.core.expression
A symbolic expression tree (AST) for qilisdk.
The public entry point is Expression, the abstract base of every node:
leaves –
Constantand the variable family (Parameter,Variable,BinaryVariable,SpinVariable) defined inqilisdk.core.variables;unary maths functions –
Functionand its concrete subclasses (Sin,Cos,Exp,Log,Tan,Sqrt).
Construction canonicalizes (a cheap, total normalization: flattening, combining like terms/powers,
folding constants, eliminating identities, ordering operands deterministically). Canonical form is
the sole definition of ==/hash – equal expressions are structurally identical and equality
is order-independent for + and * (x + y == y + x). Semantic rewrites – expand() and
derivative() – are explicit and never participate in equality.
Classes
Abstract base of every node in the expression tree. |
|
A numeric literal leaf. Replaces the old |
|
A canonical n-ary sum. Operands are deterministically ordered and combine like terms. |
|
A canonical n-ary product. Collects like powers and folds the numeric coefficient. |
|
A power |
|
Abstract base for unary maths functions ( |
|
Sine of an expression. |
|
Cosine of an expression. |
|
Exponential of an expression. |
|
Natural logarithm of an expression. |
|
Tangent of an expression. |
|
Square root of an expression. |
|
Absolute value of an expression. |
Functions
|
Build the reciprocal |
Module Contents
- class Expression[source]
Bases:
abc.ABCAbstract base of every node in the expression tree.
- property is_idempotent_under_mul: bool[source]
Whether
self * self == self(true only forBinaryVariable).
- abstractmethod evaluate(env: collections.abc.Mapping[qilisdk.core.variables.BaseVariable, qilisdk.core.types.Number | list[int]] | None = None) qilisdk.core.types.Number[source]
Numerically evaluate the expression given an assignment of symbols to values.
- abstractmethod free_symbols() set[qilisdk.core.variables.BaseVariable][source]
The set of named leaves (variables/parameters) occurring in the expression.
- property degree: int[source]
- Abstractmethod:
Polynomial degree. Raises
NonPolynomialErrorfor non-polynomial expressions.
- abstractmethod derivative(symbol: qilisdk.core.variables.BaseVariable) Expression[source]
Symbolic derivative with respect to
symbol.
- expand() Expression[source]
Distribute products over sums.
- Returns:
a canonical sum-of-monomials equal to this expression.
- Return type:
- substitute(mapping: collections.abc.Mapping[Expression, Expression | qilisdk.core.types.Number]) Expression[source]
Structurally replace sub-expressions according to
mapping.- Returns:
the expression with substitutions applied.
- Return type:
- to_binary() Expression[source]
Encode every continuous
Variableinto binary variables (no-op for most nodes).- Returns:
an equivalent expression over binary variables.
- Return type:
- free_parameters() set[qilisdk.core.variables.Parameter][source]
Collect the parameters occurring in the expression.
- Returns:
the free parameters.
- Return type:
set[Parameter]
- variables() list[qilisdk.core.variables.BaseVariable][source]
Collect the named leaves of the expression.
- Returns:
the variables and parameters, sorted by label.
- Return type:
list[BaseVariable]
- is_parameterized() bool[source]
Whether every named leaf is a
Parameter.- Returns:
Trueif the expression contains only parameters (or no variables).- Return type:
bool
- get_constant() qilisdk.core.types.Number[source]
Return the additive constant of the expression.
- Returns:
the constant term (0 unless the expression is a sum or a constant).
- Return type:
Number
- as_coefficients_dict() dict[Expression, qilisdk.core.types.Number][source]
Map each (non-constant) monomial to its numeric coefficient.
- Returns:
a mapping from monomial to coefficient.
- Return type:
dict[Expression, Number]
- monomial_factors() list[tuple[Expression, int]][source]
Decompose a single monomial into its factors.
- Returns:
the
(base, integer_power)factors.- Return type:
list[tuple[Expression, int]]
- to_list() list[Expression][source]
The node’s operands as a list: the summands of a sum, the factors of a product.
A leaf has no operands, so it yields itself. This is the readable spelling of
.args.- Returns:
the operands of this node.
- Return type:
list[Expression]
- class Constant(value: qilisdk.core.types.Number)[source]
Bases:
ExpressionA numeric literal leaf. Replaces the old
Term.CONSTsentinel.- property value: qilisdk.core.types.Number[source]
nodes are immutable so the cached hash stays valid.
- Type:
The numeric literal. Read-only
- evaluate(env: collections.abc.Mapping[qilisdk.core.variables.BaseVariable, qilisdk.core.types.Number | list[int]] | None = None) qilisdk.core.types.Number[source]
Numerically evaluate the expression given an assignment of symbols to values.
- free_symbols() set[qilisdk.core.variables.BaseVariable][source]
The set of named leaves (variables/parameters) occurring in the expression.
- property degree: int[source]
Polynomial degree. Raises
NonPolynomialErrorfor non-polynomial expressions.
- derivative(symbol: qilisdk.core.variables.BaseVariable) Expression[source]
Symbolic derivative with respect to
symbol.
- get_constant() qilisdk.core.types.Number[source]
Return the additive constant of the expression.
- Returns:
the constant term (0 unless the expression is a sum or a constant).
- Return type:
Number
- as_coefficients_dict() dict[Expression, qilisdk.core.types.Number][source]
Map each (non-constant) monomial to its numeric coefficient.
- Returns:
a mapping from monomial to coefficient.
- Return type:
dict[Expression, Number]
- monomial_factors() list[tuple[Expression, int]][source]
Decompose a single monomial into its factors.
- Returns:
the
(base, integer_power)factors.- Return type:
list[tuple[Expression, int]]
- class Add(args: tuple[Expression, ...])[source]
Bases:
ExpressionA canonical n-ary sum. Operands are deterministically ordered and combine like terms.
- property args: tuple[Expression, ...][source]
nodes are immutable so the cached hash stays valid.
- Type:
The summands. Read-only
- classmethod build(raw: tuple[Expression, ...]) Expression[source]
- evaluate(env: collections.abc.Mapping[qilisdk.core.variables.BaseVariable, qilisdk.core.types.Number | list[int]] | None = None) qilisdk.core.types.Number[source]
Numerically evaluate the expression given an assignment of symbols to values.
- free_symbols() set[qilisdk.core.variables.BaseVariable][source]
The set of named leaves (variables/parameters) occurring in the expression.
- property degree: int[source]
Polynomial degree. Raises
NonPolynomialErrorfor non-polynomial expressions.
- derivative(symbol: qilisdk.core.variables.BaseVariable) Expression[source]
Symbolic derivative with respect to
symbol.
- expand() Expression[source]
Distribute products over sums.
- Returns:
a canonical sum-of-monomials equal to this expression.
- Return type:
- substitute(mapping: collections.abc.Mapping[Expression, Expression | qilisdk.core.types.Number]) Expression[source]
Structurally replace sub-expressions according to
mapping.- Returns:
the expression with substitutions applied.
- Return type:
- to_binary() Expression[source]
Encode every continuous
Variableinto binary variables (no-op for most nodes).- Returns:
an equivalent expression over binary variables.
- Return type:
- get_constant() qilisdk.core.types.Number[source]
Return the additive constant of the expression.
- Returns:
the constant term (0 unless the expression is a sum or a constant).
- Return type:
Number
- to_list() list[Expression][source]
The node’s operands as a list: the summands of a sum, the factors of a product.
A leaf has no operands, so it yields itself. This is the readable spelling of
.args.- Returns:
the operands of this node.
- Return type:
list[Expression]
- as_coefficients_dict() dict[Expression, qilisdk.core.types.Number][source]
Map each (non-constant) monomial to its numeric coefficient.
- Returns:
a mapping from monomial to coefficient.
- Return type:
dict[Expression, Number]
- class Mul(args: tuple[Expression, ...])[source]
Bases:
ExpressionA canonical n-ary product. Collects like powers and folds the numeric coefficient.
Muldoes not distribute over sums; useexpand()for that.- property args: tuple[Expression, ...][source]
nodes are immutable so the cached hash stays valid.
- Type:
The factors. Read-only
- classmethod build(raw: tuple[Expression, ...]) Expression[source]
- coefficient() qilisdk.core.types.Number[source]
- monomial() Expression[source]
- evaluate(env: collections.abc.Mapping[qilisdk.core.variables.BaseVariable, qilisdk.core.types.Number | list[int]] | None = None) qilisdk.core.types.Number[source]
Numerically evaluate the expression given an assignment of symbols to values.
- free_symbols() set[qilisdk.core.variables.BaseVariable][source]
The set of named leaves (variables/parameters) occurring in the expression.
- property degree: int[source]
Polynomial degree. Raises
NonPolynomialErrorfor non-polynomial expressions.
- derivative(symbol: qilisdk.core.variables.BaseVariable) Expression[source]
Symbolic derivative with respect to
symbol.
- expand() Expression[source]
Distribute products over sums.
- Returns:
a canonical sum-of-monomials equal to this expression.
- Return type:
- substitute(mapping: collections.abc.Mapping[Expression, Expression | qilisdk.core.types.Number]) Expression[source]
Structurally replace sub-expressions according to
mapping.- Returns:
the expression with substitutions applied.
- Return type:
- to_binary() Expression[source]
Encode every continuous
Variableinto binary variables (no-op for most nodes).- Returns:
an equivalent expression over binary variables.
- Return type:
- to_list() list[Expression][source]
The node’s operands as a list: the summands of a sum, the factors of a product.
A leaf has no operands, so it yields itself. This is the readable spelling of
.args.- Returns:
the operands of this node.
- Return type:
list[Expression]
- as_coefficients_dict() dict[Expression, qilisdk.core.types.Number][source]
Map each (non-constant) monomial to its numeric coefficient.
- Returns:
a mapping from monomial to coefficient.
- Return type:
dict[Expression, Number]
- monomial_factors() list[tuple[Expression, int]][source]
Decompose a single monomial into its factors.
- Returns:
the
(base, integer_power)factors.- Return type:
list[tuple[Expression, int]]
- class Pow(base: Expression, exp: Expression)[source]
Bases:
ExpressionA power
base ** exp. The exponent may be a numeric or symbolicExpression.- property base: Expression[source]
nodes are immutable so the cached hash stays valid.
- Type:
The base. Read-only
- property exp: Expression[source]
nodes are immutable so the cached hash stays valid.
- Type:
The exponent. Read-only
- classmethod build(base: Expression, exp: Expression) Expression[source]
- evaluate(env: collections.abc.Mapping[qilisdk.core.variables.BaseVariable, qilisdk.core.types.Number | list[int]] | None = None) qilisdk.core.types.Number[source]
Numerically evaluate the expression given an assignment of symbols to values.
- free_symbols() set[qilisdk.core.variables.BaseVariable][source]
The set of named leaves (variables/parameters) occurring in the expression.
- property degree: int[source]
Polynomial degree. Raises
NonPolynomialErrorfor non-polynomial expressions.
- derivative(symbol: qilisdk.core.variables.BaseVariable) Expression[source]
Symbolic derivative with respect to
symbol.
- expand() Expression[source]
Distribute products over sums.
- Returns:
a canonical sum-of-monomials equal to this expression.
- Return type:
- substitute(mapping: collections.abc.Mapping[Expression, Expression | qilisdk.core.types.Number]) Expression[source]
Structurally replace sub-expressions according to
mapping.- Returns:
the expression with substitutions applied.
- Return type:
- to_binary() Expression[source]
Encode every continuous
Variableinto binary variables (no-op for most nodes).- Returns:
an equivalent expression over binary variables.
- Return type:
- monomial_factors() list[tuple[Expression, int]][source]
Decompose a single monomial into its factors.
- Returns:
the
(base, integer_power)factors.- Return type:
list[tuple[Expression, int]]
- class Function(arg: object)[source]
Bases:
Expression,abc.ABCAbstract base for unary maths functions (
sin,cos,exp, …).A concrete function declares – all at the class level so serialization carries only the operand – a stable
NAME, a numpy numeric kernel_numeric(), and the local outer derivative_outer_derivative()(the chain rule is applied byderivative()).- NAME: ClassVar[str] = ''[source]
- property arg: Expression[source]
nodes are immutable so the cached hash stays valid.
- Type:
The operand. Read-only
- evaluate(env: collections.abc.Mapping[qilisdk.core.variables.BaseVariable, qilisdk.core.types.Number | list[int]] | None = None) qilisdk.core.types.Number[source]
Numerically evaluate the expression given an assignment of symbols to values.
- free_symbols() set[qilisdk.core.variables.BaseVariable][source]
The set of named leaves (variables/parameters) occurring in the expression.
- property degree: int[source]
Polynomial degree. Raises
NonPolynomialErrorfor non-polynomial expressions.
- derivative(symbol: qilisdk.core.variables.BaseVariable) Expression[source]
Symbolic derivative with respect to
symbol.
- expand() Expression[source]
Distribute products over sums.
- Returns:
a canonical sum-of-monomials equal to this expression.
- Return type:
- substitute(mapping: collections.abc.Mapping[Expression, Expression | qilisdk.core.types.Number]) Expression[source]
Structurally replace sub-expressions according to
mapping.- Returns:
the expression with substitutions applied.
- Return type:
- to_binary() Expression[source]
Encode every continuous
Variableinto binary variables (no-op for most nodes).- Returns:
an equivalent expression over binary variables.
- Return type:
- classmethod to_yaml(representer, node)[source]
- classmethod from_yaml(constructor, node)[source]
- class Log(arg: object)[source]
Bases:
FunctionNatural logarithm of an expression.
- NAME = 'log'[source]
- class Abs(arg: object)[source]
Bases:
FunctionAbsolute value of an expression.
Absis not differentiable at zero and there is nosignnode to express its derivative away from zero, soExpression.derivative()raises on it.- NAME = 'abs'[source]
- Inv(arg: object) Expression[source]
Build the reciprocal
1 / arg.This is a thin wrapper over
Powrather than a node of its own, soInv(x)andx ** -1are the same expression.- Returns:
the reciprocal of
arg.- Return type:
- Raises:
TypeError – if
argis neither anExpressionnor a number.