Skip to content

refactor(circuit): Only flatten circuit when a ZX graph is constructed#71

Merged
rafaelha merged 3 commits intomainfrom
rafaelha/flatten_circuits
Mar 13, 2026
Merged

refactor(circuit): Only flatten circuit when a ZX graph is constructed#71
rafaelha merged 3 commits intomainfrom
rafaelha/flatten_circuits

Conversation

@rafaelha
Copy link
Collaborator

This gives better visualization for circuits with repeat blocks and now also correctly applies SHIFT_COORD instructions.

@github-actions
Copy link
Contributor

github-actions bot commented Mar 12, 2026

☂️ Python Coverage

current status: ✅

Overall Coverage

Lines Covered Coverage Threshold Status
1965 1910 97% 0% 🟢

New Files

No new covered files...

Modified Files

File Coverage Status
src/tsim/circuit.py 100% 🟢
src/tsim/utils/diagram.py 94% 🟢
TOTAL 97% 🟢

updated for commit: 42a58fd by action🐍

@github-actions
Copy link
Contributor

github-actions bot commented Mar 12, 2026

PR Preview Action v1.8.1

QR code for preview link

🚀 View preview at
https://queracomputing.github.io/tsim/pr-preview/pr-71/

Built to branch gh-pages at 2026-03-13 17:35 UTC.
Preview will be ready when the GitHub Pages deployment is complete.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors Circuit to preserve REPEAT blocks by default (improving visualization and keeping loop structure intact), while still flattening at ZX-graph construction time where needed.

Changes:

  • Stop auto-flattening circuits on construction/append/multiply; add an explicit Circuit.flattened() helper.
  • Update diagram placeholder replacement to work with circuits containing REPEAT blocks.
  • Add/adjust unit tests to validate repeat-block behavior across diagrams, parsing, and circuit operations.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/tsim/circuit.py Preserves repeat blocks in core circuit wrapper; introduces flattened() and adjusts methods affected by non-flattened circuits.
src/tsim/utils/diagram.py Refactors tagged-gate replacement to recurse into repeat blocks instead of asserting none exist.
src/tsim/utils/encoder.py Ensures encoder logic flattens its input circuit explicitly (since it assumes no repeat blocks).
test/unit/test_circuit.py Updates expectations and adds coverage for preserving repeat blocks across operations/indexing/copying.
test/unit/core/test_parse.py Adds parsing tests ensuring repeat blocks are flattened during ZX parse.
test/unit/utils/test_diagram.py Adds diagram test ensuring repeat blocks are visualized (not flattened away).
Comments suppressed due to low confidence (1)

src/tsim/circuit.py:405

  • without_annotations now iterates over self._stim_circ.flattened(), which expands all REPEAT blocks. That means calling without_annotations() will unexpectedly destroy loop structure (and can blow up circuit size) even though the method name suggests only removing annotations. Consider filtering annotations while preserving repeat blocks (e.g., recursively process stim.CircuitRepeatBlock bodies and re-wrap them).
        circ = stim.Circuit()
        for instr in self._stim_circ.flattened():
            assert not isinstance(instr, stim.CircuitRepeatBlock)
            if instr.name in ["OBSERVABLE_INCLUDE", "DETECTOR"]:
                continue
            circ.append(instr)
        return Circuit.from_stim_program(circ)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

@rafaelha rafaelha merged commit abb0461 into main Mar 13, 2026
10 checks passed
@rafaelha rafaelha deleted the rafaelha/flatten_circuits branch March 13, 2026 17:34
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.

2 participants