quantrs2-sim 0.1.3

Quantum circuit simulators for the QuantRS2 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
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
//! TPU (Tensor Processing Unit) Acceleration for Quantum Simulation
//!
//! This module provides high-performance quantum circuit simulation using Google's
//! Tensor Processing Units (TPUs) and TPU-like architectures. It leverages the massive
//! parallelism and specialized tensor operations of TPUs to accelerate quantum state
//! vector operations, gate applications, and quantum algorithm computations.
//!
//! Key features:
//! - TPU-optimized tensor operations for quantum states
//! - Batch processing of quantum circuits
//! - JAX/XLA integration for automatic differentiation
//! - Distributed quantum simulation across TPU pods
//! - Memory-efficient state representation using TPU HBM
//! - Quantum machine learning acceleration
//! - Variational quantum algorithm optimization
//! - Cloud TPU integration and resource management

use scirs2_core::ndarray::{Array1, Array2, Array3, Array4, ArrayView1, Axis};
use scirs2_core::Complex64;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, VecDeque};
use std::sync::{Arc, Mutex};

use crate::circuit_interfaces::{
    CircuitInterface, InterfaceCircuit, InterfaceGate, InterfaceGateType,
};
use crate::error::{Result, SimulatorError};
use crate::quantum_ml_algorithms::{HardwareArchitecture, QMLConfig};
use crate::statevector::StateVectorSimulator;

/// TPU device types
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TPUDeviceType {
    /// TPU v2 (Cloud TPU v2)
    TPUv2,
    /// TPU v3 (Cloud TPU v3)
    TPUv3,
    /// TPU v4 (Cloud TPU v4)
    TPUv4,
    /// TPU v5e (Edge TPU)
    TPUv5e,
    /// TPU v5p (Pod slice)
    TPUv5p,
    /// Simulated TPU (for testing)
    Simulated,
}

/// TPU configuration
#[derive(Debug, Clone)]
pub struct TPUConfig {
    /// TPU device type
    pub device_type: TPUDeviceType,
    /// Number of TPU cores
    pub num_cores: usize,
    /// Memory per core (GB)
    pub memory_per_core: f64,
    /// Enable mixed precision
    pub enable_mixed_precision: bool,
    /// Batch size for circuit execution
    pub batch_size: usize,
    /// Enable XLA compilation
    pub enable_xla_compilation: bool,
    /// TPU topology (for multi-core setups)
    pub topology: TPUTopology,
    /// Enable distributed execution
    pub enable_distributed: bool,
    /// Maximum tensor size per operation
    pub max_tensor_size: usize,
    /// Memory optimization level
    pub memory_optimization: MemoryOptimization,
}

/// TPU topology configuration
#[derive(Debug, Clone)]
pub struct TPUTopology {
    /// Number of TPU chips
    pub num_chips: usize,
    /// Chips per host
    pub chips_per_host: usize,
    /// Number of hosts
    pub num_hosts: usize,
    /// Interconnect bandwidth (GB/s)
    pub interconnect_bandwidth: f64,
}

/// Memory optimization strategies
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MemoryOptimization {
    /// No optimization
    None,
    /// Basic gradient checkpointing
    Checkpointing,
    /// Activation recomputation
    Recomputation,
    /// Memory-efficient attention
    EfficientAttention,
    /// Aggressive optimization
    Aggressive,
}

impl Default for TPUConfig {
    fn default() -> Self {
        Self {
            device_type: TPUDeviceType::TPUv4,
            num_cores: 8,
            memory_per_core: 16.0, // 16 GB HBM per core
            enable_mixed_precision: true,
            batch_size: 32,
            enable_xla_compilation: true,
            topology: TPUTopology {
                num_chips: 4,
                chips_per_host: 4,
                num_hosts: 1,
                interconnect_bandwidth: 100.0, // 100 GB/s
            },
            enable_distributed: false,
            max_tensor_size: 1 << 28, // 256M elements
            memory_optimization: MemoryOptimization::Checkpointing,
        }
    }
}

/// TPU device information
#[derive(Debug, Clone)]
pub struct TPUDeviceInfo {
    /// Device ID
    pub device_id: usize,
    /// Device type
    pub device_type: TPUDeviceType,
    /// Core count
    pub core_count: usize,
    /// Memory size (GB)
    pub memory_size: f64,
    /// Peak FLOPS (operations per second)
    pub peak_flops: f64,
    /// Memory bandwidth (GB/s)
    pub memory_bandwidth: f64,
    /// Supports bfloat16
    pub supports_bfloat16: bool,
    /// Supports complex arithmetic
    pub supports_complex: bool,
    /// XLA version
    pub xla_version: String,
}

impl TPUDeviceInfo {
    /// Create device info for specific TPU type
    #[must_use]
    pub fn for_device_type(device_type: TPUDeviceType) -> Self {
        match device_type {
            TPUDeviceType::TPUv2 => Self {
                device_id: 0,
                device_type,
                core_count: 2,
                memory_size: 8.0,
                peak_flops: 45e12, // 45 TFLOPS
                memory_bandwidth: 300.0,
                supports_bfloat16: true,
                supports_complex: false,
                xla_version: "2.8.0".to_string(),
            },
            TPUDeviceType::TPUv3 => Self {
                device_id: 0,
                device_type,
                core_count: 2,
                memory_size: 16.0,
                peak_flops: 420e12, // 420 TFLOPS
                memory_bandwidth: 900.0,
                supports_bfloat16: true,
                supports_complex: false,
                xla_version: "2.11.0".to_string(),
            },
            TPUDeviceType::TPUv4 => Self {
                device_id: 0,
                device_type,
                core_count: 2,
                memory_size: 32.0,
                peak_flops: 1100e12, // 1.1 PFLOPS
                memory_bandwidth: 1200.0,
                supports_bfloat16: true,
                supports_complex: true,
                xla_version: "2.15.0".to_string(),
            },
            TPUDeviceType::TPUv5e => Self {
                device_id: 0,
                device_type,
                core_count: 1,
                memory_size: 16.0,
                peak_flops: 197e12, // 197 TFLOPS
                memory_bandwidth: 400.0,
                supports_bfloat16: true,
                supports_complex: true,
                xla_version: "2.17.0".to_string(),
            },
            TPUDeviceType::TPUv5p => Self {
                device_id: 0,
                device_type,
                core_count: 2,
                memory_size: 95.0,
                peak_flops: 459e12, // 459 TFLOPS
                memory_bandwidth: 2765.0,
                supports_bfloat16: true,
                supports_complex: true,
                xla_version: "2.17.0".to_string(),
            },
            TPUDeviceType::Simulated => Self {
                device_id: 0,
                device_type,
                core_count: 8,
                memory_size: 64.0,
                peak_flops: 100e12, // 100 TFLOPS (simulated)
                memory_bandwidth: 1000.0,
                supports_bfloat16: true,
                supports_complex: true,
                xla_version: "2.17.0".to_string(),
            },
        }
    }
}

