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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
//! Quantum Circuit Debugger with SciRS2 Visualization Tools
//!
//! This module provides comprehensive debugging capabilities for quantum circuits
//! using SciRS2's advanced visualization and analysis tools.

use crate::gate_translation::GateType;
// use scirs2_core::memory::BufferPool;
use crate::buffer_pool::BufferPool;
use std::time::{Duration, Instant};

/// Simplified quantum gate representation for debugging
#[derive(Debug, Clone)]
pub struct QuantumGate {
    gate_type: GateType,
    target_qubits: Vec<usize>,
    control_qubits: Option<Vec<usize>>,
}

impl QuantumGate {
    pub const fn new(
        gate_type: GateType,
        target_qubits: Vec<usize>,
        control_qubits: Option<Vec<usize>>,
    ) -> Self {
        Self {
            gate_type,
            target_qubits,
            control_qubits,
        }
    }

    pub const fn gate_type(&self) -> &GateType {
        &self.gate_type
    }

    pub fn target_qubits(&self) -> &[usize] {
        &self.target_qubits
    }

    pub fn control_qubits(&self) -> Option<&[usize]> {
        self.control_qubits.as_deref()
    }
}
use crate::error::QuantRS2Error;
use std::collections::{HashMap, VecDeque};
// Serde imports would go here if needed
use scirs2_core::Complex64;

/// Performance tracking for enhanced debugging with SciRS2
#[derive(Debug, Clone)]
pub struct PerformanceTracker {
    gate_timing: HashMap<String, Duration>,
    memory_usage: Vec<usize>,
    computation_start: Option<Instant>,
    total_execution_time: Duration,
    simd_operations_count: usize,
    parallel_operations_count: usize,
}

impl PerformanceTracker {
    pub fn new() -> Self {
        Self {
            gate_timing: HashMap::new(),
            memory_usage: Vec::new(),
            computation_start: None,
            total_execution_time: Duration::new(0, 0),
            simd_operations_count: 0,
            parallel_operations_count: 0,
        }
    }

    pub fn start_timing(&mut self) {
        self.computation_start = Some(Instant::now());
    }

    pub fn record_gate_timing(&mut self, gate_type: &str, duration: Duration) {
        *self
            .gate_timing
            .entry(gate_type.to_string())
            .or_insert(Duration::new(0, 0)) += duration;
    }

    pub fn record_memory_usage(&mut self, usage: usize) {
        self.memory_usage.push(usage);
    }

    pub const fn increment_simd_ops(&mut self) {
        self.simd_operations_count += 1;
    }

    pub const fn increment_parallel_ops(&mut self) {
        self.parallel_operations_count += 1;
    }

    pub fn finish_timing(&mut self) {
        if let Some(start) = self.computation_start.take() {
            self.total_execution_time = start.elapsed();
        }
    }
}

/// Configuration for quantum debugging
#[derive(Debug, Clone)]
pub struct DebugConfig {
    /// Enable state vector tracking
    pub track_state_vectors: bool,
    /// Enable entanglement analysis
    pub analyze_entanglement: bool,
    /// Enable amplitude visualization
    pub visualize_amplitudes: bool,
    /// Enable gate effect tracking
    pub track_gate_effects: bool,
    /// Maximum number of qubits for detailed tracking
    pub max_detailed_qubits: usize,
    /// Sampling rate for large circuits
    pub sampling_rate: f64,
    /// Enable breakpoint functionality
    pub enable_breakpoints: bool,
    /// Memory limit for state storage (MB)
    pub memory_limit_mb: usize,
}

impl Default for DebugConfig {
    fn default() -> Self {
        Self {
            track_state_vectors: true,
            analyze_entanglement: true,
            visualize_amplitudes: true,
            track_gate_effects: true,
            max_detailed_qubits: 10,
            sampling_rate: 0.1,
            enable_breakpoints: true,
            memory_limit_mb: 1024,
        }
    }
}

/// Quantum circuit debugger with SciRS2 visualization
pub struct QuantumDebugger {
    config: DebugConfig,
    execution_trace: Vec<DebugStep>,
    breakpoints: Vec<Breakpoint>,
    current_step: usize,
    state_history: VecDeque<StateSnapshot>,
    gate_statistics: GateStatistics,
    buffer_pool: Option<BufferPool<Complex64>>,
    performance_metrics: PerformanceTracker,
}

