tk-pg-bitpacked 0.1.1

Clifford conjugation kernels for packed Pauli representations
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
use std::ops::{BitAnd, BitOr, BitXor, Not};

/// A trait for types that support bitwise operations and can be used in the conjugation functions.
pub trait BitOps:
    BitAnd<Output = Self>
    + BitXor<Output = Self>
    + BitOr<Output = Self>
    + Not<Output = Self>
    + Copy
    + Sized
{
}

// Blanket impl — anything satisfying all bounds gets BitOps for free
impl<T> BitOps for T where
    T: BitAnd<Output = T> + BitXor<Output = T> + BitOr<Output = T> + Not<Output = T> + Copy
{
}

use crate::u8_encodings::{XX_U8, XY_U8, XZ_U8, YX_U8, YY_U8, YZ_U8, ZX_U8, ZY_U8, ZZ_U8};

// =============================================================================
//        Clifford conjugation functions on bit-packed Pauli operators
// =============================================================================

/// Apply X gate (X P X) on bit-packed Pauli operators.
///
/// This function transforms Pauli operators:
/// - X -> X
/// - Y -> -Y
/// - Z -> -Z
///
/// # Arguments
///
/// * `z` - A `T` containing the Z component bits of the packed Pauli operators.
/// * `x` - A `T` containing the X component bits of the packed Pauli operators.
///
/// # Returns
/// The sign flip bits.
///
#[inline(always)]
pub fn x_gate<T: BitOps>(z: T, _x: T) -> T {
    // flip the sign if the letter is Z or Y
    z
}

/// Apply Y gate (Y P Y) on bit-packed Pauli operators.
///
/// This function transforms Pauli operators:
/// - X -> -X
/// - Y -> Y
/// - Z -> -Z
///
/// # Arguments
///
/// * `z` - A `T` containing the Z component bits of the packed Pauli operators.
/// * `x` - A `T` containing the X component bits of the packed Pauli operators.
///
/// # Returns
/// The sign flip bits.
///
#[inline(always)]
pub fn y_gate<T: BitOps>(z: T, x: T) -> T {
    // flip the sign if the letter is X or Z
    z ^ x
}

/// Apply Z gate (Z P Z) on bit-packed Pauli operators.
///
/// This function transforms Pauli operators:
/// - X -> -X
/// - Y -> -Y
/// - Z -> Z
///
/// # Arguments
///
/// * `z` - A `T` containing the Z component bits of the packed Pauli operators.
/// * `x` - A `T` containing the X component bits of the packed Pauli operators.
///
/// # Returns
/// The sign flip bits.
///
#[inline(always)]
pub fn z_gate<T: BitOps>(_z: T, x: T) -> T {
    // flip the sign if the letter is X or Y
    x
}

/// Apply H gate (H P H) on bit-packed Pauli operators.
///
/// This function transforms Pauli operators:
/// - X -> Z
/// - Z -> X
/// - Y -> -Y
///
/// # Arguments
///
/// * `z` - A `T` containing the Z component bits of the packed Pauli operators.
/// * `x` - A `T` containing the X component bits of the packed Pauli operators.
///
/// # Returns
///
/// A tuple containing:
/// - `T`: The new Z component bits.
/// - `T`: The new X component bits.
/// - `T`: The sign flip bits.
///
#[inline(always)]
pub fn h_gate<T: BitOps>(z: T, x: T) -> (T, T, T) {
    // swap bits
    // sign flip if both bits are 1
    (x, z, z & x)
}

/// Apply Sdg gate (Sdg P S) on bit-packed Pauli operators.
///
/// This function transforms Pauli operators:
/// - X -> -Y
/// - Y -> X
/// - Z -> Z
///
/// # Arguments
///
/// * `z` - A `T` containing the Z component bits of the packed Pauli operators.
/// * `x` - A `T` containing the X component bits of the packed Pauli operators.
///
/// # Returns
///
/// A tuple containing:
/// - `T`: The new Z component bits.
/// - `T`: The new X component bits.
/// - `T`: The sign flip bits.
///
#[inline(always)]
pub fn sdg_gate<T: BitOps>(z: T, x: T) -> (T, T, T) {
    // toggle z bits with x bits
    // sign flip if z bit is 0 and x bit is 1
    (z ^ x, x, !z & x)
}

