tk_pg_bitpacked/kernels.rs
1use std::ops::{BitAnd, BitOr, BitXor, Not};
2
3/// A trait for types that support bitwise operations and can be used in the conjugation functions.
4pub trait BitOps:
5 BitAnd<Output = Self>
6 + BitXor<Output = Self>
7 + BitOr<Output = Self>
8 + Not<Output = Self>
9 + Copy
10 + Sized
11{
12}
13
14// Blanket impl — anything satisfying all bounds gets BitOps for free
15impl<T> BitOps for T where
16 T: BitAnd<Output = T> + BitXor<Output = T> + BitOr<Output = T> + Not<Output = T> + Copy
17{
18}
19
20use crate::u8_encodings::{XX_U8, XY_U8, XZ_U8, YX_U8, YY_U8, YZ_U8, ZX_U8, ZY_U8, ZZ_U8};
21
22// =============================================================================
23// Clifford conjugation functions on bit-packed Pauli operators
24// =============================================================================
25
26/// Apply X gate (X P X) on bit-packed Pauli operators.
27///
28/// This function transforms Pauli operators:
29/// - X -> X
30/// - Y -> -Y
31/// - Z -> -Z
32///
33/// # Arguments
34///
35/// * `z` - A `T` containing the Z component bits of the packed Pauli operators.
36/// * `x` - A `T` containing the X component bits of the packed Pauli operators.
37///
38/// # Returns
39/// The sign flip bits.
40///
41#[inline(always)]
42pub fn x_gate<T: BitOps>(z: T, _x: T) -> T {
43 // flip the sign if the letter is Z or Y
44 z
45}
46
47/// Apply Y gate (Y P Y) on bit-packed Pauli operators.
48///
49/// This function transforms Pauli operators:
50/// - X -> -X
51/// - Y -> Y
52/// - Z -> -Z
53///
54/// # Arguments
55///
56/// * `z` - A `T` containing the Z component bits of the packed Pauli operators.
57/// * `x` - A `T` containing the X component bits of the packed Pauli operators.
58///
59/// # Returns
60/// The sign flip bits.
61///
62#[inline(always)]
63pub fn y_gate<T: BitOps>(z: T, x: T) -> T {
64 // flip the sign if the letter is X or Z
65 z ^ x
66}
67
68/// Apply Z gate (Z P Z) on bit-packed Pauli operators.
69///
70/// This function transforms Pauli operators:
71/// - X -> -X
72/// - Y -> -Y
73/// - Z -> Z
74///
75/// # Arguments
76///
77/// * `z` - A `T` containing the Z component bits of the packed Pauli operators.
78/// * `x` - A `T` containing the X component bits of the packed Pauli operators.
79///
80/// # Returns
81/// The sign flip bits.
82///
83#[inline(always)]
84pub fn z_gate<T: BitOps>(_z: T, x: T) -> T {
85 // flip the sign if the letter is X or Y
86 x
87}
88
89/// Apply H gate (H P H) on bit-packed Pauli operators.
90///
91/// This function transforms Pauli operators:
92/// - X -> Z
93/// - Z -> X
94/// - Y -> -Y
95///
96/// # Arguments
97///
98/// * `z` - A `T` containing the Z component bits of the packed Pauli operators.
99/// * `x` - A `T` containing the X component bits of the packed Pauli operators.
100///
101/// # Returns
102///
103/// A tuple containing:
104/// - `T`: The new Z component bits.
105/// - `T`: The new X component bits.
106/// - `T`: The sign flip bits.
107///
108#[inline(always)]
109pub fn h_gate<T: BitOps>(z: T, x: T) -> (T, T, T) {
110 // swap bits
111 // sign flip if both bits are 1
112 (x, z, z & x)
113}
114
115/// Apply Sdg gate (Sdg P S) on bit-packed Pauli operators.
116///
117/// This function transforms Pauli operators:
118/// - X -> -Y
119/// - Y -> X
120/// - Z -> Z
121///
122/// # Arguments
123///
124/// * `z` - A `T` containing the Z component bits of the packed Pauli operators.
125/// * `x` - A `T` containing the X component bits of the packed Pauli operators.
126///
127/// # Returns
128///
129/// A tuple containing:
130/// - `T`: The new Z component bits.
131/// - `T`: The new X component bits.
132/// - `T`: The sign flip bits.
133///
134#[inline(always)]
135pub fn sdg_gate<T: BitOps>(z: T, x: T) -> (T, T, T) {
136 // toggle z bits with x bits
137 // sign flip if z bit is 0 and x bit is 1
138 (z ^ x, x, !z & x)
139}
140
141/// Apply S gate (S P Sdg) on bit-packed Pauli operators.
142///
143/// This function transforms Pauli operators:
144/// - X -> Y
145/// - Y -> -X
146/// - Z -> Z
147///
148/// # Arguments
149///
150/// * `z` - A `T` containing the Z component bits of the packed Pauli operators.
151/// * `x` - A `T` containing the X component bits of the packed Pauli operators.
152///
153/// # Returns
154///
155/// A tuple containing:
156/// - `T`: The new Z component bits.
157/// - `T`: The new X component bits.
158/// - `T`: The sign flip bits.
159///
160#[inline(always)]
161pub fn s_gate<T: BitOps>(z: T, x: T) -> (T, T, T) {
162 // toggle z bits with x bits
163 (z ^ x, x, z & x)
164}
165
166/// Apply Vdg gate (Vdg P V) on bit-packed Pauli operators.
167///
168/// This function transforms Pauli operators:
169/// - X -> X
170/// - Y -> -Z
171/// - Z -> Y
172///
173/// # Arguments
174///
175/// * `z` - A `T` containing the Z component bits of the packed Pauli operators.
176/// * `x` - A `T` containing the X component bits of the packed Pauli operators.
177///
178/// # Returns
179///
180/// A tuple containing:
181/// - `T`: The new Z component bits.
182/// - `T`: The new X component bits.
183/// - `T`: The sign flip bits.
184///
185#[inline(always)]
186pub fn vdg_gate<T: BitOps>(z: T, x: T) -> (T, T, T) {
187 // toggle x bits with z bits
188 // sign flip if both bits are 1
189 (z, x ^ z, z & x)
190}
191
192/// Apply V gate (V P Vdg) on bit-packed Pauli operators.
193///
194/// This function transforms Pauli operators:
195/// - X -> X
196/// - Y -> Z
197/// - Z -> -Y
198///
199/// # Arguments
200///
201/// * `z` - A `T` containing the Z component bits of the packed Pauli operators.
202/// * `x` - A `T` containing the X component bits of the packed Pauli operators.
203///
204/// # Returns
205///
206/// A tuple containing:
207/// - `T`: The new Z component bits.
208/// - `T`: The new X component bits.
209/// - `T`: The sign flip bits.
210///
211#[inline(always)]
212pub fn v_gate<T: BitOps>(z: T, x: T) -> (T, T, T) {
213 // toggle x bits with z bits
214 (z, x ^ z, z & !x)
215}
216
217/// Apply Ry(-pi/2) gate (Ry(-pi/2) P Ry(pi/2)) on bit-packed Pauli operators.
218///
219/// This function transforms Pauli operators:
220/// - X -> Z
221/// - Y -> Y
222/// - Z -> -X
223///
224/// # Arguments
225///
226/// * `z` - A `T` containing the Z component bits of the packed Pauli operators.
227/// * `x` - A `T` containing the X component bits of the packed Pauli operators.
228///
229/// # Returns
230///
231/// A tuple containing:
232/// - `T`: The new Z component bits.
233/// - `T`: The new X component bits.
234/// - `T`: The sign flip bits.
235///
236#[inline(always)]
237pub fn ry270_gate<T: BitOps>(z: T, x: T) -> (T, T, T) {
238 // swap bits
239 (x, z, z & !x)
240}
241
242/// Apply Ry(pi/2) gate (Ry(pi/2) P Ry(-pi/2)) on bit-packed Pauli operators.
243///
244/// This function transforms Pauli operators:
245/// - X -> -Z
246/// - Y -> Y
247/// - Z -> X
248///
249/// # Arguments
250///
251/// * `z` - A `T` containing the Z component bits of the packed Pauli operators.
252/// * `x` - A `T` containing the X component bits of the packed Pauli operators.
253///
254/// # Returns
255///
256/// A tuple containing:
257/// - `T`: The new Z component bits.
258/// - `T`: The new X component bits.
259/// - `T`: The sign flip bits.
260///
261#[inline(always)]
262pub fn ry90_gate<T: BitOps>(z: T, x: T) -> (T, T, T) {
263 // swap bits
264 (x, z, !z & x)
265}
266
267/// Apply Rz(-pi/2) gate (Rz(-pi/2) P Rz(pi/2)) on bit-packed Pauli operators. Equivalent to S gate.
268#[inline(always)]
269pub fn rz270_gate<T: BitOps>(z: T, x: T) -> (T, T, T) {
270 sdg_gate(z, x)
271}
272
273/// Apply Rz(pi/2) gate (Rz(pi/2) P Rz(-pi/2)) on bit-packed Pauli operators. Equivalent to Sdg gate.
274#[inline(always)]
275pub fn rz90_gate<T: BitOps>(z: T, x: T) -> (T, T, T) {
276 s_gate(z, x)
277}
278
279/// Apply Rx(-pi/2) gate (Rx(-pi/2) P Rx(pi/2)) on bit-packed Pauli operators. Equivalent to V gate.
280#[inline(always)]
281pub fn rx270_gate<T: BitOps>(z: T, x: T) -> (T, T, T) {
282 vdg_gate(z, x)
283}
284
285/// Apply Rx(pi/2) gate (Rx(pi/2) P Rx(-pi/2)) on bit-packed Pauli operators. Equivalent to Vdg gate.
286#[inline(always)]
287pub fn rx90_gate<T: BitOps>(z: T, x: T) -> (T, T, T) {
288 v_gate(z, x)
289}
290
291/// Apply XX gate (XX P XX) on bit-packed Pauli operators.
292///
293/// # Arguments
294///
295/// * `z0` - A `T` containing the Z component bits of the packed Pauli operators
296/// on the first qubit.
297/// * `x0` - A `T` containing the X component bits of the packed Pauli operators
298/// on the first qubit.
299/// * `z1` - A `T` containing the Z component bits of the packed Pauli operators
300/// on the second qubit.
301/// * `x1` - A `T` containing the X component bits of the packed Pauli operators
302/// on the second qubit.
303///
304/// # Returns
305///
306/// A tuple containing:
307/// - `T`: The new Z component bits on the first qubit.
308/// - `T`: The new X component bits on the first qubit.
309/// - `T`: The new Z component bits on the second qubit.
310/// - `T`: The new X component bits on the second qubit.
311/// - `T`: The sign flip bits.
312///
313#[inline(always)]
314pub fn xx_gate<T: BitOps>(z0: T, x0: T, z1: T, x1: T) -> (T, T, T, T, T) {
315 // we flip sign only for YZ, and ZY
316 // (z0&x0&z1&!x1)|(z0&!x0&z1&x1) = z0&z1&(x0^x1)
317 (z0, x0 ^ z1, z1, x1 ^ z0, z0 & z1 & (x0 ^ x1))
318}
319
320/// Apply XY (XY P XY) gate on bit-packed Pauli operators.
321///
322/// # Arguments
323///
324/// * `z0` - A `T` containing the Z component bits of the packed Pauli operators
325/// on the first qubit.
326/// * `x0` - A `T` containing the X component bits of the packed Pauli operators
327/// on the first qubit.
328/// * `z1` - A `T` containing the Z component bits of the packed Pauli operators
329/// on the second qubit.
330/// * `x1` - A `T` containing the X component bits of the packed Pauli operators
331/// on the second qubit.
332///
333/// # Returns
334///
335/// A tuple containing:
336/// - `T`: The new Z component bits on the first qubit.
337/// - `T`: The new X component bits on the first qubit.
338/// - `T`: The new Z component bits on the second qubit.
339/// - `T`: The new X component bits on the second qubit.
340/// - `T`: The sign flip bits.
341///
342#[inline(always)]
343pub fn xy_gate<T: BitOps>(z0: T, x0: T, z1: T, x1: T) -> (T, T, T, T, T) {
344 // we flip sign only for YX, and ZZ
345 // (z0&x0&!z1&x1)|(z0&!x0&z1&!x1) = z0&(x0==x1&x0!=z1)
346 (
347 z0,
348 x0 ^ z1 ^ x1,
349 z1 ^ z0,
350 x1 ^ z0,
351 z0 & !(x0 ^ x1) & (x0 ^ z1),
352 )
353}
354
355/// Apply XZ (XZ P XZ) gate on bit-packed Pauli operators.
356///
357/// # Arguments
358///
359/// * `z0` - A `T` containing the Z component bits of the packed Pauli operators
360/// on the first qubit.
361/// * `x0` - A `T` containing the X component bits of the packed Pauli operators
362/// on the first qubit.
363/// * `z1` - A `T` containing the Z component bits of the packed Pauli operators
364/// on the second qubit.
365/// * `x1` - A `T` containing the X component bits of the packed Pauli operators
366/// on the second qubit.
367///
368/// # Returns
369///
370/// A tuple containing:
371/// - `T`: The new Z component bits on the first qubit.
372/// - `T`: The new X component bits on the first qubit.
373/// - `T`: The new Z component bits on the second qubit.
374/// - `T`: The new X component bits on the second qubit.
375/// - `T`: The sign flip bits.
376///
377#[inline(always)]
378pub fn xz_gate<T: BitOps>(z0: T, x0: T, z1: T, x1: T) -> (T, T, T, T, T) {
379 // we flip sign only for YY, and ZX
380 (z0, x0 ^ x1, z1 ^ z0, x1, z0 & x1 & !(x0 ^ z1))
381}
382
383/// Apply YY (YY P YY) gate on bit-packed Pauli operators.
384///
385/// # Arguments
386///
387/// * `z0` - A `T` containing the Z component bits of the packed Pauli operators
388/// on the first qubit.
389/// * `x0` - A `T` containing the X component bits of the packed Pauli operators
390/// on the first qubit.
391/// * `z1` - A `T` containing the Z component bits of the packed Pauli operators
392/// on the second qubit.
393/// * `x1` - A `T` containing the X component bits of the packed Pauli operators
394/// on the second qubit.
395///
396/// # Returns
397///
398/// A tuple containing:
399/// - `T`: The new Z component bits on the first qubit.
400/// - `T`: The new X component bits on the first qubit.
401/// - `T`: The new Z component bits on the second qubit.
402/// - `T`: The new X component bits on the second qubit.
403/// - `T`: The sign flip bits.
404///
405#[inline(always)]
406pub fn yy_gate<T: BitOps>(z0: T, x0: T, z1: T, x1: T) -> (T, T, T, T, T) {
407 // toggle both bits, if the other two bits has parity 1
408 // we flip sign only for XZ, and ZX
409 // z0!=x0 & z1!=x1 & x0==z1
410 let toggle0 = z1 ^ x1;
411 let toggle1 = z0 ^ x0;
412 (
413 z0 ^ toggle0,
414 x0 ^ toggle0,
415 z1 ^ toggle1,
416 x1 ^ toggle1,
417 toggle1 & toggle0 & !(x0 ^ z1),
418 )
419}
420
421/// Apply YZ (YZ P YZ) gate on bit-packed Pauli operators.
422///
423/// # Arguments
424///
425/// * `z0` - A `T` containing the Z component bits of the packed Pauli operators
426/// on the first qubit.
427/// * `x0` - A `T` containing the X component bits of the packed Pauli operators
428/// on the first qubit.
429/// * `z1` - A `T` containing the Z component bits of the packed Pauli operators
430/// on the second qubit.
431/// * `x1` - A `T` containing the X component bits of the packed Pauli operators
432/// on the second qubit.
433///
434/// # Returns
435///
436/// A tuple containing:
437/// - `T`: The new Z component bits on the first qubit.
438/// - `T`: The new X component bits on the first qubit.
439/// - `T`: The new Z component bits on the second qubit.
440/// - `T`: The new X component bits on the second qubit.
441/// - `T`: The sign flip bits.
442///
443#[inline(always)]
444pub fn yz_gate<T: BitOps>(z0: T, x0: T, z1: T, x1: T) -> (T, T, T, T, T) {
445 // we flip sign only for ZY, and XX
446 // x1 & z0==z1 & x0!=z0
447 (
448 z0 ^ x1,
449 x0 ^ x1,
450 z1 ^ z0 ^ x0,
451 x1,
452 x1 & !(z0 ^ z1) & (x0 ^ z0),
453 )
454}
455
456/// Apply ZZ (ZZ P ZZ) gate on bit-packed Pauli operators.
457///
458/// # Arguments
459///
460/// * `z0` - A `T` containing the Z component bits of the packed Pauli operators
461/// on the first qubit.
462/// * `x0` - A `T` containing the X component bits of the packed Pauli operators
463/// on the first qubit.
464/// * `z1` - A `T` containing the Z component bits of the packed Pauli operators
465/// on the second qubit.
466/// * `x1` - A `T` containing the X component bits of the packed Pauli operators
467/// on the second qubit.
468///
469/// # Returns
470///
471/// A tuple containing:
472/// - `T`: The new Z component bits on the first qubit.
473/// - `T`: The new X component bits on the first qubit.
474/// - `T`: The new Z component bits on the second qubit.
475/// - `T`: The new X component bits on the second qubit.
476/// - `T`: The sign flip bits.
477///
478#[inline(always)]
479pub fn zz_gate<T: BitOps>(z0: T, x0: T, z1: T, x1: T) -> (T, T, T, T, T) {
480 // we flip sign only for XY, and YX
481 // x0&x1&z0!=z1
482 (z0 ^ x1, x0, z1 ^ x0, x1, x0 & x1 & (z0 ^ z1))
483}
484
485/// Apply TQE gate where the gate type is base-3 encoded in a u8.
486/// The encoding is as follows:
487/// - 0: XX
488/// - 1: XY
489/// - 2: XZ
490/// - 3: YX
491/// - 4: YY
492/// - 5: YZ
493/// - 6: ZX
494/// - 7: ZY
495/// - 8: ZZ
496///
497/// # Arguments
498///
499/// - `z0` (`&mut T`) - z bits on the first qubit.
500/// - `x0` (`&mut T`) - x bits on the first qubit.
501/// - `z1` (`&mut T`) - z bits on the second qubit.
502/// - `x1` (`&mut T`) - x bits on the second qubit.
503/// - `signs` (`&mut T`) - sign bits.
504/// - `mask` (`T`) - mask to apply the gate.
505/// - `gate` (`u8`) - gate type encoded in base-3.
506///
507pub fn apply_u8_tqe<T: BitOps>(
508 z0: &mut T,
509 x0: &mut T,
510 z1: &mut T,
511 x1: &mut T,
512 signs: &mut T,
513 mask: T,
514 gate: u8,
515) {
516 let (new_z0, new_x0, new_z1, new_x1, sign_flips) = match gate {
517 XX_U8 => xx_gate(*z0, *x0, *z1, *x1),
518 XY_U8 => xy_gate(*z0, *x0, *z1, *x1),
519 XZ_U8 => xz_gate(*z0, *x0, *z1, *x1),
520 YX_U8 => {
521 let (a, b, c, d, e) = xy_gate(*z1, *x1, *z0, *x0);
522 (c, d, a, b, e)
523 }
524 YY_U8 => yy_gate(*z0, *x0, *z1, *x1),
525 YZ_U8 => yz_gate(*z0, *x0, *z1, *x1),
526 ZX_U8 => {
527 let (a, b, c, d, e) = xz_gate(*z1, *x1, *z0, *x0);
528 (c, d, a, b, e)
529 }
530 ZY_U8 => {
531 let (a, b, c, d, e) = yz_gate(*z1, *x1, *z0, *x0);
532 (c, d, a, b, e)
533 }
534 ZZ_U8 => zz_gate(*z0, *x0, *z1, *x1),
535 _ => panic!("Invalid TQE gate"),
536 };
537
538 let new_signs = sign_flips ^ *signs;
539 let mask_ng = !mask;
540 *z0 = (*z0 & mask_ng) | (new_z0 & mask);
541 *x0 = (*x0 & mask_ng) | (new_x0 & mask);
542 *z1 = (*z1 & mask_ng) | (new_z1 & mask);
543 *x1 = (*x1 & mask_ng) | (new_x1 & mask);
544 *signs = (*signs & mask_ng) | (new_signs & mask);
545}
546
547#[cfg(test)]
548pub(crate) mod tests {
549 use super::*;
550 use crate::test_utils::*;
551 use tk_pg_converter::compare_unitaries_via_tk;
552 use tk_pg_core::{GateData, GateType, Op, Pauli, PauliGraph, RotationData};
553
554 type SqGateFn = fn(bool, bool) -> (bool, bool, bool);
555 type PauliGateFn = fn(bool, bool) -> bool;
556 type TqeGateFn = fn(bool, bool, bool, bool) -> (bool, bool, bool, bool, bool);
557
558 pub(crate) const SQ_GATES: [&str; 11] = [
559 "H", "S", "SDG", "V", "VDG", "RX90", "RX270", "RY90", "RY270", "RZ90", "RZ270",
560 ];
561 pub(crate) const PAULI_GATES: [&str; 3] = ["X", "Y", "Z"];
562 pub(crate) const TQ_GATES: [&str; 6] = ["XX", "XY", "XZ", "YY", "YZ", "ZZ"];
563
564 pub(crate) fn sq_gate_fn(name: &str) -> SqGateFn {
565 match name {
566 "H" => h_gate,
567 "S" => s_gate,
568 "SDG" => sdg_gate,
569 "V" => v_gate,
570 "VDG" => vdg_gate,
571 "RX90" => rx90_gate,
572 "RX270" => rx270_gate,
573 "RY90" => ry90_gate,
574 "RY270" => ry270_gate,
575 "RZ90" => rz90_gate,
576 "RZ270" => rz270_gate,
577 _ => panic!("Invalid gate name"),
578 }
579 }
580
581 pub(crate) fn pauli_gate_fn(name: &str) -> PauliGateFn {
582 match name {
583 "X" => x_gate,
584 "Y" => y_gate,
585 "Z" => z_gate,
586 _ => panic!("Invalid gate name"),
587 }
588 }
589
590 pub(crate) fn tq_gate_fn(name: &str) -> TqeGateFn {
591 match name {
592 "XX" => xx_gate,
593 "XY" => xy_gate,
594 "XZ" => xz_gate,
595 "YY" => yy_gate,
596 "YZ" => yz_gate,
597 "ZZ" => zz_gate,
598 _ => panic!("Invalid gate name"),
599 }
600 }
601
602 fn get_gate(name: &str) -> Op {
603 let gate_data = match name {
604 "H" => GateData::new(GateType::H, vec![0]),
605 "S" => GateData::new(GateType::S, vec![0]),
606 "SDG" => GateData::new(GateType::Sdg, vec![0]),
607 "V" => GateData::new(GateType::V, vec![0]),
608 "VDG" => GateData::new(GateType::Vdg, vec![0]),
609 "RX90" => GateData::new(GateType::RX, vec![0]).with_params(vec![0.5]),
610 "RX270" => GateData::new(GateType::RX, vec![0]).with_params(vec![1.5]),
611 "RY90" => GateData::new(GateType::RY, vec![0]).with_params(vec![0.5]),
612 "RY270" => GateData::new(GateType::RY, vec![0]).with_params(vec![1.5]),
613 "RZ90" => GateData::new(GateType::RZ, vec![0]).with_params(vec![0.5]),
614 "RZ270" => GateData::new(GateType::RZ, vec![0]).with_params(vec![1.5]),
615 "X" => GateData::new(GateType::X, vec![0]),
616 "Y" => GateData::new(GateType::Y, vec![0]),
617 "Z" => GateData::new(GateType::Z, vec![0]),
618 "XX" => GateData::new(GateType::XX, vec![0, 1]),
619 "XY" => GateData::new(GateType::XY, vec![0, 1]),
620 "XZ" => GateData::new(GateType::XZ, vec![0, 1]),
621 "YY" => GateData::new(GateType::YY, vec![0, 1]),
622 "YZ" => GateData::new(GateType::YZ, vec![0, 1]),
623 "ZZ" => GateData::new(GateType::ZZ, vec![0, 1]),
624 _ => panic!("Invalid gate name"),
625 };
626 Op::Gate { data: gate_data }
627 }
628
629 #[test]
630 fn test_sq_gates() {
631 for gate in SQ_GATES.iter() {
632 let gate_fn = sq_gate_fn(gate);
633 for p in [Pauli::X, Pauli::Y, Pauli::Z].iter() {
634 let (z, x) = pauli_to_bits(p);
635 let (new_z, new_x, sign_flip) = gate_fn(z, x);
636 let p_prime = bits_to_pauli(new_z, new_x);
637 let cliff = get_gate(gate);
638 let p_exp = Op::Rotation {
639 data: RotationData::new(vec![*p], 1.0),
640 };
641 let new_p_exp = Op::Rotation {
642 data: RotationData::new(vec![p_prime], if sign_flip { -1.0 } else { 1.0 }),
643 };
644 let pg = PauliGraph::new(1).with_ops(vec![p_exp, cliff.clone()]);
645 let pg1 = PauliGraph::new(1).with_ops(vec![cliff, new_p_exp]);
646 assert!(compare_unitaries_via_tk(&pg, &pg1));
647 }
648 }
649 }
650
651 #[test]
652 fn test_pauli_gates() {
653 for gate in PAULI_GATES.iter() {
654 let gate_fn = pauli_gate_fn(gate);
655 for p in [Pauli::X, Pauli::Y, Pauli::Z].iter() {
656 let (z, x) = pauli_to_bits(p);
657 let sign_flip = gate_fn(z, x);
658 let cliff = get_gate(gate);
659 let p_exp = Op::Rotation {
660 data: RotationData::new(vec![*p], 1.0),
661 };
662 let new_p_exp = Op::Rotation {
663 data: RotationData::new(vec![*p], if sign_flip { -1.0 } else { 1.0 }),
664 };
665 let pg = PauliGraph::new(1).with_ops(vec![p_exp, cliff.clone()]);
666 let pg1 = PauliGraph::new(1).with_ops(vec![cliff, new_p_exp]);
667 assert!(compare_unitaries_via_tk(&pg, &pg1));
668 }
669 }
670 }
671
672 #[test]
673 fn test_tq_gates() {
674 for gate in TQ_GATES.iter() {
675 let gate_fn = tq_gate_fn(gate);
676 for p0 in [Pauli::X, Pauli::Y, Pauli::Z].iter() {
677 for p1 in [Pauli::X, Pauli::Y, Pauli::Z].iter() {
678 let (z0, x0) = pauli_to_bits(p0);
679 let (z1, x1) = pauli_to_bits(p1);
680 let (new_z0, new_x0, new_z1, new_x1, sign_flip) = gate_fn(z0, x0, z1, x1);
681 let p0_prime = bits_to_pauli(new_z0, new_x0);
682 let p1_prime = bits_to_pauli(new_z1, new_x1);
683 let cliff = get_gate(gate);
684 let p_exp = Op::Rotation {
685 data: RotationData::new(vec![*p0, *p1], 1.0),
686 };
687 let new_p_exp = Op::Rotation {
688 data: RotationData::new(
689 vec![p0_prime, p1_prime],
690 if sign_flip { -1.0 } else { 1.0 },
691 ),
692 };
693 let pg = PauliGraph::new(2).with_ops(vec![p_exp, cliff.clone()]);
694 let pg1 = PauliGraph::new(2).with_ops(vec![cliff, new_p_exp]);
695 assert!(compare_unitaries_via_tk(&pg, &pg1));
696 }
697 }
698 }
699 }
700}