loqs.tools.migrate¶
Rewrite .py/.ipynb/MyST Markdown source files still using pre-1.2
LoQS APIs.
loqs.tools.migrate and the loqs-migrate console script (see
cli) address a different problem than
Serializable's own decode-time compatibility machinery: that
machinery keeps already-serialized data readable; this tool rewrites a
user's own source code -- experiment scripts, a Jupyter notebook, or a
custom apply_fn/map_qubits_fn that will be frozen into future
serialized data -- so it stops relying on removed/renamed APIs going
forward.
Four independent passes, run in order, together making up migrate_source():
reptuple: confidently-resolvableRepTuple(...)construction calls, rewritten to the concreteOperationRepsubclass they actually meant.renames: straight(module, name)renames, always confidently rewritable -- including anythingreptuplecouldn't resolve, still bareRepTuplereferences at this point.labels: pre-1.2 positionalInstructionLabelconstruction, rewritten to modern keyword form.flags: patterns whose replacement is a semantic change or ambiguous.InstructionLabel.cast(...)/SyndromeLabel.cast(...)are confidently rewritten to.from_raw(...). Other castable classes'.cast()calls,include_idles=, and bare"Iz"strings are flagged by default, never guessed at automatically.
Two of the above are opt-in exceptions to "never guessed at automatically",
each gated behind its own migrate_source() keyword argument (and the
CLI's matching --rename-Iz/--rename-patch-label flags): rewriting a
bare "Iz" string to "Imrz" (pass 4), and renaming a colliding
inst_kwargs["patch_label"] key (pass 3) -- both still risk being wrong
in a way this tool can't verify itself, which is why they default to off.
A pass's own manual-review and rewrite items are always relative to
its own input line numbering; since an earlier pass may itself change
the surrounding line count (e.g. collapsing a multi-line call onto one
line), those items are remapped forward through each later pass via
remap_manual_review()/remap_rewrites() before being combined,
so every reported line in the final result is accurate relative to the
file actually written.
Functions¶
reptuple: confidently-resolvableRepTuple(...)construction calls, rewritten to the concreteOperationRepsubclass they actually meant.renames: straight(module, name)renames, always confidently rewritable -- including anythingreptuplecouldn't resolve, still bareRepTuplereferences at this point.labels: pre-1.2 positionalInstructionLabelconstruction, rewritten to modern keyword form.flags: patterns whose replacement is a semantic change or ambiguous.InstructionLabel.cast(...)/SyndromeLabel.cast(...)are confidently rewritten to.from_raw(...). Other castable classes'.cast()calls,include_idles=, and bare"Iz"strings are flagged by default, never guessed at automatically.
Two of the above are opt-in exceptions to "never guessed at automatically",
each gated behind its own migrate_source() keyword argument (and the
CLI's matching --rename-Iz/--rename-patch-label flags): rewriting a
bare "Iz" string to "Imrz" (pass 4), and renaming a colliding
inst_kwargs["patch_label"] key (pass 3) -- both still risk being wrong
in a way this tool can't verify itself, which is why they default to off.
A pass's own manual-review and rewrite items are always relative to
its own input line numbering; since an earlier pass may itself change
the surrounding line count (e.g. collapsing a multi-line call onto one
line), those items are remapped forward through each later pass via
remap_manual_review()/remap_rewrites() before being combined,
so every reported line in the final result is accurate relative to the
file actually written.
migrate_source ¶
migrate_source(source: str, *, rename_iz: bool = False, rename_patch_label: str | None = None) -> MigrationResult
Run every migration pass over source, returning the combined result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
str
|
The full text of a |
required |
rename_iz
|
bool
|
If |
False
|
rename_patch_label
|
str | None
|
If given, rewrite a legacy |
None
|
Returns:
| Type | Description |
|---|---|
A [](api:MigrationResult): `.source` is safe to write back to disk
|
|
even when `.manual_review` is non-empty -- only confidently-resolvable
|
|
rewrites are ever applied, so nothing already flagged was also
|
|
(possibly wrongly) rewritten. Whenever `.changed` is true, `.source`
|
|
also has a short explanatory comment inserted above each remaining
|
|
`.manual_review` line (see [](api:annotate_manual_review)), so the
|
|
file itself still documents what needs a look even without this
|
|
call's own return value kept around.
|
|