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
//! JIT-enabled quantum simulator
//!
//! This module provides a quantum simulator with JIT compilation support.

use scirs2_core::ndarray::Array1;
use scirs2_core::Complex64;
use std::time::{Duration, Instant};

use crate::circuit_interfaces::{InterfaceGate, InterfaceGateType};
use crate::error::{Result, SimulatorError};

use super::compiler::JITCompiler;
use super::profiler::{JITCompilerStats, JITSimulatorStats};
use super::types::{JITBenchmarkResults, JITConfig};

/// JIT-enabled quantum simulator
pub struct JITQuantumSimulator {
    /// State vector
    state: Array1<Complex64>,
    /// Number of qubits
    pub(crate) num_qubits: usize,
    /// JIT compiler
    pub(crate) compiler: JITCompiler,
    /// Execution statistics
    stats: JITSimulatorStats,
}

impl JITQuantumSimulator {
    /// Create new JIT-enabled simulator
    #[must_use]
    pub fn new(num_qubits: usize, config: JITConfig) -> Self {
        let state_size = 1 << num_qubits;
        let mut state = Array1::zeros(state_size);
        state[0] = Complex64::new(1.0, 0.0); // |0...0⟩ state

        Self {
            state,
            num_qubits,
            compiler: JITCompiler::new(config),
            stats: JITSimulatorStats::default(),
        }
    }

    /// Apply gate sequence with JIT optimization
    pub fn apply_gate_sequence(&mut self, gates: &[InterfaceGate]) -> Result<Duration> {
        let execution_start = Instant::now();

        // Analyze sequence for compilation opportunities
        if let Some(pattern_hash) = self.compiler.analyze_sequence(gates)? {
            // Check if compiled version exists
            if self.is_compiled(pattern_hash) {
                // Execute compiled version
                let exec_time = self
                    .compiler
                    .execute_compiled(pattern_hash, &mut self.state)?;
                self.stats.compiled_executions += 1;
                self.stats.total_compiled_time += exec_time;
                return Ok(exec_time);
            }
        }

        // Fall back to interpreted execution
        for gate in gates {
            self.apply_gate_interpreted(gate)?;
        }

        let execution_time = execution_start.elapsed();
        self.stats.interpreted_executions += 1;
        self.stats.total_interpreted_time += execution_time;

        Ok(execution_time)
    }

    /// Check if pattern is compiled
    fn is_compiled(&self, pattern_hash: u64) -> bool {
        let cache = self
            .compiler
            .compiled_cache
            .read()
            .expect("JIT cache lock should not be poisoned");
        cache.contains_key(&pattern_hash)
    }

    /// Apply single gate in interpreted mode
    pub fn apply_gate_interpreted(&mut self, gate: &InterfaceGate) -> Result<()> {
        match &gate.gate_type {
            InterfaceGateType::PauliX | InterfaceGateType::X => {
                if gate.qubits.len() != 1 {
                    return Err(SimulatorError::InvalidParameter(
                        "Pauli-X requires exactly one target".to_string(),
                    ));
                }
                self.apply_pauli_x(gate.qubits[0])
            }
            InterfaceGateType::PauliY => {
                if gate.qubits.len() != 1 {
                    return Err(SimulatorError::InvalidParameter(
                        "Pauli-Y requires exactly one target".to_string(),
                    ));
                }
                self.apply_pauli_y(gate.qubits[0])
            }
            InterfaceGateType::PauliZ => {
                if gate.qubits.len() != 1 {
                    return Err(SimulatorError::InvalidParameter(
                        "Pauli-Z requires exactly one target".to_string(),
                    ));
                }
                self.apply_pauli_z(gate.qubits[0])
            }
            InterfaceGateType::Hadamard | InterfaceGateType::H => {
                if gate.qubits.len() != 1 {
                    return Err(SimulatorError::InvalidParameter(
                        "Hadamard requires exactly one target".to_string(),
                    ));
                }
                self.apply_hadamard(gate.qubits[0])
            }
            InterfaceGateType::CNOT => {
                if gate.qubits.len() != 2 {
                    return Err(SimulatorError::InvalidParameter(
                        "CNOT requires exactly two targets".to_string(),
                    ));
                }
                self.apply_cnot(gate.qubits[0], gate.qubits[1])
            }
            InterfaceGateType::RX(angle) => {
                if gate.qubits.len() != 1 {
                    return Err(SimulatorError::InvalidParameter(
                        "RX requires one target".to_string(),
                    ));
                }
                self.apply_rx(gate.qubits[0], *angle)
            }
            InterfaceGateType::RY(angle) => {
                if gate.qubits.len() != 1 {
                    return Err(SimulatorError::InvalidParameter(
                        "RY requires one target".to_string(),
                    ));
                }
                self.apply_ry(gate.qubits[0], *angle)
            }
            InterfaceGateType::RZ(angle) => {
                if gate.qubits.len() != 1 {
                    return Err(SimulatorError::InvalidParameter(
                        "RZ requires one target".to_string(),
                    ));
                }
                self.apply_rz(gate.qubits[0], *angle)
            }
            _ => Err(SimulatorError::NotImplemented(format!(
                "Gate type {:?}",
                gate.gate_type
            ))),
        }
    }