/// TPU-accelerated quantum simulator
pub struct TPUQuantumSimulator {
    /// Configuration
    config: TPUConfig,
    /// Device information
    device_info: TPUDeviceInfo,
    /// Compiled XLA computations
    xla_computations: HashMap<String, XLAComputation>,
    /// Tensor buffers on TPU
    tensor_buffers: HashMap<String, TPUTensorBuffer>,
    /// Performance statistics
    stats: TPUStats,
    /// Distributed execution context
    distributed_context: Option<DistributedContext>,
    /// Memory manager
    memory_manager: TPUMemoryManager,
}

/// XLA computation representation
#[derive(Debug, Clone)]
pub struct XLAComputation {
    /// Computation name
    pub name: String,
    /// Input shapes
    pub input_shapes: Vec<Vec<usize>>,
    /// Output shapes
    pub output_shapes: Vec<Vec<usize>>,
    /// Compilation time (ms)
    pub compilation_time: f64,
    /// Estimated FLOPS
    pub estimated_flops: u64,
    /// Memory usage (bytes)
    pub memory_usage: usize,
}

/// TPU tensor buffer
#[derive(Debug, Clone)]
pub struct TPUTensorBuffer {
    /// Buffer ID
    pub buffer_id: usize,
    /// Shape
    pub shape: Vec<usize>,
    /// Data type
    pub dtype: TPUDataType,
    /// Size in bytes
    pub size_bytes: usize,
    /// Device placement
    pub device_id: usize,
    /// Is resident on device
    pub on_device: bool,
}

/// TPU data types
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TPUDataType {
    Float32,
    Float64,
    BFloat16,
    Complex64,
    Complex128,
    Int32,
    Int64,
}

impl TPUDataType {
    /// Get size in bytes
    #[must_use]
    pub const fn size_bytes(&self) -> usize {
        match self {
            Self::Float32 => 4,
            Self::Float64 => 8,
            Self::BFloat16 => 2,
            Self::Complex64 => 8,
            Self::Complex128 => 16,
            Self::Int32 => 4,
            Self::Int64 => 8,
        }
    }
}

/// Distributed execution context
#[derive(Debug, Clone)]
pub struct DistributedContext {
    /// Number of hosts
    pub num_hosts: usize,
    /// Host ID
    pub host_id: usize,
    /// Global device count
    pub global_device_count: usize,
    /// Local device count
    pub local_device_count: usize,
    /// Communication backend
    pub communication_backend: CommunicationBackend,
}

/// Communication backends for distributed execution
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CommunicationBackend {
    GRPC,
    MPI,
    NCCL,
    GLOO,
}

/// TPU memory manager
#[derive(Debug, Clone)]
pub struct TPUMemoryManager {
    /// Total available memory (bytes)
    pub total_memory: usize,
    /// Used memory (bytes)
    pub used_memory: usize,
    /// Memory pools
    pub memory_pools: HashMap<String, MemoryPool>,
    /// Garbage collection enabled
    pub gc_enabled: bool,
    /// Memory fragmentation ratio
    pub fragmentation_ratio: f64,
}

/// Memory pool for efficient allocation
#[derive(Debug, Clone)]
pub struct MemoryPool {
    /// Pool name
    pub name: String,
    /// Pool size (bytes)
    pub size: usize,
    /// Used memory (bytes)
    pub used: usize,
    /// Free chunks
    pub free_chunks: Vec<(usize, usize)>, // (offset, size)
    /// Allocated chunks
    pub allocated_chunks: HashMap<usize, usize>, // buffer_id -> offset
}

/// TPU performance statistics
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct TPUStats {
    /// Total operations executed
    pub total_operations: usize,
    /// Total execution time (ms)
    pub total_execution_time: f64,
    /// Average operation time (ms)
    pub avg_operation_time: f64,
    /// Total FLOPS performed
    pub total_flops: u64,
    /// Peak FLOPS utilization
    pub peak_flops_utilization: f64,
    /// Memory transfers (host to device)
    pub h2d_transfers: usize,
    /// Memory transfers (device to host)
    pub d2h_transfers: usize,
    /// Total transfer time (ms)
    pub total_transfer_time: f64,
    /// Compilation time (ms)
    pub total_compilation_time: f64,
    /// Memory usage peak (bytes)
    pub peak_memory_usage: usize,
    /// XLA compilation cache hits
    pub xla_cache_hits: usize,
    /// XLA compilation cache misses
    pub xla_cache_misses: usize,
}

impl TPUStats {
    /// Update statistics after operation
    pub fn update_operation(&mut self, execution_time: f64, flops: u64) {
        self.total_operations += 1;
        self.total_execution_time += execution_time;
        self.avg_operation_time = self.total_execution_time / self.total_operations as f64;
        self.total_flops += flops;
    }

    /// Calculate performance metrics
    #[must_use]
    pub fn get_performance_metrics(&self) -> HashMap<String, f64> {
        let mut metrics = HashMap::new();

        if self.total_execution_time > 0.0 {
            metrics.insert(
                "flops_per_second".to_string(),
                self.total_flops as f64 / (self.total_execution_time / 1000.0),
            );
            metrics.insert(
                "operations_per_second".to_string(),
                self.total_operations as f64 / (self.total_execution_time / 1000.0),
            );
        }

        metrics.insert(
            "cache_hit_rate".to_string(),
            self.xla_cache_hits as f64
                / (self.xla_cache_hits + self.xla_cache_misses).max(1) as f64,
        );
        metrics.insert(
            "peak_flops_utilization".to_string(),
            self.peak_flops_utilization,
        );

        metrics
    }
}

