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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
//! Advanced Quantum Algorithm Profiling Utilities
//!
//! This module provides comprehensive profiling and performance analysis
//! for quantum algorithms, with detailed metrics and optimization recommendations.

use crate::error::{QuantRS2Error, QuantRS2Result};
use crate::platform::PlatformCapabilities;
use std::collections::HashMap;
use std::time::{Duration, Instant};

/// Comprehensive quantum algorithm profiler
pub struct QuantumProfiler {
    /// Platform capabilities for context
    capabilities: PlatformCapabilities,
    /// Active profiling sessions
    sessions: HashMap<String, ProfilingSession>,
    /// Global statistics
    global_stats: GlobalStatistics,
}

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

impl QuantumProfiler {
    /// Create a new quantum profiler
    pub fn new() -> Self {
        Self {
            capabilities: PlatformCapabilities::detect(),
            sessions: HashMap::new(),
            global_stats: GlobalStatistics::default(),
        }
    }

    /// Start a new profiling session
    pub fn start_session(&mut self, name: impl Into<String>) -> ProfilingSessionHandle {
        let name = name.into();
        let session = ProfilingSession::new(name.clone());
        let handle = ProfilingSessionHandle {
            name: name.clone(),
            start_time: session.start_time,
        };
        self.sessions.insert(name, session);
        handle
    }

    /// End a profiling session and return results
    pub fn end_session(
        &mut self,
        handle: ProfilingSessionHandle,
    ) -> QuantRS2Result<ProfilingReport> {
        let session = self
            .sessions
            .remove(&handle.name)
            .ok_or_else(|| QuantRS2Error::InvalidInput("Session not found".to_string()))?;

        let duration = handle.start_time.elapsed();

        // Update global statistics
        self.global_stats.total_sessions += 1;
        self.global_stats.total_duration += duration;

        // Generate recommendations before moving session data
        let recommendations = self.generate_recommendations(&session);

        Ok(ProfilingReport {
            session_name: handle.name,
            duration,
            metrics: session.metrics,
            gate_counts: session.gate_counts,
            memory_usage: session.memory_usage,
            recommendations,
        })
    }

    /// Record a gate operation
    pub fn record_gate(&mut self, session_name: &str, gate_type: &str) -> QuantRS2Result<()> {
        let session = self
            .sessions
            .get_mut(session_name)
            .ok_or_else(|| QuantRS2Error::InvalidInput("Session not found".to_string()))?;

        *session
            .gate_counts
            .entry(gate_type.to_string())
            .or_insert(0) += 1;
        session.metrics.total_gates += 1;

        Ok(())
    }

    /// Record a measurement operation
    pub fn record_measurement(
        &mut self,
        session_name: &str,
        num_qubits: usize,
    ) -> QuantRS2Result<()> {
        let session = self
            .sessions
            .get_mut(session_name)
            .ok_or_else(|| QuantRS2Error::InvalidInput("Session not found".to_string()))?;

        session.metrics.measurements += 1;
        session.metrics.qubits_measured = session.metrics.qubits_measured.max(num_qubits);

        Ok(())
    }

    /// Record memory usage
    pub fn record_memory(
        &mut self,
        session_name: &str,
        bytes: usize,
        phase: &str,
    ) -> QuantRS2Result<()> {
        let session = self
            .sessions
            .get_mut(session_name)
            .ok_or_else(|| QuantRS2Error::InvalidInput("Session not found".to_string()))?;

        session.memory_usage.insert(phase.to_string(), bytes);
        session.metrics.peak_memory = session.metrics.peak_memory.max(bytes);

        Ok(())
    }

    /// Record circuit depth
    pub fn record_depth(&mut self, session_name: &str, depth: usize) -> QuantRS2Result<()> {
        let session = self
            .sessions
            .get_mut(session_name)
            .ok_or_else(|| QuantRS2Error::InvalidInput("Session not found".to_string()))?;

        session.metrics.circuit_depth = session.metrics.circuit_depth.max(depth);

        Ok(())
    }

    /// Get global statistics
    pub const fn global_statistics(&self) -> &GlobalStatistics {
        &self.global_stats
    }

