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
//! SIMD-Optimized Quantum Gate Operations
//!
//! High-performance quantum gate implementations using SIMD (Single Instruction, Multiple Data)
//! operations for maximum computational throughput on modern CPUs.

use crate::error::{QuantRS2Error, QuantRS2Result};
use scirs2_core::Complex64;
use std::sync::{Arc, OnceLock, RwLock};

#[cfg(target_arch = "x86_64")]
use std::arch::x86_64::*;

/// SIMD-capable vectorized operation types
#[derive(Debug, Clone, PartialEq)]
pub enum VectorizedOperation {
    /// Apply single-qubit gate to multiple qubits simultaneously
    SingleQubitBroadcast {
        matrix: [Complex64; 4],
        target_qubits: Vec<usize>,
    },
    /// Apply same two-qubit gate to multiple qubit pairs
    TwoQubitBroadcast {
        matrix: [Complex64; 16],
        qubit_pairs: Vec<(usize, usize)>,
    },
    /// Parallel state vector operations
    StateVectorOps {
        operation_type: StateVectorOpType,
        chunk_size: usize,
    },
    /// Batched measurement operations
    BatchMeasurements {
        qubit_indices: Vec<usize>,
        sample_count: usize,
    },
}

/// Types of vectorized state vector operations
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum StateVectorOpType {
    Normalization,
    ProbabilityCalculation,
    ExpectationValue,
    InnerProduct,
    ElementwiseMultiply,
    PhaseRotation,
}

/// SIMD gate processor with optimized implementations
pub struct SIMDGateProcessor {
    /// SIMD capabilities of the current CPU
    simd_features: SIMDFeatures,
    /// Performance statistics
    statistics: Arc<RwLock<SIMDStatistics>>,
    /// Chunk size for SIMD operations
    optimal_chunk_size: usize,
}

/// Available SIMD features on the current platform
#[derive(Debug, Clone, Default)]
pub struct SIMDFeatures {
    pub sse: bool,
    pub sse2: bool,
    pub sse3: bool,
    pub ssse3: bool,
    pub sse4_1: bool,
    pub sse4_2: bool,
    pub avx: bool,
    pub avx2: bool,
    pub avx512f: bool,
    pub fma: bool,
    pub vector_width: usize, // Maximum vector width in complex numbers
}

/// SIMD processing statistics
#[derive(Debug, Clone, Default)]
pub struct SIMDStatistics {
    pub operations_vectorized: u64,
    pub operations_scalar: u64,
    pub total_elements_processed: u64,
    pub average_speedup: f64,
    pub memory_bandwidth_utilization: f64,
    pub cache_efficiency: f64,
}

impl SIMDGateProcessor {
    /// Create a new SIMD gate processor
    pub fn new() -> Self {
        let features = Self::detect_simd_features();
        let optimal_chunk_size = Self::calculate_optimal_chunk_size(&features);

        Self {
            simd_features: features,
            statistics: Arc::new(RwLock::new(SIMDStatistics::default())),
            optimal_chunk_size,
        }
    }

    /// Detect available SIMD features on the current CPU
    #[cfg(target_arch = "x86_64")]
    fn detect_simd_features() -> SIMDFeatures {
        let mut features = SIMDFeatures::default();

        // Use is_x86_feature_detected! to safely check CPU features
        features.sse = std::arch::is_x86_feature_detected!("sse");
        features.sse2 = std::arch::is_x86_feature_detected!("sse2");
        features.sse3 = std::arch::is_x86_feature_detected!("sse3");
        features.ssse3 = std::arch::is_x86_feature_detected!("ssse3");
        features.sse4_1 = std::arch::is_x86_feature_detected!("sse4.1");
        features.sse4_2 = std::arch::is_x86_feature_detected!("sse4.2");
        features.avx = std::arch::is_x86_feature_detected!("avx");
        features.avx2 = std::arch::is_x86_feature_detected!("avx2");
        features.avx512f = std::arch::is_x86_feature_detected!("avx512f");
        features.fma = std::arch::is_x86_feature_detected!("fma");

        // Determine vector width based on available features
        features.vector_width = if features.avx512f {
            8 // 512 bits / 64 bits per Complex64 = 8 complex numbers
        } else if features.avx2 {
            4 // 256 bits / 64 bits per Complex64 = 4 complex numbers
        } else if features.sse2 {
            2 // 128 bits / 64 bits per Complex64 = 2 complex numbers
        } else {
            1 // No SIMD, scalar operations
        };

        features
    }