/// Apply S gate (S P Sdg) on bit-packed Pauli operators.
///
/// This function transforms Pauli operators:
/// - X -> Y
/// - Y -> -X
/// - Z -> Z
///
/// # Arguments
///
/// * `z` - A `T` containing the Z component bits of the packed Pauli operators.
/// * `x` - A `T` containing the X component bits of the packed Pauli operators.
///
/// # Returns
///
/// A tuple containing:
/// - `T`: The new Z component bits.
/// - `T`: The new X component bits.
/// - `T`: The sign flip bits.
///
#[inline(always)]
pub fn s_gate<T: BitOps>(z: T, x: T) -> (T, T, T) {
    // toggle z bits with x bits
    (z ^ x, x, z & x)
}

/// Apply Vdg gate (Vdg P V) on bit-packed Pauli operators.
///
/// This function transforms Pauli operators:
/// - X -> X
/// - Y -> -Z
/// - Z -> Y
///
/// # Arguments
///
/// * `z` - A `T` containing the Z component bits of the packed Pauli operators.
/// * `x` - A `T` containing the X component bits of the packed Pauli operators.
///
/// # Returns
///
/// A tuple containing:
/// - `T`: The new Z component bits.
/// - `T`: The new X component bits.
/// - `T`: The sign flip bits.
///
#[inline(always)]
pub fn vdg_gate<T: BitOps>(z: T, x: T) -> (T, T, T) {
    // toggle x bits with z bits
    // sign flip if both bits are 1
    (z, x ^ z, z & x)
}

/// Apply V gate (V P Vdg) on bit-packed Pauli operators.
///
/// This function transforms Pauli operators:
/// - X -> X
/// - Y -> Z
/// - Z -> -Y
///
/// # Arguments
///
/// * `z` - A `T` containing the Z component bits of the packed Pauli operators.
/// * `x` - A `T` containing the X component bits of the packed Pauli operators.
///
/// # Returns
///
/// A tuple containing:
/// - `T`: The new Z component bits.
/// - `T`: The new X component bits.
/// - `T`: The sign flip bits.
///
#[inline(always)]
pub fn v_gate<T: BitOps>(z: T, x: T) -> (T, T, T) {
    // toggle x bits with z bits
    (z, x ^ z, z & !x)
}

/// Apply Ry(-pi/2) gate (Ry(-pi/2) P Ry(pi/2)) on bit-packed Pauli operators.
///
/// This function transforms Pauli operators:
/// - X -> Z
/// - Y -> Y
/// - Z -> -X
///
/// # Arguments
///
/// * `z` - A `T` containing the Z component bits of the packed Pauli operators.
/// * `x` - A `T` containing the X component bits of the packed Pauli operators.
///
/// # Returns
///
/// A tuple containing:
/// - `T`: The new Z component bits.
/// - `T`: The new X component bits.
/// - `T`: The sign flip bits.
///
#[inline(always)]
pub fn ry270_gate<T: BitOps>(z: T, x: T) -> (T, T, T) {
    // swap bits
    (x, z, z & !x)
}

/// Apply Ry(pi/2) gate (Ry(pi/2) P Ry(-pi/2)) on bit-packed Pauli operators.
///
/// This function transforms Pauli operators:
/// - X -> -Z
/// - Y -> Y
/// - Z -> X
///
/// # Arguments
///
/// * `z` - A `T` containing the Z component bits of the packed Pauli operators.
/// * `x` - A `T` containing the X component bits of the packed Pauli operators.
///
/// # Returns
///
/// A tuple containing:
/// - `T`: The new Z component bits.
/// - `T`: The new X component bits.
/// - `T`: The sign flip bits.
///
#[inline(always)]
pub fn ry90_gate<T: BitOps>(z: T, x: T) -> (T, T, T) {
    // swap bits
    (x, z, !z & x)
}

/// Apply Rz(-pi/2) gate (Rz(-pi/2) P Rz(pi/2)) on bit-packed Pauli operators. Equivalent to S gate.
#[inline(always)]
pub fn rz270_gate<T: BitOps>(z: T, x: T) -> (T, T, T) {
    sdg_gate(z, x)
}

/// Apply Rz(pi/2) gate (Rz(pi/2) P Rz(-pi/2)) on bit-packed Pauli operators. Equivalent to Sdg gate.
#[inline(always)]
pub fn rz90_gate<T: BitOps>(z: T, x: T) -> (T, T, T) {
    s_gate(z, x)
}

/// Apply Rx(-pi/2) gate (Rx(-pi/2) P Rx(pi/2)) on bit-packed Pauli operators. Equivalent to V gate.
#[inline(always)]
pub fn rx270_gate<T: BitOps>(z: T, x: T) -> (T, T, T) {
    vdg_gate(z, x)
}