    /// Generate optimization recommendations
    fn generate_recommendations(&self, session: &ProfilingSession) -> Vec<String> {
        let mut recommendations = Vec::new();

        // Check gate efficiency
        let two_qubit_gates = session
            .gate_counts
            .iter()
            .filter(|(name, _)| {
                name.contains("CNOT") || name.contains("CZ") || name.contains("SWAP")
            })
            .map(|(_, count)| count)
            .sum::<usize>();

        if two_qubit_gates > session.metrics.total_gates / 2 {
            recommendations.push(
                "High two-qubit gate count detected. Consider circuit optimization.".to_string(),
            );
        }

        // Check circuit depth
        if session.metrics.circuit_depth > 100 {
            recommendations.push(
                "Deep circuit detected. Consider gate fusion or compilation optimization."
                    .to_string(),
            );
        }

        // Check memory usage
        if session.metrics.peak_memory > 1024 * 1024 * 1024 {
            // > 1GB
            recommendations.push(
                "High memory usage detected. Consider tensor network or state vector compression."
                    .to_string(),
            );
        }

        // Platform-specific recommendations
        if !self.capabilities.has_simd() {
            recommendations.push(
                "SIMD not available. Performance may be limited for large state vectors."
                    .to_string(),
            );
        }

        if !self.capabilities.has_gpu() && session.metrics.total_gates > 1000 {
            recommendations.push(
                "GPU not available. Consider GPU acceleration for large circuits.".to_string(),
            );
        }

        if recommendations.is_empty() {
            recommendations.push("Circuit is well-optimized for current platform.".to_string());
        }

        recommendations
    }
}

/// Handle for an active profiling session
#[derive(Debug, Clone)]
pub struct ProfilingSessionHandle {
    name: String,
    start_time: Instant,
}

/// Active profiling session
#[derive(Debug, Clone)]
struct ProfilingSession {
    name: String,
    start_time: Instant,
    metrics: SessionMetrics,
    gate_counts: HashMap<String, usize>,
    memory_usage: HashMap<String, usize>,
}

impl ProfilingSession {
    fn new(name: String) -> Self {
        Self {
            name,
            start_time: Instant::now(),
            metrics: SessionMetrics::default(),
            gate_counts: HashMap::new(),
            memory_usage: HashMap::new(),
        }
    }
}

/// Session-specific metrics
#[derive(Debug, Clone, Default)]
struct SessionMetrics {
    total_gates: usize,
    circuit_depth: usize,
    measurements: usize,
    qubits_measured: usize,
    peak_memory: usize,
}

/// Global profiling statistics
#[derive(Debug, Clone, Default)]
pub struct GlobalStatistics {
    /// Total number of sessions
    pub total_sessions: usize,
    /// Total cumulative duration
    pub total_duration: Duration,
}

/// Profiling report for a completed session
#[derive(Debug, Clone)]
pub struct ProfilingReport {
    /// Session name
    pub session_name: String,
    /// Session duration
    pub duration: Duration,
    /// Collected metrics
    pub metrics: SessionMetrics,
    /// Gate counts by type
    pub gate_counts: HashMap<String, usize>,
    /// Memory usage by phase
    pub memory_usage: HashMap<String, usize>,
    /// Optimization recommendations
    pub recommendations: Vec<String>,
}

impl ProfilingReport {
    /// Print a detailed report
    pub fn print_detailed(&self) {
        println!("╔══════════════════════════════════════════════════════════════╗");
        println!("║          Quantum Algorithm Profiling Report                 ║");
        println!("╚══════════════════════════════════════════════════════════════╝");
        println!();
        println!("Session: {}", self.session_name);
        println!("Duration: {:?}", self.duration);
        println!();
        println!("═══ Gate Statistics ═══");
        println!("  Total gates: {}", self.metrics.total_gates);
        println!("  Circuit depth: {}", self.metrics.circuit_depth);
        println!(
            "  Gates per layer: {:.2}",
            self.metrics.total_gates as f64 / self.metrics.circuit_depth.max(1) as f64
        );
        println!();

        if !self.gate_counts.is_empty() {
            println!("  Gate breakdown:");
            let mut sorted_gates: Vec<_> = self.gate_counts.iter().collect();
            sorted_gates.sort_by_key(|(_, count)| std::cmp::Reverse(**count));
            for (gate, count) in sorted_gates.iter().take(10) {
                let percentage = (**count as f64 / self.metrics.total_gates as f64) * 100.0;
                println!("    {gate:<10} {count:>6}  ({percentage:>5.1}%)");
            }
            println!();
        }

        println!("═══ Measurement Statistics ═══");
        println!("  Total measurements: {}", self.metrics.measurements);
        println!("  Qubits measured: {}", self.metrics.qubits_measured);
        println!();

        println!("═══ Memory Statistics ═══");
        println!(
            "  Peak memory: {} MB",
            self.metrics.peak_memory / (1024 * 1024)
        );

        if !self.memory_usage.is_empty() {
            println!("  Memory by phase:");
            for (phase, bytes) in &self.memory_usage {
                println!("    {:<20} {:>8} MB", phase, bytes / (1024 * 1024));
            }
            println!();
        }

        println!("═══ Performance Metrics ═══");
        let gates_per_sec = self.metrics.total_gates as f64 / self.duration.as_secs_f64();
        println!("  Gates per second: {gates_per_sec:.2}");
        println!(
            "  Average gate time: {:.2} µs",
            self.duration.as_micros() as f64 / self.metrics.total_gates.max(1) as f64
        );
        println!();

        println!("═══ Recommendations ═══");
        for (i, rec) in self.recommendations.iter().enumerate() {
            println!("  {}. {}", i + 1, rec);
        }
        println!("╚══════════════════════════════════════════════════════════════╝");
    }

