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