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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
//! Basic optimization passes: gate cancellation, commutation, merging, and rotation merging.
use crate::optimization::cost_model::CostModel;
use crate::optimization::gate_properties::CommutationTable;
use quantrs2_core::error::QuantRS2Result;
use quantrs2_core::gate::{
multi,
single::{self, RotationX, RotationY, RotationZ},
GateOp,
};
use quantrs2_core::qubit::QubitId;
use std::collections::HashSet;
use std::f64::consts::PI;
use super::OptimizationPass;
/// Gate cancellation pass - removes redundant gates
pub struct GateCancellation {
aggressive: bool,
}
impl GateCancellation {
#[must_use]
pub const fn new(aggressive: bool) -> Self {
Self { aggressive }
}
}
impl OptimizationPass for GateCancellation {
fn name(&self) -> &'static str {
"Gate Cancellation"
}
fn apply_to_gates(
&self,
gates: Vec<Box<dyn GateOp>>,
_cost_model: &dyn CostModel,
) -> QuantRS2Result<Vec<Box<dyn GateOp>>> {
let mut optimized = Vec::new();
let mut i = 0;
while i < gates.len() {
if i + 1 < gates.len() {
let gate1 = &gates[i];
let gate2 = &gates[i + 1];
// Check if gates act on the same qubits
if gate1.qubits() == gate2.qubits() && gate1.name() == gate2.name() {
// Check for self-inverse gates (H, X, Y, Z)
match gate1.name() {
"H" | "X" | "Y" | "Z" => {
// These gates cancel when applied twice - skip both
i += 2;
continue;
}
"RX" | "RY" | "RZ" => {
// Check if rotations cancel
if let (Some(rx1), Some(rx2)) = (
gate1.as_any().downcast_ref::<single::RotationX>(),
gate2.as_any().downcast_ref::<single::RotationX>(),
) {
let combined_angle = rx1.theta + rx2.theta;
// Check if the combined rotation is effectively zero
if (combined_angle % (2.0 * PI)).abs() < 1e-10 {
i += 2;
continue;
}
} else if let (Some(ry1), Some(ry2)) = (
gate1.as_any().downcast_ref::<single::RotationY>(),
gate2.as_any().downcast_ref::<single::RotationY>(),
) {
let combined_angle = ry1.theta + ry2.theta;
if (combined_angle % (2.0 * PI)).abs() < 1e-10 {
i += 2;
continue;
}
} else if let (Some(rz1), Some(rz2)) = (
gate1.as_any().downcast_ref::<single::RotationZ>(),
gate2.as_any().downcast_ref::<single::RotationZ>(),
) {
let combined_angle = rz1.theta + rz2.theta;
if (combined_angle % (2.0 * PI)).abs() < 1e-10 {
i += 2;
continue;
}
}
}
"CNOT" => {
// CNOT is self-inverse
if let (Some(cnot1), Some(cnot2)) = (
gate1.as_any().downcast_ref::<multi::CNOT>(),
gate2.as_any().downcast_ref::<multi::CNOT>(),
) {
if cnot1.control == cnot2.control && cnot1.target == cnot2.target {
i += 2;
continue;
}
}
}
_ => {}
}
}
// Look for more complex cancellations if aggressive mode is enabled
if self.aggressive && i + 2 < gates.len() {
// Check for patterns like X-Y-X-Y or Z-H-Z-H
let gate3 = &gates[i + 2];
if gate1.qubits() == gate3.qubits()
&& gate1.name() == gate3.name()
&& i + 3 < gates.len()
{
let gate4 = &gates[i + 3];
if gate2.qubits() == gate4.qubits()
&& gate2.name() == gate4.name()
&& gate1.qubits() == gate2.qubits()
{
// Pattern detected, check if it simplifies
match (gate1.name(), gate2.name()) {
("X", "Y") | ("Y", "X") | ("Z", "H") | ("H", "Z") => {
// These patterns can sometimes simplify
// For now, we'll keep them as they might not always cancel
}
_ => {}
}
}
}
}
}
// If we didn't skip, add the gate to optimized list
optimized.push(gates[i].clone());
i += 1;
}
Ok(optimized)
}
}
/// Gate commutation pass - reorders gates to enable other optimizations
pub struct GateCommutation {
max_lookahead: usize,
commutation_table: CommutationTable,
}
impl GateCommutation {
#[must_use]
pub fn new(max_lookahead: usize) -> Self {
Self {
max_lookahead,
commutation_table: CommutationTable::new(),
}
}
}
impl GateCommutation {
/// Check if two gates commute based on commutation rules
fn gates_commute(&self, gate1: &dyn GateOp, gate2: &dyn GateOp) -> bool {
// Use commutation table if available
if self.commutation_table.commutes(gate1.name(), gate2.name()) {
return true;
}
// Additional commutation rules
match (gate1.name(), gate2.name()) {
// Pauli gates commutation
("X", "X") | ("Y", "Y") | ("Z", "Z") => true,
("I", _) | (_, "I") => true,
// Phase/T gates commute with Z
("S" | "T", "Z") | ("Z", "S" | "T") => true,
// Same-axis rotations commute
("RX", "RX") | ("RY", "RY") | ("RZ", "RZ") => true,
// RZ commutes with Z-like gates
("RZ", "Z" | "S" | "T") | ("Z" | "S" | "T", "RZ") => true,
_ => false,
}
}
/// Check if swapping gates at position i would enable optimizations
fn would_benefit_from_swap(&self, gates: &[Box<dyn GateOp>], i: usize) -> bool {
if i + 2 >= gates.len() {
return false;
}
let gate1 = &gates[i];
let gate2 = &gates[i + 1];
let gate3 = &gates[i + 2];
// Check if swapping would create cancellation opportunities
if gate1.name() == gate3.name() && gate1.qubits() == gate3.qubits() {
// After swap, gate2 and gate3 (originally gate1) would be adjacent
match gate3.name() {
"H" | "X" | "Y" | "Z" => return true,
_ => {}
}
}
// Check if swapping would enable rotation merging
if gate2.name() == gate3.name() && gate2.qubits() == gate3.qubits() {
match gate2.name() {
"RX" | "RY" | "RZ" => return true,
_ => {}
}
}
false
}
}
impl OptimizationPass for GateCommutation {
fn name(&self) -> &'static str {
"Gate Commutation"
}
fn apply_to_gates(
&self,
gates: Vec<Box<dyn GateOp>>,
_cost_model: &dyn CostModel,
) -> QuantRS2Result<Vec<Box<dyn GateOp>>> {
if gates.len() < 2 {
return Ok(gates);
}
let mut optimized = gates;
// Bound the number of outer iterations to prevent oscillation.
// Each pass does at most one forward scan; repeated passes let reordering
// propagate, but the bound ensures we always terminate.
let max_outer = self.max_lookahead * 2 + 1;
let mut outer_iter = 0;
let mut changed = true;
// Keep trying to commute gates until no more changes or the iteration
// bound is reached.
while changed && outer_iter < max_outer {
changed = false;
outer_iter += 1;
let mut i = 0;
while i < optimized.len().saturating_sub(1) {
let can_swap = {
let gate1 = &optimized[i];
let gate2 = &optimized[i + 1];
// Check if gates act on different qubits (always commute)
let qubits1: HashSet<_> = gate1.qubits().into_iter().collect();
let qubits2: HashSet<_> = gate2.qubits().into_iter().collect();
if qubits1.is_disjoint(&qubits2) {
// Gates on disjoint qubits: only swap when it would enable
// further optimisations (not just because they commute).
self.would_benefit_from_swap(&optimized, i)
} else if qubits1 == qubits2 {
// Same qubit set: only swap when a downstream gate of the
// same type exists that could later cancel or merge.
// Swapping two identical same-qubit gates is always a no-op,
// so guard against that first.
if gate1.name() == gate2.name() {
// Identical gate names on same qubits: swapping achieves
// nothing useful — skip to avoid oscillation.
false
} else {
self.gates_commute(gate1.as_ref(), gate2.as_ref())
}
} else {
// Overlapping but not identical qubit sets
false
}
};
if can_swap {
optimized.swap(i, i + 1);
changed = true;
}
// Always advance forward to avoid cycling on the same pair.
i += 1;
// Limit lookahead to prevent excessive computation
if i >= self.max_lookahead {
break;
}
}
}
Ok(optimized)
}
}
/// Gate merging pass - combines adjacent gates
pub struct GateMerging {
merge_rotations: bool,
merge_threshold: f64,
}
impl GateMerging {
#[must_use]
pub const fn new(merge_rotations: bool, merge_threshold: f64) -> Self {
Self {
merge_rotations,
merge_threshold,
}
}
}
impl OptimizationPass for GateMerging {
fn name(&self) -> &'static str {
"Gate Merging"
}
fn apply_to_gates(
&self,
gates: Vec<Box<dyn GateOp>>,
_cost_model: &dyn CostModel,
) -> QuantRS2Result<Vec<Box<dyn GateOp>>> {
let mut optimized = Vec::new();
let mut i = 0;
while i < gates.len() {
if i + 1 < gates.len() && self.merge_rotations {
let gate1 = &gates[i];
let gate2 = &gates[i + 1];
// Try to merge rotation gates
if gate1.qubits() == gate2.qubits() {
let merged = match (gate1.name(), gate2.name()) {
// Same-axis rotations can be directly merged
("RX", "RX") | ("RY", "RY") | ("RZ", "RZ") => {
// Already handled by RotationMerging pass, skip here
None
}
// Different axis rotations might be mergeable using Euler decomposition
("RZ" | "RY", "RX") | ("RX" | "RY", "RZ") | ("RX" | "RZ", "RY")
if self.merge_threshold > 0.0 =>
{
// Complex merging would require matrix multiplication
// For now, skip this advanced optimization
None
}
// Phase gates (S, T) can sometimes be merged with RZ
("S" | "T", "RZ") | ("RZ", "S" | "T") => {
// S = RZ(π/2), T = RZ(π/4)
// These could be merged but need special handling
None
}
_ => None,
};
if let Some(merged_gate) = merged {
optimized.push(merged_gate);
i += 2;
continue;
}
}
}
// Check for special merging patterns
if i + 1 < gates.len() {
let gate1 = &gates[i];
let gate2 = &gates[i + 1];
// H-Z-H = X, H-X-H = Z (basis change)
if i + 2 < gates.len() {
let gate3 = &gates[i + 2];
if gate1.name() == "H"
&& gate3.name() == "H"
&& gate1.qubits() == gate2.qubits()
&& gate2.qubits() == gate3.qubits()
{
match gate2.name() {
"Z" => {
// H-Z-H = X
optimized.push(Box::new(single::PauliX {
target: gate1.qubits()[0],
})
as Box<dyn GateOp>);
i += 3;
continue;
}
"X" => {
// H-X-H = Z
optimized.push(Box::new(single::PauliZ {
target: gate1.qubits()[0],
})
as Box<dyn GateOp>);
i += 3;
continue;
}
_ => {}
}
}
}
}
// If no merging happened, keep the original gate
optimized.push(gates[i].clone());
i += 1;
}
Ok(optimized)
}
}
/// Rotation merging pass - specifically merges rotation gates
pub struct RotationMerging {
tolerance: f64,
}
impl RotationMerging {
#[must_use]
pub const fn new(tolerance: f64) -> Self {
Self { tolerance }
}
/// Check if angle is effectively zero (or 2π multiple)
fn is_zero_rotation(&self, angle: f64) -> bool {
let normalized = angle % (2.0 * PI);
normalized.abs() < self.tolerance || 2.0f64.mul_add(-PI, normalized).abs() < self.tolerance
}
/// Merge two rotation angles
fn merge_angles(&self, angle1: f64, angle2: f64) -> f64 {
let merged = angle1 + angle2;
let normalized = merged % (2.0 * PI);
if normalized > PI {
2.0f64.mul_add(-PI, normalized)
} else if normalized < -PI {
2.0f64.mul_add(PI, normalized)
} else {
normalized
}
}
}
impl OptimizationPass for RotationMerging {
fn name(&self) -> &'static str {
"Rotation Merging"
}
fn apply_to_gates(
&self,
gates: Vec<Box<dyn GateOp>>,
_cost_model: &dyn CostModel,
) -> QuantRS2Result<Vec<Box<dyn GateOp>>> {
let mut optimized = Vec::new();
let mut i = 0;
while i < gates.len() {
if i + 1 < gates.len() {
let gate1 = &gates[i];
let gate2 = &gates[i + 1];
// Check if both gates are rotations on the same qubit and axis
if gate1.qubits() == gate2.qubits() && gate1.name() == gate2.name() {
match gate1.name() {
"RX" => {
if let (Some(rx1), Some(rx2)) = (
gate1.as_any().downcast_ref::<single::RotationX>(),
gate2.as_any().downcast_ref::<single::RotationX>(),
) {
let merged_angle = self.merge_angles(rx1.theta, rx2.theta);
if self.is_zero_rotation(merged_angle) {
// Skip both gates if the merged rotation is effectively zero
i += 2;
continue;
}
// Create a new merged rotation gate
optimized.push(Box::new(single::RotationX {
target: rx1.target,
theta: merged_angle,
})
as Box<dyn GateOp>);
i += 2;
continue;
}
}
"RY" => {
if let (Some(ry1), Some(ry2)) = (
gate1.as_any().downcast_ref::<single::RotationY>(),
gate2.as_any().downcast_ref::<single::RotationY>(),
) {
let merged_angle = self.merge_angles(ry1.theta, ry2.theta);
if self.is_zero_rotation(merged_angle) {
i += 2;
continue;
}
optimized.push(Box::new(single::RotationY {
target: ry1.target,
theta: merged_angle,
})
as Box<dyn GateOp>);
i += 2;
continue;
}
}
"RZ" => {
if let (Some(rz1), Some(rz2)) = (
gate1.as_any().downcast_ref::<single::RotationZ>(),
gate2.as_any().downcast_ref::<single::RotationZ>(),
) {
let merged_angle = self.merge_angles(rz1.theta, rz2.theta);
if self.is_zero_rotation(merged_angle) {
i += 2;
continue;
}
optimized.push(Box::new(single::RotationZ {
target: rz1.target,
theta: merged_angle,
})
as Box<dyn GateOp>);
i += 2;
continue;
}
}
_ => {}
}
}
}
// If we didn't merge, keep the original gate
optimized.push(gates[i].clone());
i += 1;
}
Ok(optimized)
}
}