Variable ¶
Represents a symbolic variable within an optimization environment.
A Variable is the fundamental building block of algebraic expressions used in optimization models. Each variable is tied to an Environment which scopes its lifecycle and expression context. Variables can be typed and optionally bounded.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name | str | The name of the variable. | required |
env | Environment | The environment in which this variable is created. If not provided, the current environment from the context manager is used. | ... |
vtype | Vtype | The variable type (e.g., | ... |
bounds | Bounds | Bounds restricting the range of the variable. Only applicable for | ... |
Examples:
>>> from luna_quantum import Variable, Environment, Vtype, Bounds
>>> with Environment():
... x = Variable("x")
... y = Variable("y", vtype=Vtype.Integer, bounds=Bounds(0, 5))
... expr = 2 * x + y - 1
Arithmetic Overloads
Variables support standard arithmetic operations:
- Addition:
x + y,x + 2,2 + x - Subtraction:
x - y,3 - x - Multiplication:
x * y,2 * x,x * 2
All expressions return Expression objects and preserve symbolic structure.
Notes
- A
Variableis bound to a specificEnvironmentinstance. - Variables are immutable; all operations yield new
Expressionobjects. - Variables carry their environment, but the environment does not own the variable.
__init__ ¶
__init__(name: str, *, env: Environment) -> None
__init__(name: str, *, env: Environment, vtype: Vtype) -> None
__init__(name: str, *, vtype: Vtype) -> None
__init__(name: str, *, vtype: Vtype, bounds: Bounds, env: Environment) -> None
__init__(
name: str,
*,
vtype: Vtype | None = ...,
bounds: Bounds | None = ...,
env: Environment | None = ...,
) -> None
Initialize a new Variable.
See class-level docstring for full usage.
Raises:
| Type | Description |
|---|---|
NoActiveEnvironmentFoundError | If no active environment is found and none is explicitly provided. |
VariableExistsError | If a variable with the same name already exists in the environment. |
VariableCreationError | If the variable is tried to be created with incompatible bounds. |