impl TPUQuantumSimulator {
    /// Create new TPU quantum simulator
    pub fn new(config: TPUConfig) -> Result<Self> {
        let device_info = TPUDeviceInfo::for_device_type(config.device_type);

        // Initialize memory manager
        let total_memory = (config.memory_per_core * config.num_cores as f64 * 1e9) as usize;
        let memory_manager = TPUMemoryManager {
            total_memory,
            used_memory: 0,
            memory_pools: HashMap::new(),
            gc_enabled: true,
            fragmentation_ratio: 0.0,
        };

        // Initialize distributed context if enabled
        let distributed_context = if config.enable_distributed {
            Some(DistributedContext {
                num_hosts: config.topology.num_hosts,
                host_id: 0,
                global_device_count: config.topology.num_chips,
                local_device_count: config.topology.chips_per_host,
                communication_backend: CommunicationBackend::GRPC,
            })
        } else {
            None
        };

        let mut simulator = Self {
            config,
            device_info,
            xla_computations: HashMap::new(),
            tensor_buffers: HashMap::new(),
            stats: TPUStats::default(),
            distributed_context,
            memory_manager,
        };

        // Compile standard quantum operations
        simulator.compile_standard_operations()?;

        Ok(simulator)
    }

    /// Compile standard quantum operations to XLA
    fn compile_standard_operations(&mut self) -> Result<()> {
        let start_time = std::time::Instant::now();

        // Single qubit gate operations
        self.compile_single_qubit_gates()?;

        // Two qubit gate operations
        self.compile_two_qubit_gates()?;

        // State vector operations
        self.compile_state_vector_operations()?;

        // Measurement operations
        self.compile_measurement_operations()?;

        // Expectation value computations
        self.compile_expectation_operations()?;

        // Quantum machine learning operations
        self.compile_qml_operations()?;

        self.stats.total_compilation_time = start_time.elapsed().as_secs_f64() * 1000.0;

        Ok(())
    }

    /// Compile single qubit gate operations
    fn compile_single_qubit_gates(&mut self) -> Result<()> {
        // Batched single qubit gate application
        let computation = XLAComputation {
            name: "batched_single_qubit_gates".to_string(),
            input_shapes: vec![
                vec![self.config.batch_size, 1 << 20], // State vectors
                vec![2, 2],                            // Gate matrix
                vec![1],                               // Target qubit
            ],
            output_shapes: vec![
                vec![self.config.batch_size, 1 << 20], // Updated state vectors
            ],
            compilation_time: 50.0, // Simulated compilation time
            estimated_flops: (self.config.batch_size * (1 << 20) * 8) as u64,
            memory_usage: self.config.batch_size * (1 << 20) * 16, // Complex128
        };

        self.xla_computations
            .insert("batched_single_qubit_gates".to_string(), computation);

        // Fused rotation gates (RX, RY, RZ)
        let fused_rotations = XLAComputation {
            name: "fused_rotation_gates".to_string(),
            input_shapes: vec![
                vec![self.config.batch_size, 1 << 20], // State vectors
                vec![3],                               // Rotation angles (x, y, z)
                vec![1],                               // Target qubit
            ],
            output_shapes: vec![
                vec![self.config.batch_size, 1 << 20], // Updated state vectors
            ],
            compilation_time: 75.0,
            estimated_flops: (self.config.batch_size * (1 << 20) * 12) as u64,
            memory_usage: self.config.batch_size * (1 << 20) * 16,
        };

        self.xla_computations
            .insert("fused_rotation_gates".to_string(), fused_rotations);

        Ok(())
    }

    /// Compile two qubit gate operations
    fn compile_two_qubit_gates(&mut self) -> Result<()> {
        // Batched CNOT gates
        let cnot_computation = XLAComputation {
            name: "batched_cnot_gates".to_string(),
            input_shapes: vec![
                vec![self.config.batch_size, 1 << 20], // State vectors
                vec![1],                               // Control qubit
                vec![1],                               // Target qubit
            ],
            output_shapes: vec![
                vec![self.config.batch_size, 1 << 20], // Updated state vectors
            ],
            compilation_time: 80.0,
            estimated_flops: (self.config.batch_size * (1 << 20) * 4) as u64,
            memory_usage: self.config.batch_size * (1 << 20) * 16,
        };

        self.xla_computations
            .insert("batched_cnot_gates".to_string(), cnot_computation);

        // General two-qubit gates
        let general_two_qubit = XLAComputation {
            name: "general_two_qubit_gates".to_string(),
            input_shapes: vec![
                vec![self.config.batch_size, 1 << 20], // State vectors
                vec![4, 4],                            // Gate matrix
                vec![2],                               // Qubit indices
            ],
            output_shapes: vec![
                vec![self.config.batch_size, 1 << 20], // Updated state vectors
            ],
            compilation_time: 120.0,
            estimated_flops: (self.config.batch_size * (1 << 20) * 16) as u64,
            memory_usage: self.config.batch_size * (1 << 20) * 16,
        };

        self.xla_computations
            .insert("general_two_qubit_gates".to_string(), general_two_qubit);

        Ok(())
    }

