quantrs2-core 0.1.3

Core types and traits for the QuantRS2 quantum computing framework
Documentation
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//! Quantum Computing Profiling Integration with SciRS2 Beta.1
//!
//! This module provides comprehensive profiling capabilities for quantum
//! computations using the advanced profiling features in scirs2-core beta.1.

use crate::error::QuantRS2Result;
use scirs2_core::profiling::{MemoryTracker, Profiler, Timer};
use std::collections::HashMap;
use std::sync::{Arc, Mutex, OnceLock};
use std::time::{Duration, Instant};

use std::fmt::Write;
/// Quantum operation profiling data
#[derive(Debug, Clone, serde::Serialize)]
pub struct QuantumOperationProfile {
    pub operation_name: String,
    pub execution_count: u64,
    pub total_time: Duration,
    pub average_time: Duration,
    pub min_time: Duration,
    pub max_time: Duration,
    pub memory_usage: u64,
    pub gate_count: u64,
}

/// Comprehensive quantum profiler
pub struct QuantumProfiler {
    /// Operation profiles
    profiles: Arc<Mutex<HashMap<String, QuantumOperationProfile>>>,
    /// Active timers
    active_timers: Arc<Mutex<HashMap<String, Instant>>>,
    /// Global profiling enabled flag
    enabled: Arc<Mutex<bool>>,
    /// Memory tracking
    memory_tracker: Arc<Mutex<Option<MemoryTracker>>>,
}

impl Default for QuantumProfiler {
    fn default() -> Self {
        Self::new()
    }
}

impl QuantumProfiler {
    /// Create a new quantum profiler
    pub fn new() -> Self {
        Self {
            profiles: Arc::new(Mutex::new(HashMap::new())),
            active_timers: Arc::new(Mutex::new(HashMap::new())),
            enabled: Arc::new(Mutex::new(false)),
            memory_tracker: Arc::new(Mutex::new(None)),
        }
    }

    /// Enable profiling
    pub fn enable(&self) {
        *self.enabled.lock().expect("Profiler enabled lock poisoned") = true;

        // Start global profiler
        if let Ok(mut profiler) = Profiler::global().lock() {
            profiler.start();
        }
    }

    /// Disable profiling
    pub fn disable(&self) {
        *self.enabled.lock().expect("Profiler enabled lock poisoned") = false;

        // Stop global profiler
        if let Ok(mut profiler) = Profiler::global().lock() {
            profiler.stop();
        }
    }

    /// Check if profiling is enabled
    pub fn is_enabled(&self) -> bool {
        *self.enabled.lock().expect("Profiler enabled lock poisoned")
    }

    /// Start profiling a quantum operation
    pub fn start_operation(&self, operation_name: &str) {
        if !self.is_enabled() {
            return;
        }

        let mut timers = self
            .active_timers
            .lock()
            .expect("Active timers lock poisoned");
        timers.insert(operation_name.to_string(), Instant::now());

        // Start memory tracking
        let mut tracker = self
            .memory_tracker
            .lock()
            .expect("Memory tracker lock poisoned");
        *tracker = Some(MemoryTracker::start(operation_name));
    }

    /// End profiling a quantum operation
    pub fn end_operation(&self, operation_name: &str, gate_count: u64) {
        if !self.is_enabled() {
            return;
        }

        let start_time = {
            let mut timers = self
                .active_timers
                .lock()
                .expect("Active timers lock poisoned");
            timers.remove(operation_name)
        };

        if let Some(start) = start_time {
            let execution_time = start.elapsed();

            // Stop memory tracking
            let memory_usage = {
                let mut tracker = self
                    .memory_tracker
                    .lock()
                    .expect("Memory tracker lock poisoned");
                if let Some(mem_tracker) = tracker.take() {
                    mem_tracker.stop();
                    // In a real implementation, this would return actual memory usage
                    0 // Placeholder
                } else {
                    0
                }
            };

            // Update profile
            let mut profiles = self.profiles.lock().expect("Profiles lock poisoned");
            let profile = profiles
                .entry(operation_name.to_string())
                .or_insert_with(|| QuantumOperationProfile {
                    operation_name: operation_name.to_string(),
                    execution_count: 0,
                    total_time: Duration::ZERO,
                    average_time: Duration::ZERO,
                    min_time: Duration::MAX,
                    max_time: Duration::ZERO,
                    memory_usage: 0,
                    gate_count: 0,
                });

            profile.execution_count += 1;
            profile.total_time += execution_time;
            profile.average_time = profile.total_time / profile.execution_count as u32;
            profile.min_time = profile.min_time.min(execution_time);
            profile.max_time = profile.max_time.max(execution_time);
            profile.memory_usage += memory_usage;
            profile.gate_count += gate_count;
        }
    }