impl QuantumDebugger {
    /// Create a new quantum debugger
    pub fn new() -> Self {
        let config = DebugConfig::default();
        Self::with_config(config)
    }

    /// Create a new quantum debugger with custom configuration
    pub fn with_config(config: DebugConfig) -> Self {
        let buffer_pool = if config.memory_limit_mb > 0 {
            Some(BufferPool::<Complex64>::new())
        } else {
            None
        };

        Self {
            config,
            execution_trace: Vec::new(),
            breakpoints: Vec::new(),
            current_step: 0,
            state_history: VecDeque::new(),
            gate_statistics: GateStatistics::new(),
            buffer_pool,
            performance_metrics: PerformanceTracker::new(),
        }
    }

    /// Set a breakpoint at a specific step
    pub fn set_breakpoint(&mut self, step: usize, condition: BreakpointCondition) {
        self.breakpoints.push(Breakpoint {
            step,
            condition,
            enabled: true,
        });
    }

    /// Remove a breakpoint
    pub fn remove_breakpoint(&mut self, step: usize) {
        self.breakpoints.retain(|bp| bp.step != step);
    }

    /// Debug a quantum circuit execution with SciRS2 performance tracking
    pub fn debug_circuit(
        &mut self,
        circuit: &[QuantumGate],
        initial_state: &[Complex64],
        num_qubits: usize,
    ) -> Result<DebugResult, QuantRS2Error> {
        self.reset_debug_session();

        // Start performance tracking
        self.performance_metrics.start_timing();

        let mut current_state = initial_state.to_vec();
        let mut step = 0;

        // Record initial state
        if self.config.track_state_vectors {
            self.record_state_snapshot(step, &current_state, None, num_qubits)?;
        }

        for (gate_index, gate) in circuit.iter().enumerate() {
            step += 1;

            // Check breakpoints
            if self.should_break_at_step(step, &current_state, gate) {
                return Ok(DebugResult {
                    status: DebugStatus::BreakpointHit(step),
                    final_state: current_state,
                    execution_trace: self.execution_trace.clone(),
                    analysis: self.generate_analysis(num_qubits)?,
                });
            }

            // Apply gate and record debug information with performance tracking
            let gate_start = Instant::now();
            let gate_effect =
                self.apply_gate_with_debugging(gate, &mut current_state, step, num_qubits)?;
            let gate_duration = gate_start.elapsed();

            // Record performance metrics
            let gate_type_str = format!("{:?}", gate.gate_type());
            self.performance_metrics
                .record_gate_timing(&gate_type_str, gate_duration);
            self.performance_metrics
                .record_memory_usage(current_state.len() * std::mem::size_of::<Complex64>());

            self.execution_trace.push(DebugStep {
                step,
                gate: gate.clone(),
                gate_effect,
                state_before: if self.config.track_state_vectors {
                    Some(self.get_previous_state())
                } else {
                    None
                },
                state_after: if self.config.track_state_vectors {
                    Some(current_state.clone())
                } else {
                    None
                },
                entanglement_info: if self.config.analyze_entanglement {
                    Some(self.analyze_entanglement(&current_state, num_qubits)?)
                } else {
                    None
                },
                amplitude_analysis: if self.config.visualize_amplitudes {
                    Some(self.analyze_amplitudes(&current_state)?)
                } else {
                    None
                },
            });

            // Record state snapshot
            if self.config.track_state_vectors {
                self.record_state_snapshot(step, &current_state, Some(gate_index), num_qubits)?;
            }

            // Update statistics
            self.gate_statistics.record_gate(gate);
        }

        self.current_step = step;

        // Finish performance tracking
        self.performance_metrics.finish_timing();

        Ok(DebugResult {
            status: DebugStatus::Completed,
            final_state: current_state,
            execution_trace: self.execution_trace.clone(),
            analysis: self.generate_analysis(num_qubits)?,
        })
    }

    /// Reset debug session
    fn reset_debug_session(&mut self) {
        self.execution_trace.clear();
        self.current_step = 0;
        self.state_history.clear();
        self.gate_statistics = GateStatistics::new();
        self.performance_metrics = PerformanceTracker::new();
    }

