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
//! Enhanced SIMD operations for quantum computing
//!
//! This module provides highly optimized SIMD implementations for quantum gates
//! with adaptive dispatch based on hardware capabilities.

use crate::error::{QuantRS2Error, QuantRS2Result};
use crate::platform::PlatformCapabilities;
use crate::simd_ops_stubs::{SimdComplex64, SimdF64};
use scirs2_core::ndarray::{Array1, Array2, ArrayView1, ArrayView2, ArrayViewMut1};
use scirs2_core::Complex64;

/// Enhanced SIMD gate operations with hardware-adaptive dispatch
pub struct SimdGateEngine {
    /// Platform capabilities for adaptive optimization
    capabilities: PlatformCapabilities,
    /// Optimal SIMD width for f64 operations
    simd_width: usize,
    /// Cache line size for alignment
    cache_line_size: usize,
}

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

impl SimdGateEngine {
    /// Create a new SIMD gate engine with automatic hardware detection
    pub fn new() -> Self {
        let capabilities = PlatformCapabilities::detect();
        let simd_width = capabilities.optimal_simd_width_f64();
        let cache_line_size = capabilities.cpu.cache.line_size.unwrap_or(64);

        Self {
            capabilities,
            simd_width,
            cache_line_size,
        }
    }

    /// Get the platform capabilities
    pub const fn capabilities(&self) -> &PlatformCapabilities {
        &self.capabilities
    }

    /// Get the optimal SIMD width
    pub const fn simd_width(&self) -> usize {
        self.simd_width
    }

    /// Apply a rotation gate (RX, RY, RZ) with SIMD optimization
    ///
    /// This applies rotation matrices to pairs of amplitudes efficiently.
    pub fn apply_rotation_gate(
        &self,
        amplitudes: &mut [Complex64],
        qubit: usize,
        axis: RotationAxis,
        angle: f64,
    ) -> QuantRS2Result<()> {
        let num_qubits = (amplitudes.len() as f64).log2() as usize;
        if qubit >= num_qubits {
            return Err(QuantRS2Error::InvalidInput(
                "Qubit index out of range".to_string(),
            ));
        }

        match axis {
            RotationAxis::X => self.apply_rx(amplitudes, qubit, angle),
            RotationAxis::Y => self.apply_ry(amplitudes, qubit, angle),
            RotationAxis::Z => self.apply_rz(amplitudes, qubit, angle),
        }
    }

