Skip to main content

deq_runtime/
simulator.rs

1#[cfg(all(feature = "cli", feature = "simulator"))]
2use crate::misc::util::help_message;
3#[cfg(feature = "cli")]
4use clap::ValueEnum;
5use rand::Rng;
6use serde::Serialize;
7#[cfg(feature = "cli")]
8use tokio::sync::oneshot::Sender;
9
10include!("proto/deq.simulator.rs");
11
12#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Debug)]
13#[cfg_attr(feature = "cli", derive(ValueEnum))]
14pub enum SimulatorType {
15    /// do not run simulator
16    None,
17    /// a monolithic simulator that samples from a discrete distribution and run
18    /// the logical error rate and decoding time evaluation
19    Static,
20    /// a simulator that uses the JIT controller to execute programs before sampling
21    JitStatic,
22    /// a simulator that drives TableauSimulator with retry-from-BEGIN semantics
23    Preselect,
24    /// a simulator that draws each shot's measurements from a user-supplied
25    /// Python sampler (e.g. wrapping ``qdk.stim.run``); loss outcomes
26    /// returned as ``-`` are marked for the coordinator's random XOR policy.
27    #[cfg(all(feature = "simulator", feature = "python"))]
28    Python,
29}
30
31pub mod common;
32#[cfg(feature = "simulator")]
33pub mod jit_static_simulator;
34#[cfg(feature = "simulator")]
35pub mod preselect_directives;
36#[cfg(feature = "simulator")]
37pub mod preselect_simulator;
38#[cfg(all(feature = "simulator", feature = "python"))]
39pub mod python_sampler;
40#[cfg(all(feature = "simulator", feature = "python"))]
41pub mod python_simulator;
42#[cfg(feature = "simulator")]
43pub mod rhai_assert;
44#[cfg(feature = "simulator")]
45pub mod static_simulator;
46#[cfg(feature = "simulator")]
47pub mod stim_delays;
48#[cfg(feature = "simulator")]
49pub mod tableau_preselect_sampler;
50#[cfg(feature = "simulator")]
51pub use jit_static_simulator::JitStaticSimulator;
52#[cfg(feature = "simulator")]
53pub use preselect_simulator::PreselectSimulator;
54#[cfg(all(feature = "simulator", feature = "python"))]
55pub use python_simulator::PythonSimulator;
56#[cfg(feature = "simulator")]
57pub use static_simulator::StaticSimulator;
58
59#[allow(dead_code)]
60/// use Xoshiro256StarStar for deterministic random number generator
61pub type DeterministicRng = rand_xoshiro::Xoshiro256StarStar;
62
63pub trait F64Rng {
64    fn next_f64(&mut self) -> f64;
65}
66
67impl F64Rng for DeterministicRng {
68    fn next_f64(&mut self) -> f64 {
69        // 0x3ff: 10 bits among 11 bits exponent
70        // shift right 12 (11 bits exponent + 1 bit sign)
71        f64::from_bits((0x3ff << 52) | (self.next_u64() >> 12)) - 1.0
72    }
73}
74
75pub trait F32Rng {
76    fn next_f32(&mut self) -> f32;
77}
78
79impl F32Rng for DeterministicRng {
80    fn next_f32(&mut self) -> f32 {
81        // 0x7f: 7 bits among 8 bits exponent
82        // shift right 9 (8 bits exponent + 1 bit sign)
83        f32::from_bits((0x7f << 23) | (self.next_u32() >> 9)) - 1.0
84    }
85}
86
87impl SimulatorType {
88    pub fn create(&self, config: serde_json::Value) -> DynSimulator {
89        match self {
90            Self::None => DynSimulator::None,
91            #[cfg(feature = "simulator")]
92            Self::Static => DynSimulator::Static(Box::new(StaticSimulator::new(config))),
93            #[cfg(feature = "simulator")]
94            Self::JitStatic => DynSimulator::JitStatic(Box::new(JitStaticSimulator::new(config))),
95            #[cfg(feature = "simulator")]
96            Self::Preselect => DynSimulator::Preselect(Box::new(PreselectSimulator::new(config))),
97            #[cfg(all(feature = "simulator", feature = "python"))]
98            Self::Python => DynSimulator::Python(Box::new(PythonSimulator::new(config))),
99            #[cfg(not(feature = "simulator"))]
100            Self::Static | Self::JitStatic | Self::Preselect => {
101                let _ = config;
102                panic!("simulator requires the `simulator` feature; rebuild with `--features simulator`")
103            }
104        }
105    }
106
107    #[cfg(feature = "cli")]
108    pub fn config_help() -> String {
109        #[cfg(feature = "simulator")]
110        {
111            #[cfg(feature = "python")]
112            let python_help = help_message::<python_simulator::PythonSimulatorConfig>("PythonSimulatorConfig:");
113            #[cfg(not(feature = "python"))]
114            let python_help = String::new();
115            help_message::<static_simulator::StaticSimulatorConfig>("StaticSimulatorConfig:")
116                + &*help_message::<jit_static_simulator::JitStaticSimulatorConfig>("JitStaticSimulatorConfig:")
117                + &*help_message::<preselect_simulator::PreselectSimulatorConfig>("PreselectSimulatorConfig:")
118                + &*python_help
119        }
120        #[cfg(not(feature = "simulator"))]
121        String::new()
122    }
123
124    #[cfg(not(feature = "cli"))]
125    pub fn config_help() -> String {
126        String::new()
127    }
128}
129
130pub enum DynSimulator {
131    None,
132    #[cfg(feature = "simulator")]
133    Static(Box<StaticSimulator>),
134    #[cfg(feature = "simulator")]
135    JitStatic(Box<JitStaticSimulator>),
136    #[cfg(feature = "simulator")]
137    Preselect(Box<PreselectSimulator>),
138    #[cfg(all(feature = "simulator", feature = "python"))]
139    Python(Box<PythonSimulator>),
140}
141
142impl DynSimulator {
143    #[cfg(feature = "cli")]
144    pub async fn start(self, endpoint: tonic::transport::Endpoint, shutdown_signal: Sender<()>) {
145        match self {
146            DynSimulator::None => {
147                let _ = std::mem::ManuallyDrop::new(shutdown_signal);
148                let _ = endpoint;
149            }
150            #[cfg(feature = "simulator")]
151            DynSimulator::Static(simulator) => {
152                simulator.start(endpoint, shutdown_signal).await;
153            }
154            #[cfg(feature = "simulator")]
155            DynSimulator::JitStatic(simulator) => {
156                simulator.start(endpoint, shutdown_signal).await;
157            }
158            #[cfg(feature = "simulator")]
159            DynSimulator::Preselect(simulator) => {
160                simulator.start(endpoint, shutdown_signal).await;
161            }
162            #[cfg(all(feature = "simulator", feature = "python"))]
163            DynSimulator::Python(simulator) => {
164                simulator.start(endpoint, shutdown_signal).await;
165            }
166        }
167    }
168}