    /// Profile a quantum operation with automatic timing
    pub fn profile_operation<F, R>(&self, operation_name: &str, gate_count: u64, operation: F) -> R
    where
        F: FnOnce() -> R,
    {
        if !self.is_enabled() {
            return operation();
        }

        self.start_operation(operation_name);
        let result = operation();
        self.end_operation(operation_name, gate_count);
        result
    }

    /// Get profiling results for all operations
    pub fn get_profiles(&self) -> HashMap<String, QuantumOperationProfile> {
        self.profiles
            .lock()
            .expect("Profiles lock poisoned")
            .clone()
    }

    /// Get profiling results for a specific operation
    pub fn get_operation_profile(&self, operation_name: &str) -> Option<QuantumOperationProfile> {
        self.profiles
            .lock()
            .expect("Profiles lock poisoned")
            .get(operation_name)
            .cloned()
    }

    /// Generate a comprehensive profiling report
    pub fn generate_report(&self) -> String {
        let profiles = self.get_profiles();
        let mut report = String::new();

        report.push_str("=== QuantRS2 Performance Profiling Report ===\n\n");

        if profiles.is_empty() {
            report.push_str("No profiling data available.\n");
            return report;
        }

        // Sort by total execution time
        let mut sorted_profiles: Vec<_> = profiles.values().collect();
        sorted_profiles.sort_by_key(|b| std::cmp::Reverse(b.total_time));

        writeln!(
            report,
            "{:<30} {:<10} {:<12} {:<12} {:<12} {:<12} {:<10}",
            "Operation", "Count", "Total (ms)", "Avg (ms)", "Min (ms)", "Max (ms)", "Gates"
        )
        .expect("Writing to String cannot fail");
        report.push_str(&"-".repeat(110));
        report.push('\n');

        for profile in &sorted_profiles {
            writeln!(
                report,
                "{:<30} {:<10} {:<12.3} {:<12.3} {:<12.3} {:<12.3} {:<10}",
                profile.operation_name,
                profile.execution_count,
                profile.total_time.as_secs_f64() * 1000.0,
                profile.average_time.as_secs_f64() * 1000.0,
                profile.min_time.as_secs_f64() * 1000.0,
                profile.max_time.as_secs_f64() * 1000.0,
                profile.gate_count,
            )
            .expect("Writing to String cannot fail");
        }

        report.push_str("\n=== Performance Insights ===\n");

        // Find the most time-consuming operation
        if let Some(slowest) = sorted_profiles.first() {
            writeln!(
                report,
                "Most time-consuming operation: {} ({:.3}ms total)",
                slowest.operation_name,
                slowest.total_time.as_secs_f64() * 1000.0
            )
            .expect("Writing to String cannot fail");
        }

        // Find the most frequent operation
        let most_frequent = sorted_profiles.iter().max_by_key(|p| p.execution_count);
        if let Some(frequent) = most_frequent {
            writeln!(
                report,
                "Most frequent operation: {} ({} executions)",
                frequent.operation_name, frequent.execution_count
            )
            .expect("Writing to String cannot fail");
        }

        // Calculate total gate throughput
        let total_gates: u64 = profiles.values().map(|p| p.gate_count).sum();
        let total_time: Duration = profiles.values().map(|p| p.total_time).sum();
        if total_time.as_secs_f64() > 0.0 {
            let gate_throughput = total_gates as f64 / total_time.as_secs_f64();
            writeln!(
                report,
                "Total gate throughput: {gate_throughput:.0} gates/second"
            )
            .expect("Writing to String cannot fail");
        }

        report
    }

    /// Clear all profiling data
    pub fn clear(&self) {
        self.profiles
            .lock()
            .expect("Profiles lock poisoned")
            .clear();
        self.active_timers
            .lock()
            .expect("Active timers lock poisoned")
            .clear();
    }

    /// Export profiling data to JSON
    pub fn export_json(&self) -> QuantRS2Result<String> {
        let profiles = self.get_profiles();
        serde_json::to_string_pretty(&profiles).map_err(|e| e.into()) // Use the existing From<serde_json::Error> implementation
    }
}

