Environment ¶
Execution context for variable creation and expression scoping.
An Environment provides the symbolic scope in which Variable objects are defined. It is required for variable construction, and ensures consistency across expressions. The environment does not store constraints or expressions β it only facilitates their creation by acting as a context manager and anchor for Variable instances.
Environments are best used with with blocks, but can also be passed manually to models or variables.
Examples:
Create variables inside an environment:
>>> from luna_quantum import Environment, Variable
>>> with Environment() as env:
... x = Variable("x")
... y = Variable("y")
Serialize the environment state:
Notes
- The environment is required to create
Variableinstances. - It does not own constraints or expressions β they merely reference variables tied to an environment.
- Environments cannot be nested. Only one can be active at a time.
- Use
encode()/decode()to persist and recover expression trees.
__init__ ¶
Initialize a new environment for variable construction.
It is recommended to use this in a with statement to ensure proper scoping.
decode builtin ¶
decode(data: bytes) -> Environment
Reconstruct an expression from a previously encoded binary blob.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data | bytes | The binary data returned from | required |
Returns:
| Type | Description |
|---|---|
Expression | The reconstructed symbolic expression. |
Raises:
| Type | Description |
|---|---|
DecodeError | If decoding fails due to corruption or incompatibility. |
deserialize builtin ¶
deserialize(data: bytes) -> Environment
Alias for decode().
See decode() for full usage details.
encode method descriptor ¶
Serialize the environment into a compact binary format.
This is the preferred method for persisting an environment's state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
compress | bool | Whether to compress the binary output. Default is | True |
level | int | Compression level (e.g., from 0 to 9). Default is | 3 |
Returns:
| Type | Description |
|---|---|
bytes | Encoded binary representation of the environment. |
Raises:
| Type | Description |
|---|---|
IOError | If serialization fails. |
equal_contents method descriptor ¶
equal_contents(other: Environment) -> bool
Check whether this environment has equal contents as other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other | Environment | | required |
Returns:
| Type | Description |
|---|---|
bool | |
get_variable method descriptor ¶
get_variable(name: str) -> Variable
Get a variable by its label (name).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label | str | The name/label of the variable | required |
Returns:
| Type | Description |
|---|---|
Variable | The variable with the specified label/name. |
Raises:
| Type | Description |
|---|---|
VariableNotExistingError | If no variable with the specified name is registered. |
serialize method descriptor ¶
Alias for encode().
See encode() for full usage details.