1use crate::error::QuantRS2Error;
8use crate::gate_translation::GateType;
9use crate::scirs2_quantum_profiler::{
10 CircuitProfilingResult, GateProfilingResult, MemoryAnalysis, OptimizationRecommendation,
11 ProfilingPrecision, QuantumGate, SimdAnalysis,
12};
13use scirs2_core::Complex64;
14use crate::parallel_ops_stubs::*;
16use crate::buffer_pool::BufferPool;
18use crate::platform::PlatformCapabilities;
19use scirs2_core::ndarray::{Array1, Array2, ArrayView1};
20use serde::{Deserialize, Serialize};
21use std::collections::{BTreeMap, HashMap, VecDeque};
22use std::io::Write;
23use std::sync::{
24 atomic::{AtomicU64, AtomicUsize, Ordering},
25 Arc, Mutex,
26};
27use std::time::{Duration, Instant};
28
29#[derive(Debug, Clone, Serialize, Deserialize)]
31pub struct EnhancedProfilingConfig {
32 pub precision: ProfilingPrecision,
34
35 pub enable_deep_analysis: bool,
37
38 pub track_memory_patterns: bool,
40
41 pub profile_simd_operations: bool,
43
44 pub track_parallel_patterns: bool,
46
47 pub enable_cache_analysis: bool,
49
50 pub track_memory_bandwidth: bool,
52
53 pub enable_instruction_profiling: bool,
55
56 pub enable_resource_estimation: bool,
58
59 pub analyze_noise_impact: bool,
61
62 pub generate_optimizations: bool,
64
65 pub bottleneck_detection_depth: usize,
67
68 pub enable_performance_prediction: bool,
70
71 pub hardware_aware_profiling: bool,
73
74 pub export_formats: Vec<ExportFormat>,
76}
77
78impl Default for EnhancedProfilingConfig {
79 fn default() -> Self {
80 Self {
81 precision: ProfilingPrecision::High,
82 enable_deep_analysis: true,
83 track_memory_patterns: true,
84 profile_simd_operations: true,
85 track_parallel_patterns: true,
86 enable_cache_analysis: true,
87 track_memory_bandwidth: true,
88 enable_instruction_profiling: false,
89 enable_resource_estimation: true,
90 analyze_noise_impact: true,
91 generate_optimizations: true,
92 bottleneck_detection_depth: 5,
93 enable_performance_prediction: true,
94 hardware_aware_profiling: true,
95 export_formats: vec![ExportFormat::JSON, ExportFormat::HTML, ExportFormat::CSV],
96 }
97 }
98}
99
100#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
102pub enum ExportFormat {
103 JSON,
104 HTML,
105 CSV,
106 LaTeX,
107 Markdown,
108 Binary,
109}
110
111#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
113pub enum MetricType {
114 ExecutionTime,
115 MemoryUsage,
116 CacheHitRate,
117 SimdUtilization,
118 ParallelEfficiency,
119 MemoryBandwidth,
120 InstructionCount,
121 BranchMisprediction,
122 PowerConsumption,
123 ThermalThrottle,
124}
125
126#[derive(Debug, Clone, Serialize, Deserialize)]
128pub struct PerformanceMetrics {
129 pub values: HashMap<MetricType, f64>,
131
132 pub time_series: HashMap<MetricType, Vec<(f64, f64)>>,
134
135 pub statistics: MetricStatistics,
137
138 pub correlations: HashMap<(MetricType, MetricType), f64>,
140
141 pub anomalies: Vec<AnomalyEvent>,
143}
144
145#[derive(Debug, Clone, Serialize, Deserialize)]
147pub struct MetricStatistics {
148 pub mean: HashMap<MetricType, f64>,
149 pub std_dev: HashMap<MetricType, f64>,
150 pub min: HashMap<MetricType, f64>,
151 pub max: HashMap<MetricType, f64>,
152 pub percentiles: HashMap<MetricType, BTreeMap<u8, f64>>,
153}
154
155#[derive(Debug, Clone, Serialize, Deserialize)]
157pub struct AnomalyEvent {
158 pub timestamp: f64,
159 pub metric: MetricType,
160 pub severity: AnomalySeverity,
161 pub description: String,
162 pub impact: f64,
163}
164
165#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
166pub enum AnomalySeverity {
167 Low,
168 Medium,
169 High,
170 Critical,
171}
172
173#[derive(Debug, Clone, Serialize, Deserialize)]
175pub struct BottleneckAnalysis {
176 pub bottlenecks: Vec<Bottleneck>,
178
179 pub impact_analysis: HashMap<String, f64>,
181
182 pub opportunities: Vec<OptimizationOpportunity>,
184
185 pub resource_heatmap: Array2<f64>,
187}
188
189#[derive(Debug, Clone, Serialize, Deserialize)]
190pub struct Bottleneck {
191 pub location: CircuitLocation,
192 pub bottleneck_type: BottleneckType,
193 pub severity: f64,
194 pub impact_percentage: f64,
195 pub suggested_fixes: Vec<String>,
196}
197
198#[derive(Debug, Clone, Serialize, Deserialize)]
199pub enum BottleneckType {
200 MemoryBandwidth,
201 ComputeIntensive,
202 CacheMiss,
203 ParallelizationIssue,
204 SimdUnderutilization,
205 DataDependency,
206 ResourceContention,
207}
208
209#[derive(Debug, Clone, Serialize, Deserialize)]
210pub struct CircuitLocation {
211 pub gate_index: usize,
212 pub layer: usize,
213 pub qubits: Vec<usize>,
214 pub context: String,
215}
216
217#[derive(Debug, Clone, Serialize, Deserialize)]
218pub struct OptimizationOpportunity {
219 pub opportunity_type: OpportunityType,
220 pub estimated_improvement: f64,
221 pub difficulty: Difficulty,
222 pub implementation: String,
223 pub trade_offs: Vec<String>,
224}
225
226#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
227pub enum OpportunityType {
228 GateFusion,
229 Parallelization,
230 SimdOptimization,
231 MemoryReordering,
232 CacheOptimization,
233 AlgorithmicImprovement,
234 HardwareSpecific,
235}
236
237#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
238pub enum Difficulty {
239 Trivial,
240 Easy,
241 Medium,
242 Hard,
243 Expert,
244}
245
246#[derive(Serialize, Deserialize)]
248pub struct HardwarePerformanceModel {
249 #[serde(skip, default = "PlatformCapabilities::detect")]
251 pub platform: PlatformCapabilities,
252
253 pub characteristics: HardwareCharacteristics,
255
256 pub scaling_models: HashMap<String, ScalingModel>,
258
259 pub optimization_strategies: Vec<HardwareOptimizationStrategy>,
261}
262
263#[derive(Debug, Clone, Serialize, Deserialize)]
264pub struct HardwareCharacteristics {
265 pub cpu_frequency: f64,
266 pub cache_sizes: Vec<usize>,
267 pub memory_bandwidth: f64,
268 pub simd_width: usize,
269 pub num_cores: usize,
270 pub gpu_available: bool,
271 pub gpu_memory: Option<usize>,
272 pub quantum_accelerator: Option<String>,
273}
274
275#[derive(Debug, Clone, Serialize, Deserialize)]
276pub struct ScalingModel {
277 pub model_type: ScalingType,
278 pub parameters: HashMap<String, f64>,
279 pub confidence: f64,
280}
281
282#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
283pub enum ScalingType {
284 Linear,
285 Logarithmic,
286 Polynomial,
287 Exponential,
288 Custom,
289}
290
291#[derive(Debug, Clone, Serialize, Deserialize)]
292pub struct HardwareOptimizationStrategy {
293 pub strategy_name: String,
294 pub applicable_conditions: Vec<String>,
295 pub expected_speedup: f64,
296 pub implementation_cost: f64,
297}
298
299pub struct EnhancedQuantumProfiler {
301 config: EnhancedProfilingConfig,
302 platform_caps: PlatformCapabilities,
303 buffer_pool: Arc<BufferPool<Complex64>>,
304 metrics_collector: Arc<MetricsCollector>,
305 hardware_model: Option<HardwarePerformanceModel>,
306 profiling_state: Arc<Mutex<ProfilingState>>,
307}
308
309struct MetricsCollector {
311 execution_times: Mutex<HashMap<String, Vec<Duration>>>,
312 memory_usage: AtomicUsize,
313 simd_ops_count: AtomicU64,
314 parallel_ops_count: AtomicU64,
315 cache_hits: AtomicU64,
316 cache_misses: AtomicU64,
317 bandwidth_bytes: AtomicU64,
318 start_time: Instant,
319}
320
321impl MetricsCollector {
322 fn new() -> Self {
323 Self {
324 execution_times: Mutex::new(HashMap::new()),
325 memory_usage: AtomicUsize::new(0),
326 simd_ops_count: AtomicU64::new(0),
327 parallel_ops_count: AtomicU64::new(0),
328 cache_hits: AtomicU64::new(0),
329 cache_misses: AtomicU64::new(0),
330 bandwidth_bytes: AtomicU64::new(0),
331 start_time: Instant::now(),
332 }
333 }
334
335 fn record_execution(&self, operation: &str, duration: Duration) {
336 let mut times = self
337 .execution_times
338 .lock()
339 .expect("Execution times lock poisoned");
340 times
341 .entry(operation.to_string())
342 .or_insert_with(Vec::new)
343 .push(duration);
344 }
345
346 fn record_memory(&self, bytes: usize) {
347 self.memory_usage.fetch_add(bytes, Ordering::Relaxed);
348 }
349
350 fn record_simd_op(&self) {
351 self.simd_ops_count.fetch_add(1, Ordering::Relaxed);
352 }
353
354 fn record_parallel_op(&self) {
355 self.parallel_ops_count.fetch_add(1, Ordering::Relaxed);
356 }
357
358 fn record_cache_access(&self, hit: bool) {
359 if hit {
360 self.cache_hits.fetch_add(1, Ordering::Relaxed);
361 } else {
362 self.cache_misses.fetch_add(1, Ordering::Relaxed);
363 }
364 }
365
366 fn record_bandwidth(&self, bytes: usize) {
367 self.bandwidth_bytes
368 .fetch_add(bytes as u64, Ordering::Relaxed);
369 }
370
371 fn get_elapsed(&self) -> Duration {
372 self.start_time.elapsed()
373 }
374}
375
376struct ProfilingState {
378 current_depth: usize,
379 call_stack: Vec<String>,
380 gate_timings: HashMap<usize, GateTimingInfo>,
381 memory_snapshots: VecDeque<MemorySnapshot>,
382 anomaly_detector: AnomalyDetector,
383}
384
385#[derive(Debug, Clone)]
386struct GateTimingInfo {
387 gate_type: GateType,
388 start_time: Instant,
389 end_time: Option<Instant>,
390 memory_before: usize,
391 memory_after: Option<usize>,
392 simd_ops: u64,
393 parallel_ops: u64,
394}
395
396#[derive(Debug, Clone)]
397struct MemorySnapshot {
398 timestamp: Instant,
399 total_memory: usize,
400 heap_memory: usize,
401 stack_memory: usize,
402 buffer_pool_memory: usize,
403}
404
405struct AnomalyDetector {
407 baseline_metrics: HashMap<MetricType, (f64, f64)>, detection_threshold: f64,
409 history_window: usize,
410 metric_history: HashMap<MetricType, VecDeque<f64>>,
411}
412
413impl AnomalyDetector {
414 fn new(detection_threshold: f64, history_window: usize) -> Self {
415 Self {
416 baseline_metrics: HashMap::new(),
417 detection_threshold,
418 history_window,
419 metric_history: HashMap::new(),
420 }
421 }
422
423 fn update_metric(&mut self, metric: MetricType, value: f64) -> Option<AnomalyEvent> {
424 let history = self
425 .metric_history
426 .entry(metric)
427 .or_insert_with(VecDeque::new);
428 history.push_back(value);
429
430 if history.len() > self.history_window {
431 history.pop_front();
432 }
433
434 if history.len() >= 10 {
436 let mean: f64 = history.iter().sum::<f64>() / history.len() as f64;
437 let variance: f64 =
438 history.iter().map(|x| (x - mean).powi(2)).sum::<f64>() / history.len() as f64;
439 let std_dev = variance.sqrt();
440
441 self.baseline_metrics.insert(metric, (mean, std_dev));
442
443 if let Some(&(baseline_mean, baseline_std)) = self.baseline_metrics.get(&metric) {
445 let z_score = (value - baseline_mean).abs() / baseline_std;
446
447 if z_score > self.detection_threshold {
448 let severity = match z_score {
449 z if z < 3.0 => AnomalySeverity::Low,
450 z if z < 4.0 => AnomalySeverity::Medium,
451 z if z < 5.0 => AnomalySeverity::High,
452 _ => AnomalySeverity::Critical,
453 };
454
455 return Some(AnomalyEvent {
456 timestamp: history.len() as f64,
457 metric,
458 severity,
459 description: format!("Anomaly detected: z-score = {z_score:.2}"),
460 impact: z_score / 10.0, });
462 }
463 }
464 }
465
466 None
467 }
468}
469
470impl EnhancedQuantumProfiler {
471 pub fn new() -> Self {
473 Self::with_config(EnhancedProfilingConfig::default())
474 }
475
476 pub fn with_config(config: EnhancedProfilingConfig) -> Self {
478 let platform_caps = PlatformCapabilities::detect();
479 let buffer_pool = Arc::new(BufferPool::new());
480 let metrics_collector = Arc::new(MetricsCollector::new());
481
482 let hardware_model = if config.hardware_aware_profiling {
483 Some(Self::build_hardware_model(&platform_caps))
484 } else {
485 None
486 };
487
488 let profiling_state = Arc::new(Mutex::new(ProfilingState {
489 current_depth: 0,
490 call_stack: Vec::new(),
491 gate_timings: HashMap::new(),
492 memory_snapshots: VecDeque::new(),
493 anomaly_detector: AnomalyDetector::new(3.0, 100),
494 }));
495
496 Self {
497 config,
498 platform_caps,
499 buffer_pool,
500 metrics_collector,
501 hardware_model,
502 profiling_state,
503 }
504 }
505
506 fn build_hardware_model(platform_caps: &PlatformCapabilities) -> HardwarePerformanceModel {
508 let characteristics = HardwareCharacteristics {
509 cpu_frequency: 3.0e9, cache_sizes: vec![32 * 1024, 256 * 1024, 8 * 1024 * 1024], memory_bandwidth: 50.0e9, simd_width: if platform_caps.simd_available() {
513 256
514 } else {
515 128
516 },
517 num_cores: platform_caps.cpu.logical_cores,
518 gpu_available: platform_caps.gpu_available(),
519 gpu_memory: if platform_caps.gpu_available() {
520 Some(8 * 1024 * 1024 * 1024)
521 } else {
522 None
523 },
524 quantum_accelerator: None,
525 };
526
527 let mut scaling_models = HashMap::new();
528 scaling_models.insert(
529 "gate_execution".to_string(),
530 ScalingModel {
531 model_type: ScalingType::Linear,
532 parameters: vec![("slope".to_string(), 1e-6), ("intercept".to_string(), 1e-7)]
533 .into_iter()
534 .collect(),
535 confidence: 0.95,
536 },
537 );
538
539 scaling_models.insert(
540 "memory_access".to_string(),
541 ScalingModel {
542 model_type: ScalingType::Logarithmic,
543 parameters: vec![("base".to_string(), 2.0), ("coefficient".to_string(), 1e-8)]
544 .into_iter()
545 .collect(),
546 confidence: 0.90,
547 },
548 );
549
550 let optimization_strategies = vec![
551 HardwareOptimizationStrategy {
552 strategy_name: "SIMD Vectorization".to_string(),
553 applicable_conditions: vec!["vector_friendly_gates".to_string()],
554 expected_speedup: 4.0,
555 implementation_cost: 0.2,
556 },
557 HardwareOptimizationStrategy {
558 strategy_name: "Parallel Execution".to_string(),
559 applicable_conditions: vec!["independent_gates".to_string()],
560 expected_speedup: characteristics.num_cores as f64 * 0.8,
561 implementation_cost: 0.3,
562 },
563 HardwareOptimizationStrategy {
564 strategy_name: "Cache Optimization".to_string(),
565 applicable_conditions: vec!["repeated_access_patterns".to_string()],
566 expected_speedup: 2.0,
567 implementation_cost: 0.1,
568 },
569 ];
570
571 HardwarePerformanceModel {
572 platform: PlatformCapabilities::detect(),
573 characteristics,
574 scaling_models,
575 optimization_strategies,
576 }
577 }
578
579 pub fn profile_circuit(
581 &self,
582 circuit: &[QuantumGate],
583 num_qubits: usize,
584 ) -> Result<EnhancedProfilingReport, QuantRS2Error> {
585 let start_time = Instant::now();
586
587 self.initialize_profiling(num_qubits)?;
589
590 let mut gate_results = Vec::new();
592 for (idx, gate) in circuit.iter().enumerate() {
593 let gate_result = self.profile_gate(gate, idx, num_qubits)?;
594 gate_results.push(gate_result);
595 }
596
597 let performance_metrics = self.collect_performance_metrics()?;
599
600 let bottleneck_analysis = if self.config.enable_deep_analysis {
602 Some(self.analyze_bottlenecks(&gate_results, num_qubits)?)
603 } else {
604 None
605 };
606
607 let optimizations = if self.config.generate_optimizations {
609 self.generate_optimization_recommendations(&gate_results, &bottleneck_analysis)?
610 } else {
611 Vec::new()
612 };
613
614 let performance_predictions = if self.config.enable_performance_prediction {
616 Some(self.predict_performance(&gate_results, num_qubits)?)
617 } else {
618 None
619 };
620
621 let total_time = start_time.elapsed();
623
624 let export_data = self.prepare_export_data(&gate_results)?;
626
627 Ok(EnhancedProfilingReport {
628 summary: ProfilingSummary {
629 total_execution_time: total_time,
630 num_gates: circuit.len(),
631 num_qubits,
632 platform_info: PlatformCapabilities::detect(),
633 profiling_config: self.config.clone(),
634 },
635 gate_results,
636 performance_metrics,
637 bottleneck_analysis,
638 optimizations,
639 performance_predictions,
640 export_data,
641 })
642 }
643
644 fn initialize_profiling(&self, num_qubits: usize) -> Result<(), QuantRS2Error> {
646 let mut state = self
647 .profiling_state
648 .lock()
649 .map_err(|e| QuantRS2Error::RuntimeError(format!("Lock poisoned: {e}")))?;
650 state.current_depth = 0;
651 state.call_stack.clear();
652 state.gate_timings.clear();
653 state.memory_snapshots.clear();
654
655 let initial_snapshot = MemorySnapshot {
657 timestamp: Instant::now(),
658 total_memory: self.estimate_memory_usage(num_qubits),
659 heap_memory: 0,
660 stack_memory: 0,
661 buffer_pool_memory: 0,
662 };
663 state.memory_snapshots.push_back(initial_snapshot);
664
665 Ok(())
666 }
667
668 fn profile_gate(
670 &self,
671 gate: &QuantumGate,
672 gate_index: usize,
673 num_qubits: usize,
674 ) -> Result<EnhancedGateProfilingResult, QuantRS2Error> {
675 let start_time = Instant::now();
676 let memory_before = self.estimate_memory_usage(num_qubits);
677
678 {
680 let mut state = self
681 .profiling_state
682 .lock()
683 .map_err(|e| QuantRS2Error::RuntimeError(format!("Lock poisoned: {e}")))?;
684 state.gate_timings.insert(
685 gate_index,
686 GateTimingInfo {
687 gate_type: gate.gate_type().clone(),
688 start_time,
689 end_time: None,
690 memory_before,
691 memory_after: None,
692 simd_ops: 0,
693 parallel_ops: 0,
694 },
695 );
696 }
697
698 self.simulate_gate_execution(gate, num_qubits)?;
700
701 let end_time = Instant::now();
702 let memory_after = self.estimate_memory_usage(num_qubits);
703 let execution_time = end_time - start_time;
704
705 {
707 let mut state = self
708 .profiling_state
709 .lock()
710 .map_err(|e| QuantRS2Error::RuntimeError(format!("Lock poisoned: {e}")))?;
711 if let Some(timing_info) = state.gate_timings.get_mut(&gate_index) {
712 timing_info.end_time = Some(end_time);
713 timing_info.memory_after = Some(memory_after);
714 timing_info.simd_ops = self
715 .metrics_collector
716 .simd_ops_count
717 .load(Ordering::Relaxed);
718 timing_info.parallel_ops = self
719 .metrics_collector
720 .parallel_ops_count
721 .load(Ordering::Relaxed);
722 }
723 }
724
725 self.metrics_collector
727 .record_execution(&format!("{:?}", gate.gate_type()), execution_time);
728 self.metrics_collector
729 .record_memory(memory_after.saturating_sub(memory_before));
730
731 let mut anomalies = Vec::new();
733 {
734 let mut state = self
735 .profiling_state
736 .lock()
737 .map_err(|e| QuantRS2Error::RuntimeError(format!("Lock poisoned: {e}")))?;
738 if let Some(anomaly) = state
739 .anomaly_detector
740 .update_metric(MetricType::ExecutionTime, execution_time.as_secs_f64())
741 {
742 anomalies.push(anomaly);
743 }
744 }
745
746 Ok(EnhancedGateProfilingResult {
747 gate_index,
748 gate_type: gate.gate_type().clone(),
749 execution_time,
750 memory_delta: memory_after as i64 - memory_before as i64,
751 simd_operations: self
752 .metrics_collector
753 .simd_ops_count
754 .load(Ordering::Relaxed),
755 parallel_operations: self
756 .metrics_collector
757 .parallel_ops_count
758 .load(Ordering::Relaxed),
759 cache_efficiency: self.calculate_cache_efficiency(),
760 bandwidth_usage: self.calculate_bandwidth_usage(execution_time),
761 anomalies,
762 detailed_metrics: self.collect_detailed_gate_metrics(gate, execution_time)?,
763 })
764 }
765
766 fn simulate_gate_execution(
768 &self,
769 gate: &QuantumGate,
770 num_qubits: usize,
771 ) -> Result<(), QuantRS2Error> {
772 match gate.gate_type() {
774 GateType::H | GateType::X | GateType::Y | GateType::Z => {
775 if self.platform_caps.simd_available() {
777 self.metrics_collector.record_simd_op();
778 }
779 self.metrics_collector
780 .record_bandwidth(16 * (1 << num_qubits)); }
782 GateType::CNOT | GateType::CZ => {
783 if num_qubits > 10 {
785 self.metrics_collector.record_parallel_op();
786 }
787 self.metrics_collector
788 .record_bandwidth(32 * (1 << num_qubits));
789 }
790 _ => {
791 self.metrics_collector.record_parallel_op();
793 self.metrics_collector
794 .record_bandwidth(64 * (1 << num_qubits));
795 }
796 }
797
798 use scirs2_core::random::prelude::*;
800 let cache_hit = thread_rng().gen::<f64>() > 0.2; self.metrics_collector.record_cache_access(cache_hit);
802
803 Ok(())
804 }
805
806 const fn estimate_memory_usage(&self, num_qubits: usize) -> usize {
808 let state_vector_size = (1 << num_qubits) * std::mem::size_of::<Complex64>();
809 let overhead = state_vector_size / 10; state_vector_size + overhead
811 }
812
813 fn calculate_cache_efficiency(&self) -> f64 {
815 let hits = self.metrics_collector.cache_hits.load(Ordering::Relaxed) as f64;
816 let misses = self.metrics_collector.cache_misses.load(Ordering::Relaxed) as f64;
817 let total = hits + misses;
818
819 if total > 0.0 {
820 hits / total
821 } else {
822 1.0 }
824 }
825
826 fn calculate_bandwidth_usage(&self, duration: Duration) -> f64 {
828 let bytes = self
829 .metrics_collector
830 .bandwidth_bytes
831 .load(Ordering::Relaxed) as f64;
832 let seconds = duration.as_secs_f64();
833
834 if seconds > 0.0 {
835 bytes / seconds
836 } else {
837 0.0
838 }
839 }
840
841 fn collect_detailed_gate_metrics(
843 &self,
844 gate: &QuantumGate,
845 execution_time: Duration,
846 ) -> Result<HashMap<String, f64>, QuantRS2Error> {
847 let mut metrics = HashMap::new();
848
849 metrics.insert(
850 "execution_time_us".to_string(),
851 execution_time.as_micros() as f64,
852 );
853 metrics.insert(
854 "cache_efficiency".to_string(),
855 self.calculate_cache_efficiency(),
856 );
857 metrics.insert(
858 "bandwidth_mbps".to_string(),
859 self.calculate_bandwidth_usage(execution_time) / 1e6,
860 );
861
862 if let Some(ref hw_model) = self.hardware_model {
863 metrics.insert(
864 "theoretical_flops".to_string(),
865 self.estimate_flops(gate, &hw_model.characteristics),
866 );
867 }
868
869 Ok(metrics)
870 }
871
872 fn estimate_flops(&self, gate: &QuantumGate, hw_chars: &HardwareCharacteristics) -> f64 {
874 let base_flops = match gate.gate_type() {
875 GateType::H => 8.0, GateType::X | GateType::Y | GateType::Z => 4.0,
877 GateType::CNOT | GateType::CZ => 16.0,
878 _ => 32.0, };
880
881 base_flops * hw_chars.cpu_frequency
882 }
883
884 fn collect_performance_metrics(&self) -> Result<PerformanceMetrics, QuantRS2Error> {
886 let mut values = HashMap::new();
887 let elapsed = self.metrics_collector.get_elapsed();
888
889 values.insert(MetricType::ExecutionTime, elapsed.as_secs_f64());
890 values.insert(
891 MetricType::MemoryUsage,
892 self.metrics_collector.memory_usage.load(Ordering::Relaxed) as f64,
893 );
894 values.insert(
895 MetricType::SimdUtilization,
896 self.metrics_collector
897 .simd_ops_count
898 .load(Ordering::Relaxed) as f64,
899 );
900 values.insert(
901 MetricType::ParallelEfficiency,
902 self.metrics_collector
903 .parallel_ops_count
904 .load(Ordering::Relaxed) as f64,
905 );
906 values.insert(MetricType::CacheHitRate, self.calculate_cache_efficiency());
907 values.insert(
908 MetricType::MemoryBandwidth,
909 self.calculate_bandwidth_usage(elapsed),
910 );
911
912 let statistics = self.calculate_metric_statistics(&values)?;
914
915 let mut time_series = HashMap::new();
917 for (metric, value) in &values {
918 time_series.insert(*metric, vec![(0.0, 0.0), (elapsed.as_secs_f64(), *value)]);
919 }
920
921 Ok(PerformanceMetrics {
922 values,
923 time_series,
924 statistics,
925 correlations: HashMap::new(), anomalies: Vec::new(), })
928 }
929
930 fn calculate_metric_statistics(
932 &self,
933 values: &HashMap<MetricType, f64>,
934 ) -> Result<MetricStatistics, QuantRS2Error> {
935 let mut mean = HashMap::new();
936 let mut std_dev = HashMap::new();
937 let mut min = HashMap::new();
938 let mut max = HashMap::new();
939 let mut percentiles = HashMap::new();
940
941 for (metric, value) in values {
942 mean.insert(*metric, *value);
943 std_dev.insert(*metric, 0.0); min.insert(*metric, *value);
945 max.insert(*metric, *value);
946
947 let mut percs = BTreeMap::new();
948 percs.insert(50, *value); percs.insert(95, *value * 1.1); percs.insert(99, *value * 1.2); percentiles.insert(*metric, percs);
952 }
953
954 Ok(MetricStatistics {
955 mean,
956 std_dev,
957 min,
958 max,
959 percentiles,
960 })
961 }
962
963 fn analyze_bottlenecks(
965 &self,
966 gate_results: &[EnhancedGateProfilingResult],
967 num_qubits: usize,
968 ) -> Result<BottleneckAnalysis, QuantRS2Error> {
969 let mut bottlenecks = Vec::new();
970 let mut impact_analysis = HashMap::new();
971 let mut opportunities = Vec::new();
972
973 let total_time: Duration = gate_results.iter().map(|r| r.execution_time).sum();
975 let avg_time = total_time / gate_results.len() as u32;
976
977 for (idx, result) in gate_results.iter().enumerate() {
978 if result.execution_time > avg_time * 2 {
979 let impact = result.execution_time.as_secs_f64() / total_time.as_secs_f64();
980
981 bottlenecks.push(Bottleneck {
982 location: CircuitLocation {
983 gate_index: idx,
984 layer: idx / num_qubits, qubits: vec![idx % num_qubits], context: format!("Gate {:?} at index {}", result.gate_type, idx),
987 },
988 bottleneck_type: BottleneckType::ComputeIntensive,
989 severity: impact * 100.0,
990 impact_percentage: impact * 100.0,
991 suggested_fixes: vec![
992 "Consider gate decomposition".to_string(),
993 "Explore parallel execution".to_string(),
994 ],
995 });
996
997 impact_analysis.insert(format!("gate_{idx}"), impact);
998 }
999
1000 if result.cache_efficiency < 0.5 {
1002 bottlenecks.push(Bottleneck {
1003 location: CircuitLocation {
1004 gate_index: idx,
1005 layer: idx / num_qubits,
1006 qubits: vec![idx % num_qubits],
1007 context: format!("Poor cache efficiency at gate {idx}"),
1008 },
1009 bottleneck_type: BottleneckType::CacheMiss,
1010 severity: (1.0 - result.cache_efficiency) * 50.0,
1011 impact_percentage: 10.0, suggested_fixes: vec![
1013 "Reorder operations for better locality".to_string(),
1014 "Consider data prefetching".to_string(),
1015 ],
1016 });
1017 }
1018 }
1019
1020 if self.platform_caps.simd_available() {
1022 let simd_utilization = gate_results
1023 .iter()
1024 .filter(|r| r.simd_operations > 0)
1025 .count() as f64
1026 / gate_results.len() as f64;
1027
1028 if simd_utilization < 0.5 {
1029 opportunities.push(OptimizationOpportunity {
1030 opportunity_type: OpportunityType::SimdOptimization,
1031 estimated_improvement: (1.0 - simd_utilization) * 2.0,
1032 difficulty: Difficulty::Medium,
1033 implementation: "Vectorize gate operations using AVX2".to_string(),
1034 trade_offs: vec!["Increased code complexity".to_string()],
1035 });
1036 }
1037 }
1038
1039 let resource_heatmap = Array2::zeros((gate_results.len(), 4)); Ok(BottleneckAnalysis {
1043 bottlenecks,
1044 impact_analysis,
1045 opportunities,
1046 resource_heatmap,
1047 })
1048 }
1049
1050 fn generate_optimization_recommendations(
1052 &self,
1053 gate_results: &[EnhancedGateProfilingResult],
1054 bottleneck_analysis: &Option<BottleneckAnalysis>,
1055 ) -> Result<Vec<EnhancedOptimizationRecommendation>, QuantRS2Error> {
1056 let mut recommendations = Vec::new();
1057
1058 for window in gate_results.windows(2) {
1060 if Self::can_fuse_gates(&window[0].gate_type, &window[1].gate_type) {
1061 recommendations.push(EnhancedOptimizationRecommendation {
1062 recommendation_type: RecommendationType::GateFusion,
1063 priority: Priority::High,
1064 estimated_speedup: 1.5,
1065 implementation_difficulty: Difficulty::Easy,
1066 description: format!(
1067 "Fuse {:?} and {:?} gates",
1068 window[0].gate_type, window[1].gate_type
1069 ),
1070 code_example: Some(
1071 self.generate_fusion_code(&window[0].gate_type, &window[1].gate_type),
1072 ),
1073 prerequisites: vec!["Adjacent gates must commute".to_string()],
1074 risks: vec!["May increase numerical error".to_string()],
1075 });
1076 }
1077 }
1078
1079 if let Some(ref hw_model) = self.hardware_model {
1081 for strategy in &hw_model.optimization_strategies {
1082 if strategy.expected_speedup > 1.5 {
1083 recommendations.push(EnhancedOptimizationRecommendation {
1084 recommendation_type: RecommendationType::HardwareSpecific,
1085 priority: Priority::Medium,
1086 estimated_speedup: strategy.expected_speedup,
1087 implementation_difficulty: Difficulty::Hard,
1088 description: strategy.strategy_name.clone(),
1089 code_example: None,
1090 prerequisites: strategy.applicable_conditions.clone(),
1091 risks: vec!["Platform-specific code".to_string()],
1092 });
1093 }
1094 }
1095 }
1096
1097 if let Some(bottleneck_analysis) = bottleneck_analysis {
1099 for opportunity in &bottleneck_analysis.opportunities {
1100 recommendations.push(EnhancedOptimizationRecommendation {
1101 recommendation_type: match opportunity.opportunity_type {
1102 OpportunityType::GateFusion => RecommendationType::GateFusion,
1103 OpportunityType::Parallelization => RecommendationType::Parallelization,
1104 OpportunityType::SimdOptimization => RecommendationType::SimdVectorization,
1105 OpportunityType::MemoryReordering => RecommendationType::MemoryOptimization,
1106 OpportunityType::CacheOptimization => RecommendationType::CacheOptimization,
1107 OpportunityType::AlgorithmicImprovement => {
1108 RecommendationType::AlgorithmicChange
1109 }
1110 OpportunityType::HardwareSpecific => RecommendationType::HardwareSpecific,
1111 },
1112 priority: match opportunity.difficulty {
1113 Difficulty::Trivial | Difficulty::Easy => Priority::High,
1114 Difficulty::Medium => Priority::Medium,
1115 Difficulty::Hard | Difficulty::Expert => Priority::Low,
1116 },
1117 estimated_speedup: opportunity.estimated_improvement,
1118 implementation_difficulty: opportunity.difficulty,
1119 description: opportunity.implementation.clone(),
1120 code_example: None,
1121 prerequisites: Vec::new(),
1122 risks: opportunity.trade_offs.clone(),
1123 });
1124 }
1125 }
1126
1127 Ok(recommendations)
1128 }
1129
1130 const fn can_fuse_gates(gate1: &GateType, gate2: &GateType) -> bool {
1132 use GateType::{Rx, Ry, Rz, H, X, Y, Z};
1133 matches!(
1134 (gate1, gate2),
1135 (H, H) | (X, X) | (Y, Y) | (Z, Z) | (Rz(_), Rz(_)) | (Rx(_), Rx(_)) | (Ry(_), Ry(_)) )
1138 }
1139
1140 fn generate_fusion_code(&self, gate1: &GateType, gate2: &GateType) -> String {
1142 format!(
1143 "// Fused {gate1:?} and {gate2:?}\nlet fused_gate = FusedGate::new({gate1:?}, {gate2:?});\nfused_gate.apply(state);"
1144 )
1145 }
1146
1147 fn predict_performance(
1149 &self,
1150 gate_results: &[EnhancedGateProfilingResult],
1151 num_qubits: usize,
1152 ) -> Result<PerformancePredictions, QuantRS2Error> {
1153 let mut predictions = HashMap::new();
1154
1155 let current_time: Duration = gate_results.iter().map(|r| r.execution_time).sum();
1157
1158 predictions.insert(
1159 "current".to_string(),
1160 PredictedPerformance {
1161 hardware_description: "Current Platform".to_string(),
1162 estimated_time: current_time,
1163 confidence: 1.0,
1164 limiting_factors: vec!["Actual measurement".to_string()],
1165 },
1166 );
1167
1168 if self.platform_caps.gpu_available() {
1170 let gpu_speedup = (num_qubits as f64).ln() * 2.0; predictions.insert(
1172 "gpu".to_string(),
1173 PredictedPerformance {
1174 hardware_description: "GPU Acceleration".to_string(),
1175 estimated_time: current_time / gpu_speedup as u32,
1176 confidence: 0.8,
1177 limiting_factors: vec!["Memory transfer overhead".to_string()],
1178 },
1179 );
1180 }
1181
1182 predictions.insert(
1184 "quantum_hw".to_string(),
1185 PredictedPerformance {
1186 hardware_description: "Quantum Hardware (NISQ)".to_string(),
1187 estimated_time: Duration::from_millis(gate_results.len() as u64 * 10), confidence: 0.5,
1189 limiting_factors: vec![
1190 "Gate fidelity".to_string(),
1191 "Connectivity constraints".to_string(),
1192 "Decoherence".to_string(),
1193 ],
1194 },
1195 );
1196
1197 predictions.insert(
1199 "cloud_qpu".to_string(),
1200 PredictedPerformance {
1201 hardware_description: "Cloud Quantum Processor".to_string(),
1202 estimated_time: Duration::from_secs(1)
1203 + Duration::from_millis(gate_results.len() as u64),
1204 confidence: 0.6,
1205 limiting_factors: vec!["Network latency".to_string(), "Queue time".to_string()],
1206 },
1207 );
1208
1209 let hardware_recommendations =
1211 self.generate_hardware_recommendations(num_qubits, &predictions);
1212
1213 Ok(PerformancePredictions {
1214 predictions,
1215 scaling_analysis: self.analyze_scaling(num_qubits)?,
1216 hardware_recommendations,
1217 })
1218 }
1219
1220 fn analyze_scaling(&self, num_qubits: usize) -> Result<ScalingAnalysis, QuantRS2Error> {
1222 Ok(ScalingAnalysis {
1223 qubit_scaling: ScalingType::Exponential,
1224 gate_scaling: ScalingType::Linear,
1225 memory_scaling: ScalingType::Exponential,
1226 predicted_limits: HashMap::from([
1227 ("max_qubits_cpu".to_string(), 30.0),
1228 ("max_qubits_gpu".to_string(), 35.0),
1229 ("max_gates_per_second".to_string(), 1e6),
1230 ]),
1231 })
1232 }
1233
1234 fn generate_hardware_recommendations(
1236 &self,
1237 num_qubits: usize,
1238 predictions: &HashMap<String, PredictedPerformance>,
1239 ) -> Vec<String> {
1240 let mut recommendations = Vec::new();
1241
1242 if num_qubits > 20 {
1243 recommendations.push("Consider GPU acceleration for large circuits".to_string());
1244 }
1245
1246 if num_qubits > 30 {
1247 recommendations.push("Tensor network methods recommended".to_string());
1248 }
1249
1250 if let Some(gpu_pred) = predictions.get("gpu") {
1251 if gpu_pred.confidence > 0.7 {
1252 recommendations.push("GPU acceleration shows promising speedup".to_string());
1253 }
1254 }
1255
1256 recommendations
1257 }
1258
1259 fn prepare_export_data(
1261 &self,
1262 gate_results: &[EnhancedGateProfilingResult],
1263 ) -> Result<HashMap<ExportFormat, Vec<u8>>, QuantRS2Error> {
1264 let mut export_data = HashMap::new();
1265
1266 for format in &self.config.export_formats {
1267 let data = match format {
1268 ExportFormat::JSON => self.export_to_json(gate_results)?,
1269 ExportFormat::CSV => self.export_to_csv(gate_results)?,
1270 ExportFormat::HTML => self.export_to_html(gate_results)?,
1271 _ => Vec::new(), };
1273 export_data.insert(*format, data);
1274 }
1275
1276 Ok(export_data)
1277 }
1278
1279 fn export_to_json(
1281 &self,
1282 gate_results: &[EnhancedGateProfilingResult],
1283 ) -> Result<Vec<u8>, QuantRS2Error> {
1284 let json = serde_json::to_vec_pretty(gate_results)
1285 .map_err(|e| QuantRS2Error::ComputationError(format!("CSV generation failed: {e}")))?;
1286 Ok(json)
1287 }
1288
1289 fn export_to_csv(
1291 &self,
1292 gate_results: &[EnhancedGateProfilingResult],
1293 ) -> Result<Vec<u8>, QuantRS2Error> {
1294 let mut csv = Vec::new();
1295 writeln!(csv, "gate_index,gate_type,execution_time_us,memory_delta,simd_ops,parallel_ops,cache_efficiency")
1296 .map_err(|e| QuantRS2Error::ComputationError(format!("IO error: {e}")))?;
1297
1298 for result in gate_results {
1299 writeln!(
1300 csv,
1301 "{},{:?},{},{},{},{},{:.2}",
1302 result.gate_index,
1303 result.gate_type,
1304 result.execution_time.as_micros(),
1305 result.memory_delta,
1306 result.simd_operations,
1307 result.parallel_operations,
1308 result.cache_efficiency
1309 )
1310 .map_err(|e| QuantRS2Error::ComputationError(format!("IO error: {e}")))?;
1311 }
1312
1313 Ok(csv)
1314 }
1315
1316 fn export_to_html(
1318 &self,
1319 gate_results: &[EnhancedGateProfilingResult],
1320 ) -> Result<Vec<u8>, QuantRS2Error> {
1321 let mut html = Vec::new();
1322 writeln!(
1323 html,
1324 "<html><head><title>Quantum Circuit Profiling Report</title>"
1325 )
1326 .map_err(|e| QuantRS2Error::ComputationError(format!("IO error: {e}")))?;
1327 writeln!(html, "<style>table {{ border-collapse: collapse; }} th, td {{ border: 1px solid black; padding: 8px; }}</style>")
1328 .map_err(|e| QuantRS2Error::ComputationError(format!("IO error: {e}")))?;
1329 writeln!(html, "</head><body><h1>Profiling Results</h1><table>")
1330 .map_err(|e| QuantRS2Error::ComputationError(format!("IO error: {e}")))?;
1331 writeln!(html, "<tr><th>Gate</th><th>Type</th><th>Time (μs)</th><th>Memory</th><th>SIMD</th><th>Parallel</th><th>Cache</th></tr>")
1332 .map_err(|e| QuantRS2Error::ComputationError(format!("IO error: {e}")))?;
1333
1334 for result in gate_results {
1335 writeln!(html, "<tr><td>{}</td><td>{:?}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{:.1}%</td></tr>",
1336 result.gate_index,
1337 result.gate_type,
1338 result.execution_time.as_micros(),
1339 result.memory_delta,
1340 result.simd_operations,
1341 result.parallel_operations,
1342 result.cache_efficiency * 100.0
1343 ).map_err(|e| QuantRS2Error::ComputationError(format!("IO error: {e}")))?;
1344 }
1345
1346 writeln!(html, "</table></body></html>")
1347 .map_err(|e| QuantRS2Error::ComputationError(format!("IO error: {e}")))?;
1348
1349 Ok(html)
1350 }
1351}
1352
1353#[derive(Debug, Clone, Serialize, Deserialize)]
1355pub struct EnhancedGateProfilingResult {
1356 pub gate_index: usize,
1357 pub gate_type: GateType,
1358 pub execution_time: Duration,
1359 pub memory_delta: i64,
1360 pub simd_operations: u64,
1361 pub parallel_operations: u64,
1362 pub cache_efficiency: f64,
1363 pub bandwidth_usage: f64,
1364 pub anomalies: Vec<AnomalyEvent>,
1365 pub detailed_metrics: HashMap<String, f64>,
1366}
1367
1368#[derive(Debug, Clone, Serialize, Deserialize)]
1370pub struct EnhancedOptimizationRecommendation {
1371 pub recommendation_type: RecommendationType,
1372 pub priority: Priority,
1373 pub estimated_speedup: f64,
1374 pub implementation_difficulty: Difficulty,
1375 pub description: String,
1376 pub code_example: Option<String>,
1377 pub prerequisites: Vec<String>,
1378 pub risks: Vec<String>,
1379}
1380
1381#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
1382pub enum RecommendationType {
1383 GateFusion,
1384 Parallelization,
1385 SimdVectorization,
1386 MemoryOptimization,
1387 CacheOptimization,
1388 AlgorithmicChange,
1389 HardwareSpecific,
1390}
1391
1392#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
1393pub enum Priority {
1394 Low,
1395 Medium,
1396 High,
1397 Critical,
1398}
1399
1400#[derive(Debug, Clone, Serialize, Deserialize)]
1402pub struct PerformancePredictions {
1403 pub predictions: HashMap<String, PredictedPerformance>,
1404 pub scaling_analysis: ScalingAnalysis,
1405 pub hardware_recommendations: Vec<String>,
1406}
1407
1408#[derive(Debug, Clone, Serialize, Deserialize)]
1409pub struct PredictedPerformance {
1410 pub hardware_description: String,
1411 pub estimated_time: Duration,
1412 pub confidence: f64,
1413 pub limiting_factors: Vec<String>,
1414}
1415
1416#[derive(Debug, Clone, Serialize, Deserialize)]
1417pub struct ScalingAnalysis {
1418 pub qubit_scaling: ScalingType,
1419 pub gate_scaling: ScalingType,
1420 pub memory_scaling: ScalingType,
1421 pub predicted_limits: HashMap<String, f64>,
1422}
1423
1424#[derive(Serialize, Deserialize)]
1426pub struct EnhancedProfilingReport {
1427 pub summary: ProfilingSummary,
1428 pub gate_results: Vec<EnhancedGateProfilingResult>,
1429 pub performance_metrics: PerformanceMetrics,
1430 pub bottleneck_analysis: Option<BottleneckAnalysis>,
1431 pub optimizations: Vec<EnhancedOptimizationRecommendation>,
1432 pub performance_predictions: Option<PerformancePredictions>,
1433 pub export_data: HashMap<ExportFormat, Vec<u8>>,
1434}
1435
1436#[derive(Serialize, Deserialize)]
1437pub struct ProfilingSummary {
1438 pub total_execution_time: Duration,
1439 pub num_gates: usize,
1440 pub num_qubits: usize,
1441 #[serde(skip, default = "PlatformCapabilities::detect")]
1442 pub platform_info: PlatformCapabilities,
1443 pub profiling_config: EnhancedProfilingConfig,
1444}
1445
1446#[cfg(test)]
1447mod tests {
1448 use super::*;
1449
1450 #[test]
1451 fn test_enhanced_profiler_creation() {
1452 let profiler = EnhancedQuantumProfiler::new();
1453 assert!(profiler.platform_caps.simd_available());
1454 }
1455
1456 #[test]
1457 fn test_basic_profiling() {
1458 let profiler = EnhancedQuantumProfiler::new();
1459 let gates = vec![
1460 QuantumGate::new(GateType::H, vec![0], None),
1461 QuantumGate::new(GateType::CNOT, vec![0, 1], None),
1462 QuantumGate::new(GateType::H, vec![1], None),
1463 ];
1464
1465 let result = profiler
1466 .profile_circuit(&gates, 2)
1467 .expect("Failed to profile circuit");
1468 assert_eq!(result.gate_results.len(), 3);
1469 assert!(result.summary.total_execution_time.as_nanos() > 0);
1470 }
1471
1472 #[test]
1473 fn test_bottleneck_detection() {
1474 let config = EnhancedProfilingConfig {
1475 enable_deep_analysis: true,
1476 ..Default::default()
1477 };
1478 let profiler = EnhancedQuantumProfiler::with_config(config);
1479
1480 let gates = vec![
1481 QuantumGate::new(GateType::H, vec![0], None),
1482 QuantumGate::new(GateType::T, vec![0], None),
1483 QuantumGate::new(GateType::H, vec![0], None),
1484 ];
1485
1486 let result = profiler
1487 .profile_circuit(&gates, 1)
1488 .expect("Failed to profile circuit");
1489 assert!(result.bottleneck_analysis.is_some());
1490 }
1491
1492 #[test]
1493 fn test_optimization_recommendations() {
1494 let config = EnhancedProfilingConfig {
1495 generate_optimizations: true,
1496 ..Default::default()
1497 };
1498 let profiler = EnhancedQuantumProfiler::with_config(config);
1499
1500 let gates = vec![
1501 QuantumGate::new(GateType::H, vec![0], None),
1502 QuantumGate::new(GateType::H, vec![0], None), ];
1504
1505 let result = profiler
1506 .profile_circuit(&gates, 1)
1507 .expect("Failed to profile circuit");
1508 assert!(!result.optimizations.is_empty());
1509 assert!(result
1510 .optimizations
1511 .iter()
1512 .any(|opt| opt.recommendation_type == RecommendationType::GateFusion));
1513 }
1514
1515 #[test]
1516 fn test_performance_prediction() {
1517 let config = EnhancedProfilingConfig {
1518 enable_performance_prediction: true,
1519 ..Default::default()
1520 };
1521 let profiler = EnhancedQuantumProfiler::with_config(config);
1522
1523 let gates = vec![
1524 QuantumGate::new(GateType::X, vec![0], None),
1525 QuantumGate::new(GateType::Y, vec![1], None),
1526 QuantumGate::new(GateType::Z, vec![2], None),
1527 ];
1528
1529 let result = profiler
1530 .profile_circuit(&gates, 3)
1531 .expect("Failed to profile circuit");
1532 assert!(result.performance_predictions.is_some());
1533
1534 let predictions = result
1535 .performance_predictions
1536 .expect("Missing performance predictions");
1537 assert!(predictions.predictions.contains_key("current"));
1538 assert!(predictions.predictions.contains_key("quantum_hw"));
1539 }
1540
1541 #[test]
1542 fn test_export_formats() {
1543 let config = EnhancedProfilingConfig {
1544 export_formats: vec![ExportFormat::JSON, ExportFormat::CSV, ExportFormat::HTML],
1545 ..Default::default()
1546 };
1547 let profiler = EnhancedQuantumProfiler::with_config(config);
1548
1549 let gates = vec![QuantumGate::new(GateType::H, vec![0], None)];
1550
1551 let result = profiler
1552 .profile_circuit(&gates, 1)
1553 .expect("Failed to profile circuit");
1554 assert_eq!(result.export_data.len(), 3);
1555 assert!(result.export_data.contains_key(&ExportFormat::JSON));
1556 assert!(result.export_data.contains_key(&ExportFormat::CSV));
1557 assert!(result.export_data.contains_key(&ExportFormat::HTML));
1558 }
1559}