Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/tsim/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,11 @@ def strip(circuit: stim.Circuit) -> stim.Circuit:
for instr in circuit:
if isinstance(instr, stim.CircuitRepeatBlock):
stripped = strip(instr.body_copy())
result.append(stim.CircuitRepeatBlock(instr.repeat_count, stripped))
result.append(
stim.CircuitRepeatBlock(
instr.repeat_count, stripped, tag=instr.tag
)
)
continue
if instr.name in ["OBSERVABLE_INCLUDE", "DETECTOR"]:
continue
Expand Down Expand Up @@ -881,7 +885,11 @@ def fix_tags(circuit: stim.Circuit) -> stim.Circuit:
for instr in circuit:
if isinstance(instr, stim.CircuitRepeatBlock):
fixed = fix_tags(instr.body_copy())
result.append(stim.CircuitRepeatBlock(instr.repeat_count, fixed))
result.append(
stim.CircuitRepeatBlock(
instr.repeat_count, fixed, tag=instr.tag
)
)
continue

name = instr.name
Expand Down
4 changes: 3 additions & 1 deletion src/tsim/utils/clifford.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,9 @@ def expand_clifford_rotations(source: stim.Circuit) -> stim.Circuit:
if isinstance(instr, stim.CircuitRepeatBlock):
out.append(
stim.CircuitRepeatBlock(
instr.repeat_count, expand_clifford_rotations(instr.body_copy())
instr.repeat_count,
expand_clifford_rotations(instr.body_copy()),
tag=instr.tag,
)
)
continue
Expand Down
4 changes: 3 additions & 1 deletion src/tsim/utils/diagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,9 @@ def _replace_tagged_gates(
if isinstance(instr, stim.CircuitRepeatBlock):
modified_body = _replace_tagged_gates(instr.body_copy(), replace_dict)
modified_circ.append(
stim.CircuitRepeatBlock(instr.repeat_count, modified_body)
stim.CircuitRepeatBlock(
instr.repeat_count, modified_body, tag=instr.tag
)
)
continue

Expand Down
14 changes: 14 additions & 0 deletions test/unit/test_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,20 @@ def test_stim_circuit_repeat_block_preserves_non_clifford():
assert block.repeat_count == 2


def test_stim_circuit_preserves_repeat_block_tags():
"""Tags on REPEAT blocks survive the round-trip through stim_circuit."""
c = Circuit(
"REPEAT[outer] 3 {\n H 0\n REPEAT[inner] 2 {\n CX 0 1\n }\n}"
)
stim_c = c.stim_circuit
outer = stim_c[0]
assert isinstance(outer, stim.CircuitRepeatBlock)
assert outer.tag == "outer"
inner = outer.body_copy()[-1]
assert isinstance(inner, stim.CircuitRepeatBlock)
assert inner.tag == "inner"


def test_get_graph():
"""Test get_graph returns a ZX graph."""
c = Circuit("H 0\nCNOT 0 1")
Expand Down