    /// Compile state vector operations
    fn compile_state_vector_operations(&mut self) -> Result<()> {
        // Batch normalization
        let normalization = XLAComputation {
            name: "batch_normalize".to_string(),
            input_shapes: vec![
                vec![self.config.batch_size, 1 << 20], // State vectors
            ],
            output_shapes: vec![
                vec![self.config.batch_size, 1 << 20], // Normalized state vectors
                vec![self.config.batch_size],          // Norms
            ],
            compilation_time: 30.0,
            estimated_flops: (self.config.batch_size * (1 << 20) * 3) as u64,
            memory_usage: self.config.batch_size * (1 << 20) * 16,
        };

        self.xla_computations
            .insert("batch_normalize".to_string(), normalization);

        // Inner product computation
        let inner_product = XLAComputation {
            name: "batch_inner_product".to_string(),
            input_shapes: vec![
                vec![self.config.batch_size, 1 << 20], // State vectors 1
                vec![self.config.batch_size, 1 << 20], // State vectors 2
            ],
            output_shapes: vec![
                vec![self.config.batch_size], // Inner products
            ],
            compilation_time: 40.0,
            estimated_flops: (self.config.batch_size * (1 << 20) * 6) as u64,
            memory_usage: self.config.batch_size * (1 << 20) * 32,
        };

        self.xla_computations
            .insert("batch_inner_product".to_string(), inner_product);

        Ok(())
    }

    /// Compile measurement operations
    fn compile_measurement_operations(&mut self) -> Result<()> {
        // Probability computation
        let probabilities = XLAComputation {
            name: "compute_probabilities".to_string(),
            input_shapes: vec![
                vec![self.config.batch_size, 1 << 20], // State vectors
            ],
            output_shapes: vec![
                vec![self.config.batch_size, 1 << 20], // Probabilities
            ],
            compilation_time: 25.0,
            estimated_flops: (self.config.batch_size * (1 << 20) * 2) as u64,
            memory_usage: self.config.batch_size * (1 << 20) * 24,
        };

        self.xla_computations
            .insert("compute_probabilities".to_string(), probabilities);

        // Sampling operation
        let sampling = XLAComputation {
            name: "quantum_sampling".to_string(),
            input_shapes: vec![
                vec![self.config.batch_size, 1 << 20], // Probabilities
                vec![self.config.batch_size],          // Random numbers
            ],
            output_shapes: vec![
                vec![self.config.batch_size], // Sample results
            ],
            compilation_time: 35.0,
            estimated_flops: (self.config.batch_size * (1 << 20)) as u64,
            memory_usage: self.config.batch_size * (1 << 20) * 8,
        };

        self.xla_computations
            .insert("quantum_sampling".to_string(), sampling);

        Ok(())
    }

    /// Compile expectation value operations
    fn compile_expectation_operations(&mut self) -> Result<()> {
        // Pauli expectation values
        let pauli_expectation = XLAComputation {
            name: "pauli_expectation_values".to_string(),
            input_shapes: vec![
                vec![self.config.batch_size, 1 << 20], // State vectors
                vec![20],                              // Pauli strings (encoded)
            ],
            output_shapes: vec![
                vec![self.config.batch_size, 20], // Expectation values
            ],
            compilation_time: 60.0,
            estimated_flops: (self.config.batch_size * (1 << 20) * 20 * 4) as u64,
            memory_usage: self.config.batch_size * (1 << 20) * 16,
        };

        self.xla_computations
            .insert("pauli_expectation_values".to_string(), pauli_expectation);

        // Hamiltonian expectation
        let hamiltonian_expectation = XLAComputation {
            name: "hamiltonian_expectation".to_string(),
            input_shapes: vec![
                vec![self.config.batch_size, 1 << 20], // State vectors
                vec![1 << 20, 1 << 20],                // Hamiltonian matrix
            ],
            output_shapes: vec![
                vec![self.config.batch_size], // Expectation values
            ],
            compilation_time: 150.0,
            estimated_flops: (self.config.batch_size * (1 << 40)) as u64,
            memory_usage: (1 << 40) * 16 + self.config.batch_size * (1 << 20) * 16,
        };

        self.xla_computations.insert(
            "hamiltonian_expectation".to_string(),
            hamiltonian_expectation,
        );

        Ok(())
    }

    /// Compile quantum machine learning operations
    fn compile_qml_operations(&mut self) -> Result<()> {
        // Variational circuit execution
        let variational_circuit = XLAComputation {
            name: "variational_circuit_batch".to_string(),
            input_shapes: vec![
                vec![self.config.batch_size, 1 << 20], // Initial states
                vec![100],                             // Parameters
                vec![50],                              // Circuit structure
            ],
            output_shapes: vec![
                vec![self.config.batch_size, 1 << 20], // Final states
            ],
            compilation_time: 200.0,
            estimated_flops: (self.config.batch_size * 100 * (1 << 20) * 8) as u64,
            memory_usage: self.config.batch_size * (1 << 20) * 16,
        };

        self.xla_computations
            .insert("variational_circuit_batch".to_string(), variational_circuit);

        // Gradient computation using parameter shift
        let parameter_shift_gradients = XLAComputation {
            name: "parameter_shift_gradients".to_string(),
            input_shapes: vec![
                vec![self.config.batch_size, 1 << 20], // States
                vec![100],                             // Parameters
                vec![50],                              // Circuit structure
                vec![20],                              // Observables
            ],
            output_shapes: vec![
                vec![self.config.batch_size, 100], // Gradients
            ],
            compilation_time: 300.0,
            estimated_flops: (self.config.batch_size * 100 * 20 * (1 << 20) * 16) as u64,
            memory_usage: self.config.batch_size * (1 << 20) * 16 * 4, // 4 evaluations per gradient
        };

        self.xla_computations.insert(
            "parameter_shift_gradients".to_string(),
            parameter_shift_gradients,
        );

        Ok(())
    }

