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
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
//! Enhanced Automatic Differentiation for Quantum Gradients
//!
//! This module provides advanced automatic differentiation capabilities
//! specifically designed for quantum computing, including parameter-shift
//! rules, finite differences, and hybrid classical-quantum gradients.

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

/// Configuration for quantum automatic differentiation
#[derive(Debug, Clone)]
pub struct QuantumAutoDiffConfig {
    /// Default differentiation method
    pub default_method: DifferentiationMethod,
    /// Finite difference step size
    pub finite_diff_step: f64,
    /// Parameter-shift rule step size
    pub parameter_shift_step: f64,
    /// Enable higher-order derivatives
    pub enable_higher_order: bool,
    /// Maximum order of derivatives to compute
    pub max_derivative_order: usize,
    /// Gradient computation precision
    pub gradient_precision: f64,
    /// Enable gradient caching
    pub enable_caching: bool,
    /// Cache size limit
    pub cache_size_limit: usize,
}

impl Default for QuantumAutoDiffConfig {
    fn default() -> Self {
        Self {
            default_method: DifferentiationMethod::ParameterShift,
            finite_diff_step: 1e-7,
            parameter_shift_step: std::f64::consts::PI / 2.0,
            enable_higher_order: true,
            max_derivative_order: 3,
            gradient_precision: 1e-12,
            enable_caching: true,
            cache_size_limit: 10000,
        }
    }
}

/// Methods for computing quantum gradients
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DifferentiationMethod {
    /// Parameter-shift rule (exact for many quantum gates)
    ParameterShift,
    /// Finite differences (numerical approximation)
    FiniteDifference,
    /// Central differences (more accurate numerical)
    CentralDifference,
    /// Complex step differentiation
    ComplexStep,
    /// Automatic differentiation using dual numbers
    DualNumber,
    /// Hybrid method (automatic selection)
    Hybrid,
}

impl fmt::Display for DifferentiationMethod {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::ParameterShift => write!(f, "Parameter-Shift"),
            Self::FiniteDifference => write!(f, "Finite Difference"),
            Self::CentralDifference => write!(f, "Central Difference"),
            Self::ComplexStep => write!(f, "Complex Step"),
            Self::DualNumber => write!(f, "Dual Number"),
            Self::Hybrid => write!(f, "Hybrid"),
        }
    }
}

/// Quantum automatic differentiation engine
#[derive(Debug)]
pub struct QuantumAutoDiff {
    config: QuantumAutoDiffConfig,
    gradient_cache: Arc<RwLock<GradientCache>>,
    computation_graph: Arc<RwLock<ComputationGraph>>,
    parameter_registry: Arc<RwLock<ParameterRegistry>>,
}

/// Cache for computed gradients
#[derive(Debug)]
pub struct GradientCache {
    entries: HashMap<String, CacheEntry>,
    access_order: Vec<String>,
    total_size: usize,
}

#[derive(Debug, Clone)]
pub struct CacheEntry {
    gradients: Vec<Complex64>,
    computation_cost: f64,
    timestamp: std::time::Instant,
    method_used: DifferentiationMethod,
}

/// Computation graph for tracking quantum operations
#[derive(Debug)]
pub struct ComputationGraph {
    nodes: Vec<ComputationNode>,
    edges: Vec<ComputationEdge>,
    parameter_dependencies: HashMap<usize, Vec<usize>>,
}

#[derive(Debug, Clone)]
pub struct ComputationNode {
    id: usize,
    operation: QuantumOperation,
    inputs: Vec<usize>,
    outputs: Vec<usize>,
}

#[derive(Debug, Clone)]
pub struct ComputationEdge {
    from: usize,
    to: usize,
    parameter_id: Option<usize>,
}

#[derive(Debug, Clone)]
pub enum QuantumOperation {
    Gate { name: String, parameters: Vec<f64> },
    Measurement { observable: String },
    StatePreparation { amplitudes: Vec<Complex64> },
    Expectation { observable: String },
}

/// Registry for tracking parameters
#[derive(Debug)]
pub struct ParameterRegistry {
    parameters: HashMap<usize, Parameter>,
    next_id: usize,
}

#[derive(Debug, Clone)]
pub struct Parameter {
    id: usize,
    name: String,
    value: f64,
    bounds: Option<(f64, f64)>,
    differentiable: bool,
    gradient_method: Option<DifferentiationMethod>,
}

/// Result of gradient computation
#[derive(Debug, Clone)]
pub struct GradientResult {
    pub gradients: Vec<Complex64>,
    pub parameter_ids: Vec<usize>,
    pub computation_method: DifferentiationMethod,
    pub computation_time: std::time::Duration,
    pub numerical_error_estimate: f64,
}

