Skip to content

loqs.tools.migrate.reptuple

Rewrite a confidently-resolvable RepTuple(rep, qubits, reptype) call to the modern concrete OperationRep subclass it actually meant, instead of only flagging it (see RENAMES's own RepTuple entry).

reptype is only resolvable when it's a literal GateRep.<NAME> or InstrumentRep.<NAME> attribute reference -- one of the ten pre-refactor enum members loqs/backends/reps/legacy.py's _upgrade_legacy_gaterep/ _upgrade_legacy_instrumentrep already dispatch on for decode, reused here as this pass's own mapping table. GateRep/InstrumentRep are matched by bare name only, not by resolving the actual import (unlike rewrite_renames()'s QualifiedNameProvider-based matching) -- deliberately, since these names refer to the old, now-nonexistent enum in this specific context, not the modern classes of the same name a real import would resolve to. A bare int reptype, or any other non-literal expression, is left unresolved: gate values 1-6 and instrument values 1-4 overlap, so a bare int is genuinely ambiguous without knowing which enum it came from, and guessing wrong would construct the wrong class outright rather than merely fail to rewrite.

For every GateRep target, and one InstrumentRep target (STIM_CIRCUIT_STR), rep passes straight through as that class's own single payload argument. The other three InstrumentRep targets' rep was always a nested tuple that decode destructures positionally (e.g. reset, include_outcome = rep); NewClass(*rep, qubit_labels=qubits) reproduces that destructuring exactly for any expression, not just a literal tuple, since Python's *-unpack enforces the same arity a tuple-assignment would. KrausGateRep specifically skips decode's tp_check_abstol=None (used there to avoid re-validating already-accepted data) -- a live rewrite is fresh construction, not a decode, so it gets the class's own default trace-preservation check like any new KrausGateRep(...) call would.

The old RepTuple import is dropped once every reference to it in the file has been resolved this way (see RemoveImportsVisitor); GateRep/InstrumentRep's own (old-enum-style) import is deliberately left alone, since this pass doesn't know which module a bare GateRep/ InstrumentRep name was actually imported from without resolving it properly, and removing the wrong one would be worse than leaving an unused import behind.

Functions

rewrite_reptuple_construction

rewrite_reptuple_construction(source: str) -> MigrationResult

Rewrite every confidently-resolvable RepTuple(...) call in source to the modern OperationRep subclass it meant (see the module docstring), leaving anything else about RepTuple -- an unresolvable call, or a bare reference with no call at all -- for rewrite_renames()'s own generic deleted-name handling.

Parameters:

Name Type Description Default
source str

The full text of a .py file (or an extracted code cell).

required