    /// Execute batched quantum circuit
    pub fn execute_batch_circuit(
        &mut self,
        circuits: &[InterfaceCircuit],
        initial_states: &[Array1<Complex64>],
    ) -> Result<Vec<Array1<Complex64>>> {
        let start_time = std::time::Instant::now();

        if circuits.len() != initial_states.len() {
            return Err(SimulatorError::InvalidInput(
                "Circuit and state count mismatch".to_string(),
            ));
        }

        if circuits.len() > self.config.batch_size {
            return Err(SimulatorError::InvalidInput(
                "Batch size exceeded".to_string(),
            ));
        }

        // Allocate device memory for batch
        self.allocate_batch_memory(circuits.len(), initial_states[0].len())?;

        // Transfer initial states to device
        self.transfer_states_to_device(initial_states)?;

        // Execute circuits in batch
        let mut final_states = Vec::with_capacity(circuits.len());

        for (i, circuit) in circuits.iter().enumerate() {
            let mut current_state = initial_states[i].clone();

            // Process gates sequentially (could be optimized for parallel execution)
            for gate in &circuit.gates {
                current_state = self.apply_gate_tpu(&current_state, gate)?;
            }

            final_states.push(current_state);
        }

        // Transfer results back to host
        self.transfer_states_to_host(&final_states)?;

        let execution_time = start_time.elapsed().as_secs_f64() * 1000.0;
        let estimated_flops = circuits.len() as u64 * 1000; // Rough estimate
        self.stats.update_operation(execution_time, estimated_flops);

        Ok(final_states)
    }

    /// Apply quantum gate using TPU acceleration
    fn apply_gate_tpu(
        &mut self,
        state: &Array1<Complex64>,
        gate: &InterfaceGate,
    ) -> Result<Array1<Complex64>> {
        match gate.gate_type {
            InterfaceGateType::Hadamard
            | InterfaceGateType::PauliX
            | InterfaceGateType::PauliY
            | InterfaceGateType::PauliZ => self.apply_single_qubit_gate_tpu(state, gate),
            InterfaceGateType::RX(_) | InterfaceGateType::RY(_) | InterfaceGateType::RZ(_) => {
                self.apply_rotation_gate_tpu(state, gate)
            }
            InterfaceGateType::CNOT | InterfaceGateType::CZ => {
                self.apply_two_qubit_gate_tpu(state, gate)
            }
            _ => {
                // Fallback to CPU simulation for unsupported gates
                self.apply_gate_cpu_fallback(state, gate)
            }
        }
    }

    /// Apply single qubit gate using TPU
    fn apply_single_qubit_gate_tpu(
        &mut self,
        state: &Array1<Complex64>,
        gate: &InterfaceGate,
    ) -> Result<Array1<Complex64>> {
        let start_time = std::time::Instant::now();

        if gate.qubits.is_empty() {
            return Ok(state.clone());
        }

        let target_qubit = gate.qubits[0];
        let num_qubits = (state.len() as f64).log2().ceil() as usize;

        // Simulate TPU execution
        let mut result_state = state.clone();

        // Apply gate matrix (simplified simulation)
        let gate_matrix = self.get_gate_matrix(&gate.gate_type);
        for i in 0..state.len() {
            if (i >> target_qubit) & 1 == 0 {
                let j = i | (1 << target_qubit);
                if j < state.len() {
                    let state_0 = result_state[i];
                    let state_1 = result_state[j];

                    result_state[i] = gate_matrix[0] * state_0 + gate_matrix[1] * state_1;
                    result_state[j] = gate_matrix[2] * state_0 + gate_matrix[3] * state_1;
                }
            }
        }

        let execution_time = start_time.elapsed().as_secs_f64() * 1000.0;
        let flops = (state.len() * 8) as u64; // Rough estimate
        self.stats.update_operation(execution_time, flops);

        Ok(result_state)
    }

    /// Apply rotation gate using TPU
    fn apply_rotation_gate_tpu(
        &mut self,
        state: &Array1<Complex64>,
        gate: &InterfaceGate,
    ) -> Result<Array1<Complex64>> {
        // Use fused rotation computation if available
        let computation_name = "fused_rotation_gates";

        if self.xla_computations.contains_key(computation_name) {
            let start_time = std::time::Instant::now();

            // Simulate XLA execution
            let mut result_state = state.clone();

            // Apply rotation (simplified)
            let angle = 0.1; // Default angle for simulation
            self.apply_rotation_simulation(
                &mut result_state,
                gate.qubits[0],
                &gate.gate_type,
                angle,
            );

            let execution_time = start_time.elapsed().as_secs_f64() * 1000.0;
            self.stats
                .update_operation(execution_time, (state.len() * 12) as u64);

            Ok(result_state)
        } else {
            self.apply_single_qubit_gate_tpu(state, gate)
        }
    }

    /// Apply two qubit gate using TPU
    fn apply_two_qubit_gate_tpu(
        &mut self,
        state: &Array1<Complex64>,
        gate: &InterfaceGate,
    ) -> Result<Array1<Complex64>> {
        let start_time = std::time::Instant::now();

        if gate.qubits.len() < 2 {
            return Ok(state.clone());
        }

        let control_qubit = gate.qubits[0];
        let target_qubit = gate.qubits[1];

        // Simulate TPU execution for CNOT
        let mut result_state = state.clone();

        match gate.gate_type {
            InterfaceGateType::CNOT => {
                for i in 0..state.len() {
                    if ((i >> control_qubit) & 1) == 1 {
                        let j = i ^ (1 << target_qubit);
                        if j < state.len() && i != j {
                            result_state.swap(i, j);
                        }
                    }
                }
            }
            InterfaceGateType::CZ => {
                for i in 0..state.len() {
                    if ((i >> control_qubit) & 1) == 1 && ((i >> target_qubit) & 1) == 1 {
                        result_state[i] *= -1.0;
                    }
                }
            }
            _ => return self.apply_gate_cpu_fallback(state, gate),
        }

        let execution_time = start_time.elapsed().as_secs_f64() * 1000.0;
        let flops = (state.len() * 4) as u64;
        self.stats.update_operation(execution_time, flops);

        Ok(result_state)
    }

    /// Apply gate using CPU fallback
    fn apply_gate_cpu_fallback(
        &self,
        state: &Array1<Complex64>,
        _gate: &InterfaceGate,
    ) -> Result<Array1<Complex64>> {
        // Fallback to CPU implementation
        Ok(state.clone())
    }