/// Apply Rx(pi/2) gate (Rx(pi/2) P Rx(-pi/2)) on bit-packed Pauli operators. Equivalent to Vdg gate.
#[inline(always)]
pub fn rx90_gate<T: BitOps>(z: T, x: T) -> (T, T, T) {
    v_gate(z, x)
}

/// Apply XX gate (XX P XX) on bit-packed Pauli operators.
///
/// # Arguments
///
/// * `z0` - A `T` containing the Z component bits of the packed Pauli operators
///   on the first qubit.
/// * `x0` - A `T` containing the X component bits of the packed Pauli operators
///   on the first qubit.
/// * `z1` - A `T` containing the Z component bits of the packed Pauli operators
///   on the second qubit.
/// * `x1` - A `T` containing the X component bits of the packed Pauli operators
///   on the second qubit.
///
/// # Returns
///
/// A tuple containing:
/// - `T`: The new Z component bits on the first qubit.
/// - `T`: The new X component bits on the first qubit.
/// - `T`: The new Z component bits on the second qubit.
/// - `T`: The new X component bits on the second qubit.
/// - `T`: The sign flip bits.
///
#[inline(always)]
pub fn xx_gate<T: BitOps>(z0: T, x0: T, z1: T, x1: T) -> (T, T, T, T, T) {
    // we flip sign only for YZ, and ZY
    // (z0&x0&z1&!x1)|(z0&!x0&z1&x1) = z0&z1&(x0^x1)
    (z0, x0 ^ z1, z1, x1 ^ z0, z0 & z1 & (x0 ^ x1))
}

/// Apply XY (XY P XY) gate on bit-packed Pauli operators.
///
/// # Arguments
///
/// * `z0` - A `T` containing the Z component bits of the packed Pauli operators
///   on the first qubit.
/// * `x0` - A `T` containing the X component bits of the packed Pauli operators
///   on the first qubit.
/// * `z1` - A `T` containing the Z component bits of the packed Pauli operators
///   on the second qubit.
/// * `x1` - A `T` containing the X component bits of the packed Pauli operators
///   on the second qubit.
///
/// # Returns
///
/// A tuple containing:
/// - `T`: The new Z component bits on the first qubit.
/// - `T`: The new X component bits on the first qubit.
/// - `T`: The new Z component bits on the second qubit.
/// - `T`: The new X component bits on the second qubit.
/// - `T`: The sign flip bits.
///
#[inline(always)]
pub fn xy_gate<T: BitOps>(z0: T, x0: T, z1: T, x1: T) -> (T, T, T, T, T) {
    // we flip sign only for YX, and ZZ
    // (z0&x0&!z1&x1)|(z0&!x0&z1&!x1) = z0&(x0==x1&x0!=z1)
    (
        z0,
        x0 ^ z1 ^ x1,
        z1 ^ z0,
        x1 ^ z0,
        z0 & !(x0 ^ x1) & (x0 ^ z1),
    )
}

/// Apply XZ (XZ P XZ) gate on bit-packed Pauli operators.
///
/// # Arguments
///
/// * `z0` - A `T` containing the Z component bits of the packed Pauli operators
///   on the first qubit.
/// * `x0` - A `T` containing the X component bits of the packed Pauli operators
///   on the first qubit.
/// * `z1` - A `T` containing the Z component bits of the packed Pauli operators
///   on the second qubit.
/// * `x1` - A `T` containing the X component bits of the packed Pauli operators
///   on the second qubit.
///
/// # Returns
///
/// A tuple containing:
/// - `T`: The new Z component bits on the first qubit.
/// - `T`: The new X component bits on the first qubit.
/// - `T`: The new Z component bits on the second qubit.
/// - `T`: The new X component bits on the second qubit.
/// - `T`: The sign flip bits.
///
#[inline(always)]
pub fn xz_gate<T: BitOps>(z0: T, x0: T, z1: T, x1: T) -> (T, T, T, T, T) {
    // we flip sign only for YY, and ZX
    (z0, x0 ^ x1, z1 ^ z0, x1, z0 & x1 & !(x0 ^ z1))
}