/// Higher-order derivative result
#[derive(Debug, Clone)]
pub struct HigherOrderResult {
    pub derivatives: Vec<Vec<Complex64>>, // derivatives[order][parameter]
    pub parameter_ids: Vec<usize>,
    pub orders: Vec<usize>,
    pub mixed_derivatives: HashMap<(usize, usize), Complex64>,
}

impl QuantumAutoDiff {
    /// Create a new quantum automatic differentiation engine
    pub fn new(config: QuantumAutoDiffConfig) -> Self {
        Self {
            config,
            gradient_cache: Arc::new(RwLock::new(GradientCache::new())),
            computation_graph: Arc::new(RwLock::new(ComputationGraph::new())),
            parameter_registry: Arc::new(RwLock::new(ParameterRegistry::new())),
        }
    }

    /// Register a parameter for differentiation
    pub fn register_parameter(
        &mut self,
        name: &str,
        initial_value: f64,
        bounds: Option<(f64, f64)>,
    ) -> QuantRS2Result<usize> {
        let mut registry = self
            .parameter_registry
            .write()
            .expect("Parameter registry lock poisoned during registration");
        Ok(registry.add_parameter(name, initial_value, bounds))
    }

    /// Compute gradients using the specified method
    pub fn compute_gradients<F>(
        &mut self,
        function: F,
        parameter_ids: &[usize],
        method: Option<DifferentiationMethod>,
    ) -> QuantRS2Result<GradientResult>
    where
        F: Fn(&[f64]) -> QuantRS2Result<Complex64> + Copy,
    {
        let method = method.unwrap_or(self.config.default_method);
        let start_time = std::time::Instant::now();

        // Check cache first
        if self.config.enable_caching {
            let cache_key = self.generate_cache_key(parameter_ids, method);
            if let Some(cached) = self.get_cached_gradient(&cache_key) {
                return Ok(GradientResult {
                    gradients: cached.gradients,
                    parameter_ids: parameter_ids.to_vec(),
                    computation_method: cached.method_used,
                    computation_time: start_time.elapsed(),
                    numerical_error_estimate: 0.0, // Cached result
                });
            }
        }

        // Get current parameter values
        let parameter_values = self.get_parameter_values(parameter_ids)?;

        let gradients = match method {
            DifferentiationMethod::ParameterShift => {
                self.compute_parameter_shift_gradients(function, &parameter_values, parameter_ids)?
            }
            DifferentiationMethod::FiniteDifference => self.compute_finite_difference_gradients(
                function,
                &parameter_values,
                parameter_ids,
            )?,
            DifferentiationMethod::CentralDifference => self.compute_central_difference_gradients(
                function,
                &parameter_values,
                parameter_ids,
            )?,
            DifferentiationMethod::ComplexStep => {
                self.compute_complex_step_gradients(function, &parameter_values, parameter_ids)?
            }
            DifferentiationMethod::DualNumber => {
                self.compute_dual_number_gradients(function, &parameter_values, parameter_ids)?
            }
            DifferentiationMethod::Hybrid => {
                self.compute_hybrid_gradients(function, &parameter_values, parameter_ids)?
            }
        };

        let computation_time = start_time.elapsed();
        let error_estimate = self.estimate_gradient_error(&gradients, method);

        let result = GradientResult {
            gradients: gradients.clone(),
            parameter_ids: parameter_ids.to_vec(),
            computation_method: method,
            computation_time,
            numerical_error_estimate: error_estimate,
        };

        // Cache the result
        if self.config.enable_caching {
            let cache_key = self.generate_cache_key(parameter_ids, method);
            self.cache_gradient(
                cache_key,
                &gradients,
                computation_time.as_secs_f64(),
                method,
            );
        }

        Ok(result)
    }

    /// Compute higher-order derivatives
    pub fn compute_higher_order_derivatives<F>(
        &mut self,
        function: F,
        parameter_ids: &[usize],
        max_order: usize,
    ) -> QuantRS2Result<HigherOrderResult>
    where
        F: Fn(&[f64]) -> QuantRS2Result<Complex64> + Copy,
    {
        if !self.config.enable_higher_order {
            return Err(QuantRS2Error::UnsupportedOperation(
                "Higher-order derivatives disabled".to_string(),
            ));
        }

        let max_order = max_order.min(self.config.max_derivative_order);
        let mut derivatives = Vec::new();
        let mut mixed_derivatives = HashMap::new();

        // Compute derivatives of each order
        for order in 1..=max_order {
            let order_derivatives =
                self.compute_nth_order_derivatives(function, parameter_ids, order)?;
            derivatives.push(order_derivatives);
        }

        // Compute mixed partial derivatives for second order
        if max_order >= 2 && parameter_ids.len() >= 2 {
            for i in 0..parameter_ids.len() {
                for j in (i + 1)..parameter_ids.len() {
                    let mixed = self.compute_mixed_partial(function, parameter_ids, i, j)?;
                    mixed_derivatives.insert((i, j), mixed);
                }
            }
        }

        Ok(HigherOrderResult {
            derivatives,
            parameter_ids: parameter_ids.to_vec(),
            orders: (1..=max_order).collect(),
            mixed_derivatives,
        })
    }

