Skip to content

Utils API Reference

quicksum

Efficiently sum an iterable of expressions, variables, and floats.

This function provides an optimized way to sum multiple expressions or variables, which is more efficient than using repeated addition.

Parameters:

  • iterable (Iterable) –

    An iterable containing Expression, Variable, and/or float objects.

  • start (Expression or Variable, default: None ) –

    Optional starting value for the sum.

Returns:

  • Expression

    An expression representing the sum.

Examples:

Sum a list of variables:

>>> from luna_model import Environment, Variable
>>> from luna_model.utils import quicksum
>>> with Environment():
...     vars = [Variable(f"x{i}") for i in range(10)]
>>> expr = quicksum(vars)
>>> print(expr)
x0 + x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9

Sum with coefficients:

>>> coeffs = [1, 2, 3, 4, 5]
>>> terms = [c * v for c, v in zip(coeffs, vars[:5])]
>>> expr = quicksum(terms)
>>> print(expr)
x0 + 2 x1 + 3 x2 + 4 x3 + 5 x4
Notes

This is significantly faster than using sum() or repeated + operations for large numbers of terms.

Timer

Timer for measuring execution time.

Provides start/stop functionality for timing operations.

Examples:

>>> from luna_model.timer import Timer
>>> timer = Timer.start()
>>> # ... perform operations ...
>>> timing = timer.stop()
>>> print(f"Elapsed: {timing.total_seconds} seconds")
Elapsed: ... seconds

start() -> Timer classmethod

Start a new timer.

Returns:

  • Timer

    A running timer instance.

stop() -> Timing

Stop the timer and return timing information.

Returns:

  • Timing

    The timing information for the measured interval.

Timing

Bases: Protocol

Timing information for an operation.

Records the start time, end time, and optional QPU time for an operation.

Attributes:

start: datetime property

Get the start time.

end: datetime property

Get the end time.

total: timedelta property

Get the total elapsed time.

total_seconds: float property

Get the total elapsed time in seconds.

qpu: float | None property writable

Get the QPU time in seconds.

add_qpu(value: float) -> None

Add time to the QPU counter.

__str__() -> str

Return human-readable string representation.

Returns:

  • str

    String representation of the timing.