cqlib-core 0.1.0-beta.5

The high-performance core foundation for the CQLib quantum computing ecosystem.
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
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
// This code is part of Cqlib.
//
// (C) Copyright China Telecom Quantum Group 2026
//
// This code is licensed under the Apache License, Version 2.0. You may
// obtain a copy of this license in the LICENSE.txt file in the root directory
// of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
//
// Any modifications or derivative works of this code must retain this
// copyright notice, and modified files need to carry a notice indicating
// that they have been altered from the originals.

// Identity transformation rules: equivalent rearrangements useful for
// topology matching, canonicalization, and gate movement.

// 1. H conjugation (H·gate·H = gate')

// H·X·H = Z
rule identity_hxh_to_z {
    match { H 0, X 0, H 0 }
    rewrite { Z 0 }
}

// H·Z·H = X
rule identity_hzh_to_x {
    match { H 0, Z 0, H 0 }
    rewrite { X 0 }
}

// H·Y·H = -Y → Y + GPhase(π)
rule identity_hyh_to_my {
    match { H 0, Y 0, H 0 }
    rewrite { Y 0, GPhase(π) }
}

// H·S·H = e^(iπ/4)·X2P
rule identity_hsh_to_sdg {
    match { H 0, S 0, H 0 }
    rewrite { X2P 0, GPhase(π/4) }
}

// H·SDG·H = e^(-iπ/4)·X2M
rule identity_hsdgh_to_s {
    match { H 0, SDG 0, H 0 }
    rewrite { X2M 0, GPhase(-π/4) }
}


// 2. S conjugation (S·gate·SDG = gate')


// S·X·SDG = Y
rule identity_sxs_to_y {
    match { S 0, X 0, SDG 0 }
    rewrite { Y 0, GPhase(π) }
}

// SDG·Y·S = X
rule identity_sdg_ys_to_x {
    match { SDG 0, Y 0, S 0 }
    rewrite { X 0, GPhase(π) }
}

// S·Z·SDG = Z (Z commutes with S)
rule identity_szs_to_z {
    match { S 0, Z 0, SDG 0 }
    rewrite { Z 0 }
}

// S·X2P·SDG = Y2M
rule identity_sx2ps_to_x2m {
    match { S 0, X2P 0, SDG 0 }
    rewrite { Y2M 0 }
}

// S·X2M·SDG = Y2P
rule identity_sx2ms_to_x2p {
    match { S 0, X2M 0, SDG 0 }
    rewrite { Y2P 0 }
}

// S·Y2P·SDG = X2P
rule identity_sy2ps_to_y2m {
    match { S 0, Y2P 0, SDG 0 }
    rewrite { X2P 0 }
}

// S·Y2M·SDG = X2M
rule identity_sy2ms_to_y2p {
    match { S 0, Y2M 0, SDG 0 }
    rewrite { X2M 0 }
}


// 3. X2P/X2M conjugation (X2P·gate·X2M = gate')

// X2P·Z·X2M = Y
rule identity_x2pzx2m_to_y {
    match { X2P 0, Z 0, X2M 0 }
    rewrite { Y 0 }
}

// X2M·Z·X2P = -Y
rule identity_x2mzx2p_to_my {
    match { X2M 0, Z 0, X2P 0 }
    rewrite { Y 0, GPhase(π) }
}

// X2P·X·X2M = X
rule identity_x2pxx2m_to_x {
    match { X2P 0, X 0, X2M 0 }
    rewrite { X 0 }
}

// X2M·Y·X2P = Z
rule identity_x2myx2p_to_z {
    match { X2M 0, Y 0, X2P 0 }
    rewrite { Z 0 }
}

// Y2P·Z·Y2M = -X
rule identity_y2pzy2m_to_mx {
    match { Y2P 0, Z 0, Y2M 0 }
    rewrite { X 0, GPhase(π) }
}

// Y2M·Z·Y2P = X
rule identity_y2mzy2p_to_x {
    match { Y2M 0, Z 0, Y2P 0 }
    rewrite { X 0 }
}

// Y2P·X·Y2M = Z
rule identity_y2pxy2m_to_z {
    match { Y2P 0, X 0, Y2M 0 }
    rewrite { Z 0 }
}

// Y2M·Y·Y2P = Y (needs verification)
rule identity_y2myy2p_to_y {
    match { Y2M 0, Y 0, Y2P 0 }
    rewrite { Y 0 }
}

// 4. CZ / CX conjugation (2-qubit Clifford)


// CZ·(X⊗I)·CZ = X⊗Z
rule identity_cz_x0_cz_to_xz {
    match { CZ 0 1, X 0, CZ 0 1 }
    rewrite { X 0, Z 1 }
}