/// Global quantum profiler instance
static GLOBAL_QUANTUM_PROFILER: OnceLock<QuantumProfiler> = OnceLock::new();

/// Get the global quantum profiler
pub fn global_quantum_profiler() -> &'static QuantumProfiler {
    GLOBAL_QUANTUM_PROFILER.get_or_init(QuantumProfiler::new)
}

/// Enable quantum profiling globally
pub fn enable_quantum_profiling() {
    global_quantum_profiler().enable();
}

/// Disable quantum profiling globally
pub fn disable_quantum_profiling() {
    global_quantum_profiler().disable();
}

/// Check if quantum profiling is active
pub fn is_profiling_active() -> bool {
    global_quantum_profiler().is_enabled()
}

/// Macro for easy profiling of quantum operations
#[macro_export]
macro_rules! profile_quantum_operation {
    ($operation_name:expr, $gate_count:expr, $operation:expr) => {{
        $crate::optimizations::profiling_integration::global_quantum_profiler().profile_operation(
            $operation_name,
            $gate_count,
            || $operation,
        )
    }};
}

/// Macro for easy profiling with automatic gate counting
#[macro_export]
macro_rules! profile_gate_operation {
    ($gate_name:expr, $operation:expr) => {{
        $crate::optimizations::profiling_integration::global_quantum_profiler().profile_operation(
            $gate_name,
            1,
            || $operation,
        )
    }};
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::thread;

    #[test]
    fn test_basic_profiling() {
        let profiler = QuantumProfiler::new();
        profiler.enable();

        // Profile a simple operation
        let result = profiler.profile_operation("test_gate", 1, || {
            thread::sleep(Duration::from_millis(10));
            42
        });

        assert_eq!(result, 42);

        let profiles = profiler.get_profiles();
        assert!(profiles.contains_key("test_gate"));

        let test_profile = &profiles["test_gate"];
        assert_eq!(test_profile.execution_count, 1);
        assert_eq!(test_profile.gate_count, 1);
        assert!(test_profile.total_time >= Duration::from_millis(10));
    }

    #[test]
    fn test_multiple_operations() {
        let profiler = QuantumProfiler::new();
        profiler.enable();

        // Profile multiple operations
        for i in 0..5 {
            profiler.profile_operation("hadamard", 1, || {
                thread::sleep(Duration::from_millis(1));
            });
        }

        for i in 0..3 {
            profiler.profile_operation("cnot", 2, || {
                thread::sleep(Duration::from_millis(2));
            });
        }

        let profiles = profiler.get_profiles();

        let hadamard_profile = &profiles["hadamard"];
        assert_eq!(hadamard_profile.execution_count, 5);
        assert_eq!(hadamard_profile.gate_count, 5);

        let cnot_profile = &profiles["cnot"];
        assert_eq!(cnot_profile.execution_count, 3);
        assert_eq!(cnot_profile.gate_count, 6); // 2 gates * 3 executions
    }

    #[test]
    fn test_profiling_disabled() {
        let profiler = QuantumProfiler::new();
        // Don't enable profiling

        let result = profiler.profile_operation("test_gate", 1, || 42);

        assert_eq!(result, 42);

        let profiles = profiler.get_profiles();
        assert!(profiles.is_empty()); // No profiling data should be collected
    }

    #[test]
    fn test_report_generation() {
        let profiler = QuantumProfiler::new();
        profiler.enable();

        profiler.profile_operation("fast_gate", 1, || {
            thread::sleep(Duration::from_millis(1));
        });

        profiler.profile_operation("slow_gate", 1, || {
            thread::sleep(Duration::from_millis(10));
        });

        let report = profiler.generate_report();
        assert!(report.contains("QuantRS2 Performance Profiling Report"));
        assert!(report.contains("fast_gate"));
        assert!(report.contains("slow_gate"));
        assert!(report.contains("Performance Insights"));
    }

    #[test]
    fn test_json_export() {
        let profiler = QuantumProfiler::new();
        profiler.enable();

        profiler.profile_operation("test_operation", 1, || {
            thread::sleep(Duration::from_millis(1));
        });

        let json_result = profiler.export_json();
        assert!(json_result.is_ok());

        let json = json_result.expect("Failed to export JSON");
        assert!(json.contains("test_operation"));
        assert!(json.contains("execution_count"));
    }
}