    /// Apply RX gate: RX(θ) = exp(-iθX/2) = cos(θ/2)I - i*sin(θ/2)X
    fn apply_rx(
        &self,
        amplitudes: &mut [Complex64],
        qubit: usize,
        angle: f64,
    ) -> QuantRS2Result<()> {
        let half_angle = angle / 2.0;
        let cos_half = half_angle.cos();
        let sin_half = half_angle.sin();

        let qubit_mask = 1 << qubit;
        let mut idx0_list = Vec::new();
        let mut idx1_list = Vec::new();

        // Collect index pairs
        for i in 0..(amplitudes.len() / 2) {
            let idx0 = (i & !(qubit_mask >> 1)) | ((i & (qubit_mask >> 1)) << 1);
            let idx1 = idx0 | qubit_mask;

            if idx1 < amplitudes.len() {
                idx0_list.push(idx0);
                idx1_list.push(idx1);
            }
        }

        let pair_count = idx0_list.len();
        if pair_count == 0 {
            return Ok(());
        }

        // Extract amplitude pairs
        let mut a0_real = Vec::with_capacity(pair_count);
        let mut a0_imag = Vec::with_capacity(pair_count);
        let mut a1_real = Vec::with_capacity(pair_count);
        let mut a1_imag = Vec::with_capacity(pair_count);

        for i in 0..pair_count {
            let a0 = amplitudes[idx0_list[i]];
            let a1 = amplitudes[idx1_list[i]];
            a0_real.push(a0.re);
            a0_imag.push(a0.im);
            a1_real.push(a1.re);
            a1_imag.push(a1.im);
        }

        // Apply RX matrix using SIMD
        let a0_real_view = ArrayView1::from(&a0_real);
        let a0_imag_view = ArrayView1::from(&a0_imag);
        let a1_real_view = ArrayView1::from(&a1_real);
        let a1_imag_view = ArrayView1::from(&a1_imag);

        // RX matrix: [[cos, -i*sin], [-i*sin, cos]]
        // New a0 = cos*a0 - i*sin*a1 = (cos*a0_r, cos*a0_i) + (sin*a1_i, -sin*a1_r)
        // New a1 = -i*sin*a0 + cos*a1 = (sin*a0_i, -sin*a0_r) + (cos*a1_r, cos*a1_i)

        let cos_a0_r = <f64 as SimdF64>::simd_scalar_mul(&a0_real_view, cos_half);
        let cos_a0_i = <f64 as SimdF64>::simd_scalar_mul(&a0_imag_view, cos_half);
        let sin_a1_i = <f64 as SimdF64>::simd_scalar_mul(&a1_imag_view, sin_half);
        let sin_a1_r = <f64 as SimdF64>::simd_scalar_mul(&a1_real_view, sin_half);

        let new_a0_r = <f64 as SimdF64>::simd_add_arrays(&cos_a0_r.view(), &sin_a1_i.view());
        let new_a0_i = <f64 as SimdF64>::simd_sub_arrays(&cos_a0_i.view(), &sin_a1_r.view());

        let sin_a0_i = <f64 as SimdF64>::simd_scalar_mul(&a0_imag_view, sin_half);
        let sin_a0_r = <f64 as SimdF64>::simd_scalar_mul(&a0_real_view, sin_half);
        let cos_a1_r = <f64 as SimdF64>::simd_scalar_mul(&a1_real_view, cos_half);
        let cos_a1_i = <f64 as SimdF64>::simd_scalar_mul(&a1_imag_view, cos_half);

        let new_a1_r = <f64 as SimdF64>::simd_add_arrays(&sin_a0_i.view(), &cos_a1_r.view());
        let new_a1_i = <f64 as SimdF64>::simd_sub_arrays(&cos_a1_i.view(), &sin_a0_r.view());

        // Write back results
        for i in 0..pair_count {
            amplitudes[idx0_list[i]] = Complex64::new(new_a0_r[i], new_a0_i[i]);
            amplitudes[idx1_list[i]] = Complex64::new(new_a1_r[i], new_a1_i[i]);
        }

        Ok(())
    }