// CZ·(I⊗X)·CZ = Z⊗X
rule identity_cz_x1_cz_to_zx {
    match { CZ 0 1, X 1, CZ 0 1 }
    rewrite { Z 0, X 1 }
}

// CX·(X⊗I)·CX = X⊗X
rule identity_cx_x0_cx_to_xx {
    match { CX 0 1, X 0, CX 0 1 }
    rewrite { X 0, X 1 }
}

// CX·(I⊗Z)·CX = Z⊗Z
rule identity_cx_z1_cx_to_zz {
    match { CX 0 1, Z 1, CX 0 1 }
    rewrite { Z 0, Z 1 }
}


// 5. Gate movement through CZ/CX (partial commutation)

// S on either qubit passes through CZ
rule identity_cz_s0_to_s_cz {
    match { CZ 0 1, S 0 }
    rewrite { S 0, CZ 0 1 }
}

rule identity_cz_s1_to_s_cz {
    match { CZ 0 1, S 1 }
    rewrite { S 1, CZ 0 1 }
}


// 6. CX direction flipping


// CX 0 1 → H⊗H · CX 1 0 · H⊗H
rule identity_cx_flip {
    match { CX 0 1 }
    rewrite { H 0, H 1, CX 1 0, H 0, H 1 }
}


// 7. Other identities

// SWAP symmetry
rule identity_swap_sym {
    match { SWAP 0 1 }
    rewrite { SWAP 1 0 }
}

// CZ = H⊗H · CX(reverse) · H⊗H (alternative CZ expression)
rule identity_cz_to_hcxh {
    match { CZ 0 1 }
    rewrite { H 1, CX 0 1, H 1 }
}

// X·Z = -Y
rule identity_xz_to_y_phase {
    match { X 0, Z 0 }
    rewrite { Y 0, GPhase(π/2) }
}

// Z·X = -Y
rule identity_zx_to_y_phase {
    match { Z 0, X 0 }
    rewrite { Y 0, GPhase(-π/2) }
}

// X2P·Y2M composition
rule identity_x2p_y2m {
    match { X2P 0, Y2M 0 }
    rewrite { H 0, SDG 0, GPhase(-π/4) }
}

// Y2P·X2M composition
rule identity_y2p_x2m {
    match { Y2P 0, X2M 0 }
    rewrite { S 0, H 0, GPhase(π/4) }
}

rule compose_cx_rz_cx_to_rzz {
    match { CX 0 1, RZ(theta) 1, CX 0 1 }
    rewrite { RZZ(theta) 0 1 }
}

rule compose_cx_phase_cx_to_rzz {
    match { CX 0 1, Phase(theta) 1, CX 0 1 }
    rewrite { RZZ(theta) 0 1, GPhase(theta/2) }
}

rule compose_cz_rx_cz_to_rzx {
    match { CZ 0 1, RX(theta) 1, CZ 0 1 }
    rewrite { RZX(theta) 0 1 }
}

rule compose_cz_rx_cz_to_rzx_swapped {
    match { CZ 0 1, RX(theta) 0, CZ 0 1 }
    rewrite { RZX(theta) 1 0 }
}

rule compose_cx_cz_to_cy {
    match { CX 0 1, CZ 0 1 }
    rewrite { Phase(π/2) 0, CY 0 1 }
}

rule compose_cx_cz_to_cy_cz_swapped {
    match { CX 0 1, CZ 1 0 }
    rewrite { Phase(π/2) 0, CY 0 1 }
}

rule compose_cz_cx_to_cy {
    match { CZ 0 1, CX 0 1 }
    rewrite { Phase(-π/2) 0, CY 0 1 }
}

rule compose_cz_cx_to_cy_cz_swapped {
    match { CZ 1 0, CX 0 1 }
    rewrite { Phase(-π/2) 0, CY 0 1 }
}

rule identity_cx_to_hczh {
    match { CX 0 1 }
    rewrite { H 1, CZ 0 1, H 1 }
}

rule identity_rz_to_phase_gphase {
    match { RZ(a) 0 }
    rewrite { Phase(a) 0, GPhase(-a/2) }
}

rule identity_hx2ph_to_s {
    match { H 0, X2P 0, H 0 }
    rewrite { S 0, GPhase(-π/4) }
}

rule identity_hx2mh_to_sdg {
    match { H 0, X2M 0, H 0 }
    rewrite { SDG 0, GPhase(π/4) }
}

rule identity_hy2ph_to_y2m {
    match { H 0, Y2P 0, H 0 }
    rewrite { Y2M 0 }
}

