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
//! Mixed-precision quantum simulator implementation.
//!
//! This module provides the main simulator class that automatically
//! manages precision levels for optimal performance and accuracy.

use crate::adaptive_gate_fusion::{FusedGateBlock, GateType, QuantumGate};
use crate::error::{Result, SimulatorError};
use crate::prelude::SciRS2Backend;
use scirs2_core::ndarray::Array1;
use scirs2_core::random::prelude::*;
use scirs2_core::Complex64;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

use super::analysis::{PrecisionAnalysis, PrecisionAnalyzer};
use super::config::{MixedPrecisionConfig, QuantumPrecision};
use super::state_vector::MixedPrecisionStateVector;

/// Mixed-precision quantum simulator
pub struct MixedPrecisionSimulator {
    /// Configuration
    config: MixedPrecisionConfig,
    /// Current state vector
    state: Option<MixedPrecisionStateVector>,
    /// Number of qubits
    num_qubits: usize,
    /// SciRS2 backend for advanced operations
    #[cfg(feature = "advanced_math")]
    backend: Option<SciRS2Backend>,
    /// Performance statistics
    stats: MixedPrecisionStats,
    /// Precision analyzer
    analyzer: PrecisionAnalyzer,
}

/// Performance statistics for mixed-precision simulation
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct MixedPrecisionStats {
    /// Total number of gates applied
    pub total_gates: usize,
    /// Number of precision adaptations
    pub precision_adaptations: usize,
    /// Total execution time in milliseconds
    pub total_time_ms: f64,
    /// Memory usage by precision level
    pub memory_usage_by_precision: HashMap<QuantumPrecision, usize>,
    /// Gate execution time by precision
    pub gate_time_by_precision: HashMap<QuantumPrecision, f64>,
    /// Error estimates by precision
    pub error_estimates: HashMap<QuantumPrecision, f64>,
}

impl MixedPrecisionSimulator {
    /// Create a new mixed-precision simulator
    pub fn new(num_qubits: usize, config: MixedPrecisionConfig) -> Result<Self> {
        config.validate()?;

        let state = Some(MixedPrecisionStateVector::computational_basis(
            num_qubits,
            config.state_vector_precision,
        ));

        Ok(Self {
            config,
            state,
            num_qubits,
            #[cfg(feature = "advanced_math")]
            backend: None,
            stats: MixedPrecisionStats::default(),
            analyzer: PrecisionAnalyzer::new(),
        })
    }

    /// Initialize with SciRS2 backend
    #[cfg(feature = "advanced_math")]
    pub fn with_backend(mut self) -> Result<Self> {
        self.backend = Some(SciRS2Backend::new());
        Ok(self)
    }

    /// Apply a quantum gate with automatic precision selection
    pub fn apply_gate(&mut self, gate: &QuantumGate) -> Result<()> {
        let start_time = std::time::Instant::now();

        // Select optimal precision for this gate
        let gate_precision = self.select_gate_precision(gate)?;

        // Ensure state vector is in the correct precision
        self.adapt_state_precision(gate_precision)?;

        // Apply the gate
        self.apply_gate_with_precision(gate, gate_precision)?;

        // Update statistics
        let execution_time = start_time.elapsed().as_millis() as f64;
        self.stats.total_gates += 1;
        self.stats.total_time_ms += execution_time;
        *self
            .stats
            .gate_time_by_precision
            .entry(gate_precision)
            .or_insert(0.0) += execution_time;

        Ok(())
    }

    /// Apply multiple gates as a fused block
    pub fn apply_fused_block(&mut self, block: &FusedGateBlock) -> Result<()> {
        let optimal_precision = self.select_block_precision(block)?;
        self.adapt_state_precision(optimal_precision)?;

        // Apply each gate in the block
        for gate in &block.gates {
            self.apply_gate_with_precision(gate, optimal_precision)?;
        }

        Ok(())
    }

    /// Measure a qubit and return the result
    pub fn measure_qubit(&mut self, qubit: usize) -> Result<bool> {
        if qubit >= self.num_qubits {
            return Err(SimulatorError::InvalidInput(format!(
                "Qubit {} out of range for {}-qubit system",
                qubit, self.num_qubits
            )));
        }

        // Use measurement precision for this operation
        self.adapt_state_precision(self.config.measurement_precision)?;

        let state = self.state.as_ref().ok_or_else(|| {
            SimulatorError::InvalidOperation("State vector not initialized".to_string())
        })?;

        // Calculate probability of measuring |1⟩
        let mut prob_one = 0.0;
        let mask = 1 << qubit;
        for i in 0..state.len() {
            if i & mask != 0 {
                prob_one += state.probability(i)?;
            }
        }

        // Simulate random measurement
        let result = thread_rng().random::<f64>() < prob_one;

        // Collapse the state vector
        self.collapse_state(qubit, result)?;

        Ok(result)
    }

