Skip to content

Commit

Permalink
Fix stimcirq failing to round trip stim's example surface code circuit (
Browse files Browse the repository at this point in the history
#874)

- Fix MeasureAndOrResetGate not putting its key into the record
  • Loading branch information
Strilanc authored Jan 27, 2025
1 parent 3751bef commit a0b0321
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
14 changes: 13 additions & 1 deletion glue/cirq/stimcirq/_cirq_to_stim_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ def test_on_tagged_loop():
repetitions=3,
).with_tags('my_tag')
)

stim_circuit = stimcirq.cirq_circuit_to_stim_circuit(c)
assert stim.CircuitRepeatBlock in {type(instr) for instr in stim_circuit}

Expand Down Expand Up @@ -410,3 +410,15 @@ def test_custom_tagging():
H[PAIR] 0
TICK
""")


def test_round_trip_example_circuit():
stim_circuit = stim.Circuit.generated(
"surface_code:rotated_memory_x",
distance=3,
rounds=1,
after_clifford_depolarization=0.01,
)
cirq_circuit = stimcirq.stim_circuit_to_cirq_circuit(stim_circuit.flattened())
circuit_back = stimcirq.cirq_circuit_to_stim_circuit(cirq_circuit)
assert len(circuit_back.shortest_graphlike_error()) == 3
6 changes: 4 additions & 2 deletions glue/cirq/stimcirq/_measure_and_or_reset_gate.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Dict, List
from typing import Any, Dict, List, Tuple

import cirq
import stim
Expand Down Expand Up @@ -91,7 +91,9 @@ def _stim_op_name(self) -> str:
result += self.basis
return result

def _stim_conversion_(self, *, edit_circuit: stim.Circuit, targets: List[int], tag: str, **kwargs):
def _stim_conversion_(self, *, edit_circuit: stim.Circuit, targets: List[int], tag: str, edit_measurement_key_lengths: List[Tuple[str, int]], **kwargs):
if self.measure:
edit_measurement_key_lengths.append((self.key, 1))
if self.invert_measure:
targets[0] = stim.target_inv(targets[0])
if self.measure_flip_probability:
Expand Down

0 comments on commit a0b0321

Please sign in to comment.