Skip to content

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():

  1. reptuple: confidently-resolvable RepTuple(...) construction calls, rewritten to the concrete OperationRep subclass they actually meant.
  2. renames: straight (module, name) renames, always confidently rewritable -- including anything reptuple couldn't resolve, still bare RepTuple references at this point.
  3. labels: pre-1.2 positional InstructionLabel construction, rewritten to modern keyword form.
  4. 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

  1. reptuple: confidently-resolvable RepTuple(...) construction calls, rewritten to the concrete OperationRep subclass they actually meant.
  2. renames: straight (module, name) renames, always confidently rewritable -- including anything reptuple couldn't resolve, still bare RepTuple references at this point.
  3. labels: pre-1.2 positional InstructionLabel construction, rewritten to modern keyword form.
  4. 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 .py file (or an extracted MyST code cell -- see notebook).

required
rename_iz bool

If True, confidently rewrite every bare "Iz"/'Iz' string literal to "Imrz"/'Imrz' (see rewrite_iz_literal()) instead of only flagging it. Off by default, since a blind string rewrite can't rule out an unrelated match.

False
rename_patch_label str | None

If given, rewrite a legacy InstructionLabel's colliding inst_kwargs["patch_label"] key (see labels) to this name instead of only flagging it. Still flagged either way, as a reminder that the corresponding Instruction's apply_fn parameter needs the same rename by hand.

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.