refactor(circuit): Only flatten circuit when a ZX graph is constructed#71
Merged
refactor(circuit): Only flatten circuit when a ZX graph is constructed#71
Conversation
Contributor
☂️ Python Coverage
Overall Coverage
New FilesNo new covered files... Modified Files
|
Contributor
|
There was a problem hiding this comment.
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
REPEATblocks. - 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_annotationsnow iterates overself._stim_circ.flattened(), which expands all REPEAT blocks. That means callingwithout_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 processstim.CircuitRepeatBlockbodies 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This gives better visualization for circuits with repeat blocks and now also correctly applies
SHIFT_COORDinstructions.