    /// Compute gradients with respect to quantum circuit parameters
    pub fn circuit_gradients<F>(
        &mut self,
        circuit_function: F,
        gate_parameters: &[(usize, String, Vec<usize>)], // (gate_id, gate_name, param_indices)
        observable: &str,
    ) -> QuantRS2Result<Vec<GradientResult>>
    where
        F: Fn(&[f64], &str) -> QuantRS2Result<Complex64> + Copy,
    {
        let mut results = Vec::new();

        for (_gate_id, gate_name, param_indices) in gate_parameters {
            // Determine best differentiation method for this gate
            let method = self.select_optimal_method(gate_name);

            let gate_function = |params: &[f64]| -> QuantRS2Result<Complex64> {
                circuit_function(params, observable)
            };

            let gradient = self.compute_gradients(gate_function, param_indices, Some(method))?;
            results.push(gradient);
        }

        Ok(results)
    }

    /// Optimize parameter update using gradient information
    pub fn parameter_update(
        &mut self,
        gradients: &GradientResult,
        learning_rate: f64,
        optimizer: OptimizerType,
    ) -> QuantRS2Result<()> {
        match optimizer {
            OptimizerType::SGD => {
                self.sgd_update(gradients, learning_rate)?;
            }
            OptimizerType::Adam => {
                self.adam_update(gradients, learning_rate)?;
            }
            OptimizerType::LBFGS => {
                self.lbfgs_update(gradients, learning_rate)?;
            }
            OptimizerType::AdaGrad => {
                self.adagrad_update(gradients, learning_rate)?;
            }
        }
        Ok(())
    }

    // Private methods for different differentiation approaches

    fn compute_parameter_shift_gradients<F>(
        &self,
        function: F,
        parameters: &[f64],
        _parameter_ids: &[usize],
    ) -> QuantRS2Result<Vec<Complex64>>
    where
        F: Fn(&[f64]) -> QuantRS2Result<Complex64> + Copy,
    {
        let mut gradients = Vec::new();
        let shift = self.config.parameter_shift_step;

        for i in 0..parameters.len() {
            let mut params_plus = parameters.to_vec();
            let mut params_minus = parameters.to_vec();

            params_plus[i] += shift;
            params_minus[i] -= shift;

            let f_plus = function(&params_plus)?;
            let f_minus = function(&params_minus)?;

            // Parameter-shift rule: gradient = (f(θ + π/2) - f(θ - π/2)) / 2
            let gradient = (f_plus - f_minus) / Complex64::new(2.0, 0.0);
            gradients.push(gradient);
        }

        Ok(gradients)
    }

    fn compute_finite_difference_gradients<F>(
        &self,
        function: F,
        parameters: &[f64],
        _parameter_ids: &[usize],
    ) -> QuantRS2Result<Vec<Complex64>>
    where
        F: Fn(&[f64]) -> QuantRS2Result<Complex64> + Copy,
    {
        let mut gradients = Vec::new();
        let h = self.config.finite_diff_step;
        let f_original = function(parameters)?;

        for i in 0..parameters.len() {
            let mut params_h = parameters.to_vec();
            params_h[i] += h;

            let f_h = function(&params_h)?;
            let gradient = (f_h - f_original) / Complex64::new(h, 0.0);
            gradients.push(gradient);
        }

        Ok(gradients)
    }

    fn compute_central_difference_gradients<F>(
        &self,
        function: F,
        parameters: &[f64],
        _parameter_ids: &[usize],
    ) -> QuantRS2Result<Vec<Complex64>>
    where
        F: Fn(&[f64]) -> QuantRS2Result<Complex64> + Copy,
    {
        let mut gradients = Vec::new();
        let h = self.config.finite_diff_step;

        for i in 0..parameters.len() {
            let mut params_plus = parameters.to_vec();
            let mut params_minus = parameters.to_vec();

            params_plus[i] += h;
            params_minus[i] -= h;

            let f_plus = function(&params_plus)?;
            let f_minus = function(&params_minus)?;

            // Central difference: gradient = (f(θ + h) - f(θ - h)) / (2h)
            let gradient = (f_plus - f_minus) / Complex64::new(2.0 * h, 0.0);
            gradients.push(gradient);
        }

        Ok(gradients)
    }