rule identity_hy2mh_to_y2p {
    match { H 0, Y2M 0, H 0 }
    rewrite { Y2P 0 }
}

rule identity_sysdg_to_x {
    match { S 0, Y 0, SDG 0 }
    rewrite { X 0 }
}

rule identity_sdg_xs_to_y {
    match { SDG 0, X 0, S 0 }
    rewrite { Y 0 }
}

rule identity_xy_to_z_phase {
    match { X 0, Y 0 }
    rewrite { Z 0, GPhase(-π/2) }
}

rule identity_yx_to_z_phase {
    match { Y 0, X 0 }
    rewrite { Z 0, GPhase(π/2) }
}

rule identity_yz_to_x_phase {
    match { Y 0, Z 0 }
    rewrite { X 0, GPhase(-π/2) }
}

rule identity_zy_to_x_phase {
    match { Z 0, Y 0 }
    rewrite { X 0, GPhase(π/2) }
}

rule identity_hth_to_rx {
    match { H 0, T 0, H 0 }
    rewrite { RX(π/4) 0, GPhase(π/8) }
}

rule identity_htdgh_to_rx {
    match { H 0, TDG 0, H 0 }
    rewrite { RX(-π/4) 0, GPhase(-π/8) }
}

rule compose_cx_bridge_4cx {
    match { CX 0 1, CX 1 2, CX 0 1, CX 1 2 }
    rewrite { CX 0 2 }
}

rule compose_cx_bridge_4cx_rev {
    match { CX 1 2, CX 0 1, CX 1 2, CX 0 1 }
    rewrite { CX 0 2 }
}

rule compose_swap_cx_bridge {
    match { SWAP 0 1, CX 1 2, SWAP 0 1 }
    rewrite { CX 0 2 }
}

rule compose_swap_cx_bridge_far {
    match { SWAP 0 1, CX 0 2, SWAP 0 1 }
    rewrite { CX 1 2 }
}

rule compose_swap_cz_bridge {
    match { SWAP 0 1, CZ 1 2, SWAP 0 1 }
    rewrite { CZ 0 2 }
}

rule compose_swap_swap_swap {
    match { SWAP 0 1, SWAP 0 2, SWAP 0 1 }
    rewrite { SWAP 1 2 }
}

rule compose_cx3_to_swap {
    match { CX 0 1, CX 1 0, CX 0 1 }
    rewrite { SWAP 0 1 }
}

rule compose_cx3_to_swap_rev {
    match { CX 1 0, CX 0 1, CX 1 0 }
    rewrite { SWAP 0 1 }
}

rule compose_swap_cx_swap_to_cx_flip {
    match { SWAP 0 1, CX 0 1, SWAP 0 1 }
    rewrite { CX 1 0 }
}

rule compose_swap_cz_swap_to_cz {
    match { SWAP 0 1, CZ 0 1, SWAP 0 1 }
    rewrite { CZ 0 1 }
}

rule compose_swap_crz_swap {
    match { SWAP 0 1, CRZ(a) 0 1, SWAP 0 1 }
    rewrite { CRZ(a) 1 0 }
}

rule compose_swap_crx_swap {
    match { SWAP 0 1, CRX(a) 0 1, SWAP 0 1 }
    rewrite { CRX(a) 1 0 }
}

rule compose_hh_cx_hh_to_cx_flip {
    match { H 0, H 1, CX 0 1, H 0, H 1 }
    rewrite { CX 1 0 }
}

rule compose_h1_cz_h1_to_cx {
    match { H 1, CZ 0 1, H 1 }
    rewrite { CX 0 1 }
}

rule compose_h0_cz_h0_to_cx_flip {
    match { H 0, CZ 0 1, H 0 }
    rewrite { CX 1 0 }
}

rule compose_h0_cx_h0_to_cz {
    match { H 0, CX 1 0, H 0 }
    rewrite { CZ 0 1 }
}

rule compose_h1_cx_h1_to_cz {
    match { H 1, CX 0 1, H 1 }
    rewrite { CZ 0 1 }
}

rule compose_y2m_cz_y2p_to_cx {
    match { Y2M 1, CZ 0 1, Y2P 1 }
    rewrite { CX 0 1 }
}

rule compose_y2p_cz_y2m_to_z_cx {
    match { Y2P 1, CZ 0 1, Y2M 1 }
    rewrite { Z 0, CX 0 1 }
}

rule compose_x2p_cz_x2m_to_cy {
    match { X2P 1, CZ 0 1, X2M 1 }
    rewrite { CY 0 1 }
}