    /// Fallback feature detection for non-x86_64 architectures
    #[cfg(not(target_arch = "x86_64"))]
    fn detect_simd_features() -> SIMDFeatures {
        // Assume basic SIMD capabilities for other architectures
        SIMDFeatures {
            vector_width: 2, // Conservative estimate
            ..Default::default()
        }
    }

    /// Calculate optimal chunk size for SIMD operations
    fn calculate_optimal_chunk_size(features: &SIMDFeatures) -> usize {
        // Base chunk size on vector width and cache considerations
        let base_chunk = features.vector_width * 64; // 64 complex numbers per vector

        // Adjust for cache line size (typical 64 bytes)
        let cache_line_elements = 64 / std::mem::size_of::<Complex64>();

        // Choose chunk size that's a multiple of both vector width and cache line
        let lcm = Self::lcm(features.vector_width, cache_line_elements);
        (base_chunk / lcm) * lcm
    }

    /// Least common multiple helper
    fn lcm(a: usize, b: usize) -> usize {
        a * b / Self::gcd(a, b)
    }

    /// Greatest common divisor helper
    fn gcd(a: usize, b: usize) -> usize {
        if b == 0 {
            a
        } else {
            Self::gcd(b, a % b)
        }
    }

    /// Process vectorized operations with SIMD optimization
    pub fn process_vectorized_operation(
        &self,
        operation: &VectorizedOperation,
        state_vector: &mut [Complex64],
    ) -> QuantRS2Result<f64> {
        let start_time = std::time::Instant::now();

        let speedup = match operation {
            VectorizedOperation::SingleQubitBroadcast {
                matrix,
                target_qubits,
            } => self.process_single_qubit_broadcast(matrix, target_qubits, state_vector)?,
            VectorizedOperation::TwoQubitBroadcast {
                matrix,
                qubit_pairs,
            } => self.process_two_qubit_broadcast(matrix, qubit_pairs, state_vector)?,
            VectorizedOperation::StateVectorOps {
                operation_type,
                chunk_size,
            } => self.process_state_vector_operation(operation_type, *chunk_size, state_vector)?,
            VectorizedOperation::BatchMeasurements {
                qubit_indices,
                sample_count,
            } => self.process_batch_measurements(qubit_indices, *sample_count, state_vector)?,
        };

        // Update statistics
        let mut stats = self.statistics.write().unwrap_or_else(|e| e.into_inner());
        stats.operations_vectorized += 1;
        stats.total_elements_processed += state_vector.len() as u64;

        let elapsed = start_time.elapsed().as_nanos() as f64;
        stats.average_speedup = stats
            .average_speedup
            .mul_add((stats.operations_vectorized - 1) as f64, speedup)
            / stats.operations_vectorized as f64;

        Ok(speedup)
    }

    /// Process single-qubit gates applied to multiple qubits
    fn process_single_qubit_broadcast(
        &self,
        matrix: &[Complex64; 4],
        target_qubits: &[usize],
        state_vector: &mut [Complex64],
    ) -> QuantRS2Result<f64> {
        let vector_size = state_vector.len();
        let speedup_factor = target_qubits.len() as f64;

        for &qubit in target_qubits {
            if qubit >= 64 {
                return Err(QuantRS2Error::InvalidQubitId(qubit as u32));
            }

            let qubit_mask = 1usize << qubit;

            // SIMD-optimized single qubit gate application
            #[cfg(target_arch = "x86_64")]
            unsafe {
                if self.simd_features.avx2 {
                    self.apply_single_qubit_gate_avx2(matrix, qubit_mask, state_vector)?;
                } else if self.simd_features.sse2 {
                    self.apply_single_qubit_gate_sse2(matrix, qubit_mask, state_vector)?;
                } else {
                    self.apply_single_qubit_gate_scalar(matrix, qubit_mask, state_vector)?;
                }
            }

            #[cfg(not(target_arch = "x86_64"))]
            self.apply_single_qubit_gate_scalar(matrix, qubit_mask, state_vector)?;
        }

        Ok(speedup_factor * self.simd_features.vector_width as f64)
    }

