Skip to content

Constraints

A collection of symbolic constraints used to define a model.

The Constraints object serves as a container for individual Constraint instances. It supports adding constraints programmatically and exporting them for serialization.

Constraints are typically added using add_constraint() or the += operator.

Examples:

>>> from luna_quantum import Constraints, Constraint, Environment, Variable
>>> with Environment():
...     x = Variable("x")
...     c = Constraint(x + 1, 0.0, Comparator.Le)
>>> cs = Constraints()
>>> cs.add_constraint(c)
>>> cs += x >= 1.0

Serialization:

>>> blob = cs.encode()
>>> expr = Constraints.decode(blob)
Notes
  • This class does not check feasibility or enforce satisfaction.
  • Use encode()/decode() to serialize constraints alongside expressions.

__init__

__init__() -> None

add_constraint

add_constraint(constraint: Constraint)
add_constraint(constraint: Constraint, name: str)
add_constraint(constraint: Constraint, name: str | None = ...)

Add a constraint to the collection.

Parameters:

Name Type Description Default
constraint Constraint

The constraint to be added.

required
name str

The name of the constraint to be added.

...

decode classmethod

decode(data: bytes, env: Environment) -> Expression

Deserialize an expression from binary constraint data.

Parameters:

Name Type Description Default
data bytes

Encoded blob from encode().

required

Returns:

Type Description
Expression

Expression reconstructed from the constraint context.

Raises:

Type Description
DecodeError

If decoding fails due to corruption or incompatibility.

deserialize classmethod

deserialize(data: bytes, env: Environment) -> Expression

Alias for decode().

See decode() for usage.

encode

encode() -> bytes
encode(*, compress: bool) -> bytes
encode(*, level: int) -> bytes
encode(compress: bool, level: int) -> bytes
encode(compress: bool | None = ..., level: int | None = ...) -> bytes

Serialize the constraint collection to a binary blob.

Parameters:

Name Type Description Default
compress bool

Whether to compress the result. Default is True.

...
level int

Compression level (0–9). Default is 3.

...

Returns:

Type Description
bytes

Encoded representation of the constraints.

Raises:

Type Description
IOError

If serialization fails.

serialize

serialize() -> bytes
serialize(*, compress: bool) -> bytes
serialize(*, level: int) -> bytes
serialize(compress: bool, level: int) -> bytes
serialize(compress: bool | None = ..., level: int | None = ...) -> bytes

Alias for encode().

See encode() for details.