rule compose_cx_rx_cx_to_rxx {
    match { CX 0 1, RX(a) 0, CX 0 1 }
    rewrite { RXX(a) 0 1 }
}

rule move_x_ctrl_cx {
    match { X 0, CX 0 1 }
    rewrite { CX 0 1, X 0, X 1 }
}

rule move_z_tgt_cx {
    match { Z 1, CX 0 1 }
    rewrite { CX 0 1, Z 0, Z 1 }
}

rule move_y_ctrl_cx {
    match { Y 0, CX 0 1 }
    rewrite { CX 0 1, Y 0, X 1 }
}

rule move_x0_cz {
    match { X 0, CZ 0 1 }
    rewrite { CZ 0 1, X 0, Z 1 }
}

rule move_x1_cz {
    match { X 1, CZ 0 1 }
    rewrite { CZ 0 1, Z 0, X 1 }
}

// Shortening identities, in circuit execution order; preserve scalar phases.
rule identity_rz_xy_absorb {
    match { RZ(a) 0, XY(b) 0 }
    rewrite { XY(b - a/2) 0 }
}

rule identity_xy_rz_absorb {
    match { XY(b) 0, RZ(a) 0 }
    rewrite { XY(b + a/2) 0 }
}

rule compose_rz_xy2p_rz {
    match { RZ(a) 0, XY2P(b) 0, RZ(c) 0 }
    rewrite { XY2P(b - a) 0, RZ(a + c) 0 }
}

rule compose_rz_xy2p_rz_cancel {
    match { RZ(a) 0, XY2P(b) 0, RZ(c) 0 }
    require { a + c == 0 mod 4*π }
    rewrite { XY2P(b - a) 0 }
}

rule compose_rz_xy2p_rz_global_phase {
    match { RZ(a) 0, XY2P(b) 0, RZ(c) 0 }
    require { a + c == 2*π mod 4*π }
    rewrite { XY2P(b - a) 0, GPhase(π) }
}

rule compose_rz_xy2m_rz {
    match { RZ(a) 0, XY2M(b) 0, RZ(c) 0 }
    rewrite { XY2M(b - a) 0, RZ(a + c) 0 }
}

rule compose_rz_xy2m_rz_cancel {
    match { RZ(a) 0, XY2M(b) 0, RZ(c) 0 }
    require { a + c == 0 mod 4*π }
    rewrite { XY2M(b - a) 0 }
}

rule compose_rz_xy2m_rz_global_phase {
    match { RZ(a) 0, XY2M(b) 0, RZ(c) 0 }
    require { a + c == 2*π mod 4*π }
    rewrite { XY2M(b - a) 0, GPhase(π) }
}

rule compose_rz_rxy_rz {
    match { RZ(a) 0, RXY(theta, b) 0, RZ(c) 0 }
    rewrite { RXY(theta, b - a) 0, RZ(a + c) 0 }
}

rule compose_rz_rxy_rz_cancel {
    match { RZ(a) 0, RXY(theta, b) 0, RZ(c) 0 }
    require { a + c == 0 mod 4*π }
    rewrite { RXY(theta, b - a) 0 }
}

rule compose_rz_rxy_rz_global_phase {
    match { RZ(a) 0, RXY(theta, b) 0, RZ(c) 0 }
    require { a + c == 2*π mod 4*π }
    rewrite { RXY(theta, b - a) 0, GPhase(π) }
}

rule compose_x2p_rz_x2m {
    match { X2P 0, RZ(a) 0, X2M 0 }
    rewrite { RY(a) 0 }
}

rule compose_x2m_rz_x2p {
    match { X2M 0, RZ(a) 0, X2P 0 }
    rewrite { RY(-a) 0 }
}

rule compose_y2m_rz_y2p {
    match { Y2M 0, RZ(a) 0, Y2P 0 }
    rewrite { RX(a) 0 }
}

rule compose_y2p_rz_y2m {
    match { Y2P 0, RZ(a) 0, Y2M 0 }
    rewrite { RX(-a) 0 }
}

rule identity_x2p_rz_pi_echo {
    match { X2P 0, RZ(a) 0, X2P 0 }
    require { a == π mod 2*π }
    rewrite { RZ(a) 0 }
}

rule identity_x2m_rz_pi_echo {
    match { X2M 0, RZ(a) 0, X2M 0 }
    require { a == π mod 2*π }
    rewrite { RZ(a) 0 }
}

rule identity_y2p_rz_pi_echo {
    match { Y2P 0, RZ(a) 0, Y2P 0 }
    require { a == π mod 2*π }
    rewrite { RZ(a) 0 }
}