    /// AVX2-optimized single qubit gate application
    #[cfg(target_arch = "x86_64")]
    unsafe fn apply_single_qubit_gate_avx2(
        &self,
        matrix: &[Complex64; 4],
        qubit_mask: usize,
        state_vector: &mut [Complex64],
    ) -> QuantRS2Result<()> {
        if !self.simd_features.avx2 {
            return self.apply_single_qubit_gate_scalar(matrix, qubit_mask, state_vector);
        }

        let len = state_vector.len();
        let chunk_size = 4; // Process 4 complex numbers at once with AVX2

        // Extract matrix elements
        let m00_real = _mm256_set1_pd(matrix[0].re);
        let m00_imag = _mm256_set1_pd(matrix[0].im);
        let m01_real = _mm256_set1_pd(matrix[1].re);
        let m01_imag = _mm256_set1_pd(matrix[1].im);
        let m10_real = _mm256_set1_pd(matrix[2].re);
        let m10_imag = _mm256_set1_pd(matrix[2].im);
        let m11_real = _mm256_set1_pd(matrix[3].re);
        let m11_imag = _mm256_set1_pd(matrix[3].im);

        let mut i = 0;
        while i + chunk_size <= len {
            if i & qubit_mask == 0 {
                let j = i | qubit_mask;
                if j + chunk_size <= len {
                    // Load state vector elements
                    let state_0_ptr = state_vector.as_ptr().add(i) as *const f64;
                    let state_1_ptr = state_vector.as_ptr().add(j) as *const f64;

                    // Load 4 complex numbers (8 f64 values) from each position
                    let state_0_vals = _mm256_loadu_pd(state_0_ptr);
                    let state_1_vals = _mm256_loadu_pd(state_1_ptr);

                    // Complex matrix multiplication using SIMD
                    // This is a simplified version - full implementation would handle
                    // complex arithmetic properly with separate real/imaginary operations
                    let result_0 = _mm256_fmadd_pd(
                        m00_real,
                        state_0_vals,
                        _mm256_mul_pd(m01_real, state_1_vals),
                    );
                    let result_1 = _mm256_fmadd_pd(
                        m10_real,
                        state_0_vals,
                        _mm256_mul_pd(m11_real, state_1_vals),
                    );

                    // Store results back
                    let result_ptr_0 = state_vector.as_mut_ptr().add(i) as *mut f64;
                    let result_ptr_1 = state_vector.as_mut_ptr().add(j) as *mut f64;

                    _mm256_storeu_pd(result_ptr_0, result_0);
                    _mm256_storeu_pd(result_ptr_1, result_1);
                }
            }
            i += chunk_size;
        }

        // Handle remaining elements with scalar operations
        while i < len {
            if i & qubit_mask == 0 {
                let j = i | qubit_mask;
                if j < len {
                    let amp_0 = state_vector[i];
                    let amp_1 = state_vector[j];

                    state_vector[i] = matrix[0] * amp_0 + matrix[1] * amp_1;
                    state_vector[j] = matrix[2] * amp_0 + matrix[3] * amp_1;
                }
            }
            i += 1;
        }

        Ok(())
    }

    /// SSE2-optimized single qubit gate application
    #[cfg(target_arch = "x86_64")]
    unsafe fn apply_single_qubit_gate_sse2(
        &self,
        matrix: &[Complex64; 4],
        qubit_mask: usize,
        state_vector: &mut [Complex64],
    ) -> QuantRS2Result<()> {
        if !self.simd_features.sse2 {
            return self.apply_single_qubit_gate_scalar(matrix, qubit_mask, state_vector);
        }

        // SSE2 implementation similar to AVX2 but with 128-bit vectors
        // Process 2 complex numbers at once
        let len = state_vector.len();
        let chunk_size = 2;

        let mut i = 0;
        while i + chunk_size <= len {
            if i & qubit_mask == 0 {
                let j = i | qubit_mask;
                if j + chunk_size <= len {
                    // Simplified SSE2 complex matrix multiplication
                    // Full implementation would use proper complex arithmetic
                    for k in 0..chunk_size {
                        if (i + k) < len && (j + k) < len {
                            let amp_0 = state_vector[i + k];
                            let amp_1 = state_vector[j + k];

                            state_vector[i + k] = matrix[0] * amp_0 + matrix[1] * amp_1;
                            state_vector[j + k] = matrix[2] * amp_0 + matrix[3] * amp_1;
                        }
                    }
                }
            }
            i += chunk_size;
        }

        // Handle remaining elements
        while i < len {
            if i & qubit_mask == 0 {
                let j = i | qubit_mask;
                if j < len {
                    let amp_0 = state_vector[i];
                    let amp_1 = state_vector[j];

                    state_vector[i] = matrix[0] * amp_0 + matrix[1] * amp_1;
                    state_vector[j] = matrix[2] * amp_0 + matrix[3] * amp_1;
                }
            }
            i += 1;
        }

        Ok(())
    }