    /// Check if execution should break at current step
    fn should_break_at_step(&self, step: usize, state: &[Complex64], gate: &QuantumGate) -> bool {
        if !self.config.enable_breakpoints {
            return false;
        }

        for breakpoint in &self.breakpoints {
            if breakpoint.enabled && breakpoint.step == step {
                match &breakpoint.condition {
                    BreakpointCondition::Always => return true,
                    BreakpointCondition::StateCondition(condition) => {
                        if self.evaluate_state_condition(state, condition) {
                            return true;
                        }
                    }
                    BreakpointCondition::GateType(gate_type) => {
                        if std::mem::discriminant(gate.gate_type())
                            == std::mem::discriminant(gate_type)
                        {
                            return true;
                        }
                    }
                    BreakpointCondition::QubitIndex(qubit) => {
                        if gate.target_qubits().contains(qubit) {
                            return true;
                        }
                    }
                }
            }
        }

        false
    }

    /// Evaluate a state condition
    fn evaluate_state_condition(&self, state: &[Complex64], condition: &StateCondition) -> bool {
        match condition {
            StateCondition::AmplitudeThreshold { qubit, threshold } => {
                if let Some(amplitude) = self.get_qubit_amplitude(state, *qubit) {
                    amplitude.norm_sqr() > *threshold
                } else {
                    false
                }
            }
            StateCondition::EntanglementThreshold { threshold } => {
                if let Ok(entanglement) = self.calculate_total_entanglement(state) {
                    entanglement > *threshold
                } else {
                    false
                }
            }
            StateCondition::PhaseDifference {
                qubit1,
                qubit2,
                threshold,
            } => {
                if let (Some(amp1), Some(amp2)) = (
                    self.get_qubit_amplitude(state, *qubit1),
                    self.get_qubit_amplitude(state, *qubit2),
                ) {
                    let phase_diff = (amp1.arg() - amp2.arg()).abs();
                    phase_diff > *threshold
                } else {
                    false
                }
            }
        }
    }

    /// Apply gate with debugging information
    fn apply_gate_with_debugging(
        &self,
        gate: &QuantumGate,
        state: &mut Vec<Complex64>,
        _step: usize,
        num_qubits: usize,
    ) -> Result<GateEffect, QuantRS2Error> {
        let state_before = state.clone();

        // Apply the gate (simplified implementation)
        self.apply_gate_to_state(gate, state, num_qubits)?;

        // Calculate gate effect
        let effect = self.calculate_gate_effect(&state_before, state, gate)?;

        Ok(effect)
    }

    /// Apply a gate to the state vector (simplified implementation)
    fn apply_gate_to_state(
        &self,
        gate: &QuantumGate,
        state: &mut [Complex64],
        num_qubits: usize,
    ) -> Result<(), QuantRS2Error> {
        use crate::gate_translation::GateType;

        match gate.gate_type() {
            GateType::X => {
                if let Some(&target) = gate.target_qubits().first() {
                    let target_bit = 1 << target;
                    for i in 0..(1 << num_qubits) {
                        let j = i ^ target_bit;
                        if i < j {
                            state.swap(i, j);
                        }
                    }
                }
            }
            GateType::Y => {
                if let Some(&target) = gate.target_qubits().first() {
                    let target_bit = 1 << target;
                    for i in 0..(1 << num_qubits) {
                        let j = i ^ target_bit;
                        if i < j {
                            let temp = state[i];
                            state[i] = Complex64::new(0.0, 1.0) * state[j];
                            state[j] = Complex64::new(0.0, -1.0) * temp;
                        }
                    }
                }
            }
            GateType::Z => {
                if let Some(&target) = gate.target_qubits().first() {
                    let target_bit = 1 << target;
                    for i in 0..(1 << num_qubits) {
                        if i & target_bit != 0 {
                            state[i] *= -1.0;
                        }
                    }
                }
            }
            GateType::H => {
                if let Some(&target) = gate.target_qubits().first() {
                    let target_bit = 1 << target;
                    let inv_sqrt2 = 1.0 / std::f64::consts::SQRT_2;
                    for i in 0..(1 << num_qubits) {
                        let j = i ^ target_bit;
                        if i < j {
                            let temp = state[i];
                            state[i] = inv_sqrt2 * (temp + state[j]);
                            state[j] = inv_sqrt2 * (temp - state[j]);
                        }
                    }
                }
            }
            GateType::CNOT => {
                if gate.target_qubits().len() >= 2 {
                    let control = gate.target_qubits()[0];
                    let target = gate.target_qubits()[1];
                    let control_bit = 1 << control;
                    let target_bit = 1 << target;

                    for i in 0..(1 << num_qubits) {
                        if i & control_bit != 0 {
                            let j = i ^ target_bit;
                            if i != j {
                                state.swap(i, j);
                            }
                        }
                    }
                }
            }
            _ => {
                // For other gates, no operation (placeholder)
            }
        }

        Ok(())
    }

