quantrs2_sim/
optimized_simulator_chunked.rs1use 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_chunked::ChunkedStateVector;
14
15#[derive(Debug, Clone)]
17pub struct OptimizedSimulatorChunked;
18
19impl OptimizedSimulatorChunked {
20 #[must_use]
22 pub const fn new() -> Self {
23 Self
24 }
25}
26
27impl Default for OptimizedSimulatorChunked {
28 fn default() -> Self {
29 Self::new()
30 }
31}
32
33impl<const N: usize> Simulator<N> for OptimizedSimulatorChunked {
34 fn run(&self, circuit: &Circuit<N>) -> QuantRS2Result<Register<N>> {
35 if N > 25 {
37 let mut state_vector = ChunkedStateVector::new(N);
39
40 for gate in circuit.gates() {
42 match gate.name() {
43 "H" => {
45 if let Some(g) = gate.as_any().downcast_ref::<single::Hadamard>() {
46 let matrix = g.matrix()?;
47 state_vector.apply_single_qubit_gate(&matrix, g.target.id() as usize);
48 }
49 }
50 "X" => {
51 if let Some(g) = gate.as_any().downcast_ref::<single::PauliX>() {
52 let matrix = g.matrix()?;
53 state_vector.apply_single_qubit_gate(&matrix, g.target.id() as usize);
54 }
55 }
56 "Y" => {
57 if let Some(g) = gate.as_any().downcast_ref::<single::PauliY>() {
58 let matrix = g.matrix()?;
59 state_vector.apply_single_qubit_gate(&matrix, g.target.id() as usize);
60 }
61 }
62 "Z" => {
63 if let Some(g) = gate.as_any().downcast_ref::<single::PauliZ>() {
64 let matrix = g.matrix()?;
65 state_vector.apply_single_qubit_gate(&matrix, g.target.id() as usize);
66 }
67 }
68 "RX" => {
69 if let Some(g) = gate.as_any().downcast_ref::<single::RotationX>() {
70 let matrix = g.matrix()?;
71 state_vector.apply_single_qubit_gate(&matrix, g.target.id() as usize);
72 }
73 }
74 "RY" => {
75 if let Some(g) = gate.as_any().downcast_ref::<single::RotationY>() {
76 let matrix = g.matrix()?;
77 state_vector.apply_single_qubit_gate(&matrix, g.target.id() as usize);
78 }
79 }
80 "RZ" => {
81 if let Some(g) = gate.as_any().downcast_ref::<single::RotationZ>() {
82 let matrix = g.matrix()?;
83 state_vector.apply_single_qubit_gate(&matrix, g.target.id() as usize);
84 }
85 }
86 "S" => {
87 if let Some(g) = gate.as_any().downcast_ref::<single::Phase>() {
88 let matrix = g.matrix()?;
89 state_vector.apply_single_qubit_gate(&matrix, g.target.id() as usize);
90 }
91 }
92 "T" => {
93 if let Some(g) = gate.as_any().downcast_ref::<single::T>() {
94 let matrix = g.matrix()?;
95 state_vector.apply_single_qubit_gate(&matrix, g.target.id() as usize);
96 }
97 }
98
99 "CNOT" => {
101 if let Some(g) = gate.as_any().downcast_ref::<multi::CNOT>() {
102 state_vector
103 .apply_cnot(g.control.id() as usize, g.target.id() as usize);
104 }
105 }
106 "CZ" => {
107 if let Some(g) = gate.as_any().downcast_ref::<multi::CZ>() {
108 let matrix = g.matrix()?;
109 state_vector.apply_two_qubit_gate(
110 &matrix,
111 g.control.id() as usize,
112 g.target.id() as usize,
113 );
114 }
115 }
116 "SWAP" => {
117 if let Some(g) = gate.as_any().downcast_ref::<multi::SWAP>() {
118 let matrix = g.matrix()?;
119 state_vector.apply_two_qubit_gate(
120 &matrix,
121 g.qubit1.id() as usize,
122 g.qubit2.id() as usize,
123 );
124 }
125 }
126
127 "Toffoli" | "Fredkin" => {
129 return Err(QuantRS2Error::UnsupportedOperation(
130 format!("Direct {} gate not yet implemented in optimized simulator. Use gate decomposition.", gate.name())
131 ));
132 }
133
134 _ => {
135 return Err(QuantRS2Error::UnsupportedOperation(format!(
136 "Gate {} not supported in optimized simulator",
137 gate.name()
138 )));
139 }
140 }
141 }
142
143 if N > 30 {
146 let amplitudes = state_vector.as_vec();
149 Register::<N>::with_amplitudes(amplitudes)
150 } else {
151 let amplitudes = state_vector.as_vec();
153 Register::<N>::with_amplitudes(amplitudes)
154 }
155 } else {
156 use crate::optimized_simple::OptimizedStateVector;
159
160 let mut state_vector = OptimizedStateVector::new(N);
161
162 for gate in circuit.gates() {
164 match gate.name() {
165 "H" => {
167 if let Some(g) = gate.as_any().downcast_ref::<single::Hadamard>() {
168 let matrix = g.matrix()?;
169 state_vector.apply_single_qubit_gate(&matrix, g.target.id() as usize);
170 }
171 }
172 "X" => {
173 if let Some(g) = gate.as_any().downcast_ref::<single::PauliX>() {
174 let matrix = g.matrix()?;
175 state_vector.apply_single_qubit_gate(&matrix, g.target.id() as usize);
176 }
177 }
178 "Y" => {
179 if let Some(g) = gate.as_any().downcast_ref::<single::PauliY>() {
180 let matrix = g.matrix()?;
181 state_vector.apply_single_qubit_gate(&matrix, g.target.id() as usize);
182 }
183 }
184 "Z" => {
185 if let Some(g) = gate.as_any().downcast_ref::<single::PauliZ>() {
186 let matrix = g.matrix()?;
187 state_vector.apply_single_qubit_gate(&matrix, g.target.id() as usize);
188 }
189 }
190 "RX" => {
191 if let Some(g) = gate.as_any().downcast_ref::<single::RotationX>() {
192 let matrix = g.matrix()?;
193 state_vector.apply_single_qubit_gate(&matrix, g.target.id() as usize);
194 }
195 }
196 "RY" => {
197 if let Some(g) = gate.as_any().downcast_ref::<single::RotationY>() {
198 let matrix = g.matrix()?;
199 state_vector.apply_single_qubit_gate(&matrix, g.target.id() as usize);
200 }
201 }
202 "RZ" => {
203 if let Some(g) = gate.as_any().downcast_ref::<single::RotationZ>() {
204 let matrix = g.matrix()?;
205 state_vector.apply_single_qubit_gate(&matrix, g.target.id() as usize);
206 }
207 }
208 "S" => {
209 if let Some(g) = gate.as_any().downcast_ref::<single::Phase>() {
210 let matrix = g.matrix()?;
211 state_vector.apply_single_qubit_gate(&matrix, g.target.id() as usize);
212 }
213 }
214 "T" => {
215 if let Some(g) = gate.as_any().downcast_ref::<single::T>() {
216 let matrix = g.matrix()?;
217 state_vector.apply_single_qubit_gate(&matrix, g.target.id() as usize);
218 }
219 }
220
221 "CNOT" => {
223 if let Some(g) = gate.as_any().downcast_ref::<multi::CNOT>() {
224 state_vector
225 .apply_cnot(g.control.id() as usize, g.target.id() as usize);
226 }
227 }
228 "CZ" => {
229 if let Some(g) = gate.as_any().downcast_ref::<multi::CZ>() {
230 let matrix = g.matrix()?;
231 state_vector.apply_two_qubit_gate(
232 &matrix,
233 g.control.id() as usize,
234 g.target.id() as usize,
235 );
236 }
237 }
238 "SWAP" => {
239 if let Some(g) = gate.as_any().downcast_ref::<multi::SWAP>() {
240 let matrix = g.matrix()?;
241 state_vector.apply_two_qubit_gate(
242 &matrix,
243 g.qubit1.id() as usize,
244 g.qubit2.id() as usize,
245 );
246 }
247 }
248
249 "Toffoli" | "Fredkin" => {
251 return Err(QuantRS2Error::UnsupportedOperation(
252 format!("Direct {} gate not yet implemented in optimized simulator. Use gate decomposition.", gate.name())
253 ));
254 }
255
256 _ => {
257 return Err(QuantRS2Error::UnsupportedOperation(format!(
258 "Gate {} not supported in optimized simulator",
259 gate.name()
260 )));
261 }
262 }
263 }
264
265 Register::<N>::with_amplitudes(state_vector.state().to_vec())
267 }
268 }
269}