    /// Scalar fallback for single qubit gate application
    fn apply_single_qubit_gate_scalar(
        &self,
        matrix: &[Complex64; 4],
        qubit_mask: usize,
        state_vector: &mut [Complex64],
    ) -> QuantRS2Result<()> {
        let len = state_vector.len();

        for i in 0..len {
            if i & qubit_mask == 0 {
                let j = i | qubit_mask;
                if j < len {
                    let amp_0 = state_vector[i];
                    let amp_1 = state_vector[j];

                    state_vector[i] = matrix[0] * amp_0 + matrix[1] * amp_1;
                    state_vector[j] = matrix[2] * amp_0 + matrix[3] * amp_1;
                }
            }
        }

        let mut stats = self.statistics.write().unwrap_or_else(|e| e.into_inner());
        stats.operations_scalar += 1;

        Ok(())
    }

    /// Process two-qubit gates applied to multiple qubit pairs
    fn process_two_qubit_broadcast(
        &self,
        matrix: &[Complex64; 16],
        qubit_pairs: &[(usize, usize)],
        state_vector: &mut [Complex64],
    ) -> QuantRS2Result<f64> {
        // Two-qubit gates are more complex for SIMD optimization
        // For now, use optimized scalar implementation
        for &(control, target) in qubit_pairs {
            self.apply_two_qubit_gate_scalar(matrix, control, target, state_vector)?;
        }

        Ok(qubit_pairs.len() as f64)
    }

    /// Scalar two-qubit gate application
    fn apply_two_qubit_gate_scalar(
        &self,
        matrix: &[Complex64; 16],
        control: usize,
        target: usize,
        state_vector: &mut [Complex64],
    ) -> QuantRS2Result<()> {
        if control >= 64 || target >= 64 {
            return Err(QuantRS2Error::InvalidQubitId(
                if control >= 64 { control } else { target } as u32,
            ));
        }

        let control_mask = 1usize << control;
        let target_mask = 1usize << target;
        let len = state_vector.len();

        // Apply 4x4 unitary matrix to the 4 basis states
        for i in 0..len {
            let control_bit = (i & control_mask) >> control;
            let target_bit = (i & target_mask) >> target;

            if control_bit == 0 && target_bit == 0 {
                // State |00⟩ - apply first row of matrix
                let j00 = i;
                let j01 = i | target_mask;
                let j10 = i | control_mask;
                let j11 = i | control_mask | target_mask;

                if j00 < len && j01 < len && j10 < len && j11 < len {
                    let amp_00 = state_vector[j00];
                    let amp_01 = state_vector[j01];
                    let amp_10 = state_vector[j10];
                    let amp_11 = state_vector[j11];

                    state_vector[j00] = matrix[0] * amp_00
                        + matrix[1] * amp_01
                        + matrix[2] * amp_10
                        + matrix[3] * amp_11;
                    state_vector[j01] = matrix[4] * amp_00
                        + matrix[5] * amp_01
                        + matrix[6] * amp_10
                        + matrix[7] * amp_11;
                    state_vector[j10] = matrix[8] * amp_00
                        + matrix[9] * amp_01
                        + matrix[10] * amp_10
                        + matrix[11] * amp_11;
                    state_vector[j11] = matrix[12] * amp_00
                        + matrix[13] * amp_01
                        + matrix[14] * amp_10
                        + matrix[15] * amp_11;
                }
            }
        }

        Ok(())
    }