    /// Apply RY gate: RY(θ) = exp(-iθY/2) = cos(θ/2)I - i*sin(θ/2)Y
    fn apply_ry(
        &self,
        amplitudes: &mut [Complex64],
        qubit: usize,
        angle: f64,
    ) -> QuantRS2Result<()> {
        let half_angle = angle / 2.0;
        let cos_half = half_angle.cos();
        let sin_half = half_angle.sin();

        let qubit_mask = 1 << qubit;
        let mut idx0_list = Vec::new();
        let mut idx1_list = Vec::new();

        // Collect index pairs
        for i in 0..(amplitudes.len() / 2) {
            let idx0 = (i & !(qubit_mask >> 1)) | ((i & (qubit_mask >> 1)) << 1);
            let idx1 = idx0 | qubit_mask;

            if idx1 < amplitudes.len() {
                idx0_list.push(idx0);
                idx1_list.push(idx1);
            }
        }

        let pair_count = idx0_list.len();
        if pair_count == 0 {
            return Ok(());
        }

        // Extract amplitude pairs
        let mut a0_real = Vec::with_capacity(pair_count);
        let mut a0_imag = Vec::with_capacity(pair_count);
        let mut a1_real = Vec::with_capacity(pair_count);
        let mut a1_imag = Vec::with_capacity(pair_count);

        for i in 0..pair_count {
            let a0 = amplitudes[idx0_list[i]];
            let a1 = amplitudes[idx1_list[i]];
            a0_real.push(a0.re);
            a0_imag.push(a0.im);
            a1_real.push(a1.re);
            a1_imag.push(a1.im);
        }

        // Apply RY matrix using SIMD
        let a0_real_view = ArrayView1::from(&a0_real);
        let a0_imag_view = ArrayView1::from(&a0_imag);
        let a1_real_view = ArrayView1::from(&a1_real);
        let a1_imag_view = ArrayView1::from(&a1_imag);

        // RY matrix: [[cos, -sin], [sin, cos]]
        let cos_a0_r = <f64 as SimdF64>::simd_scalar_mul(&a0_real_view, cos_half);
        let cos_a0_i = <f64 as SimdF64>::simd_scalar_mul(&a0_imag_view, cos_half);
        let sin_a1_r = <f64 as SimdF64>::simd_scalar_mul(&a1_real_view, sin_half);
        let sin_a1_i = <f64 as SimdF64>::simd_scalar_mul(&a1_imag_view, sin_half);

        let new_a0_r = <f64 as SimdF64>::simd_sub_arrays(&cos_a0_r.view(), &sin_a1_r.view());
        let new_a0_i = <f64 as SimdF64>::simd_sub_arrays(&cos_a0_i.view(), &sin_a1_i.view());

        let sin_a0_r = <f64 as SimdF64>::simd_scalar_mul(&a0_real_view, sin_half);
        let sin_a0_i = <f64 as SimdF64>::simd_scalar_mul(&a0_imag_view, sin_half);
        let cos_a1_r = <f64 as SimdF64>::simd_scalar_mul(&a1_real_view, cos_half);
        let cos_a1_i = <f64 as SimdF64>::simd_scalar_mul(&a1_imag_view, cos_half);

        let new_a1_r = <f64 as SimdF64>::simd_add_arrays(&sin_a0_r.view(), &cos_a1_r.view());
        let new_a1_i = <f64 as SimdF64>::simd_add_arrays(&sin_a0_i.view(), &cos_a1_i.view());

        // Write back results
        for i in 0..pair_count {
            amplitudes[idx0_list[i]] = Complex64::new(new_a0_r[i], new_a0_i[i]);
            amplitudes[idx1_list[i]] = Complex64::new(new_a1_r[i], new_a1_i[i]);
        }

        Ok(())
    }

