Skip to content

Constraint

A symbolic constraint formed by comparing an expression to a constant.

A Constraint captures a relation of the form: expression comparator constant, where the comparator is one of: ==, <=, or >=.

While constraints are usually created by comparing an Expression to a scalar (e.g., expr == 3.0), they can also be constructed manually using this class.

Parameters:

Name Type Description Default
lhs Expression

The left-hand side expression.

required
rhs float

The scalar right-hand side value.

required
comparator Comparator

The relation between lhs and rhs (e.g., Comparator.Eq).

required

Examples:

>>> from luna_quantum import Environment, Variable, Constraint, Comparator
>>> with Environment():
...     x = Variable("x")
...     c = Constraint(x + 2, 5.0, Comparator.Eq)

Or create via comparison:

>>> expr = 2 * x + 1
>>> c2 = expr <= 10.0

comparator property

comparator: Comparator

Get the comparator of the constraint.

Returns:

Type Description
Comparator

The comparator of the constraint.

lhs property

Get the left-hand side of the constraint.

Returns:

Type Description
Expression

The left-hand side expression.

name property

name: str | None

Get the name of the constraint.

Returns:

Type Description
(str, optional)

Returns the name of the constraint as a string or None if it is unnamed.

rhs property

rhs: float

Get the right-hand side of the constraint.

Returns:

Type Description
float

The right-hand side expression.

__init__

__init__(lhs: Expression, rhs: Expression, comparator: Comparator) -> None
__init__(lhs: Expression, rhs: Variable, comparator: Comparator) -> None
__init__(lhs: Expression, rhs: int, comparator: Comparator) -> None
__init__(lhs: Expression, rhs: float, comparator: Comparator) -> None
__init__(
    lhs: Expression, rhs: Expression, comparator: Comparator, name: str
) -> None
__init__(
    lhs: Expression, rhs: Variable, comparator: Comparator, name: str
) -> None
__init__(lhs: Expression, rhs: int, comparator: Comparator, name: str) -> None
__init__(
    lhs: Expression, rhs: float, comparator: Comparator, name: str
) -> None
__init__(lhs: Variable, rhs: Expression, comparator: Comparator) -> None
__init__(lhs: Variable, rhs: Variable, comparator: Comparator) -> None
__init__(lhs: Variable, rhs: int, comparator: Comparator) -> None
__init__(lhs: Variable, rhs: float, comparator: Comparator) -> None
__init__(
    lhs: Variable, rhs: Expression, comparator: Comparator, name: str
) -> None
__init__(
    lhs: Variable, rhs: Variable, comparator: Comparator, name: str
) -> None
__init__(lhs: Variable, rhs: int, comparator: Comparator, name: str) -> None
__init__(lhs: Variable, rhs: float, comparator: Comparator, name: str) -> None
__init__(
    lhs: Variable | Expression,
    rhs: int | float | Expression | Variable,
    comparator: Comparator,
    name: str,
) -> None

Construct a new symbolic constraint.

Parameters:

Name Type Description Default
lhs Expression | Variable

Left-hand side symbolic expression or variable.

required
rhs int | float | Expression | Variable

Scalar right-hand side constant.

required
comparator Comparator

Relational operator (e.g., Comparator.Eq, Comparator.Le).

required
name str

The name of the constraint

required

Raises:

Type Description
TypeError

If lhs is not an Expression or rhs is not a scalar float.

IllegalConstraintNameError

If the constraint is tried to be created with an illegal name.