/// Apply YY (YY P YY) gate on bit-packed Pauli operators.
///
/// # Arguments
///
/// * `z0` - A `T` containing the Z component bits of the packed Pauli operators
///   on the first qubit.
/// * `x0` - A `T` containing the X component bits of the packed Pauli operators
///   on the first qubit.
/// * `z1` - A `T` containing the Z component bits of the packed Pauli operators
///   on the second qubit.
/// * `x1` - A `T` containing the X component bits of the packed Pauli operators
///   on the second qubit.
///
/// # Returns
///
/// A tuple containing:
/// - `T`: The new Z component bits on the first qubit.
/// - `T`: The new X component bits on the first qubit.
/// - `T`: The new Z component bits on the second qubit.
/// - `T`: The new X component bits on the second qubit.
/// - `T`: The sign flip bits.
///
#[inline(always)]
pub fn yy_gate<T: BitOps>(z0: T, x0: T, z1: T, x1: T) -> (T, T, T, T, T) {
    // toggle both bits, if the other two bits has parity 1
    // we flip sign only for XZ, and ZX
    // z0!=x0 & z1!=x1 & x0==z1
    let toggle0 = z1 ^ x1;
    let toggle1 = z0 ^ x0;
    (
        z0 ^ toggle0,
        x0 ^ toggle0,
        z1 ^ toggle1,
        x1 ^ toggle1,
        toggle1 & toggle0 & !(x0 ^ z1),
    )
}

/// Apply YZ (YZ P YZ) gate on bit-packed Pauli operators.
///
/// # Arguments
///
/// * `z0` - A `T` containing the Z component bits of the packed Pauli operators
///   on the first qubit.
/// * `x0` - A `T` containing the X component bits of the packed Pauli operators
///   on the first qubit.
/// * `z1` - A `T` containing the Z component bits of the packed Pauli operators
///   on the second qubit.
/// * `x1` - A `T` containing the X component bits of the packed Pauli operators
///   on the second qubit.
///
/// # Returns
///
/// A tuple containing:
/// - `T`: The new Z component bits on the first qubit.
/// - `T`: The new X component bits on the first qubit.
/// - `T`: The new Z component bits on the second qubit.
/// - `T`: The new X component bits on the second qubit.
/// - `T`: The sign flip bits.
///
#[inline(always)]
pub fn yz_gate<T: BitOps>(z0: T, x0: T, z1: T, x1: T) -> (T, T, T, T, T) {
    // we flip sign only for ZY, and XX
    // x1 & z0==z1 & x0!=z0
    (
        z0 ^ x1,
        x0 ^ x1,
        z1 ^ z0 ^ x0,
        x1,
        x1 & !(z0 ^ z1) & (x0 ^ z0),
    )
}

/// Apply ZZ (ZZ P ZZ) gate on bit-packed Pauli operators.
///
/// # Arguments
///
/// * `z0` - A `T` containing the Z component bits of the packed Pauli operators
///   on the first qubit.
/// * `x0` - A `T` containing the X component bits of the packed Pauli operators
///   on the first qubit.
/// * `z1` - A `T` containing the Z component bits of the packed Pauli operators
///   on the second qubit.
/// * `x1` - A `T` containing the X component bits of the packed Pauli operators
///   on the second qubit.
///
/// # Returns
///
/// A tuple containing:
/// - `T`: The new Z component bits on the first qubit.
/// - `T`: The new X component bits on the first qubit.
/// - `T`: The new Z component bits on the second qubit.
/// - `T`: The new X component bits on the second qubit.
/// - `T`: The sign flip bits.
///
#[inline(always)]
pub fn zz_gate<T: BitOps>(z0: T, x0: T, z1: T, x1: T) -> (T, T, T, T, T) {
    // we flip sign only for XY, and YX
    // x0&x1&z0!=z1
    (z0 ^ x1, x0, z1 ^ x0, x1, x0 & x1 & (z0 ^ z1))
}