    /// Get gate matrix for standard gates
    fn get_gate_matrix(&self, gate_type: &InterfaceGateType) -> [Complex64; 4] {
        match gate_type {
            InterfaceGateType::Hadamard | InterfaceGateType::H => [
                Complex64::new(1.0 / 2.0_f64.sqrt(), 0.0),
                Complex64::new(1.0 / 2.0_f64.sqrt(), 0.0),
                Complex64::new(1.0 / 2.0_f64.sqrt(), 0.0),
                Complex64::new(-1.0 / 2.0_f64.sqrt(), 0.0),
            ],
            InterfaceGateType::PauliX | InterfaceGateType::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),
            ],
            InterfaceGateType::PauliY => [
                Complex64::new(0.0, 0.0),
                Complex64::new(0.0, -1.0),
                Complex64::new(0.0, 1.0),
                Complex64::new(0.0, 0.0),
            ],
            InterfaceGateType::PauliZ => [
                Complex64::new(1.0, 0.0),
                Complex64::new(0.0, 0.0),
                Complex64::new(0.0, 0.0),
                Complex64::new(-1.0, 0.0),
            ],
            _ => [
                Complex64::new(1.0, 0.0),
                Complex64::new(0.0, 0.0),
                Complex64::new(0.0, 0.0),
                Complex64::new(1.0, 0.0),
            ],
        }
    }

    /// Apply rotation simulation
    fn apply_rotation_simulation(
        &self,
        state: &mut Array1<Complex64>,
        qubit: usize,
        gate_type: &InterfaceGateType,
        angle: f64,
    ) {
        let cos_half = (angle / 2.0).cos();
        let sin_half = (angle / 2.0).sin();

        for i in 0..state.len() {
            if (i >> qubit) & 1 == 0 {
                let j = i | (1 << qubit);
                if j < state.len() {
                    let state_0 = state[i];
                    let state_1 = state[j];

                    match gate_type {
                        InterfaceGateType::RX(_) => {
                            state[i] = Complex64::new(cos_half, 0.0) * state_0
                                + Complex64::new(0.0, -sin_half) * state_1;
                            state[j] = Complex64::new(0.0, -sin_half) * state_0
                                + Complex64::new(cos_half, 0.0) * state_1;
                        }
                        InterfaceGateType::RY(_) => {
                            state[i] = Complex64::new(cos_half, 0.0) * state_0
                                + Complex64::new(-sin_half, 0.0) * state_1;
                            state[j] = Complex64::new(sin_half, 0.0) * state_0
                                + Complex64::new(cos_half, 0.0) * state_1;
                        }
                        InterfaceGateType::RZ(_) => {
                            state[i] = Complex64::new(cos_half, -sin_half) * state_0;
                            state[j] = Complex64::new(cos_half, sin_half) * state_1;
                        }
                        _ => {}
                    }
                }
            }
        }
    }

    /// Allocate batch memory on TPU
    fn allocate_batch_memory(&mut self, batch_size: usize, state_size: usize) -> Result<()> {
        let total_size = batch_size * state_size * 16; // Complex128

        if total_size > self.memory_manager.total_memory {
            return Err(SimulatorError::MemoryError(
                "Insufficient TPU memory".to_string(),
            ));
        }

        // Create tensor buffer
        let buffer = TPUTensorBuffer {
            buffer_id: self.tensor_buffers.len(),
            shape: vec![batch_size, state_size],
            dtype: TPUDataType::Complex128,
            size_bytes: total_size,
            device_id: 0,
            on_device: true,
        };

        self.tensor_buffers
            .insert("batch_states".to_string(), buffer);
        self.memory_manager.used_memory += total_size;

        if self.memory_manager.used_memory > self.stats.peak_memory_usage {
            self.stats.peak_memory_usage = self.memory_manager.used_memory;
        }

        Ok(())
    }

    /// Transfer states to TPU device
    fn transfer_states_to_device(&mut self, _states: &[Array1<Complex64>]) -> Result<()> {
        let start_time = std::time::Instant::now();

        // Simulate host-to-device transfer
        std::thread::sleep(std::time::Duration::from_micros(100)); // Simulate transfer time

        let transfer_time = start_time.elapsed().as_secs_f64() * 1000.0;
        self.stats.h2d_transfers += 1;
        self.stats.total_transfer_time += transfer_time;

        Ok(())
    }

    /// Transfer states from TPU device
    fn transfer_states_to_host(&mut self, _states: &[Array1<Complex64>]) -> Result<()> {
        let start_time = std::time::Instant::now();

        // Simulate device-to-host transfer
        std::thread::sleep(std::time::Duration::from_micros(50)); // Simulate transfer time

        let transfer_time = start_time.elapsed().as_secs_f64() * 1000.0;
        self.stats.d2h_transfers += 1;
        self.stats.total_transfer_time += transfer_time;

        Ok(())
    }

    /// Compute expectation values using TPU
    pub fn compute_expectation_values_tpu(
        &mut self,
        states: &[Array1<Complex64>],
        observables: &[String],
    ) -> Result<Array2<f64>> {
        let start_time = std::time::Instant::now();

        let batch_size = states.len();
        let num_observables = observables.len();
        let mut results = Array2::zeros((batch_size, num_observables));

        // Simulate TPU computation
        for (i, state) in states.iter().enumerate() {
            for (j, _observable) in observables.iter().enumerate() {
                // Simulate expectation value computation
                let expectation = fastrand::f64().mul_add(2.0, -1.0); // Random value between -1 and 1
                results[[i, j]] = expectation;
            }
        }

        let execution_time = start_time.elapsed().as_secs_f64() * 1000.0;
        let flops = (batch_size * num_observables * states[0].len() * 4) as u64;
        self.stats.update_operation(execution_time, flops);

        Ok(results)
    }

    /// Get device information
    #[must_use]
    pub const fn get_device_info(&self) -> &TPUDeviceInfo {
        &self.device_info
    }

    /// Get performance statistics
    #[must_use]
    pub const fn get_stats(&self) -> &TPUStats {
        &self.stats
    }

    /// Reset performance statistics
    pub fn reset_stats(&mut self) {
        self.stats = TPUStats::default();
    }

    /// Check TPU availability
    #[must_use]
    pub fn is_tpu_available(&self) -> bool {
        !self.xla_computations.is_empty()
    }

    /// Get memory usage
    #[must_use]
    pub const fn get_memory_usage(&self) -> (usize, usize) {
        (
            self.memory_manager.used_memory,
            self.memory_manager.total_memory,
        )
    }

    /// Perform garbage collection
    pub fn garbage_collect(&mut self) -> Result<usize> {
        if !self.memory_manager.gc_enabled {
            return Ok(0);
        }

        let start_time = std::time::Instant::now();
        let initial_usage = self.memory_manager.used_memory;

        // Simulate garbage collection
        let freed_memory = (self.memory_manager.used_memory as f64 * 0.1) as usize;
        self.memory_manager.used_memory =
            self.memory_manager.used_memory.saturating_sub(freed_memory);

        let gc_time = start_time.elapsed().as_secs_f64() * 1000.0;

        Ok(freed_memory)
    }
}