    /// Calculate the effect of a gate on the quantum state
    fn calculate_gate_effect(
        &self,
        state_before: &[Complex64],
        state_after: &[Complex64],
        gate: &QuantumGate,
    ) -> Result<GateEffect, QuantRS2Error> {
        let amplitude_changes = self.calculate_amplitude_changes(state_before, state_after);
        let phase_changes = self.calculate_phase_changes(state_before, state_after);
        let entanglement_change = self.calculate_entanglement_change(state_before, state_after)?;

        Ok(GateEffect {
            gate_type: format!("{:?}", gate.gate_type()),
            target_qubits: gate.target_qubits().to_vec(),
            amplitude_changes,
            phase_changes,
            entanglement_change,
            fidelity: self.calculate_state_fidelity(state_before, state_after),
        })
    }

    /// Calculate amplitude changes between states
    fn calculate_amplitude_changes(&self, before: &[Complex64], after: &[Complex64]) -> Vec<f64> {
        before
            .iter()
            .zip(after.iter())
            .map(|(b, a)| (a.norm() - b.norm()).abs())
            .collect()
    }

    /// Calculate phase changes between states
    fn calculate_phase_changes(&self, before: &[Complex64], after: &[Complex64]) -> Vec<f64> {
        before
            .iter()
            .zip(after.iter())
            .map(|(b, a)| {
                if b.norm() > 1e-10 && a.norm() > 1e-10 {
                    (a.arg() - b.arg()).abs()
                } else {
                    0.0
                }
            })
            .collect()
    }

    /// Calculate entanglement change
    fn calculate_entanglement_change(
        &self,
        before: &[Complex64],
        after: &[Complex64],
    ) -> Result<f64, QuantRS2Error> {
        let ent_before = self.calculate_total_entanglement(before)?;
        let ent_after = self.calculate_total_entanglement(after)?;
        Ok(ent_after - ent_before)
    }

    /// Calculate state fidelity
    fn calculate_state_fidelity(&self, state1: &[Complex64], state2: &[Complex64]) -> f64 {
        let overlap: Complex64 = state1
            .iter()
            .zip(state2.iter())
            .map(|(s1, s2)| s1.conj() * s2)
            .sum();
        overlap.norm_sqr()
    }

    /// Record a state snapshot
    fn record_state_snapshot(
        &mut self,
        step: usize,
        state: &[Complex64],
        gate_index: Option<usize>,
        num_qubits: usize,
    ) -> Result<(), QuantRS2Error> {
        // Check memory limit
        let estimated_size = state.len() * 16; // 16 bytes per Complex64
        let total_size = self.state_history.len() * estimated_size;

        if total_size > self.config.memory_limit_mb * 1024 * 1024 {
            // Remove oldest snapshots
            while self.state_history.len() > 100 {
                self.state_history.pop_front();
            }
        }

        let snapshot = StateSnapshot {
            step,
            gate_index,
            state: if num_qubits <= self.config.max_detailed_qubits {
                state.to_vec()
            } else {
                // Sample the state for large systems
                self.sample_state(state)?
            },
            metadata: StateMetadata {
                entropy: self.calculate_von_neumann_entropy(state)?,
                purity: self.calculate_purity(state)?,
                norm: state.iter().map(|c| c.norm_sqr()).sum::<f64>().sqrt(),
            },
        };

        self.state_history.push_back(snapshot);
        Ok(())
    }

    /// Sample state for large quantum systems
    fn sample_state(&self, state: &[Complex64]) -> Result<Vec<Complex64>, QuantRS2Error> {
        let sample_size = (state.len() as f64 * self.config.sampling_rate).ceil() as usize;
        let step = state.len() / sample_size;

        Ok(state.iter().step_by(step.max(1)).copied().collect())
    }

    /// Get previous state from history
    fn get_previous_state(&self) -> Vec<Complex64> {
        self.state_history
            .back()
            .map(|snapshot| snapshot.state.clone())
            .unwrap_or_default()
    }