/// Apply TQE gate where the gate type is base-3 encoded in a u8.
/// The encoding is as follows:
/// - 0: XX
/// - 1: XY
/// - 2: XZ
/// - 3: YX
/// - 4: YY
/// - 5: YZ
/// - 6: ZX
/// - 7: ZY
/// - 8: ZZ
///
/// # Arguments
///
/// - `z0` (`&mut T`) - z bits on the first qubit.
/// - `x0` (`&mut T`) - x bits on the first qubit.
/// - `z1` (`&mut T`) - z bits on the second qubit.
/// - `x1` (`&mut T`) - x bits on the second qubit.
/// - `signs` (`&mut T`) - sign bits.
/// - `mask` (`T`) - mask to apply the gate.
/// - `gate` (`u8`) - gate type encoded in base-3.
///
pub fn apply_u8_tqe<T: BitOps>(
    z0: &mut T,
    x0: &mut T,
    z1: &mut T,
    x1: &mut T,
    signs: &mut T,
    mask: T,
    gate: u8,
) {
    let (new_z0, new_x0, new_z1, new_x1, sign_flips) = match gate {
        XX_U8 => xx_gate(*z0, *x0, *z1, *x1),
        XY_U8 => xy_gate(*z0, *x0, *z1, *x1),
        XZ_U8 => xz_gate(*z0, *x0, *z1, *x1),
        YX_U8 => {
            let (a, b, c, d, e) = xy_gate(*z1, *x1, *z0, *x0);
            (c, d, a, b, e)
        }
        YY_U8 => yy_gate(*z0, *x0, *z1, *x1),
        YZ_U8 => yz_gate(*z0, *x0, *z1, *x1),
        ZX_U8 => {
            let (a, b, c, d, e) = xz_gate(*z1, *x1, *z0, *x0);
            (c, d, a, b, e)
        }
        ZY_U8 => {
            let (a, b, c, d, e) = yz_gate(*z1, *x1, *z0, *x0);
            (c, d, a, b, e)
        }
        ZZ_U8 => zz_gate(*z0, *x0, *z1, *x1),
        _ => panic!("Invalid TQE gate"),
    };

    let new_signs = sign_flips ^ *signs;
    let mask_ng = !mask;
    *z0 = (*z0 & mask_ng) | (new_z0 & mask);
    *x0 = (*x0 & mask_ng) | (new_x0 & mask);
    *z1 = (*z1 & mask_ng) | (new_z1 & mask);
    *x1 = (*x1 & mask_ng) | (new_x1 & mask);
    *signs = (*signs & mask_ng) | (new_signs & mask);
}

#[cfg(test)]
pub(crate) mod tests {
    use super::*;
    use crate::test_utils::*;
    use tk_pg_converter::compare_unitaries_via_tk;
    use tk_pg_core::{GateData, GateType, Op, Pauli, PauliGraph, RotationData};

    type SqGateFn = fn(bool, bool) -> (bool, bool, bool);
    type PauliGateFn = fn(bool, bool) -> bool;
    type TqeGateFn = fn(bool, bool, bool, bool) -> (bool, bool, bool, bool, bool);

    pub(crate) const SQ_GATES: [&str; 11] = [
        "H", "S", "SDG", "V", "VDG", "RX90", "RX270", "RY90", "RY270", "RZ90", "RZ270",
    ];
    pub(crate) const PAULI_GATES: [&str; 3] = ["X", "Y", "Z"];
    pub(crate) const TQ_GATES: [&str; 6] = ["XX", "XY", "XZ", "YY", "YZ", "ZZ"];

    pub(crate) fn sq_gate_fn(name: &str) -> SqGateFn {
        match name {
            "H" => h_gate,
            "S" => s_gate,
            "SDG" => sdg_gate,
            "V" => v_gate,
            "VDG" => vdg_gate,
            "RX90" => rx90_gate,
            "RX270" => rx270_gate,
            "RY90" => ry90_gate,
            "RY270" => ry270_gate,
            "RZ90" => rz90_gate,
            "RZ270" => rz270_gate,
            _ => panic!("Invalid gate name"),
        }
    }

    pub(crate) fn pauli_gate_fn(name: &str) -> PauliGateFn {
        match name {
            "X" => x_gate,
            "Y" => y_gate,
            "Z" => z_gate,
            _ => panic!("Invalid gate name"),
        }
    }

    pub(crate) fn tq_gate_fn(name: &str) -> TqeGateFn {
        match name {
            "XX" => xx_gate,
            "XY" => xy_gate,
            "XZ" => xz_gate,
            "YY" => yy_gate,
            "YZ" => yz_gate,
            "ZZ" => zz_gate,
            _ => panic!("Invalid gate name"),
        }
    }

