1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use crate::;
/// An item that can appear at the top level of a [`crate::Circuit`].
///
/// A Stim circuit is a flat sequence of items, where each item is either a
/// single gate instruction (like `H 0 1` or `X_ERROR(0.01) 5`) or a
/// `REPEAT` block that loops a sub-circuit a fixed number of times. This
/// enum captures that two-variant structure.
///
/// `CircuitItem` is the Rust equivalent of the Python pattern where
/// iterating a `stim.Circuit` yields either `stim.CircuitInstruction` or
/// `stim.CircuitRepeatBlock` objects. In Rust the distinction is made
/// explicit through this enum rather than dynamic dispatch.
///
/// # When you encounter a `CircuitItem`
///
/// You will typically receive `CircuitItem` values when iterating over a
/// circuit. Match on the two variants to handle instructions and repeat
/// blocks differently, or use the shared `name()` / `tag()` methods on
/// the inner types for duck-typed access.
///
/// # Ordering and hashing
///
/// `CircuitItem` derives [`Eq`], [`Ord`], and [`Hash`] by delegating to
/// the inner types. The `Instruction` variant sorts before `RepeatBlock`
/// (enum variant order).
///
/// # Examples
///
/// ```
/// let instruction = stim::CircuitInstruction::new(
/// stim::Gate::H, [0u32, 1u32], std::iter::empty::<f64>(), "",
/// ).expect("H is a valid gate");
///
/// let item = stim::CircuitItem::Instruction(instruction.clone());
/// assert_eq!(item, stim::CircuitItem::Instruction(instruction));
/// ```
///
/// Matching on variants:
///
/// ```
/// let circuit: stim::Circuit = "H 0\nREPEAT 3 {\n M 0\n}".parse().unwrap();
/// match circuit.get(0).unwrap() {
/// stim::CircuitItem::Instruction(inst) => assert_eq!(inst.name(), "H"),
/// stim::CircuitItem::RepeatBlock(_) => panic!("expected instruction"),
/// }
/// match circuit.get(1).unwrap() {
/// stim::CircuitItem::Instruction(_) => panic!("expected repeat block"),
/// stim::CircuitItem::RepeatBlock(block) => assert_eq!(block.repeat_count(), 3),
/// }
/// ```