Skip to content

Advanced Transformation Topics

Nested pipelines

Use Pipeline to group steps and keep records structured.

Python
from luna_model.transformation import PassManager, Pipeline
from luna_model.transformation.passes import ChangeSensePass, BinarySpinPass
from luna_model import Sense, Vtype

pre = Pipeline(
    [ChangeSensePass(sense=Sense.MIN), BinarySpinPass(vtype=Vtype.SPIN)],
    name="preprocessing",
)

output = PassManager([pre]).run(model)

Nested execution is captured in output.record.

Control-flow branching

Control-flow passes decide at runtime which branch to execute.

Python
from luna_model.transformation import PassManager, Pipeline
from luna_model.transformation.passes import IfElsePass, BinarySpinPass
from luna_model import Vtype

branch = IfElsePass(
    condition=lambda model, ctx: model.num_variables() > 100,
    then=Pipeline([BinarySpinPass(vtype=Vtype.SPIN)], name="then"),
    otherwise=Pipeline([], name="else"),
)

output = PassManager([branch]).run(model)

Backward replay follows the actually recorded branch, not a recomputed branch.

Context-driven orchestration

Use analysis passes to feed typed data into later passes:

Python
from luna_model.transformation import PassManager, analyze

@analyze(name="var-count", provides="decorated_analysis::var-count")
def var_count(model, ctx) -> int:
    return model.num_variables()

output = PassManager([var_count]).run(model)
count = output.context.require_analysis(var_count.key())

Record introspection

Inspect pass history for debugging/auditing:

Python
entry = output.record.find("binary-spin")
print(entry)

for e in output.record.entries:
    print(e)

Serialization-safe replay

Persist records and replay later:

Python
blob = output.record.encode()
record = type(output.record).decode(blob)

# original_solution = record.backward(transformed_solution)

This guarantees deterministic backward behavior after serialize/deserialize.

Legacy API notes

  • Prefer @analyze (not @analyse)
  • Prefer output.record.backward(solution) (not PassManager.backwards(...))
  • Prefer output.context over deprecated cache-style access

See also