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
use super::*;
use crate::cycle::{CycleId, CycleIndex};
use crate::instruction::{Instruction, InstructionId};
/// Access and Iteration
impl QuditCircuit {
/// Try to retrieve an instruction from its id.
pub fn get(&self, inst_id: InstructionId) -> Option<&Instruction> {
self.cycles
.get_from_id(inst_id.cycle())
.and_then(|cycle| cycle.get_from_id(inst_id.inner()))
}
/// Return an iterator over the identifiers of instructions in the circuit.
///
/// The ordering is not guaranteed to be consistent, but it will
/// be in a simulation/topological order.
pub fn id_iter(&self) -> impl Iterator<Item = InstructionId> + '_ {
self.cycles.iter().flat_map(|cycle| {
cycle
.id_iter()
.map(|inner| InstructionId::new(cycle.id, inner))
})
}
/// Return an iterator over the instructions in the circuit.
///
/// The ordering is not guaranteed to be consistent, but it will
/// be in a simulation/topological order. For more control over the
/// ordering of iteration see [QuditCircuit::iter_sorted]
pub fn iter(&self) -> impl Iterator<Item = &Instruction> + '_ {
self.cycles.iter().flat_map(|cycle| cycle.iter())
}
/// Return a sorted iterator over the instructions in the circuit.
///
/// Will always iterate over the instructions in the same order. This
/// iteration is a valid simulation order.
pub fn iter_sorted(&self) -> impl Iterator<Item = &Instruction> + '_ {
self.cycles.iter().flat_map(|cycle| cycle.iter_sorted())
}
/// Convert a cycle ID to a cycle index
#[inline]
pub fn cycle_id_to_index(&self, cycle_id: CycleId) -> Option<CycleIndex> {
self.cycles.id_to_index(cycle_id)
}
/// Convert a cycle index to a cycle ID
#[inline]
pub fn cycle_index_to_id(&self, cycle_index: CycleIndex) -> CycleId {
self.cycles.index_to_id(cycle_index)
}
}