    /// Apply RZ gate: RZ(θ) = exp(-iθZ/2) = [[e^(-iθ/2), 0], [0, e^(iθ/2)]]
    fn apply_rz(
        &self,
        amplitudes: &mut [Complex64],
        qubit: usize,
        angle: f64,
    ) -> QuantRS2Result<()> {
        let half_angle = angle / 2.0;
        let cos_half = half_angle.cos();
        let sin_half = half_angle.sin();

        let qubit_mask = 1 << qubit;

        // Collect indices for each computational basis state
        let mut idx0_list = Vec::new(); // Qubit is |0⟩
        let mut idx1_list = Vec::new(); // Qubit is |1⟩

        for i in 0..amplitudes.len() {
            if (i & qubit_mask) == 0 {
                idx0_list.push(i);
            } else {
                idx1_list.push(i);
            }
        }

        // Apply phase e^(-iθ/2) to |0⟩ states
        if !idx0_list.is_empty() {
            let mut real_parts = Vec::with_capacity(idx0_list.len());
            let mut imag_parts = Vec::with_capacity(idx0_list.len());

            for &idx in &idx0_list {
                real_parts.push(amplitudes[idx].re);
                imag_parts.push(amplitudes[idx].im);
            }

            let real_view = ArrayView1::from(&real_parts);
            let imag_view = ArrayView1::from(&imag_parts);

            // Multiply by e^(-iθ/2) = cos(-θ/2) + i*sin(-θ/2) = cos(θ/2) - i*sin(θ/2)
            let real_cos = <f64 as SimdF64>::simd_scalar_mul(&real_view, cos_half);
            let imag_sin = <f64 as SimdF64>::simd_scalar_mul(&imag_view, sin_half);
            let new_real = <f64 as SimdF64>::simd_add_arrays(&real_cos.view(), &imag_sin.view());

            let real_sin = <f64 as SimdF64>::simd_scalar_mul(&real_view, -sin_half);
            let imag_cos = <f64 as SimdF64>::simd_scalar_mul(&imag_view, cos_half);
            let new_imag = <f64 as SimdF64>::simd_add_arrays(&real_sin.view(), &imag_cos.view());

            for (i, &idx) in idx0_list.iter().enumerate() {
                amplitudes[idx] = Complex64::new(new_real[i], new_imag[i]);
            }
        }

        // Apply phase e^(iθ/2) to |1⟩ states
        if !idx1_list.is_empty() {
            let mut real_parts = Vec::with_capacity(idx1_list.len());
            let mut imag_parts = Vec::with_capacity(idx1_list.len());

            for &idx in &idx1_list {
                real_parts.push(amplitudes[idx].re);
                imag_parts.push(amplitudes[idx].im);
            }

            let real_view = ArrayView1::from(&real_parts);
            let imag_view = ArrayView1::from(&imag_parts);

            // Multiply by e^(iθ/2) = cos(θ/2) + i*sin(θ/2)
            let real_cos = <f64 as SimdF64>::simd_scalar_mul(&real_view, cos_half);
            let imag_sin = <f64 as SimdF64>::simd_scalar_mul(&imag_view, sin_half);
            let new_real = <f64 as SimdF64>::simd_sub_arrays(&real_cos.view(), &imag_sin.view());

            let real_sin = <f64 as SimdF64>::simd_scalar_mul(&real_view, sin_half);
            let imag_cos = <f64 as SimdF64>::simd_scalar_mul(&imag_view, cos_half);
            let new_imag = <f64 as SimdF64>::simd_add_arrays(&real_sin.view(), &imag_cos.view());

            for (i, &idx) in idx1_list.iter().enumerate() {
                amplitudes[idx] = Complex64::new(new_real[i], new_imag[i]);
            }
        }

        Ok(())
    }

    /// Apply CNOT gate with SIMD optimization
    pub fn apply_cnot(
        &self,
        amplitudes: &mut [Complex64],
        control: usize,
        target: usize,
    ) -> QuantRS2Result<()> {
        let num_qubits = (amplitudes.len() as f64).log2() as usize;
        if control >= num_qubits || target >= num_qubits {
            return Err(QuantRS2Error::InvalidInput(
                "Qubit index out of range".to_string(),
            ));
        }

        if control == target {
            return Err(QuantRS2Error::InvalidInput(
                "Control and target must be different qubits".to_string(),
            ));
        }

        let control_mask = 1 << control;
        let target_mask = 1 << target;

        // Collect index pairs that need to be swapped
        let mut idx0_list = Vec::new();
        let mut idx1_list = Vec::new();

        // Iterate through all amplitudes
        // For CNOT: if control qubit is 1, flip target qubit
        for i in 0..amplitudes.len() {
            // Check if control bit is 1
            if (i & control_mask) != 0 {
                // Check if target bit is 0 (we only count each pair once)
                if (i & target_mask) == 0 {
                    let idx0 = i; // Target is currently 0
                    let idx1 = i ^ target_mask; // Flip target to 1
                    idx0_list.push(idx0);
                    idx1_list.push(idx1);
                }
            }
        }

        // Swap amplitude pairs using SIMD
        let pair_count = idx0_list.len();
        if pair_count == 0 {
            return Ok(());
        }

        // Simple swap without SIMD overhead for small counts
        if pair_count < 4 {
            for i in 0..pair_count {
                amplitudes.swap(idx0_list[i], idx1_list[i]);
            }
            return Ok(());
        }

        // Extract amplitudes for SIMD swap
        let mut a0_real = Vec::with_capacity(pair_count);
        let mut a0_imag = Vec::with_capacity(pair_count);
        let mut a1_real = Vec::with_capacity(pair_count);
        let mut a1_imag = Vec::with_capacity(pair_count);

        for i in 0..pair_count {
            let a0 = amplitudes[idx0_list[i]];
            let a1 = amplitudes[idx1_list[i]];
            a0_real.push(a0.re);
            a0_imag.push(a0.im);
            a1_real.push(a1.re);
            a1_imag.push(a1.im);
        }

        // Write back swapped (no computation needed, just assignment)
        for i in 0..pair_count {
            amplitudes[idx0_list[i]] = Complex64::new(a1_real[i], a1_imag[i]);
            amplitudes[idx1_list[i]] = Complex64::new(a0_real[i], a0_imag[i]);
        }

        Ok(())
    }