    /// Analyze entanglement in the quantum state
    fn analyze_entanglement(
        &self,
        state: &[Complex64],
        num_qubits: usize,
    ) -> Result<EntanglementAnalysis, QuantRS2Error> {
        let total_entanglement = self.calculate_total_entanglement(state)?;
        let pairwise_entanglement = self.calculate_pairwise_entanglement(state, num_qubits)?;
        let entanglement_spectrum = self.calculate_entanglement_spectrum(state, num_qubits)?;
        let max_entangled_pair = self.find_max_entangled_pair(&pairwise_entanglement);

        Ok(EntanglementAnalysis {
            total_entanglement,
            pairwise_entanglement,
            entanglement_spectrum,
            max_entangled_pair,
        })
    }

    /// Calculate total entanglement (simplified von Neumann entropy)
    fn calculate_total_entanglement(&self, state: &[Complex64]) -> Result<f64, QuantRS2Error> {
        // Simplified calculation - in practice, this would use reduced density matrices
        let probabilities: Vec<f64> = state.iter().map(|c| c.norm_sqr()).collect();
        let entropy = -probabilities
            .iter()
            .filter(|&&p| p > 1e-15)
            .map(|&p| p * p.log2())
            .sum::<f64>();
        Ok(entropy)
    }

    /// Calculate pairwise entanglement between qubits
    fn calculate_pairwise_entanglement(
        &self,
        state: &[Complex64],
        num_qubits: usize,
    ) -> Result<HashMap<(usize, usize), f64>, QuantRS2Error> {
        let mut pairwise = HashMap::new();

        // Simplified pairwise entanglement calculation
        for i in 0..num_qubits {
            for j in (i + 1)..num_qubits {
                let entanglement =
                    self.calculate_bipartite_entanglement(state, i, j, num_qubits)?;
                pairwise.insert((i, j), entanglement);
            }
        }

        Ok(pairwise)
    }

    /// Calculate bipartite entanglement between two qubits
    fn calculate_bipartite_entanglement(
        &self,
        state: &[Complex64],
        qubit1: usize,
        qubit2: usize,
        num_qubits: usize,
    ) -> Result<f64, QuantRS2Error> {
        // Simplified calculation - would use partial trace in practice
        let mut correlation = 0.0;

        for i in 0..(1 << num_qubits) {
            let bit1 = (i >> qubit1) & 1;
            let bit2 = (i >> qubit2) & 1;

            if bit1 == bit2 {
                correlation += state[i].norm_sqr();
            } else {
                correlation -= state[i].norm_sqr();
            }
        }

        Ok(correlation.abs())
    }

    /// Calculate entanglement spectrum
    fn calculate_entanglement_spectrum(
        &self,
        state: &[Complex64],
        _num_qubits: usize,
    ) -> Result<Vec<f64>, QuantRS2Error> {
        // Simplified entanglement spectrum
        let mut spectrum: Vec<f64> = state.iter().map(|c| c.norm_sqr()).collect();
        spectrum.sort_by(|a, b| b.partial_cmp(a).unwrap_or(std::cmp::Ordering::Equal));
        Ok(spectrum)
    }

    /// Find the pair of qubits with maximum entanglement
    fn find_max_entangled_pair(
        &self,
        pairwise: &HashMap<(usize, usize), f64>,
    ) -> Option<(usize, usize)> {
        pairwise
            .iter()
            .max_by(|(_, v1), (_, v2)| v1.partial_cmp(v2).unwrap_or(std::cmp::Ordering::Equal))
            .map(|(pair, _)| *pair)
    }

    /// Analyze amplitude distribution
    fn analyze_amplitudes(&self, state: &[Complex64]) -> Result<AmplitudeAnalysis, QuantRS2Error> {
        let amplitudes: Vec<f64> = state.iter().map(|c| c.norm()).collect();
        let phases: Vec<f64> = state.iter().map(|c| c.arg()).collect();

        let max_amplitude_index = amplitudes
            .iter()
            .enumerate()
            .max_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
            .map_or(0, |(i, _)| i);

        let mean_amplitude = amplitudes.iter().sum::<f64>() / amplitudes.len() as f64;
        let amplitude_variance = amplitudes
            .iter()
            .map(|a| (a - mean_amplitude).powi(2))
            .sum::<f64>()
            / amplitudes.len() as f64;

        let significant_states = self.find_significant_states(&amplitudes);

        Ok(AmplitudeAnalysis {
            amplitudes,
            phases,
            max_amplitude_index,
            mean_amplitude,
            amplitude_variance,
            significant_states,
        })
    }

