quantrs2_sim/
optimized_simulator_simple.rs

1//! Optimized state vector simulator for quantum circuits (Simplified version)
2//!
3//! This module provides a high-performance simulator implementation focused on
4//! correctness and simplicity for quantum state vector simulation.
5
6use quantrs2_circuit::builder::{Circuit, Simulator};
7use quantrs2_core::{
8    error::{QuantRS2Error, QuantRS2Result},
9    gate::{multi, single, GateOp},
10    register::Register,
11};
12
13use crate::optimized_simple::OptimizedStateVector;
14
15/// A simplified optimized state vector simulator for quantum circuits
16#[derive(Debug, Clone)]
17pub struct OptimizedSimulatorSimple;
18
19impl OptimizedSimulatorSimple {
20    /// Create a new optimized simulator
21    #[must_use]
22    pub const fn new() -> Self {
23        Self
24    }
25}
26
27impl Default for OptimizedSimulatorSimple {
28    fn default() -> Self {
29        Self::new()
30    }
31}
32
33impl<const N: usize> Simulator<N> for OptimizedSimulatorSimple {
34    fn run(&self, circuit: &Circuit<N>) -> QuantRS2Result<Register<N>> {
35        // Initialize state vector
36        let mut state_vector = OptimizedStateVector::new(N);
37
38        // Apply each gate in the circuit
39        for gate in circuit.gates() {
40            match gate.name() {
41                // Single-qubit gates
42                "H" => {
43                    if let Some(g) = gate.as_any().downcast_ref::<single::Hadamard>() {
44                        let matrix = g.matrix()?;
45                        state_vector.apply_single_qubit_gate(&matrix, g.target.id() as usize);
46                    }
47                }
48                "X" => {
49                    if let Some(g) = gate.as_any().downcast_ref::<single::PauliX>() {
50                        let matrix = g.matrix()?;
51                        state_vector.apply_single_qubit_gate(&matrix, g.target.id() as usize);
52                    }
53                }
54                "Y" => {
55                    if let Some(g) = gate.as_any().downcast_ref::<single::PauliY>() {
56                        let matrix = g.matrix()?;
57                        state_vector.apply_single_qubit_gate(&matrix, g.target.id() as usize);
58                    }
59                }
60                "Z" => {
61                    if let Some(g) = gate.as_any().downcast_ref::<single::PauliZ>() {
62                        let matrix = g.matrix()?;
63                        state_vector.apply_single_qubit_gate(&matrix, g.target.id() as usize);
64                    }
65                }
66                "RX" => {
67                    if let Some(g) = gate.as_any().downcast_ref::<single::RotationX>() {
68                        let matrix = g.matrix()?;
69                        state_vector.apply_single_qubit_gate(&matrix, g.target.id() as usize);
70                    }
71                }
72                "RY" => {
73                    if let Some(g) = gate.as_any().downcast_ref::<single::RotationY>() {
74                        let matrix = g.matrix()?;
75                        state_vector.apply_single_qubit_gate(&matrix, g.target.id() as usize);
76                    }
77                }
78                "RZ" => {
79                    if let Some(g) = gate.as_any().downcast_ref::<single::RotationZ>() {
80                        let matrix = g.matrix()?;
81                        state_vector.apply_single_qubit_gate(&matrix, g.target.id() as usize);
82                    }
83                }
84                "S" => {
85                    if let Some(g) = gate.as_any().downcast_ref::<single::Phase>() {
86                        let matrix = g.matrix()?;
87                        state_vector.apply_single_qubit_gate(&matrix, g.target.id() as usize);
88                    }
89                }
90                "T" => {
91                    if let Some(g) = gate.as_any().downcast_ref::<single::T>() {
92                        let matrix = g.matrix()?;
93                        state_vector.apply_single_qubit_gate(&matrix, g.target.id() as usize);
94                    }
95                }
96
97                // Two-qubit gates
98                "CNOT" => {
99                    if let Some(g) = gate.as_any().downcast_ref::<multi::CNOT>() {
100                        state_vector.apply_cnot(g.control.id() as usize, g.target.id() as usize);
101                    }
102                }
103                "CZ" => {
104                    if let Some(g) = gate.as_any().downcast_ref::<multi::CZ>() {
105                        let matrix = g.matrix()?;
106                        state_vector.apply_two_qubit_gate(
107                            &matrix,
108                            g.control.id() as usize,
109                            g.target.id() as usize,
110                        );
111                    }
112                }
113                "SWAP" => {
114                    if let Some(g) = gate.as_any().downcast_ref::<multi::SWAP>() {
115                        let matrix = g.matrix()?;
116                        state_vector.apply_two_qubit_gate(
117                            &matrix,
118                            g.qubit1.id() as usize,
119                            g.qubit2.id() as usize,
120                        );
121                    }
122                }
123
124                // Three-qubit gates are not directly supported yet
125                "Toffoli" | "Fredkin" => {
126                    return Err(QuantRS2Error::UnsupportedOperation(
127                        format!("Direct {} gate not yet implemented in optimized simulator. Use gate decomposition.", gate.name())
128                    ));
129                }
130
131                _ => {
132                    return Err(QuantRS2Error::UnsupportedOperation(format!(
133                        "Gate {} not supported in optimized simulator",
134                        gate.name()
135                    )));
136                }
137            }
138        }
139
140        // Create register from final state
141        Register::<N>::with_amplitudes(state_vector.state().to_vec())
142    }
143}