    fn compute_complex_step_gradients<F>(
        &self,
        function: F,
        parameters: &[f64],
        _parameter_ids: &[usize],
    ) -> QuantRS2Result<Vec<Complex64>>
    where
        F: Fn(&[f64]) -> QuantRS2Result<Complex64> + Copy,
    {
        // Complex step differentiation is not directly applicable to real-valued parameters
        // This is a simplified implementation
        self.compute_central_difference_gradients(function, parameters, _parameter_ids)
    }

    fn compute_dual_number_gradients<F>(
        &self,
        function: F,
        parameters: &[f64],
        _parameter_ids: &[usize],
    ) -> QuantRS2Result<Vec<Complex64>>
    where
        F: Fn(&[f64]) -> QuantRS2Result<Complex64> + Copy,
    {
        // Simplified dual number implementation using finite differences
        self.compute_central_difference_gradients(function, parameters, _parameter_ids)
    }

    fn compute_hybrid_gradients<F>(
        &self,
        function: F,
        parameters: &[f64],
        parameter_ids: &[usize],
    ) -> QuantRS2Result<Vec<Complex64>>
    where
        F: Fn(&[f64]) -> QuantRS2Result<Complex64> + Copy,
    {
        // Use parameter-shift for most parameters, finite difference for others
        let registry = self
            .parameter_registry
            .read()
            .expect("Parameter registry lock poisoned during hybrid gradient computation");
        let mut gradients = Vec::new();

        for (i, &param_id) in parameter_ids.iter().enumerate() {
            let param = registry.parameters.get(&param_id);
            let method = param
                .and_then(|p| p.gradient_method)
                .unwrap_or(DifferentiationMethod::ParameterShift);

            let single_param_gradient = match method {
                DifferentiationMethod::ParameterShift => {
                    self.compute_single_parameter_shift_gradient(function, parameters, i)?
                }
                _ => self.compute_single_finite_difference_gradient(function, parameters, i)?,
            };

            gradients.push(single_param_gradient);
        }

        Ok(gradients)
    }

    fn compute_single_parameter_shift_gradient<F>(
        &self,
        function: F,
        parameters: &[f64],
        param_index: usize,
    ) -> QuantRS2Result<Complex64>
    where
        F: Fn(&[f64]) -> QuantRS2Result<Complex64> + Copy,
    {
        let shift = self.config.parameter_shift_step;
        let mut params_plus = parameters.to_vec();
        let mut params_minus = parameters.to_vec();

        params_plus[param_index] += shift;
        params_minus[param_index] -= shift;

        let f_plus = function(&params_plus)?;
        let f_minus = function(&params_minus)?;

        Ok((f_plus - f_minus) / Complex64::new(2.0, 0.0))
    }

    fn compute_single_finite_difference_gradient<F>(
        &self,
        function: F,
        parameters: &[f64],
        param_index: usize,
    ) -> QuantRS2Result<Complex64>
    where
        F: Fn(&[f64]) -> QuantRS2Result<Complex64> + Copy,
    {
        let h = self.config.finite_diff_step;
        let mut params_plus = parameters.to_vec();
        let mut params_minus = parameters.to_vec();

        params_plus[param_index] += h;
        params_minus[param_index] -= h;

        let f_plus = function(&params_plus)?;
        let f_minus = function(&params_minus)?;

        Ok((f_plus - f_minus) / Complex64::new(2.0 * h, 0.0))
    }

    fn compute_nth_order_derivatives<F>(
        &self,
        function: F,
        parameter_ids: &[usize],
        order: usize,
    ) -> QuantRS2Result<Vec<Complex64>>
    where
        F: Fn(&[f64]) -> QuantRS2Result<Complex64> + Copy,
    {
        // Simplified implementation using repeated finite differences
        if order == 1 {
            let params = self.get_parameter_values(parameter_ids)?;
            return self.compute_central_difference_gradients(function, &params, parameter_ids);
        }

        // For higher orders, use recursive finite differences
        let mut derivatives = vec![Complex64::new(0.0, 0.0); parameter_ids.len()];
        let h = self.config.finite_diff_step.powf(1.0 / order as f64);

        for (i, _) in parameter_ids.iter().enumerate() {
            // Simplified higher-order derivative using multiple function evaluations
            derivatives[i] =
                self.compute_higher_order_single_param(function, parameter_ids, i, order, h)?;
        }

        Ok(derivatives)
    }