    /// Measure all qubits and return the bit string
    pub fn measure_all(&mut self) -> Result<Vec<bool>> {
        let mut results = Vec::new();
        for qubit in 0..self.num_qubits {
            results.push(self.measure_qubit(qubit)?);
        }
        Ok(results)
    }

    /// Get the current state vector
    #[must_use]
    pub const fn get_state(&self) -> Option<&MixedPrecisionStateVector> {
        self.state.as_ref()
    }

    /// Calculate expectation value of a Pauli operator
    pub fn expectation_value(&self, pauli_string: &str) -> Result<f64> {
        if pauli_string.len() != self.num_qubits {
            return Err(SimulatorError::InvalidInput(
                "Pauli string length must match number of qubits".to_string(),
            ));
        }

        let state = self.state.as_ref().ok_or_else(|| {
            SimulatorError::InvalidOperation("State vector not initialized".to_string())
        })?;
        let mut expectation = 0.0;

        for i in 0..state.len() {
            let mut sign = 1.0;
            let mut amplitude = state.amplitude(i)?;

            // Apply Pauli operators
            for (qubit, pauli) in pauli_string.chars().enumerate() {
                match pauli {
                    'I' => {} // Identity - no change
                    'X' => {
                        // Flip bit
                        let flipped = i ^ (1 << qubit);
                        amplitude = state.amplitude(flipped)?;
                    }
                    'Y' => {
                        // Flip bit and apply phase
                        if i & (1 << qubit) != 0 {
                            sign *= -1.0;
                        }
                        amplitude *= Complex64::new(0.0, sign);
                    }
                    'Z' => {
                        // Apply phase
                        if i & (1 << qubit) != 0 {
                            sign *= -1.0;
                        }
                    }
                    _ => {
                        return Err(SimulatorError::InvalidInput(format!(
                            "Invalid Pauli operator: {pauli}"
                        )))
                    }
                }
            }

            expectation += (amplitude.conj() * amplitude * sign).re;
        }

        Ok(expectation)
    }

    /// Run precision analysis
    pub fn analyze_precision(&mut self) -> Result<PrecisionAnalysis> {
        Ok(self
            .analyzer
            .analyze_for_tolerance(self.config.error_tolerance))
    }

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

    /// Reset the simulator to initial state
    pub fn reset(&mut self) -> Result<()> {
        self.state = Some(MixedPrecisionStateVector::computational_basis(
            self.num_qubits,
            self.config.state_vector_precision,
        ));
        self.stats = MixedPrecisionStats::default();
        self.analyzer.reset();
        Ok(())
    }

    /// Select optimal precision for a gate
    fn select_gate_precision(&self, gate: &QuantumGate) -> Result<QuantumPrecision> {
        if !self.config.adaptive_precision {
            return Ok(self.config.gate_precision);
        }

        // Use heuristics to select precision based on gate type
        let precision = match gate.gate_type {
            GateType::PauliX
            | GateType::PauliY
            | GateType::PauliZ
            | GateType::Hadamard
            | GateType::Phase
            | GateType::T
            | GateType::RotationX
            | GateType::RotationY
            | GateType::RotationZ
            | GateType::Identity => {
                // Single qubit gates are usually numerically stable
                if self.config.gate_precision == QuantumPrecision::Adaptive {
                    QuantumPrecision::Single
                } else {
                    self.config.gate_precision
                }
            }
            GateType::CNOT | GateType::CZ | GateType::SWAP | GateType::ISwap => {
                // Two qubit gates may require higher precision
                if self.config.gate_precision == QuantumPrecision::Adaptive {
                    if self.num_qubits > self.config.large_system_threshold {
                        QuantumPrecision::Single
                    } else {
                        QuantumPrecision::Double
                    }
                } else {
                    self.config.gate_precision
                }
            }
            GateType::Toffoli | GateType::Fredkin => {
                // Multi-qubit gates typically need higher precision
                if self.config.gate_precision == QuantumPrecision::Adaptive {
                    QuantumPrecision::Double
                } else {
                    self.config.gate_precision
                }
            }
            GateType::Custom(_) => {
                // Custom gates - use conservative precision
                QuantumPrecision::Double
            }
        };

        Ok(precision)
    }

    /// Select optimal precision for a fused gate block
    const fn select_block_precision(&self, _block: &FusedGateBlock) -> Result<QuantumPrecision> {
        // For fused blocks, use a conservative approach
        if self.config.adaptive_precision {
            Ok(QuantumPrecision::Single)
        } else {
            Ok(self.config.gate_precision)
        }
    }