    /// Apply Pauli-X gate
    fn apply_pauli_x(&mut self, target: usize) -> Result<()> {
        if target >= self.num_qubits {
            return Err(SimulatorError::InvalidParameter(
                "Target qubit out of range".to_string(),
            ));
        }

        for i in 0..(1 << self.num_qubits) {
            let j = i ^ (1 << target);
            if i < j {
                let temp = self.state[i];
                self.state[i] = self.state[j];
                self.state[j] = temp;
            }
        }

        Ok(())
    }

    /// Apply Pauli-Y gate
    fn apply_pauli_y(&mut self, target: usize) -> Result<()> {
        if target >= self.num_qubits {
            return Err(SimulatorError::InvalidParameter(
                "Target qubit out of range".to_string(),
            ));
        }

        for i in 0..(1 << self.num_qubits) {
            if (i >> target) & 1 == 0 {
                let j = i | (1 << target);
                let temp = self.state[i];
                self.state[i] = Complex64::new(0.0, 1.0) * self.state[j];
                self.state[j] = Complex64::new(0.0, -1.0) * temp;
            }
        }

        Ok(())
    }

    /// Apply Pauli-Z gate
    fn apply_pauli_z(&mut self, target: usize) -> Result<()> {
        if target >= self.num_qubits {
            return Err(SimulatorError::InvalidParameter(
                "Target qubit out of range".to_string(),
            ));
        }

        for i in 0..(1 << self.num_qubits) {
            if (i >> target) & 1 == 1 {
                self.state[i] = -self.state[i];
            }
        }

        Ok(())
    }

    /// Apply Hadamard gate
    fn apply_hadamard(&mut self, target: usize) -> Result<()> {
        if target >= self.num_qubits {
            return Err(SimulatorError::InvalidParameter(
                "Target qubit out of range".to_string(),
            ));
        }

        let sqrt2_inv = 1.0 / (2.0_f64).sqrt();

        for i in 0..(1 << self.num_qubits) {
            if (i >> target) & 1 == 0 {
                let j = i | (1 << target);
                let amp0 = self.state[i];
                let amp1 = self.state[j];

                self.state[i] = sqrt2_inv * (amp0 + amp1);
                self.state[j] = sqrt2_inv * (amp0 - amp1);
            }
        }

        Ok(())
    }

    /// Apply CNOT gate
    fn apply_cnot(&mut self, control: usize, target: usize) -> Result<()> {
        if control >= self.num_qubits || target >= self.num_qubits {
            return Err(SimulatorError::InvalidParameter(
                "Qubit index out of range".to_string(),
            ));
        }

        for i in 0..(1 << self.num_qubits) {
            if (i >> control) & 1 == 1 {
                let j = i ^ (1 << target);
                if i < j {
                    let temp = self.state[i];
                    self.state[i] = self.state[j];
                    self.state[j] = temp;
                }
            }
        }

        Ok(())
    }

    /// Apply RX gate
    fn apply_rx(&mut self, target: usize, angle: f64) -> Result<()> {
        if target >= self.num_qubits {
            return Err(SimulatorError::InvalidParameter(
                "Target qubit out of range".to_string(),
            ));
        }

        let cos_half = (angle / 2.0).cos();
        let sin_half = (angle / 2.0).sin();

        for i in 0..(1 << self.num_qubits) {
            if (i >> target) & 1 == 0 {
                let j = i | (1 << target);
                let amp0 = self.state[i];
                let amp1 = self.state[j];

                self.state[i] = cos_half * amp0 - Complex64::new(0.0, sin_half) * amp1;
                self.state[j] = -Complex64::new(0.0, sin_half) * amp0 + cos_half * amp1;
            }
        }

        Ok(())
    }

    /// Apply RY gate
    fn apply_ry(&mut self, target: usize, angle: f64) -> Result<()> {
        if target >= self.num_qubits {
            return Err(SimulatorError::InvalidParameter(
                "Target qubit out of range".to_string(),
            ));
        }

        let cos_half = (angle / 2.0).cos();
        let sin_half = (angle / 2.0).sin();

        for i in 0..(1 << self.num_qubits) {
            if (i >> target) & 1 == 0 {
                let j = i | (1 << target);
                let amp0 = self.state[i];
                let amp1 = self.state[j];

                self.state[i] = cos_half * amp0 - sin_half * amp1;
                self.state[j] = sin_half * amp0 + cos_half * amp1;
            }
        }

        Ok(())
    }