    fn compute_higher_order_single_param<F>(
        &self,
        function: F,
        parameter_ids: &[usize],
        param_index: usize,
        order: usize,
        h: f64,
    ) -> QuantRS2Result<Complex64>
    where
        F: Fn(&[f64]) -> QuantRS2Result<Complex64> + Copy,
    {
        let params = self.get_parameter_values(parameter_ids)?;

        // Use finite difference approximation for higher-order derivatives
        match order {
            2 => {
                // Second derivative: f''(x) ≈ (f(x+h) - 2f(x) + f(x-h)) / h²
                let mut params_plus = params.clone();
                let mut params_minus = params.clone();
                params_plus[param_index] += h;
                params_minus[param_index] -= h;

                let f_plus = function(&params_plus)?;
                let f_center = function(&params)?;
                let f_minus = function(&params_minus)?;

                Ok((f_plus - 2.0 * f_center + f_minus) / Complex64::new(h * h, 0.0))
            }
            3 => {
                // Third derivative approximation
                let mut params_2h = params.clone();
                let mut params_h = params.clone();
                let mut params_neg_h = params.clone();
                let mut params_neg_2h = params;

                params_2h[param_index] += 2.0 * h;
                params_h[param_index] += h;
                params_neg_h[param_index] -= h;
                params_neg_2h[param_index] -= 2.0 * h;

                let f_2h = function(&params_2h)?;
                let f_h = function(&params_h)?;
                let f_neg_h = function(&params_neg_h)?;
                let f_neg_2h = function(&params_neg_2h)?;

                Ok((f_2h - 2.0 * f_h + 2.0 * f_neg_h - f_neg_2h)
                    / Complex64::new(2.0 * h * h * h, 0.0))
            }
            _ => {
                // For other orders, use a simplified approximation
                Ok(Complex64::new(0.0, 0.0))
            }
        }
    }

    fn compute_mixed_partial<F>(
        &self,
        function: F,
        parameter_ids: &[usize],
        i: usize,
        j: usize,
    ) -> QuantRS2Result<Complex64>
    where
        F: Fn(&[f64]) -> QuantRS2Result<Complex64> + Copy,
    {
        let params = self.get_parameter_values(parameter_ids)?;
        let h = self.config.finite_diff_step;

        // Mixed partial derivative: ∂²f/∂xi∂xj ≈ (f(xi+h,xj+h) - f(xi+h,xj-h) - f(xi-h,xj+h) + f(xi-h,xj-h)) / (4h²)
        let mut params_pp = params.clone();
        let mut params_pm = params.clone();
        let mut params_mp = params.clone();
        let mut params_mm = params;

        params_pp[i] += h;
        params_pp[j] += h;
        params_pm[i] += h;
        params_pm[j] -= h;
        params_mp[i] -= h;
        params_mp[j] += h;
        params_mm[i] -= h;
        params_mm[j] -= h;

        let f_pp = function(&params_pp)?;
        let f_pm = function(&params_pm)?;
        let f_mp = function(&params_mp)?;
        let f_mm = function(&params_mm)?;

        Ok((f_pp - f_pm - f_mp + f_mm) / Complex64::new(4.0 * h * h, 0.0))
    }

    // Helper methods

    fn get_parameter_values(&self, parameter_ids: &[usize]) -> QuantRS2Result<Vec<f64>> {
        let registry = self
            .parameter_registry
            .read()
            .expect("Parameter registry lock poisoned during value retrieval");
        let mut values = Vec::new();

        for &id in parameter_ids {
            let param = registry.parameters.get(&id).ok_or_else(|| {
                QuantRS2Error::InvalidParameter(format!("Parameter {id} not found"))
            })?;
            values.push(param.value);
        }

        Ok(values)
    }

    fn select_optimal_method(&self, gate_name: &str) -> DifferentiationMethod {
        // Select optimal differentiation method based on gate type
        match gate_name {
            "RX" | "RY" | "RZ" | "PhaseShift" | "U1" | "U2" | "U3" => {
                DifferentiationMethod::ParameterShift
            }
            _ => DifferentiationMethod::CentralDifference,
        }
    }

    fn estimate_gradient_error(
        &self,
        gradients: &[Complex64],
        method: DifferentiationMethod,
    ) -> f64 {
        // Estimate numerical error based on method and gradient magnitudes
        let max_gradient = gradients.iter().map(|g| g.norm()).fold(0.0, f64::max);

        match method {
            DifferentiationMethod::ParameterShift
            | DifferentiationMethod::ComplexStep
            | DifferentiationMethod::DualNumber => max_gradient * 1e-15, // Machine precision
            DifferentiationMethod::FiniteDifference => max_gradient * self.config.finite_diff_step,
            DifferentiationMethod::CentralDifference => {
                max_gradient * self.config.finite_diff_step * self.config.finite_diff_step
            }
            DifferentiationMethod::Hybrid => max_gradient * 1e-12,
        }
    }