    /// Adapt state vector to the target precision
    fn adapt_state_precision(&mut self, target_precision: QuantumPrecision) -> Result<()> {
        if let Some(ref state) = self.state {
            if state.precision() != target_precision {
                let new_state = state.to_precision(target_precision)?;
                self.state = Some(new_state);
                self.stats.precision_adaptations += 1;
            }
        }
        Ok(())
    }

    /// Apply a gate with a specific precision
    fn apply_gate_with_precision(
        &mut self,
        gate: &QuantumGate,
        _precision: QuantumPrecision,
    ) -> Result<()> {
        // This is a simplified implementation
        // In practice, this would apply the actual gate operation
        if let Some(ref mut state) = self.state {
            // For demonstration, just record that we applied a gate
            // Real implementation would perform matrix multiplication

            // Update memory usage statistics
            let memory_usage = state.memory_usage();
            self.stats
                .memory_usage_by_precision
                .insert(state.precision(), memory_usage);
        }

        Ok(())
    }

    /// Collapse the state vector after measurement
    fn collapse_state(&mut self, qubit: usize, result: bool) -> Result<()> {
        if let Some(ref mut state) = self.state {
            let mask = 1 << qubit;
            let mut norm_factor = 0.0;

            // Calculate normalization factor
            for i in 0..state.len() {
                let bit_value = (i & mask) != 0;
                if bit_value == result {
                    norm_factor += state.probability(i)?;
                }
            }

            if norm_factor == 0.0 {
                return Err(SimulatorError::InvalidInput(
                    "Invalid measurement result: zero probability".to_string(),
                ));
            }

            norm_factor = norm_factor.sqrt();

            // Update amplitudes
            for i in 0..state.len() {
                let bit_value = (i & mask) != 0;
                if bit_value == result {
                    let amplitude = state.amplitude(i)?;
                    state.set_amplitude(i, amplitude / norm_factor)?;
                } else {
                    state.set_amplitude(i, Complex64::new(0.0, 0.0))?;
                }
            }
        }

        Ok(())
    }
}

impl MixedPrecisionStats {
    /// Calculate average gate time
    #[must_use]
    pub fn average_gate_time(&self) -> f64 {
        if self.total_gates > 0 {
            self.total_time_ms / self.total_gates as f64
        } else {
            0.0
        }
    }

    /// Get total memory usage across all precisions
    #[must_use]
    pub fn total_memory_usage(&self) -> usize {
        self.memory_usage_by_precision.values().sum()
    }

    /// Get adaptation rate (adaptations per gate)
    #[must_use]
    pub fn adaptation_rate(&self) -> f64 {
        if self.total_gates > 0 {
            self.precision_adaptations as f64 / self.total_gates as f64
        } else {
            0.0
        }
    }

    /// Get performance summary
    #[must_use]
    pub fn summary(&self) -> String {
        format!(
            "Gates: {}, Adaptations: {}, Avg Time: {:.2}ms, Total Memory: {}MB",
            self.total_gates,
            self.precision_adaptations,
            self.average_gate_time(),
            self.total_memory_usage() / (1024 * 1024)
        )
    }
}

/// Utility functions for mixed precision simulation
pub mod utils {
    use super::{
        Array1, Complex64, MixedPrecisionConfig, MixedPrecisionStateVector, QuantumPrecision,
        Result,
    };

    /// Convert a regular state vector to mixed precision
    pub fn convert_state_vector(
        state: &Array1<Complex64>,
        precision: QuantumPrecision,
    ) -> Result<MixedPrecisionStateVector> {
        let mut mp_state = MixedPrecisionStateVector::new(state.len(), precision);
        for (i, &amplitude) in state.iter().enumerate() {
            mp_state.set_amplitude(i, amplitude)?;
        }
        Ok(mp_state)
    }

    /// Extract a regular state vector from mixed precision
    pub fn extract_state_vector(mp_state: &MixedPrecisionStateVector) -> Result<Array1<Complex64>> {
        let mut state = Array1::zeros(mp_state.len());
        for i in 0..mp_state.len() {
            state[i] = mp_state.amplitude(i)?;
        }
        Ok(state)
    }

    /// Calculate memory savings compared to double precision
    #[must_use]
    pub fn memory_savings(config: &MixedPrecisionConfig, num_qubits: usize) -> f64 {
        let double_precision_size = (1 << num_qubits) * std::mem::size_of::<Complex64>();
        let mixed_precision_size = config.estimate_memory_usage(num_qubits);
        1.0 - (mixed_precision_size as f64 / double_precision_size as f64)
    }

    /// Estimate performance improvement factor
    #[must_use]
    pub fn performance_improvement_factor(precision: QuantumPrecision) -> f64 {
        1.0 / precision.computation_factor()
    }
}