    /// Export to JSON format
    pub fn to_json(&self) -> String {
        format!(
            r#"{{
  "session_name": "{}",
  "duration_ms": {},
  "metrics": {{
    "total_gates": {},
    "circuit_depth": {},
    "measurements": {},
    "qubits_measured": {},
    "peak_memory": {}
  }},
  "gate_counts": {:?},
  "memory_usage": {:?},
  "recommendations": {:?}
}}"#,
            self.session_name,
            self.duration.as_millis(),
            self.metrics.total_gates,
            self.metrics.circuit_depth,
            self.metrics.measurements,
            self.metrics.qubits_measured,
            self.metrics.peak_memory,
            self.gate_counts,
            self.memory_usage,
            self.recommendations
        )
    }
}

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

    #[test]
    fn test_profiler_creation() {
        let profiler = QuantumProfiler::new();
        assert_eq!(profiler.global_stats.total_sessions, 0);
    }

    #[test]
    fn test_session_lifecycle() {
        let mut profiler = QuantumProfiler::new();

        let handle = profiler.start_session("test_circuit");
        assert_eq!(handle.name, "test_circuit");

        let report = profiler.end_session(handle).expect("failed to end session");
        assert_eq!(report.session_name, "test_circuit");
        assert_eq!(profiler.global_stats.total_sessions, 1);
    }

    #[test]
    fn test_gate_recording() {
        let mut profiler = QuantumProfiler::new();
        let handle = profiler.start_session("test");

        profiler
            .record_gate("test", "H")
            .expect("failed to record H gate");
        profiler
            .record_gate("test", "CNOT")
            .expect("failed to record CNOT gate");
        profiler
            .record_gate("test", "H")
            .expect("failed to record H gate");

        let report = profiler.end_session(handle).expect("failed to end session");
        assert_eq!(report.metrics.total_gates, 3);
        assert_eq!(
            *report.gate_counts.get("H").expect("H gate count not found"),
            2
        );
        assert_eq!(
            *report
                .gate_counts
                .get("CNOT")
                .expect("CNOT gate count not found"),
            1
        );
    }

    #[test]
    fn test_measurement_recording() {
        let mut profiler = QuantumProfiler::new();
        let handle = profiler.start_session("test");

        profiler
            .record_measurement("test", 5)
            .expect("failed to record measurement");
        profiler
            .record_measurement("test", 3)
            .expect("failed to record measurement");

        let report = profiler.end_session(handle).expect("failed to end session");
        assert_eq!(report.metrics.measurements, 2);
        assert_eq!(report.metrics.qubits_measured, 5);
    }

    #[test]
    fn test_memory_recording() {
        let mut profiler = QuantumProfiler::new();
        let handle = profiler.start_session("test");

        profiler
            .record_memory("test", 1024 * 1024, "initialization")
            .expect("failed to record memory");
        profiler
            .record_memory("test", 2 * 1024 * 1024, "computation")
            .expect("failed to record memory");

        let report = profiler.end_session(handle).expect("failed to end session");
        assert_eq!(report.metrics.peak_memory, 2 * 1024 * 1024);
        assert_eq!(report.memory_usage.len(), 2);
    }

    #[test]
    fn test_recommendations() {
        let mut profiler = QuantumProfiler::new();
        let handle = profiler.start_session("deep_circuit");

        // Record many gates to trigger deep circuit warning
        for _ in 0..150 {
            profiler
                .record_gate("deep_circuit", "H")
                .expect("failed to record gate");
        }
        profiler
            .record_depth("deep_circuit", 150)
            .expect("failed to record depth");

        let report = profiler.end_session(handle).expect("failed to end session");
        assert!(!report.recommendations.is_empty());
        assert!(report
            .recommendations
            .iter()
            .any(|r| r.contains("deep circuit") || r.contains("Deep circuit")));
    }

    #[test]
    fn test_report_formatting() {
        let mut profiler = QuantumProfiler::new();
        let handle = profiler.start_session("test");

        profiler
            .record_gate("test", "H")
            .expect("failed to record gate");
        profiler
            .record_measurement("test", 1)
            .expect("failed to record measurement");

        let report = profiler.end_session(handle).expect("failed to end session");

        // Test that print_detailed doesn't panic
        report.print_detailed();

        // Test JSON export
        let json = report.to_json();
        assert!(json.contains("session_name"));
        assert!(json.contains("test"));
    }

    #[test]
    fn test_global_statistics() {
        let mut profiler = QuantumProfiler::new();

        for i in 0..5 {
            let handle = profiler.start_session(format!("session_{}", i));
            profiler.end_session(handle).expect("failed to end session");
        }

        let stats = profiler.global_statistics();
        assert_eq!(stats.total_sessions, 5);
        assert!(stats.total_duration.as_nanos() > 0);
    }
}