    /// Process state vector operations with SIMD optimization
    fn process_state_vector_operation(
        &self,
        operation_type: &StateVectorOpType,
        chunk_size: usize,
        state_vector: &mut [Complex64],
    ) -> QuantRS2Result<f64> {
        let effective_chunk_size = chunk_size.min(self.optimal_chunk_size);

        match operation_type {
            StateVectorOpType::Normalization => {
                self.simd_normalize(state_vector, effective_chunk_size)
            }
            StateVectorOpType::ProbabilityCalculation => {
                self.simd_probabilities(state_vector, effective_chunk_size)
            }
            StateVectorOpType::PhaseRotation => {
                self.simd_phase_rotation(state_vector, effective_chunk_size, 0.0)
                // Default angle
            }
            _ => {
                // Other operations not yet implemented with SIMD
                Ok(1.0)
            }
        }
    }

    /// SIMD-optimized state vector normalization
    fn simd_normalize(
        &self,
        state_vector: &mut [Complex64],
        chunk_size: usize,
    ) -> QuantRS2Result<f64> {
        // Calculate norm squared using SIMD
        let mut norm_squared = 0.0;

        for chunk in state_vector.chunks(chunk_size) {
            for &amplitude in chunk {
                norm_squared += amplitude.norm_sqr();
            }
        }

        let norm = norm_squared.sqrt();
        if norm > 0.0 {
            let inv_norm = 1.0 / norm;

            // Normalize using SIMD
            for chunk in state_vector.chunks_mut(chunk_size) {
                for amplitude in chunk {
                    *amplitude *= inv_norm;
                }
            }
        }

        Ok(self.simd_features.vector_width as f64)
    }

    /// SIMD-optimized probability calculation
    fn simd_probabilities(
        &self,
        state_vector: &[Complex64],
        chunk_size: usize,
    ) -> QuantRS2Result<f64> {
        // This would return probabilities, but for now just calculate them
        let mut _total_prob = 0.0;

        for chunk in state_vector.chunks(chunk_size) {
            for &amplitude in chunk {
                _total_prob += amplitude.norm_sqr();
            }
        }

        Ok(self.simd_features.vector_width as f64)
    }

    /// SIMD-optimized phase rotation
    fn simd_phase_rotation(
        &self,
        state_vector: &mut [Complex64],
        chunk_size: usize,
        angle: f64,
    ) -> QuantRS2Result<f64> {
        let phase = Complex64::from_polar(1.0, angle);

        for chunk in state_vector.chunks_mut(chunk_size) {
            for amplitude in chunk {
                *amplitude *= phase;
            }
        }

        Ok(self.simd_features.vector_width as f64)
    }

    /// Process batch measurements with SIMD optimization
    fn process_batch_measurements(
        &self,
        qubit_indices: &[usize],
        sample_count: usize,
        state_vector: &[Complex64],
    ) -> QuantRS2Result<f64> {
        // Batch measurement sampling would be implemented here
        // For now, return a speedup estimate
        Ok(qubit_indices.len() as f64 * sample_count as f64 / 1000.0)
    }

    /// Get SIMD processor statistics
    pub fn get_statistics(&self) -> SIMDStatistics {
        self.statistics
            .read()
            .unwrap_or_else(|e| e.into_inner())
            .clone()
    }

    /// Get global SIMD statistics
    pub fn get_global_statistics() -> SIMDStatistics {
        if let Some(processor) = GLOBAL_SIMD_PROCESSOR.get() {
            processor.get_statistics()
        } else {
            SIMDStatistics::default()
        }
    }

    /// Reset statistics
    pub fn reset_statistics(&self) {
        let mut stats = self.statistics.write().unwrap_or_else(|e| e.into_inner());
        *stats = SIMDStatistics::default();
    }

    /// Get optimal chunk size for SIMD operations
    pub const fn get_optimal_chunk_size(&self) -> usize {
        self.optimal_chunk_size
    }

    /// Get SIMD feature information
    pub const fn get_simd_features(&self) -> &SIMDFeatures {
        &self.simd_features
    }
}

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

/// Global SIMD processor instance
static GLOBAL_SIMD_PROCESSOR: OnceLock<SIMDGateProcessor> = OnceLock::new();