/// Benchmark TPU acceleration performance
pub fn benchmark_tpu_acceleration() -> Result<HashMap<String, f64>> {
    let mut results = HashMap::new();

    // Test different TPU configurations
    let configs = vec![
        TPUConfig {
            device_type: TPUDeviceType::TPUv4,
            num_cores: 8,
            batch_size: 16,
            ..Default::default()
        },
        TPUConfig {
            device_type: TPUDeviceType::TPUv5p,
            num_cores: 16,
            batch_size: 32,
            ..Default::default()
        },
        TPUConfig {
            device_type: TPUDeviceType::Simulated,
            num_cores: 32,
            batch_size: 64,
            enable_mixed_precision: true,
            ..Default::default()
        },
    ];

    for (i, config) in configs.into_iter().enumerate() {
        let start = std::time::Instant::now();

        let mut simulator = TPUQuantumSimulator::new(config)?;

        // Create test circuits
        let mut circuits = Vec::new();
        let mut initial_states = Vec::new();

        for _ in 0..simulator.config.batch_size.min(8) {
            let mut circuit = InterfaceCircuit::new(10, 0);

            // Add some gates
            circuit.add_gate(InterfaceGate::new(InterfaceGateType::Hadamard, vec![0]));
            circuit.add_gate(InterfaceGate::new(InterfaceGateType::CNOT, vec![0, 1]));
            circuit.add_gate(InterfaceGate::new(InterfaceGateType::RY(0.5), vec![2]));
            circuit.add_gate(InterfaceGate::new(InterfaceGateType::CZ, vec![1, 2]));

            circuits.push(circuit);

            // Create initial state
            let mut state = Array1::zeros(1 << 10);
            state[0] = Complex64::new(1.0, 0.0);
            initial_states.push(state);
        }

        // Execute batch
        let _final_states = simulator.execute_batch_circuit(&circuits, &initial_states)?;

        // Test expectation values
        let observables = vec!["Z0".to_string(), "X1".to_string(), "Y2".to_string()];
        let _expectations =
            simulator.compute_expectation_values_tpu(&initial_states, &observables)?;

        let time = start.elapsed().as_secs_f64() * 1000.0;
        results.insert(format!("tpu_config_{i}"), time);

        // Add performance metrics
        let stats = simulator.get_stats();
        results.insert(
            format!("tpu_config_{i}_operations"),
            stats.total_operations as f64,
        );
        results.insert(format!("tpu_config_{i}_avg_time"), stats.avg_operation_time);
        results.insert(
            format!("tpu_config_{i}_total_flops"),
            stats.total_flops as f64,
        );

        let performance_metrics = stats.get_performance_metrics();
        for (key, value) in performance_metrics {
            results.insert(format!("tpu_config_{i}_{key}"), value);
        }
    }

    Ok(results)
}

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

    #[test]
    fn test_tpu_simulator_creation() {
        let config = TPUConfig::default();
        let simulator = TPUQuantumSimulator::new(config);
        assert!(simulator.is_ok());
    }

    #[test]
    fn test_device_info_creation() {
        let device_info = TPUDeviceInfo::for_device_type(TPUDeviceType::TPUv4);
        assert_eq!(device_info.device_type, TPUDeviceType::TPUv4);
        assert_eq!(device_info.core_count, 2);
        assert_eq!(device_info.memory_size, 32.0);
        assert!(device_info.supports_complex);
    }

    #[test]
    fn test_xla_compilation() {
        let config = TPUConfig::default();
        let simulator = TPUQuantumSimulator::new(config).expect("Failed to create TPU simulator");

        assert!(simulator
            .xla_computations
            .contains_key("batched_single_qubit_gates"));
        assert!(simulator
            .xla_computations
            .contains_key("batched_cnot_gates"));
        assert!(simulator.xla_computations.contains_key("batch_normalize"));
        assert!(simulator.stats.total_compilation_time > 0.0);
    }

    #[test]
    fn test_memory_allocation() {
        let config = TPUConfig::default();
        let mut simulator =
            TPUQuantumSimulator::new(config).expect("Failed to create TPU simulator");

        let result = simulator.allocate_batch_memory(4, 1024);
        assert!(result.is_ok());
        assert!(simulator.tensor_buffers.contains_key("batch_states"));
        assert!(simulator.memory_manager.used_memory > 0);
    }

    #[test]
    fn test_memory_limit() {
        let config = TPUConfig {
            memory_per_core: 0.001, // Very small memory
            num_cores: 1,
            ..Default::default()
        };
        let mut simulator =
            TPUQuantumSimulator::new(config).expect("Failed to create TPU simulator");

        let result = simulator.allocate_batch_memory(1000, 1_000_000); // Large allocation
        assert!(result.is_err());
    }

    #[test]
    fn test_gate_matrix_generation() {
        let config = TPUConfig::default();
        let simulator = TPUQuantumSimulator::new(config).expect("Failed to create TPU simulator");

        let h_matrix = simulator.get_gate_matrix(&InterfaceGateType::H);
        assert_abs_diff_eq!(h_matrix[0].re, 1.0 / 2.0_f64.sqrt(), epsilon = 1e-10);

        let x_matrix = simulator.get_gate_matrix(&InterfaceGateType::X);
        assert_abs_diff_eq!(x_matrix[1].re, 1.0, epsilon = 1e-10);
        assert_abs_diff_eq!(x_matrix[2].re, 1.0, epsilon = 1e-10);
    }

    #[test]
    fn test_single_qubit_gate_application() {
        let config = TPUConfig::default();
        let mut simulator =
            TPUQuantumSimulator::new(config).expect("Failed to create TPU simulator");

        let mut state = Array1::zeros(4);
        state[0] = Complex64::new(1.0, 0.0);

        let gate = InterfaceGate::new(InterfaceGateType::H, vec![0]);
        let result = simulator
            .apply_single_qubit_gate_tpu(&state, &gate)
            .expect("Failed to apply single qubit gate");

        // After Hadamard, |0⟩ becomes (|0⟩ + |1⟩)/√2
        assert_abs_diff_eq!(result[0].norm(), 1.0 / 2.0_f64.sqrt(), epsilon = 1e-10);
        assert_abs_diff_eq!(result[1].norm(), 1.0 / 2.0_f64.sqrt(), epsilon = 1e-10);
    }

    #[test]
    fn test_two_qubit_gate_application() {
        let config = TPUConfig::default();
        let mut simulator =
            TPUQuantumSimulator::new(config).expect("Failed to create TPU simulator");

        let mut state = Array1::zeros(4);
        state[0] = Complex64::new(1.0 / 2.0_f64.sqrt(), 0.0);
        state[1] = Complex64::new(1.0 / 2.0_f64.sqrt(), 0.0);

        let gate = InterfaceGate::new(InterfaceGateType::CNOT, vec![0, 1]);
        let result = simulator
            .apply_two_qubit_gate_tpu(&state, &gate)
            .expect("Failed to apply two qubit gate");

        assert!(result.len() == 4);
    }

    #[test]
    fn test_batch_circuit_execution() {
        let config = TPUConfig {
            batch_size: 2,
            ..Default::default()
        };
        let mut simulator =
            TPUQuantumSimulator::new(config).expect("Failed to create TPU simulator");

        // Create test circuits
        let mut circuit1 = InterfaceCircuit::new(2, 0);
        circuit1.add_gate(InterfaceGate::new(InterfaceGateType::H, vec![0]));

        let mut circuit2 = InterfaceCircuit::new(2, 0);
        circuit2.add_gate(InterfaceGate::new(InterfaceGateType::X, vec![1]));

        let circuits = vec![circuit1, circuit2];

        // Create initial states
        let mut state1 = Array1::zeros(4);
        state1[0] = Complex64::new(1.0, 0.0);

        let mut state2 = Array1::zeros(4);
        state2[0] = Complex64::new(1.0, 0.0);

        let initial_states = vec![state1, state2];

        let result = simulator.execute_batch_circuit(&circuits, &initial_states);
        assert!(result.is_ok());

        let final_states = result.expect("Failed to execute batch circuit");
        assert_eq!(final_states.len(), 2);
    }

    #[test]
    fn test_expectation_value_computation() {
        let config = TPUConfig::default();
        let mut simulator =
            TPUQuantumSimulator::new(config).expect("Failed to create TPU simulator");

        // Create test states
        let mut state1 = Array1::zeros(4);
        state1[0] = Complex64::new(1.0, 0.0);

        let mut state2 = Array1::zeros(4);
        state2[3] = Complex64::new(1.0, 0.0);

        let states = vec![state1, state2];
        let observables = vec!["Z0".to_string(), "X1".to_string()];

        let result = simulator.compute_expectation_values_tpu(&states, &observables);
        assert!(result.is_ok());

        let expectations = result.expect("Failed to compute expectation values");
        assert_eq!(expectations.shape(), &[2, 2]);
    }

    #[test]
    fn test_stats_tracking() {
        let config = TPUConfig::default();
        let mut simulator =
            TPUQuantumSimulator::new(config).expect("Failed to create TPU simulator");

        simulator.stats.update_operation(10.0, 1000);
        simulator.stats.update_operation(20.0, 2000);

        assert_eq!(simulator.stats.total_operations, 2);
        assert_abs_diff_eq!(simulator.stats.total_execution_time, 30.0, epsilon = 1e-10);
        assert_abs_diff_eq!(simulator.stats.avg_operation_time, 15.0, epsilon = 1e-10);
        assert_eq!(simulator.stats.total_flops, 3000);
    }

    #[test]
    fn test_performance_metrics() {
        let config = TPUConfig::default();
        let mut simulator =
            TPUQuantumSimulator::new(config).expect("Failed to create TPU simulator");

        simulator.stats.total_operations = 100;
        simulator.stats.total_execution_time = 1000.0; // 1 second
        simulator.stats.total_flops = 1_000_000;
        simulator.stats.xla_cache_hits = 80;
        simulator.stats.xla_cache_misses = 20;

        let metrics = simulator.stats.get_performance_metrics();

        assert!(metrics.contains_key("flops_per_second"));
        assert!(metrics.contains_key("operations_per_second"));
        assert!(metrics.contains_key("cache_hit_rate"));

        assert_abs_diff_eq!(metrics["operations_per_second"], 100.0, epsilon = 1e-10);
        assert_abs_diff_eq!(metrics["cache_hit_rate"], 0.8, epsilon = 1e-10);
    }

    #[test]
    fn test_garbage_collection() {
        let config = TPUConfig::default();
        let mut simulator =
            TPUQuantumSimulator::new(config).expect("Failed to create TPU simulator");

        // Allocate some memory
        simulator.memory_manager.used_memory = 1_000_000;

        let result = simulator.garbage_collect();
        assert!(result.is_ok());

        let freed = result.expect("Failed garbage collection");
        assert!(freed > 0);
        assert!(simulator.memory_manager.used_memory < 1_000_000);
    }

    #[test]
    fn test_tpu_data_types() {
        assert_eq!(TPUDataType::Float32.size_bytes(), 4);
        assert_eq!(TPUDataType::Float64.size_bytes(), 8);
        assert_eq!(TPUDataType::BFloat16.size_bytes(), 2);
        assert_eq!(TPUDataType::Complex64.size_bytes(), 8);
        assert_eq!(TPUDataType::Complex128.size_bytes(), 16);
    }
}