rule identity_y2m_rz_pi_echo {
    match { Y2M 0, RZ(a) 0, Y2M 0 }
    require { a == π mod 2*π }
    rewrite { RZ(a) 0 }
}

rule identity_xy2p_rz_pi_echo {
    match { XY2P(phi) 0, RZ(a) 0, XY2P(phi) 0 }
    require { a == π mod 2*π }
    rewrite { RZ(a) 0 }
}

rule identity_xy2m_rz_pi_echo {
    match { XY2M(phi) 0, RZ(a) 0, XY2M(phi) 0 }
    require { a == π mod 2*π }
    rewrite { RZ(a) 0 }
}

// CZ conjugation of pi pulses removes both entanglers; include native RZ targets.
rule identity_cz_y0_cz {
    match { CZ 0 1, Y 0, CZ 0 1 }
    rewrite { Y 0, Z 1 }
}

rule identity_cz_y0_cz_to_rz {
    match { CZ 0 1, Y 0, CZ 0 1 }
    rewrite { Y 0, RZ(π) 1, GPhase(π/2) }
}

rule identity_cz_y0_cz_swapped {
    match { CZ 0 1, Y 0, CZ 1 0 }
    rewrite { Y 0, Z 1 }
}

rule identity_cz_y0_cz_to_rz_swapped {
    match { CZ 0 1, Y 0, CZ 1 0 }
    rewrite { Y 0, RZ(π) 1, GPhase(π/2) }
}

rule identity_cz_y1_cz {
    match { CZ 0 1, Y 1, CZ 0 1 }
    rewrite { Y 1, Z 0 }
}

rule identity_cz_y1_cz_to_rz {
    match { CZ 0 1, Y 1, CZ 0 1 }
    rewrite { Y 1, RZ(π) 0, GPhase(π/2) }
}

rule identity_cz_y1_cz_swapped {
    match { CZ 0 1, Y 1, CZ 1 0 }
    rewrite { Y 1, Z 0 }
}

rule identity_cz_y1_cz_to_rz_swapped {
    match { CZ 0 1, Y 1, CZ 1 0 }
    rewrite { Y 1, RZ(π) 0, GPhase(π/2) }
}

rule identity_cz_xy0_cz {
    match { CZ 0 1, XY(phi) 0, CZ 0 1 }
    rewrite { XY(phi) 0, Z 1 }
}

rule identity_cz_xy0_cz_to_rz {
    match { CZ 0 1, XY(phi) 0, CZ 0 1 }
    rewrite { XY(phi) 0, RZ(π) 1, GPhase(π/2) }
}

rule identity_cz_xy0_cz_swapped {
    match { CZ 0 1, XY(phi) 0, CZ 1 0 }
    rewrite { XY(phi) 0, Z 1 }
}

rule identity_cz_xy0_cz_to_rz_swapped {
    match { CZ 0 1, XY(phi) 0, CZ 1 0 }
    rewrite { XY(phi) 0, RZ(π) 1, GPhase(π/2) }
}

rule identity_cz_xy1_cz {
    match { CZ 0 1, XY(phi) 1, CZ 0 1 }
    rewrite { XY(phi) 1, Z 0 }
}

rule identity_cz_xy1_cz_to_rz {
    match { CZ 0 1, XY(phi) 1, CZ 0 1 }
    rewrite { XY(phi) 1, RZ(π) 0, GPhase(π/2) }
}

rule identity_cz_xy1_cz_swapped {
    match { CZ 0 1, XY(phi) 1, CZ 1 0 }
    rewrite { XY(phi) 1, Z 0 }
}

rule identity_cz_xy1_cz_to_rz_swapped {
    match { CZ 0 1, XY(phi) 1, CZ 1 0 }
    rewrite { XY(phi) 1, RZ(π) 0, GPhase(π/2) }
}

// Also accept Z after the central RZ(pi) has been specialized.
rule identity_x2p_z_echo {
    match { X2P 0, Z 0, X2P 0 }
    rewrite { Z 0 }
}

rule identity_x2m_z_echo {
    match { X2M 0, Z 0, X2M 0 }
    rewrite { Z 0 }
}

rule identity_y2p_z_echo {
    match { Y2P 0, Z 0, Y2P 0 }
    rewrite { Z 0 }
}

rule identity_y2m_z_echo {
    match { Y2M 0, Z 0, Y2M 0 }
    rewrite { Z 0 }
}

rule identity_xy2p_z_echo {
    match { XY2P(phi) 0, Z 0, XY2P(phi) 0 }
    rewrite { Z 0 }
}

rule identity_xy2m_z_echo {
    match { XY2M(phi) 0, Z 0, XY2M(phi) 0 }
    rewrite { Z 0 }
}