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:
Timing
Bases: Protocol
Timing information for an operation.
Records the start time, end time, and optional QPU time for an operation.
Attributes:
-
start(datetime) –When the operation started.
-
end(datetime) –When the operation ended.
-
total(timedelta) –Total elapsed time.
-
total_seconds(float) –Total elapsed time in seconds.
-
qpu(float or None) –QPU time in seconds, if applicable.
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.