    fn get_gate(name: &str) -> Op {
        let gate_data = match name {
            "H" => GateData::new(GateType::H, vec![0]),
            "S" => GateData::new(GateType::S, vec![0]),
            "SDG" => GateData::new(GateType::Sdg, vec![0]),
            "V" => GateData::new(GateType::V, vec![0]),
            "VDG" => GateData::new(GateType::Vdg, vec![0]),
            "RX90" => GateData::new(GateType::RX, vec![0]).with_params(vec![0.5]),
            "RX270" => GateData::new(GateType::RX, vec![0]).with_params(vec![1.5]),
            "RY90" => GateData::new(GateType::RY, vec![0]).with_params(vec![0.5]),
            "RY270" => GateData::new(GateType::RY, vec![0]).with_params(vec![1.5]),
            "RZ90" => GateData::new(GateType::RZ, vec![0]).with_params(vec![0.5]),
            "RZ270" => GateData::new(GateType::RZ, vec![0]).with_params(vec![1.5]),
            "X" => GateData::new(GateType::X, vec![0]),
            "Y" => GateData::new(GateType::Y, vec![0]),
            "Z" => GateData::new(GateType::Z, vec![0]),
            "XX" => GateData::new(GateType::XX, vec![0, 1]),
            "XY" => GateData::new(GateType::XY, vec![0, 1]),
            "XZ" => GateData::new(GateType::XZ, vec![0, 1]),
            "YY" => GateData::new(GateType::YY, vec![0, 1]),
            "YZ" => GateData::new(GateType::YZ, vec![0, 1]),
            "ZZ" => GateData::new(GateType::ZZ, vec![0, 1]),
            _ => panic!("Invalid gate name"),
        };
        Op::Gate { data: gate_data }
    }

    #[test]
    fn test_sq_gates() {
        for gate in SQ_GATES.iter() {
            let gate_fn = sq_gate_fn(gate);
            for p in [Pauli::X, Pauli::Y, Pauli::Z].iter() {
                let (z, x) = pauli_to_bits(p);
                let (new_z, new_x, sign_flip) = gate_fn(z, x);
                let p_prime = bits_to_pauli(new_z, new_x);
                let cliff = get_gate(gate);
                let p_exp = Op::Rotation {
                    data: RotationData::new(vec![*p], 1.0),
                };
                let new_p_exp = Op::Rotation {
                    data: RotationData::new(vec![p_prime], if sign_flip { -1.0 } else { 1.0 }),
                };
                let pg = PauliGraph::new(1).with_ops(vec![p_exp, cliff.clone()]);
                let pg1 = PauliGraph::new(1).with_ops(vec![cliff, new_p_exp]);
                assert!(compare_unitaries_via_tk(&pg, &pg1));
            }
        }
    }

    #[test]
    fn test_pauli_gates() {
        for gate in PAULI_GATES.iter() {
            let gate_fn = pauli_gate_fn(gate);
            for p in [Pauli::X, Pauli::Y, Pauli::Z].iter() {
                let (z, x) = pauli_to_bits(p);
                let sign_flip = gate_fn(z, x);
                let cliff = get_gate(gate);
                let p_exp = Op::Rotation {
                    data: RotationData::new(vec![*p], 1.0),
                };
                let new_p_exp = Op::Rotation {
                    data: RotationData::new(vec![*p], if sign_flip { -1.0 } else { 1.0 }),
                };
                let pg = PauliGraph::new(1).with_ops(vec![p_exp, cliff.clone()]);
                let pg1 = PauliGraph::new(1).with_ops(vec![cliff, new_p_exp]);
                assert!(compare_unitaries_via_tk(&pg, &pg1));
            }
        }
    }

    #[test]
    fn test_tq_gates() {
        for gate in TQ_GATES.iter() {
            let gate_fn = tq_gate_fn(gate);
            for p0 in [Pauli::X, Pauli::Y, Pauli::Z].iter() {
                for p1 in [Pauli::X, Pauli::Y, Pauli::Z].iter() {
                    let (z0, x0) = pauli_to_bits(p0);
                    let (z1, x1) = pauli_to_bits(p1);
                    let (new_z0, new_x0, new_z1, new_x1, sign_flip) = gate_fn(z0, x0, z1, x1);
                    let p0_prime = bits_to_pauli(new_z0, new_x0);
                    let p1_prime = bits_to_pauli(new_z1, new_x1);
                    let cliff = get_gate(gate);
                    let p_exp = Op::Rotation {
                        data: RotationData::new(vec![*p0, *p1], 1.0),
                    };
                    let new_p_exp = Op::Rotation {
                        data: RotationData::new(
                            vec![p0_prime, p1_prime],
                            if sign_flip { -1.0 } else { 1.0 },
                        ),
                    };
                    let pg = PauliGraph::new(2).with_ops(vec![p_exp, cliff.clone()]);
                    let pg1 = PauliGraph::new(2).with_ops(vec![cliff, new_p_exp]);
                    assert!(compare_unitaries_via_tk(&pg, &pg1));
                }
            }
        }
    }
}