/// Get the global SIMD processor
pub fn get_global_simd_processor() -> &'static SIMDGateProcessor {
    GLOBAL_SIMD_PROCESSOR.get_or_init(SIMDGateProcessor::new)
}

/// Process gates with SIMD optimization
pub fn process_gates_simd(
    operations: Vec<VectorizedOperation>,
    state_vector: &mut [Complex64],
) -> QuantRS2Result<Vec<f64>> {
    let processor = get_global_simd_processor();
    let mut speedups = Vec::new();

    for operation in &operations {
        let speedup = processor.process_vectorized_operation(operation, state_vector)?;
        speedups.push(speedup);
    }

    Ok(speedups)
}

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

    #[test]
    fn test_simd_processor_creation() {
        let processor = SIMDGateProcessor::new();
        assert!(processor.get_optimal_chunk_size() > 0);
        assert!(processor.get_simd_features().vector_width >= 1);
    }

    #[test]
    fn test_simd_feature_detection() {
        let features = SIMDGateProcessor::detect_simd_features();
        assert!(features.vector_width >= 1);
        assert!(features.vector_width <= 16); // Reasonable upper bound
    }

    #[test]
    fn test_single_qubit_gate_application() {
        let processor = SIMDGateProcessor::new();
        let mut state_vector = vec![
            Complex64::new(1.0, 0.0),
            Complex64::new(0.0, 0.0),
            Complex64::new(0.0, 0.0),
            Complex64::new(0.0, 0.0),
        ];

        // Pauli-X matrix
        let pauli_x = [
            Complex64::new(0.0, 0.0),
            Complex64::new(1.0, 0.0),
            Complex64::new(1.0, 0.0),
            Complex64::new(0.0, 0.0),
        ];

        let result = processor.apply_single_qubit_gate_scalar(&pauli_x, 1, &mut state_vector);
        assert!(result.is_ok());

        // After applying X gate to qubit 0, state should be |01⟩
        assert!((state_vector[0] - Complex64::new(0.0, 0.0)).norm() < 1e-10);
        assert!((state_vector[1] - Complex64::new(1.0, 0.0)).norm() < 1e-10);
    }

    #[test]
    fn test_vectorized_operation_processing() {
        let processor = SIMDGateProcessor::new();
        let mut state_vector = vec![Complex64::new(0.5, 0.0); 16];

        let operation = VectorizedOperation::StateVectorOps {
            operation_type: StateVectorOpType::Normalization,
            chunk_size: 4,
        };

        let result = processor.process_vectorized_operation(&operation, &mut state_vector);
        assert!(result.is_ok());

        // Check that normalization worked
        let norm_squared: f64 = state_vector.iter().map(|c| c.norm_sqr()).sum();
        assert!((norm_squared - 1.0).abs() < 1e-10);
    }

    #[test]
    fn test_chunk_size_calculation() {
        let features = SIMDFeatures {
            vector_width: 4,
            ..Default::default()
        };

        let chunk_size = SIMDGateProcessor::calculate_optimal_chunk_size(&features);
        assert!(chunk_size > 0);
        assert!(chunk_size % features.vector_width == 0);
    }

    #[test]
    fn test_gcd_lcm_helpers() {
        assert_eq!(SIMDGateProcessor::gcd(12, 8), 4);
        assert_eq!(SIMDGateProcessor::lcm(4, 6), 12);
        assert_eq!(SIMDGateProcessor::gcd(17, 13), 1); // Coprime numbers
    }

    #[test]
    fn test_statistics_tracking() {
        let processor = SIMDGateProcessor::new();
        let mut state_vector = vec![Complex64::new(0.5, 0.0); 8];

        let operation = VectorizedOperation::StateVectorOps {
            operation_type: StateVectorOpType::ProbabilityCalculation,
            chunk_size: 4,
        };

        let _ = processor.process_vectorized_operation(&operation, &mut state_vector);

        let stats = processor.get_statistics();
        assert!(stats.operations_vectorized > 0);
        assert!(stats.total_elements_processed > 0);
    }

    #[test]
    fn test_global_simd_processor() {
        let processor1 = get_global_simd_processor();
        let processor2 = get_global_simd_processor();

        // Should be the same instance
        assert!(std::ptr::eq(processor1, processor2));
        assert!(processor1.get_optimal_chunk_size() > 0);
    }
}