    /// Batch apply multiple single-qubit gates
    ///
    /// This is more efficient than applying gates one by one when multiple
    /// gates need to be applied to different qubits.
    pub fn batch_apply_single_qubit(
        &self,
        amplitudes: &mut [Complex64],
        gates: &[(usize, RotationAxis, f64)],
    ) -> QuantRS2Result<()> {
        // Sort gates by qubit to improve cache locality
        let mut sorted_gates = gates.to_vec();
        sorted_gates.sort_by_key(|(qubit, _, _)| *qubit);

        for (qubit, axis, angle) in sorted_gates {
            self.apply_rotation_gate(amplitudes, qubit, axis, angle)?;
        }

        Ok(())
    }

    /// Compute fidelity between two quantum states with SIMD
    pub fn fidelity(&self, state1: &[Complex64], state2: &[Complex64]) -> QuantRS2Result<f64> {
        if state1.len() != state2.len() {
            return Err(QuantRS2Error::InvalidInput(
                "States must have the same length".to_string(),
            ));
        }

        // Compute inner product ⟨ψ|φ⟩
        let len = state1.len();

        let mut state1_real = Vec::with_capacity(len);
        let mut state1_imag = Vec::with_capacity(len);
        let mut state2_real = Vec::with_capacity(len);
        let mut state2_imag = Vec::with_capacity(len);

        for (a, b) in state1.iter().zip(state2.iter()) {
            state1_real.push(a.re);
            state1_imag.push(a.im);
            state2_real.push(b.re);
            state2_imag.push(b.im);
        }

        let state1_real_view = ArrayView1::from(&state1_real);
        let state1_imag_view = ArrayView1::from(&state1_imag);
        let state2_real_view = ArrayView1::from(&state2_real);
        let state2_imag_view = ArrayView1::from(&state2_imag);

        // Inner product: (a* · b) = Σ (a_r - i*a_i)(b_r + i*b_i)
        let rr = <f64 as SimdF64>::simd_mul_arrays(&state1_real_view, &state2_real_view);
        let ii = <f64 as SimdF64>::simd_mul_arrays(&state1_imag_view, &state2_imag_view);
        let real_sum = <f64 as SimdF64>::simd_add_arrays(&rr.view(), &ii.view());
        let real_part = <f64 as SimdF64>::simd_sum_array(&real_sum.view());

        let ri = <f64 as SimdF64>::simd_mul_arrays(&state1_real_view, &state2_imag_view);
        let ir = <f64 as SimdF64>::simd_mul_arrays(&state1_imag_view, &state2_real_view);
        let imag_diff = <f64 as SimdF64>::simd_sub_arrays(&ri.view(), &ir.view());
        let imag_part = <f64 as SimdF64>::simd_sum_array(&imag_diff.view());

        // Fidelity is |⟨ψ|φ⟩|²
        let fidelity = real_part.mul_add(real_part, imag_part * imag_part);
        Ok(fidelity)
    }
}

