Skip to content

Copy command domains in Pattern.copy - #658

Open
KainanP wants to merge 1 commit into
TeamGraphix:masterfrom
KainanP:fix-pattern-copy-domains
Open

KainanP wants to merge 1 commit into
TeamGraphix:masterfrom
KainanP:fix-pattern-copy-domains

Conversation

@KainanP

@KainanP KainanP commented Nov 29, 2026 •

Copy link
Copy Markdown

Context (if applicable):

Pattern.copy built the new sequence with copy.copy(cmd), so the s_domain/t_domain/domain sets were shared between a pattern and its copy. shift_signals(method="mc") updates those sets in place (_commute_xs, _commute_zs, _commute_ms, _commute_ss), so shifting the signals of a copy also rewrote the original, which then no longer implemented the circuit (lowest fidelity 0.078 on the H·H·H example from the issue).

Description of the change:

  • Pattern.copy now copies each command with a small helper _copy_command, which makes a shallow copy and then replaces every set-valued attribute with a fresh set. This keeps copy.copy semantics for the other fields (measurements, parameters, custom BaseN/BaseM subclasses) and makes a copy independent of any in-place domain update, not only the four _commute_* functions.
  • New test test_shift_signals_mc_on_copy_keeps_original: transpiles H·H·H, shifts the signals of a copy with "mc", checks that the original command list is unchanged and that both patterns reproduce the circuit on every measurement branch (FixedBranchSelector). It fails on master and passes with the change.
  • CHANGELOG entry under [Unreleased] / Fixed.

Checks run locally (WSL, uv sync --extra dev): full pytest tests (6840 passed, 562 skipped, 26 xfailed), ruff check, ruff format --check, mypy and pyright on the touched files.

Related issue:

Fixes #653

AI use: the fix, test and description were written with Claude Code (Claude Opus 5.5); Claude Code also ran the failing/passing test and the local runs.

🤖 Generated with Claude Code

Pattern.copy made shallow copies of the commands, so the s_domain,
t_domain and domain sets were shared with the original pattern.
shift_signals(method="mc") updates these sets in place, so shifting the
signals of a copy also rewrote the original, which then no longer
implemented the same map. Copy the set-valued fields of each command.

Fixes TeamGraphix#653.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: shift_signals(method="mc") on a copy of a pattern changes the original pattern

1 participant