Expression ¶
Polynomial expression supporting symbolic arithmetic, constraint creation, and encoding.
An Expression represents a real-valued mathematical function composed of variables,
scalars, and coefficients. Expressions may include constant, linear, quadratic, and
higher-order terms (cubic and beyond). They are used to build objective functions
and constraints in symbolic optimization models.
Expressions support both regular and in-place arithmetic, including addition and
multiplication with integers, floats, Variable instances, and other Expressions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
Environment
|
Environment used to scope the expression when explicitly instantiating it. Typically, expressions are constructed implicitly via arithmetic on variables. |
...
|
Examples:
Constructing expressions from variables:
>>> from luna_quantum import Environment, Variable
>>> with Environment():
... x = Variable("x")
... y = Variable("y")
... expr = 1 + 2 * x + 3 * x * y + x * y * y
Inspecting terms:
>>> expr.get_offset()
1.0
>>> expr.get_linear(x)
2.0
>>> expr.get_quadratic(x, y)
3.0
>>> expr.get_higher_order((x, y, y))
1.0
In-place arithmetic:
Creating constraints:
Serialization:
Supported Arithmetic
The following operations are supported:
-
Addition:
expr + exprβExpressionexpr + variableβExpressionexpr + int | floatβExpressionint | float + exprβExpression
-
In-place addition:
expr += exprexpr += variableexpr += int | float
-
Multiplication:
expr * exprexpr * variableexpr * int | floatint | float * expr
-
In-place multiplication:
expr *= exprexpr *= variableexpr *= int | float
-
Constraint creation:
expr == constantβConstraintexpr <= constantβConstraintexpr >= constantβConstraint
Notes
- Expressions are mutable: in-place operations (
+=,*=) modify the instance. - Expressions are scoped to an environment via the variables they reference.
- Comparisons like
expr == exprreturnbool, not constraints. - Use
==,<=,>=with numeric constants to create constraints.
num_variables
property
¶
num_variables: int
Return the number of distinct variables in the expression.
Returns:
| Type | Description |
|---|---|
int
|
Number of variables with non-zero coefficients. |
__init__ ¶
__init__(env: Environment) -> None
__init__(env: Environment | None = ...) -> None
Create a new empty expression scoped to an environment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
Environment
|
The environment to which this expression is bound. |
...
|
Raises:
| Type | Description |
|---|---|
NoActiveEnvironmentFoundError
|
If no environment is provided and none is active in the context. |
const
staticmethod
¶
const(val: float, env: Environment | None = None) -> Expression
Create constant expression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
val
|
float
|
The constant |
required |
Returns:
| Type | Description |
|---|---|
Expression
|
|
decode
builtin
¶
decode(data: bytes, env: Environment) -> Expression
Reconstruct an expression from encoded bytes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
bytes
|
Binary blob returned by |
required |
Returns:
| Type | Description |
|---|---|
Expression
|
Deserialized expression object. |
Raises:
| Type | Description |
|---|---|
DecodeError
|
If decoding fails due to corruption or incompatibility. |
deep_clone_many
staticmethod
¶
deep_clone_many(py_exprs) -> list[Expression]
Deep clones all provided expressions into new environment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exprs
|
list[Expression]
|
The expressions to move to new_environment |
required |
Returns:
| Type | Description |
|---|---|
list[Expressions]
|
The same expressions but part of a new environment |
deserialize
builtin
¶
deserialize(data: bytes, env: Environment) -> Expression
Alias for decode().
See decode() for full documentation.
encode
method descriptor
¶
Serialize the expression into a compact binary format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
compress
|
bool
|
Whether to compress the data. Default is True. |
True
|
level
|
int
|
Compression level (0β9). Default is 3. |
3
|
Returns:
| Type | Description |
|---|---|
bytes
|
Encoded representation of the expression. |
Raises:
| Type | Description |
|---|---|
IOError
|
If serialization fails. |
equal_contents
method descriptor
¶
equal_contents(other: Expression) -> bool
Check whether this expression has equal contents as other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Expression
|
|
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
get_higher_order
method descriptor
¶
Get the coefficient for a higher-order term (degree β₯ 3).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
variables
|
tuple of Variable
|
A tuple of variables specifying the term. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The coefficient, or 0.0 if not present. |
Raises:
| Type | Description |
|---|---|
VariableOutOfRangeError
|
If any variable is out of bounds for the environment. |
get_linear
method descriptor
¶
Get the coefficient of a linear term for a given variable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
variable
|
Variable
|
The variable whose linear coefficient is being queried. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The coefficient, or 0.0 if the variable is not present. |
Raises:
| Type | Description |
|---|---|
VariableOutOfRangeError
|
If the variable index is not valid in this expression's environment. |
get_offset
method descriptor
¶
get_offset() -> float
Get the constant (offset) term in the expression.
Returns:
| Type | Description |
|---|---|
float
|
The constant term. |
get_quadratic
method descriptor
¶
Get the coefficient for a quadratic term (u * v).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
u
|
Variable
|
|
required |
v
|
Variable
|
|
required |
Returns:
| Type | Description |
|---|---|
float
|
The coefficient, or 0.0 if not present. |
Raises:
| Type | Description |
|---|---|
VariableOutOfRangeError
|
If either variable is out of bounds for the expression's environment. |
has_higher_order
method descriptor
¶
has_higher_order() -> bool
Check if expression has higher-order.
Returns:
| Type | Description |
|---|---|
bool
|
If the expression has higher-order |
higher_order_items
method descriptor
¶
is_equal
method descriptor
¶
is_equal(other: Expression) -> bool
Compare two expressions for equality.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Expression
|
The expression to which |
required |
Returns:
| Type | Description |
|---|---|
bool
|
If the two expressions are equal. |
items
method descriptor
¶
items() -> ExpressionIterator
Iterate over the single components of an expression. An component refers to a single constant, linear, quadratic, or higher-order term of an expression.
Returns:
| Type | Description |
|---|---|
ExpressionIterator
|
The iterator over the expression's components. |
linear_items
method descriptor
¶
quadratic_items
method descriptor
¶
separate
method descriptor
¶
separate(variables: list[Variable]) -> tuple[Expression, Expression]
Separates expression into two expressions based on presence of variables.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
variables
|
list[Variable]
|
The variables of which one must at least be present in a left term. |
required |
Returns:
| Type | Description |
|---|---|
tuple[Expression, Expression]
|
Two expressions, left contains one of the variables right does not, i.e. (contains, does not contain) |
serialize
method descriptor
¶
Alias for encode().
See encode() for full documentation.
substitute
method descriptor
¶
substitute(target: Variable, replacement: Expression | Variable) -> Expression
Substitute every occurrence of a variable in an expression with another expression.
Given an expression self, this method replaces all occurrences of target
with replacement. If the substitution would cross differing environments
(e.g. captures from two different scopes), it returns a DifferentEnvsError.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
VarRef
|
The variable reference to replace. |
required |
replacement
|
Expression | Variable
|
The expression to insert in place of |
required |
Returns:
| Type | Description |
|---|---|
Expression
|
The resulting expression after substitution. |
Raises:
| Type | Description |
|---|---|
DifferentEnvsError
|
If the environments of |