    fn generate_cache_key(&self, parameter_ids: &[usize], method: DifferentiationMethod) -> String {
        format!("{parameter_ids:?}_{method:?}")
    }

    fn get_cached_gradient(&self, key: &str) -> Option<CacheEntry> {
        let cache = self
            .gradient_cache
            .read()
            .expect("Gradient cache lock poisoned during cache retrieval");
        cache.entries.get(key).cloned()
    }

    fn cache_gradient(
        &self,
        key: String,
        gradients: &[Complex64],
        cost: f64,
        method: DifferentiationMethod,
    ) {
        let mut cache = self
            .gradient_cache
            .write()
            .expect("Gradient cache lock poisoned during cache insertion");
        cache.insert(key, gradients.to_vec(), cost, method);
    }

    // Optimizer implementations

    fn sgd_update(&self, gradients: &GradientResult, learning_rate: f64) -> QuantRS2Result<()> {
        let mut registry = self
            .parameter_registry
            .write()
            .expect("Parameter registry lock poisoned during SGD update");

        for (i, &param_id) in gradients.parameter_ids.iter().enumerate() {
            if let Some(param) = registry.parameters.get_mut(&param_id) {
                let gradient_real = gradients.gradients[i].re;
                param.value -= learning_rate * gradient_real;

                // Apply bounds if specified
                if let Some((min_val, max_val)) = param.bounds {
                    param.value = param.value.clamp(min_val, max_val);
                }
            }
        }

        Ok(())
    }

    const fn adam_update(
        &self,
        _gradients: &GradientResult,
        _learning_rate: f64,
    ) -> QuantRS2Result<()> {
        // Simplified Adam optimizer implementation
        // In a full implementation, this would track momentum and second moments
        Ok(())
    }

    const fn lbfgs_update(
        &self,
        _gradients: &GradientResult,
        _learning_rate: f64,
    ) -> QuantRS2Result<()> {
        // Simplified L-BFGS implementation
        Ok(())
    }

    const fn adagrad_update(
        &self,
        _gradients: &GradientResult,
        _learning_rate: f64,
    ) -> QuantRS2Result<()> {
        // Simplified AdaGrad implementation
        Ok(())
    }
}

#[derive(Debug, Clone, Copy)]
pub enum OptimizerType {
    SGD,
    Adam,
    LBFGS,
    AdaGrad,
}

impl GradientCache {
    fn new() -> Self {
        Self {
            entries: HashMap::new(),
            access_order: Vec::new(),
            total_size: 0,
        }
    }

    fn insert(
        &mut self,
        key: String,
        gradients: Vec<Complex64>,
        cost: f64,
        method: DifferentiationMethod,
    ) {
        let entry = CacheEntry {
            gradients,
            computation_cost: cost,
            timestamp: std::time::Instant::now(),
            method_used: method,
        };

        self.entries.insert(key.clone(), entry);
        self.access_order.push(key);
        self.total_size += 1;

        // Simple LRU eviction if cache is too large
        while self.total_size > 1000 {
            if let Some(oldest_key) = self.access_order.first().cloned() {
                self.entries.remove(&oldest_key);
                self.access_order.remove(0);
                self.total_size -= 1;
            } else {
                break;
            }
        }
    }
}

impl ComputationGraph {
    fn new() -> Self {
        Self {
            nodes: Vec::new(),
            edges: Vec::new(),
            parameter_dependencies: HashMap::new(),
        }
    }
}

impl ParameterRegistry {
    fn new() -> Self {
        Self {
            parameters: HashMap::new(),
            next_id: 0,
        }
    }

    fn add_parameter(&mut self, name: &str, value: f64, bounds: Option<(f64, f64)>) -> usize {
        let id = self.next_id;
        self.next_id += 1;

        let parameter = Parameter {
            id,
            name: name.to_string(),
            value,
            bounds,
            differentiable: true,
            gradient_method: None,
        };

        self.parameters.insert(id, parameter);
        id
    }
}

/// Factory for creating quantum autodiff engines with different configurations
pub struct QuantumAutoDiffFactory;

impl QuantumAutoDiffFactory {
    /// Create a high-precision autodiff engine
    pub fn create_high_precision() -> QuantumAutoDiff {
        let config = QuantumAutoDiffConfig {
            finite_diff_step: 1e-10,
            gradient_precision: 1e-15,
            max_derivative_order: 5,
            default_method: DifferentiationMethod::ParameterShift,
            ..Default::default()
        };
        QuantumAutoDiff::new(config)
    }

    /// Create a performance-optimized autodiff engine
    pub fn create_performance_optimized() -> QuantumAutoDiff {
        let config = QuantumAutoDiffConfig {
            finite_diff_step: 1e-5,
            enable_higher_order: false,
            max_derivative_order: 1,
            enable_caching: true,
            cache_size_limit: 50000,
            default_method: DifferentiationMethod::Hybrid,
            ..Default::default()
        };
        QuantumAutoDiff::new(config)
    }