/// Rotation axis for single-qubit rotation gates
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RotationAxis {
    X,
    Y,
    Z,
}

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

    #[test]
    fn test_simd_engine_creation() {
        let engine = SimdGateEngine::new();
        assert!(engine.simd_width() >= 1);
        assert!(engine.simd_width() <= 8);
    }

    #[test]
    fn test_rx_gate() {
        let engine = SimdGateEngine::new();
        let mut state = vec![Complex64::new(1.0, 0.0), Complex64::new(0.0, 0.0)];

        // Apply RX(π) which should flip the state to |1⟩
        engine
            .apply_rotation_gate(&mut state, 0, RotationAxis::X, std::f64::consts::PI)
            .expect("Failed to apply RX gate");

        // After RX(π), |0⟩ → -i|1⟩
        assert!(state[0].norm() < 0.1);
        assert!((state[1].norm() - 1.0).abs() < 1e-10);
    }

    #[test]
    fn test_ry_gate() {
        let engine = SimdGateEngine::new();
        let mut state = vec![Complex64::new(1.0, 0.0), Complex64::new(0.0, 0.0)];

        // Apply RY(π/2) which creates equal superposition
        engine
            .apply_rotation_gate(&mut state, 0, RotationAxis::Y, std::f64::consts::PI / 2.0)
            .expect("Failed to apply RY gate");

        let sqrt2_inv = 1.0 / std::f64::consts::SQRT_2;
        assert!((state[0].norm() - sqrt2_inv).abs() < 1e-10);
        assert!((state[1].norm() - sqrt2_inv).abs() < 1e-10);
    }

    #[test]
    fn test_rz_gate() {
        let engine = SimdGateEngine::new();
        let mut state = vec![
            Complex64::new(1.0 / 2.0_f64.sqrt(), 0.0),
            Complex64::new(1.0 / 2.0_f64.sqrt(), 0.0),
        ];

        // Apply RZ(π/4)
        engine
            .apply_rotation_gate(&mut state, 0, RotationAxis::Z, std::f64::consts::PI / 4.0)
            .expect("Failed to apply RZ gate");

        // Magnitudes should be preserved
        let sqrt2_inv = 1.0 / std::f64::consts::SQRT_2;
        assert!((state[0].norm() - sqrt2_inv).abs() < 1e-10);
        assert!((state[1].norm() - sqrt2_inv).abs() < 1e-10);
    }

    #[test]
    fn test_cnot_gate() {
        let engine = SimdGateEngine::new();

        // Test CNOT on |10⟩ state (control=1, target=0, should flip to |11⟩)
        let mut state = vec![
            Complex64::new(0.0, 0.0),
            Complex64::new(0.0, 0.0),
            Complex64::new(1.0, 0.0),
            Complex64::new(0.0, 0.0),
        ];

        engine
            .apply_cnot(&mut state, 1, 0)
            .expect("Failed to apply CNOT gate");

        // After CNOT with control=1, target=0: |10⟩ → |11⟩
        // When control (bit 1) is 1, flip target (bit 0)
        // |10⟩ (index 2) → |11⟩ (index 3)
        assert!(state[0].norm() < 1e-10);
        assert!(state[1].norm() < 1e-10);
        assert!(state[2].norm() < 1e-10);
        assert!((state[3].norm() - 1.0).abs() < 1e-10);
    }

    #[test]
    fn test_fidelity() {
        let engine = SimdGateEngine::new();

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

        let fid = engine
            .fidelity(&state1, &state2)
            .expect("Failed to compute fidelity");
        assert!((fid - 1.0).abs() < 1e-10);

        let state3 = vec![Complex64::new(0.0, 0.0), Complex64::new(1.0, 0.0)];
        let fid2 = engine
            .fidelity(&state1, &state3)
            .expect("Failed to compute fidelity for orthogonal states");
        assert!(fid2.abs() < 1e-10);
    }

    #[test]
    fn test_batch_gates() {
        let engine = SimdGateEngine::new();
        // Use single-qubit state for now
        let mut state = vec![Complex64::new(1.0, 0.0), Complex64::new(0.0, 0.0)];

        let gates = vec![(0, RotationAxis::X, std::f64::consts::PI / 2.0)];

        engine
            .batch_apply_single_qubit(&mut state, &gates)
            .expect("Failed to apply batch gates");

        // Check normalization
        let norm_sqr: f64 = state.iter().map(|c| c.norm_sqr()).sum();
        assert!((norm_sqr - 1.0).abs() < 1e-8, "Norm squared: {}", norm_sqr);
    }
}