    /// Apply RZ gate
    fn apply_rz(&mut self, target: usize, angle: f64) -> Result<()> {
        if target >= self.num_qubits {
            return Err(SimulatorError::InvalidParameter(
                "Target qubit out of range".to_string(),
            ));
        }

        let exp_neg = Complex64::new(0.0, -angle / 2.0).exp();
        let exp_pos = Complex64::new(0.0, angle / 2.0).exp();

        for i in 0..(1 << self.num_qubits) {
            if (i >> target) & 1 == 0 {
                self.state[i] *= exp_neg;
            } else {
                self.state[i] *= exp_pos;
            }
        }

        Ok(())
    }

    /// Get current state vector
    #[must_use]
    pub const fn get_state(&self) -> &Array1<Complex64> {
        &self.state
    }

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

    /// Get compiler statistics
    #[must_use]
    pub fn get_compiler_stats(&self) -> JITCompilerStats {
        self.compiler.get_stats()
    }
}

/// Benchmark JIT compilation system
pub fn benchmark_jit_compilation() -> Result<JITBenchmarkResults> {
    let num_qubits = 4;
    let config = JITConfig::default();
    let mut simulator = JITQuantumSimulator::new(num_qubits, config);

    // Create test gate sequences
    let gate_sequences = create_test_gate_sequences(num_qubits);

    let mut results = JITBenchmarkResults {
        total_sequences: gate_sequences.len(),
        compiled_sequences: 0,
        interpreted_sequences: 0,
        average_compilation_time: Duration::from_secs(0),
        average_execution_time_compiled: Duration::from_secs(0),
        average_execution_time_interpreted: Duration::from_secs(0),
        speedup_factor: 1.0,
        compilation_success_rate: 0.0,
        memory_usage_reduction: 0.0,
    };

    let mut total_execution_time_compiled = Duration::from_secs(0);
    let mut total_execution_time_interpreted = Duration::from_secs(0);

    // Run benchmarks
    for sequence in &gate_sequences {
        // First run (interpreted)
        let interpreted_time = simulator.apply_gate_sequence(sequence)?;
        total_execution_time_interpreted += interpreted_time;
        results.interpreted_sequences += 1;

        // Second run (potentially compiled)
        let execution_time = simulator.apply_gate_sequence(sequence)?;

        // Check if it was compiled
        if simulator.get_stats().compiled_executions > results.compiled_sequences {
            total_execution_time_compiled += execution_time;
            results.compiled_sequences += 1;
        }
    }

    // Calculate averages
    if results.compiled_sequences > 0 {
        results.average_execution_time_compiled =
            total_execution_time_compiled / results.compiled_sequences as u32;
    }

    if results.interpreted_sequences > 0 {
        results.average_execution_time_interpreted =
            total_execution_time_interpreted / results.interpreted_sequences as u32;
    }

    // Calculate speedup factor
    if results.average_execution_time_compiled.as_secs_f64() > 0.0 {
        results.speedup_factor = results.average_execution_time_interpreted.as_secs_f64()
            / results.average_execution_time_compiled.as_secs_f64();
    }

    // Calculate compilation success rate
    results.compilation_success_rate =
        results.compiled_sequences as f64 / results.total_sequences as f64;

    // Get compiler stats
    let compiler_stats = simulator.get_compiler_stats();
    if compiler_stats.total_compilations > 0 {
        results.average_compilation_time =
            compiler_stats.total_compilation_time / compiler_stats.total_compilations as u32;
    }

    Ok(results)
}

/// Create test gate sequences for benchmarking
pub fn create_test_gate_sequences(num_qubits: usize) -> Vec<Vec<InterfaceGate>> {
    let mut sequences = Vec::new();

    // Simple sequences
    for target in 0..num_qubits {
        sequences.push(vec![InterfaceGate::new(
            InterfaceGateType::PauliX,
            vec![target],
        )]);

        sequences.push(vec![InterfaceGate::new(
            InterfaceGateType::Hadamard,
            vec![target],
        )]);

        sequences.push(vec![InterfaceGate::new(
            InterfaceGateType::RX(std::f64::consts::PI / 4.0),
            vec![target],
        )]);
    }

    // Two-qubit sequences
    for control in 0..num_qubits {
        for target in 0..num_qubits {
            if control != target {
                sequences.push(vec![InterfaceGate::new(
                    InterfaceGateType::CNOT,
                    vec![control, target],
                )]);
            }
        }
    }

    // Longer sequences for compilation testing
    for target in 0..num_qubits {
        let sequence = vec![
            InterfaceGate::new(InterfaceGateType::Hadamard, vec![target]),
            InterfaceGate::new(
                InterfaceGateType::RZ(std::f64::consts::PI / 8.0),
                vec![target],
            ),
            InterfaceGate::new(InterfaceGateType::Hadamard, vec![target]),
        ];
        sequences.push(sequence);
    }

    // Repeat sequences multiple times to trigger compilation
    let mut repeated_sequences = Vec::new();
    for sequence in &sequences[0..5] {
        for _ in 0..15 {
            repeated_sequences.push(sequence.clone());
        }
    }

    sequences.extend(repeated_sequences);
    sequences
}