    /// Find states with significant amplitudes
    fn find_significant_states(&self, amplitudes: &[f64]) -> Vec<usize> {
        let threshold = 0.1; // 10% threshold
        amplitudes
            .iter()
            .enumerate()
            .filter(|(_, &amp)| amp > threshold)
            .map(|(i, _)| i)
            .collect()
    }

    /// Get qubit amplitude (simplified)
    fn get_qubit_amplitude(&self, state: &[Complex64], qubit: usize) -> Option<Complex64> {
        // Simplified - would calculate reduced amplitude in practice
        if qubit < 64 && (1 << qubit) < state.len() {
            Some(state[1 << qubit])
        } else {
            None
        }
    }

    /// Calculate von Neumann entropy
    fn calculate_von_neumann_entropy(&self, state: &[Complex64]) -> Result<f64, QuantRS2Error> {
        let probabilities: Vec<f64> = state.iter().map(|c| c.norm_sqr()).collect();
        let entropy = -probabilities
            .iter()
            .filter(|&&p| p > 1e-15)
            .map(|&p| p * p.log2())
            .sum::<f64>();
        Ok(entropy)
    }

    /// Calculate state purity
    fn calculate_purity(&self, state: &[Complex64]) -> Result<f64, QuantRS2Error> {
        let purity = state.iter().map(|c| c.norm_sqr().powi(2)).sum::<f64>();
        Ok(purity)
    }

    /// Generate comprehensive analysis with SciRS2 enhanced metrics
    fn generate_analysis(&self, _num_qubits: usize) -> Result<DebugAnalysis, QuantRS2Error> {
        Ok(DebugAnalysis {
            gate_statistics: self.gate_statistics.clone(),
            execution_summary: ExecutionSummary {
                total_steps: self.execution_trace.len(),
                total_gates: self.gate_statistics.total_gates(),
                circuit_depth: self.calculate_circuit_depth(),
                execution_time: self.performance_metrics.total_execution_time.as_secs_f64(),
            },
            performance_metrics: self.calculate_enhanced_performance_metrics()?,
            recommendations: self.generate_enhanced_recommendations()?,
        })
    }

    /// Calculate circuit depth
    fn calculate_circuit_depth(&self) -> usize {
        // Simplified depth calculation
        self.execution_trace.len()
    }

    /// Estimate execution time
    fn estimate_execution_time(&self) -> f64 {
        // Simplified time estimation (microseconds)
        self.execution_trace.len() as f64 * 0.1
    }

    /// Calculate enhanced performance metrics with SciRS2 features
    fn calculate_enhanced_performance_metrics(&self) -> Result<PerformanceMetrics, QuantRS2Error> {
        Ok(PerformanceMetrics {
            average_gate_fidelity: self.calculate_average_fidelity(),
            entanglement_efficiency: self.calculate_entanglement_efficiency(),
            state_overlap_preservation: self.calculate_state_overlap_preservation(),
        })
    }

    /// Legacy performance metrics calculation (kept for compatibility)
    fn calculate_performance_metrics(&self) -> Result<PerformanceMetrics, QuantRS2Error> {
        self.calculate_enhanced_performance_metrics()
    }

    /// Calculate average gate fidelity
    fn calculate_average_fidelity(&self) -> f64 {
        if self.execution_trace.is_empty() {
            return 1.0;
        }

        let total_fidelity: f64 = self
            .execution_trace
            .iter()
            .map(|step| step.gate_effect.fidelity)
            .sum();

        total_fidelity / self.execution_trace.len() as f64
    }

    /// Calculate entanglement efficiency
    const fn calculate_entanglement_efficiency(&self) -> f64 {
        // Simplified calculation
        0.85 // Placeholder
    }

    /// Calculate state overlap preservation
    const fn calculate_state_overlap_preservation(&self) -> f64 {
        // Simplified calculation
        0.92 // Placeholder
    }

