loqs.tools.migrate.labels¶
Rewrite pre-1.2 positional InstructionLabel construction to the
modern keyword form.
Two source shapes are recognized, both matching pre-1.2's (instruction,
patch_label, inst_args, inst_kwargs) convention: a direct
InstructionLabel(...) call with more than one positional argument, or a
bare 3-/4-tuple literal. A direct call is rewritten to a modern call
(InstructionLabel("Increment", increment_by=2, patch_label="L0")); a
bare tuple is rewritten to a bare {"instruction": ..., **kwargs} dict
instead, matching InstructionLabel's own modern raw-dict shape, since
building a call there would introduce a new InstructionLabel reference
into a file that may never have needed to import it before. A 3-tuple
with a string 2nd element is deliberately not treated as a
candidate, even though it matches this shape for a per-patch instruction,
since it collides constantly with an unrelated, far more common pattern
-- a raw pyGSTi circuit-layer gate-label tuple, e.g. ("Gcphase", "A0",
"D4"), has the exact same 3-string-element shape and is expected to
dominate real source files.
instruction and patch_label are carried through verbatim regardless
of their own shape -- modern keyword-form InstructionLabel accepts
either a bare name string or an already-resolved Instruction object
identically, so neither needs to be known statically. inst_args
similarly never needs to be inspected: if it isn't provably empty, it's
carried through verbatim under the reserved
LEGACY_PENDING_INST_ARGS key, the same mechanism
_label_kwargs already uses to remap a pending
positional-args stash once the real instruction is available at run
time -- no static lookup needed either way.
inst_kwargs is spliced in as individual named keyword arguments (e.g.
increment_by=2) whenever it's a literal dict with string-literal keys,
since that's the more readable result; otherwise it's spliced in whole
via **inst_kwargs -- a **-unpack is behaviorally identical to the old
dict regardless of what keys it actually holds. The one real behavior
change from the old dict-based call: if inst_kwargs and an explicit
patch_label/keyword argument disagree on a key, Python raises
TypeError at call time instead of one silently overriding the other --
preferable to guessing which was meant.
One case is flagged rather than rewritten by default: pre-1.2, the
patch_label positional slot and a same-named "patch_label" entry
inside inst_kwargs were unrelated -- the slot picked global-vs-per-patch
resolution, while an inst_kwargs["patch_label"] was just an ordinary
apply_fn kwarg with no reserved meaning. The modern dict form has only
one "patch_label" key, read for both purposes at once (see
QuantumProgram._resolve_instruction/_collect_kwarg), so merging the
two could silently change which instruction gets resolved -- when
inst_kwargs is a literal dict that already has a "patch_label" key,
this is flagged instead of guessed. Passing rename_patch_label renames
that colliding key instead of just flagging it, but a manual-review item
is still emitted either way, since renaming the label's own key alone
does nothing without also renaming the matching apply_fn parameter on
the corresponding Instruction -- a rename this tool can't perform
itself, since it has no visibility into where or how that Instruction
is defined. A non-literal inst_kwargs spliced via ** can't be checked
(or rewritten) the same way, since its keys aren't known statically --
this exact collision can still recur silently at runtime in that case, a
residual limitation this tool can't close without evaluating the
expression.
Functions¶
migrate_instruction_labels ¶
migrate_instruction_labels(source: str, *, rename_patch_label: str | None = None) -> MigrationResult
Detect and rewrite pre-1.2 positional InstructionLabel
construction throughout source (see the module docstring for exactly
which shapes are recognized and when a confident rewrite is possible).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
str
|
The full text of a |
required |
rename_patch_label
|
str | None
|
If given, rewrite a colliding |
None
|