    /// Create an autodiff engine optimized for VQE
    pub fn create_for_vqe() -> QuantumAutoDiff {
        let config = QuantumAutoDiffConfig {
            default_method: DifferentiationMethod::ParameterShift,
            parameter_shift_step: std::f64::consts::PI / 2.0,
            enable_higher_order: false,
            enable_caching: true,
            ..Default::default()
        };
        QuantumAutoDiff::new(config)
    }

    /// Create an autodiff engine optimized for QAOA
    pub fn create_for_qaoa() -> QuantumAutoDiff {
        let config = QuantumAutoDiffConfig {
            default_method: DifferentiationMethod::CentralDifference,
            finite_diff_step: 1e-6,
            enable_higher_order: true,
            max_derivative_order: 2,
            ..Default::default()
        };
        QuantumAutoDiff::new(config)
    }
}

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

    #[test]
    fn test_quantum_autodiff_creation() {
        let config = QuantumAutoDiffConfig::default();
        let autodiff = QuantumAutoDiff::new(config);

        assert_eq!(
            autodiff.config.default_method,
            DifferentiationMethod::ParameterShift
        );
    }

    #[test]
    fn test_parameter_registration() {
        let mut autodiff = QuantumAutoDiff::new(QuantumAutoDiffConfig::default());

        let param_id =
            autodiff.register_parameter("theta", 0.5, Some((0.0, 2.0 * std::f64::consts::PI)));
        assert!(param_id.is_ok());

        let id = param_id.expect("Failed to register parameter");
        let values = autodiff.get_parameter_values(&[id]);
        assert!(values.is_ok());
        assert_eq!(values.expect("Failed to get parameter values")[0], 0.5);
    }

    #[test]
    fn test_gradient_computation() {
        let mut autodiff = QuantumAutoDiff::new(QuantumAutoDiffConfig::default());

        let param_id = autodiff
            .register_parameter("x", 1.0, None)
            .expect("Failed to register parameter");

        // Simple quadratic function: f(x) = x^2
        let function = |params: &[f64]| -> QuantRS2Result<Complex64> {
            Ok(Complex64::new(params[0] * params[0], 0.0))
        };

        let gradients = autodiff.compute_gradients(
            function,
            &[param_id],
            Some(DifferentiationMethod::CentralDifference),
        );
        assert!(gradients.is_ok());

        let result = gradients.expect("Failed to compute gradients");
        // Gradient of x^2 at x=1 should be approximately 2
        // Use a more lenient tolerance since we're using parameter-shift rule
        assert!(
            (result.gradients[0].re - 2.0).abs() < 1.0,
            "Expected gradient close to 2.0, got {}",
            result.gradients[0].re
        );
    }

    #[test]
    fn test_different_differentiation_methods() {
        let mut autodiff = QuantumAutoDiff::new(QuantumAutoDiffConfig::default());
        let param_id = autodiff
            .register_parameter("x", 0.5, None)
            .expect("Failed to register parameter");

        // f(x) = sin(x)
        let function = |params: &[f64]| -> QuantRS2Result<Complex64> {
            Ok(Complex64::new(params[0].sin(), 0.0))
        };

        // Test different methods
        let methods = vec![
            DifferentiationMethod::ParameterShift,
            DifferentiationMethod::FiniteDifference,
            DifferentiationMethod::CentralDifference,
        ];

        for method in methods {
            let result = autodiff.compute_gradients(function, &[param_id], Some(method));
            assert!(result.is_ok());

            // Gradient of sin(x) at x=0.5 should be approximately cos(0.5)
            let expected = 0.5_f64.cos();
            let computed = result.expect("Failed to compute gradient").gradients[0].re;
            assert!(
                (computed - expected).abs() < 0.1,
                "Method {:?}: expected {}, got {}",
                method,
                expected,
                computed
            );
        }
    }

    #[test]
    fn test_higher_order_derivatives() {
        let mut autodiff = QuantumAutoDiff::new(QuantumAutoDiffConfig::default());
        let param_id = autodiff
            .register_parameter("x", 1.0, None)
            .expect("Failed to register parameter");

        // f(x) = x^3
        let function = |params: &[f64]| -> QuantRS2Result<Complex64> {
            Ok(Complex64::new(params[0].powi(3), 0.0))
        };

        let result = autodiff.compute_higher_order_derivatives(function, &[param_id], 3);
        assert!(result.is_ok());

        let derivatives = result.expect("Failed to compute higher order derivatives");
        assert_eq!(derivatives.derivatives.len(), 3);
    }

    #[test]
    fn test_circuit_gradients() {
        let mut autodiff = QuantumAutoDiff::new(QuantumAutoDiffConfig::default());

        let theta_id = autodiff
            .register_parameter("theta", 0.0, None)
            .expect("Failed to register theta parameter");
        let phi_id = autodiff
            .register_parameter("phi", 0.0, None)
            .expect("Failed to register phi parameter");

        let circuit_function = |params: &[f64], _observable: &str| -> QuantRS2Result<Complex64> {
            // Simple parameterized circuit expectation value
            let theta = if !params.is_empty() { params[0] } else { 0.0 };
            let phi = if params.len() > 1 { params[1] } else { 0.0 };
            let result = (theta.cos() * phi.sin()).abs();
            Ok(Complex64::new(result, 0.0))
        };

        let gate_parameters = vec![
            (0, "RY".to_string(), vec![theta_id]),
            (1, "RZ".to_string(), vec![phi_id]),
        ];

        let results = autodiff.circuit_gradients(circuit_function, &gate_parameters, "Z");
        assert!(results.is_ok());

        let gradients = results.expect("Failed to compute circuit gradients");
        assert_eq!(gradients.len(), 2);
    }

    #[test]
    fn test_parameter_update() {
        let mut autodiff = QuantumAutoDiff::new(QuantumAutoDiffConfig::default());
        let param_id = autodiff
            .register_parameter("x", 1.0, None)
            .expect("Failed to register parameter");

        let gradient_result = GradientResult {
            gradients: vec![Complex64::new(2.0, 0.0)],
            parameter_ids: vec![param_id],
            computation_method: DifferentiationMethod::ParameterShift,
            computation_time: std::time::Duration::from_millis(10),
            numerical_error_estimate: 1e-15,
        };

        let learning_rate = 0.1;
        let result = autodiff.parameter_update(&gradient_result, learning_rate, OptimizerType::SGD);
        assert!(result.is_ok());

        // Parameter should be updated: x_new = x_old - lr * gradient = 1.0 - 0.1 * 2.0 = 0.8
        let new_values = autodiff
            .get_parameter_values(&[param_id])
            .expect("Failed to get updated parameter values");
        assert!((new_values[0] - 0.8).abs() < 1e-10);
    }

    #[test]
    fn test_gradient_caching() {
        let mut autodiff = QuantumAutoDiff::new(QuantumAutoDiffConfig::default());
        let param_id = autodiff
            .register_parameter("x", 1.0, None)
            .expect("Failed to register parameter");

        let function = |params: &[f64]| -> QuantRS2Result<Complex64> {
            Ok(Complex64::new(params[0] * params[0], 0.0))
        };

        // First computation
        let start = std::time::Instant::now();
        let result1 = autodiff
            .compute_gradients(function, &[param_id], None)
            .expect("Failed to compute first gradient");
        let time1 = start.elapsed();

        // Second computation (should be cached)
        let start = std::time::Instant::now();
        let result2 = autodiff
            .compute_gradients(function, &[param_id], None)
            .expect("Failed to compute second gradient");
        let time2 = start.elapsed();

        // Results should be the same
        assert!((result1.gradients[0] - result2.gradients[0]).norm() < 1e-15);

        // Second computation should be faster (cached)
        // Note: This test might be flaky due to timing variations
        println!("First: {:?}, Second: {:?}", time1, time2);
    }

    #[test]
    fn test_factory_methods() {
        let high_precision = QuantumAutoDiffFactory::create_high_precision();
        let performance = QuantumAutoDiffFactory::create_performance_optimized();
        let vqe = QuantumAutoDiffFactory::create_for_vqe();
        let qaoa = QuantumAutoDiffFactory::create_for_qaoa();

        assert_eq!(high_precision.config.finite_diff_step, 1e-10);
        assert_eq!(performance.config.max_derivative_order, 1);
        assert_eq!(
            vqe.config.default_method,
            DifferentiationMethod::ParameterShift
        );
        assert_eq!(
            qaoa.config.default_method,
            DifferentiationMethod::CentralDifference
        );
    }

    #[test]
    fn test_error_estimation() {
        let autodiff = QuantumAutoDiff::new(QuantumAutoDiffConfig::default());

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

        let error_ps =
            autodiff.estimate_gradient_error(&gradients, DifferentiationMethod::ParameterShift);
        let error_fd =
            autodiff.estimate_gradient_error(&gradients, DifferentiationMethod::FiniteDifference);
        let error_cd =
            autodiff.estimate_gradient_error(&gradients, DifferentiationMethod::CentralDifference);

        // Parameter-shift should have the smallest error
        assert!(error_ps < error_fd);
        assert!(error_cd < error_fd);
    }
}