    /// Generate enhanced optimization recommendations using SciRS2 analysis
    fn generate_enhanced_recommendations(&self) -> Result<Vec<String>, QuantRS2Error> {
        let mut recommendations = Vec::new();

        // Standard recommendations
        if self.gate_statistics.two_qubit_gate_ratio() > 0.5 {
            recommendations
                .push("Consider reducing two-qubit gates to improve fidelity".to_string());
        }

        if self.calculate_circuit_depth() > 100 {
            recommendations.push("Circuit depth is high - consider parallelization".to_string());
        }

        // SciRS2-enhanced recommendations based on performance metrics
        if self.performance_metrics.simd_operations_count > 0 {
            recommendations
                .push("SIMD operations detected - good use of vectorization".to_string());
        } else {
            recommendations
                .push("Consider enabling SIMD operations for better performance".to_string());
        }

        if self.performance_metrics.parallel_operations_count > 0 {
            recommendations
                .push("Parallel operations used - optimal for multi-core systems".to_string());
        }

        // Memory usage analysis
        if let Some(max_memory) = self.performance_metrics.memory_usage.iter().max() {
            if *max_memory > 1024 * 1024 * 100 {
                // > 100MB
                recommendations.push(
                    "High memory usage detected - consider SciRS2 memory optimization".to_string(),
                );
            }
        }

        // Performance-based recommendations
        let avg_gate_time = self.performance_metrics.total_execution_time.as_nanos() as f64
            / (self.execution_trace.len() as f64 + 1.0);
        if avg_gate_time > 1000.0 {
            // > 1μs per gate
            recommendations.push("Gate execution time is high - consider optimization".to_string());
        }

        recommendations.push("Apply error mitigation techniques".to_string());
        recommendations.push("Use SciRS2 advanced quantum state management".to_string());

        Ok(recommendations)
    }

    /// Legacy recommendations method (kept for compatibility)
    fn generate_recommendations(&self) -> Result<Vec<String>, QuantRS2Error> {
        self.generate_enhanced_recommendations()
    }
}

/// Debug step information
#[derive(Debug, Clone)]
pub struct DebugStep {
    pub step: usize,
    pub gate: QuantumGate,
    pub gate_effect: GateEffect,
    pub state_before: Option<Vec<Complex64>>,
    pub state_after: Option<Vec<Complex64>>,
    pub entanglement_info: Option<EntanglementAnalysis>,
    pub amplitude_analysis: Option<AmplitudeAnalysis>,
}

/// Gate effect information
#[derive(Debug, Clone)]
pub struct GateEffect {
    pub gate_type: String,
    pub target_qubits: Vec<usize>,
    pub amplitude_changes: Vec<f64>,
    pub phase_changes: Vec<f64>,
    pub entanglement_change: f64,
    pub fidelity: f64,
}

/// Breakpoint configuration
#[derive(Debug, Clone)]
pub struct Breakpoint {
    pub step: usize,
    pub condition: BreakpointCondition,
    pub enabled: bool,
}

/// Breakpoint conditions
#[derive(Debug, Clone)]
pub enum BreakpointCondition {
    Always,
    StateCondition(StateCondition),
    GateType(GateType),
    QubitIndex(usize),
}

/// State-based breakpoint conditions
#[derive(Debug, Clone)]
pub enum StateCondition {
    AmplitudeThreshold {
        qubit: usize,
        threshold: f64,
    },
    EntanglementThreshold {
        threshold: f64,
    },
    PhaseDifference {
        qubit1: usize,
        qubit2: usize,
        threshold: f64,
    },
}

/// State snapshot for debugging
#[derive(Debug, Clone)]
pub struct StateSnapshot {
    pub step: usize,
    pub gate_index: Option<usize>,
    pub state: Vec<Complex64>,
    pub metadata: StateMetadata,
}

/// State metadata
#[derive(Debug, Clone)]
pub struct StateMetadata {
    pub entropy: f64,
    pub purity: f64,
    pub norm: f64,
}

/// Entanglement analysis results
#[derive(Debug, Clone)]
pub struct EntanglementAnalysis {
    pub total_entanglement: f64,
    pub pairwise_entanglement: HashMap<(usize, usize), f64>,
    pub entanglement_spectrum: Vec<f64>,
    pub max_entangled_pair: Option<(usize, usize)>,
}

/// Amplitude analysis results
#[derive(Debug, Clone)]
pub struct AmplitudeAnalysis {
    pub amplitudes: Vec<f64>,
    pub phases: Vec<f64>,
    pub max_amplitude_index: usize,
    pub mean_amplitude: f64,
    pub amplitude_variance: f64,
    pub significant_states: Vec<usize>,
}

/// Gate statistics tracking
#[derive(Debug, Clone)]
pub struct GateStatistics {
    gate_counts: HashMap<String, usize>,
    total_count: usize,
}

impl GateStatistics {
    pub fn new() -> Self {
        Self {
            gate_counts: HashMap::new(),
            total_count: 0,
        }
    }

    pub fn record_gate(&mut self, gate: &QuantumGate) {
        let gate_type = format!("{:?}", gate.gate_type());
        *self.gate_counts.entry(gate_type).or_insert(0) += 1;
        self.total_count += 1;
    }

    pub const fn total_gates(&self) -> usize {
        self.total_count
    }

    pub fn two_qubit_gate_ratio(&self) -> f64 {
        let two_qubit_gates =
            *self.gate_counts.get("CNOT").unwrap_or(&0) + *self.gate_counts.get("CZ").unwrap_or(&0);
        if self.total_count > 0 {
            two_qubit_gates as f64 / self.total_count as f64
        } else {
            0.0
        }
    }
}

/// Debug result
#[derive(Debug, Clone)]
pub struct DebugResult {
    pub status: DebugStatus,
    pub final_state: Vec<Complex64>,
    pub execution_trace: Vec<DebugStep>,
    pub analysis: DebugAnalysis,
}

/// Debug status
#[derive(Debug, Clone)]
pub enum DebugStatus {
    Completed,
    BreakpointHit(usize),
    Error(String),
}

/// Debug analysis results
#[derive(Debug, Clone)]
pub struct DebugAnalysis {
    pub gate_statistics: GateStatistics,
    pub execution_summary: ExecutionSummary,
    pub performance_metrics: PerformanceMetrics,
    pub recommendations: Vec<String>,
}

/// Execution summary
#[derive(Debug, Clone)]
pub struct ExecutionSummary {
    pub total_steps: usize,
    pub total_gates: usize,
    pub circuit_depth: usize,
    pub execution_time: f64,
}

/// Performance metrics
#[derive(Debug, Clone)]
pub struct PerformanceMetrics {
    pub average_gate_fidelity: f64,
    pub entanglement_efficiency: f64,
    pub state_overlap_preservation: f64,
}

#[cfg(test)]
mod tests {
    use super::*;
    use super::{GateType, QuantumGate};

    #[test]
    fn test_debugger_creation() {
        let debugger = QuantumDebugger::new();
        assert!(debugger.config.track_state_vectors);
    }

    #[test]
    fn test_breakpoint_setting() {
        let mut debugger = QuantumDebugger::new();
        debugger.set_breakpoint(5, BreakpointCondition::Always);
        assert_eq!(debugger.breakpoints.len(), 1);
        assert_eq!(debugger.breakpoints[0].step, 5);
    }

    #[test]
    fn test_gate_statistics() {
        let mut stats = GateStatistics::new();
        let gate = QuantumGate::new(GateType::H, vec![0], None);
        stats.record_gate(&gate);
        assert_eq!(stats.total_gates(), 1);
    }

    #[test]
    fn test_simple_circuit_debugging() {
        let mut debugger = QuantumDebugger::new();
        let circuit = vec![QuantumGate::new(GateType::H, vec![0], None)];

        let initial_state = vec![Complex64::new(1.0, 0.0), Complex64::new(0.0, 0.0)];

        let result = debugger
            .debug_circuit(&circuit, &initial_state, 1)
            .expect("debug_circuit should succeed with valid input");
        assert!(matches!(result.status, DebugStatus::Completed));
        assert_eq!(result.execution_trace.len(), 1);
    }

    #[test]
    fn test_amplitude_analysis() {
        let debugger = QuantumDebugger::new();
        let state = vec![
            Complex64::new(std::f64::consts::FRAC_1_SQRT_2, 0.0),
            Complex64::new(std::f64::consts::FRAC_1_SQRT_2, 0.0),
        ];

        let analysis = debugger
            .analyze_amplitudes(&state)
            .expect("analyze_amplitudes should succeed with valid state");
        assert_eq!(analysis.amplitudes.len(), 2);
        assert!((analysis.mean_amplitude - std::f64::consts::FRAC_1_SQRT_2).abs() < 1e-4);
    }
}