tk-pg-optimize 0.1.1

Optimization passes for Pauli graphs
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
use crate::packed_op::{PackedOp, XZPackedPaulis};
use std::collections::HashMap;
use tk_pg_core::{
    BlackBoxData, ConditionalBoxData, MeasureData, Op, PGPass, PauliGraph, ResetData, RotationData,
};
use tk_pg_ir_kernels::PGTableau;
use tk_pg_qm_tableau::Tableau;
use tk_pg_utils::cliff_angle;

fn rotation_merging(pg: &PauliGraph) -> PauliGraph {
    let n_qubits = pg.get_n_qubits();
    let n_ops = pg.get_ops().len();
    let mut output_pg = PauliGraph::new(n_qubits);
    let mut output_ops: Vec<PackedOp> = Vec::with_capacity(n_ops);
    // Use `removed` to flag operations removed from `output_ops`.
    let mut removed: Vec<bool> = Vec::with_capacity(n_ops);
    let mut tab: Tableau = Tableau::eye(n_qubits);
    let mut tab_touched = false;
    // Group rotations by their Pauli strings while preserving their order in
    // the original graph. For each rotation, find the most recent rotation with
    // the same Pauli string. If it commutes with every intervening operation,
    // merge its angle into the current rotation and mark the earlier rotation
    // for removal. Otherwise, append the current rotation without merging it.
    //
    // Lookup keys use XZ bit-packing, whereas output operations use ZX
    // bit-packing. Commutation checks require these opposite encodings.
    let mut lookup: HashMap<XZPackedPaulis, Vec<usize>> = HashMap::new();
    for op in pg.get_ops().iter() {
        match op {
            Op::Rotation { data } => {
                let (s, sign_bit) = tab.conjugate_string(data.get_string());
                let mut angle = if sign_bit {
                    -data.get_angle()
                } else {
                    data.get_angle()
                };
                let key = XZPackedPaulis::from(s.as_slice());
                // Check whether a previous rotation with the same Pauli string
                // can be merged with the current one.
                if let Some(indices) = lookup.get_mut(&key)
                    && let Some(&last_index) = indices.last()
                {
                    let last_op = &output_ops[last_index];
                    // Check whether `last_op` commutes with every intervening
                    // op, ignoring operations already marked for removal.
                    let commute =
                        output_ops[last_index + 1..]
                            .iter()
                            .enumerate()
                            .all(|(i, inter_op)| {
                                removed[last_index + 1 + i] || inter_op.commute_with_string(&key)
                            });
                    if commute {
                        // Merge the angles.
                        if let Op::Rotation { data: last_data } = &last_op.op {
                            angle += last_data.get_angle();
                        } else {
                            // The lookup contains only rotations.
                            panic!("expected a rotation op");
                        }
                        // Mark the previous rotation for removal.
                        removed[last_index] = true;
                        // Remove the previous rotation from the lookup.
                        indices.pop();
                    }
                }
                // If the angle is Clifford, fold the rotation into the tableau.
                // Because the tableau faces backwards and the rotation is to
                // its left, post-compose the inverse rotation (with the
                // negative angle).
                let cliff = cliff_angle(angle);
                if cliff.is_some() {
                    tab.postcompose_op(&Op::Rotation {
                        data: RotationData::new(s, -angle),
                    });
                    tab_touched = true;
                } else {
                    output_ops.push(PackedOp::new(Op::Rotation {
                        data: RotationData::new(s.clone(), angle),
                    }));
                    removed.push(false);
                    lookup.entry(key).or_default().push(output_ops.len() - 1);
                }
            }
            Op::Measure { data } => {
                let (s, sign_bit) = tab.conjugate_string(data.get_string());
                output_ops.push(PackedOp::new(Op::Measure {
                    data: MeasureData::new(s, sign_bit ^ data.get_sign_bit(), data.get_cbit()),
                }));
                removed.push(false);
            }
            Op::Reset { data } => {
                let (z_string, mut z_sign_bit) = tab.conjugate_string(data.get_first_string());
                let (x_string, mut x_sign_bit) = tab.conjugate_string(data.get_second_string());
                z_sign_bit ^= data.get_first_sign_bit();
                x_sign_bit ^= data.get_second_sign_bit();
                output_ops.push(PackedOp::new(Op::Reset {
                    data: ResetData::new(z_string, x_string, z_sign_bit, x_sign_bit),
                }));
                removed.push(false);
            }
            Op::ConditionalBox { data } => {
                let mut new_cond_ops = Vec::with_capacity(data.get_ops().len());
                for cond_op in data.get_ops() {
                    match cond_op {
                        Op::Rotation { data } => {
                            let (s, sign_bit) = tab.conjugate_string(data.get_string());
                            let theta = if sign_bit {
                                -data.get_angle()
                            } else {
                                data.get_angle()
                            };
                            new_cond_ops.push(Op::Rotation {
                                data: RotationData::new(s, theta),
                            });
                        }
                        Op::Reset { data } => {
                            let (z_string, mut z_sign_bit) =
                                tab.conjugate_string(data.get_first_string());
                            let (x_string, mut x_sign_bit) =
                                tab.conjugate_string(data.get_second_string());
                            z_sign_bit ^= data.get_first_sign_bit();
                            x_sign_bit ^= data.get_second_sign_bit();
                            new_cond_ops.push(Op::Reset {
                                data: ResetData::new(z_string, x_string, z_sign_bit, x_sign_bit),
                            });
                        }
                        Op::Measure { data } => {
                            let (s, mut sign_bit) = tab.conjugate_string(data.get_string());
                            sign_bit ^= data.get_sign_bit();
                            new_cond_ops.push(Op::Measure {
                                data: MeasureData::new(s, sign_bit, data.get_cbit()),
                            });
                        }
                        _ => unimplemented!(
                            "only non-blocking Pauli operations (`Rotation`, `Reset`, and `Measure`) are currently supported inside conditional boxes"
                        ),
                    }
                }
                output_ops.push(PackedOp::new(Op::ConditionalBox {
                    data: ConditionalBoxData::new(
                        new_cond_ops,
                        data.get_conditional_bits().clone(),
                        data.get_conditional_values().clone(),
                    ),
                }));
                removed.push(false);
            }
            Op::BlackBox { data } => {
                if tab_touched {
                    output_ops.push(PackedOp::new(Op::Tableau {
                        data: tab.invert().into(),
                    }));
                    removed.push(false);
                }
                output_ops.push(PackedOp::new(Op::BlackBox {
                    data: BlackBoxData::new(data.get_qubits().clone(), data.get_content().clone()),
                }));
                removed.push(false);
                tab = Tableau::eye(n_qubits);
                tab_touched = false;
            }
            Op::Tableau { data } => {
                let mut tableau_from_op = Tableau::from(data.clone()).invert();
                tableau_from_op.compose(&tab);
                tab = tableau_from_op;
                tab_touched = true;
            }
            Op::SetBoundary => {
                // Remove set boundaries because rotation merging can produce
                // empty sets.
                continue;
            }
            _ => {
                panic!(
                    "Unsupported op type in `RotationMergingPass`. Consider running the `CanonicalFormPass` first."
                );
            }
        }
    }
    assert_eq!(output_ops.len(), removed.len());
    for (op, is_removed) in output_ops.into_iter().zip(removed) {
        if !is_removed {
            output_pg.add_op(op.op);
        }
    }
    if tab_touched {
        output_pg.add_op(Op::Tableau {
            data: tab.invert().into(),
        });
    }
    output_pg
}

/// Greedily merges Pauli rotations with the same Pauli string when possible.
///
/// Consider running the `CanonicalFormPass` first.
///
/// # Panics
///
/// The supported operations are [`Op::Rotation`], [`Op::Measure`],
/// [`Op::Reset`], [`Op::ConditionalBox`], [`Op::BlackBox`], [`Op::Tableau`],
/// and [`Op::SetBoundary`]. The pass panics if the graph contains any other
/// operation, or if a conditional box contains an operation other than
/// [`Op::Rotation`], [`Op::Reset`], or [`Op::Measure`].
///
pub struct RotationMergingPass {}

impl Default for RotationMergingPass {
    fn default() -> Self {
        Self::new()
    }
}

impl RotationMergingPass {
    /// Create a new instance of `RotationMergingPass`.
    pub fn new() -> Self {
        Self {}
    }
}
impl PGPass for RotationMergingPass {
    fn transform(&self, pg: &PauliGraph) -> PauliGraph {
        rotation_merging(pg)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use rand::{RngExt, SeedableRng, rngs::StdRng};
    use tk_pg_converter::compare_unitaries_via_tk;
    use tk_pg_core::{GateData, GateType, Pauli, TableauData};

    fn s_tableau_op() -> Op {
        let mut tableau = Tableau::eye(1);
        tableau.postcompose_op(&Op::Gate {
            data: GateData::new(GateType::S, vec![0]),
        });
        Op::Tableau {
            data: tableau.into(),
        }
    }

    #[test]
    fn test_rotation_merging() {
        let mut pg = PauliGraph::new(2);
        pg.add_op(Op::Rotation {
            data: RotationData::new(vec![Pauli::X, Pauli::I], 0.1),
        });
        pg.add_op(Op::Rotation {
            data: RotationData::new(vec![Pauli::X, Pauli::I], 0.2),
        });
        pg.add_op(Op::Rotation {
            data: RotationData::new(vec![Pauli::Z, Pauli::I], 0.1),
        });
        pg.add_op(Op::Rotation {
            data: RotationData::new(vec![Pauli::Z, Pauli::I], 0.2),
        });
        let merged_pg = rotation_merging(&pg);
        assert_eq!(merged_pg.get_ops().len(), 2);
        assert!(compare_unitaries_via_tk(&pg, &merged_pg));
    }

    #[test]
    fn test_rotation_merging_v() {
        let mut pg = PauliGraph::new(2);
        pg.add_op(Op::Rotation {
            data: RotationData::new(vec![Pauli::X, Pauli::I], 0.1),
        });
        pg.add_op(Op::Rotation {
            data: RotationData::new(vec![Pauli::X, Pauli::I], 0.4),
        });
        let merged_pg = rotation_merging(&pg);
        assert_eq!(merged_pg.get_ops().len(), 1);
        assert!(compare_unitaries_via_tk(&pg, &merged_pg));
    }

    #[test]
    fn test_x_rotations_only() {
        let mut pg = PauliGraph::new(2);
        pg.add_op(Op::Rotation {
            data: RotationData::new(vec![Pauli::X, Pauli::I], 0.1),
        });
        pg.add_op(Op::Rotation {
            data: RotationData::new(vec![Pauli::X, Pauli::X], 0.4),
        });

        pg.add_op(Op::Rotation {
            data: RotationData::new(vec![Pauli::X, Pauli::X], 0.4),
        });

        pg.add_op(Op::Rotation {
            data: RotationData::new(vec![Pauli::X, Pauli::X], 0.4),
        });
        pg.add_op(Op::Rotation {
            data: RotationData::new(vec![Pauli::X, Pauli::I], 0.4),
        });
        let merged_pg = rotation_merging(&pg);
        assert_eq!(merged_pg.get_ops().len(), 2);
        assert!(compare_unitaries_via_tk(&pg, &merged_pg));
    }

    #[test]
    fn test_rotation_merging_across_commuting_ops() {
        let commuting_ops = [
            (
                "measurement",
                Op::Measure {
                    data: MeasureData::new(vec![Pauli::I, Pauli::Z], true, 3),
                },
            ),
            (
                "reset",
                Op::Reset {
                    data: ResetData::new(
                        vec![Pauli::I, Pauli::Z],
                        vec![Pauli::I, Pauli::X],
                        true,
                        false,
                    ),
                },
            ),
            (
                "conditional box",
                Op::ConditionalBox {
                    data: ConditionalBoxData::new(
                        vec![Op::Rotation {
                            data: RotationData::new(vec![Pauli::I, Pauli::Z], 0.125),
                        }],
                        vec![2],
                        vec![false],
                    ),
                },
            ),
        ];

        for (case, commuting_op) in commuting_ops {
            let pg = PauliGraph::new(2).with_ops(vec![
                Op::Rotation {
                    data: RotationData::new(vec![Pauli::X, Pauli::I], 0.125),
                },
                commuting_op.clone(),
                Op::Rotation {
                    data: RotationData::new(vec![Pauli::X, Pauli::I], 0.125),
                },
            ]);

            let merged_pg = rotation_merging(&pg);

            assert_eq!(
                merged_pg,
                PauliGraph::new(2).with_ops(vec![
                    commuting_op,
                    Op::Rotation {
                        data: RotationData::new(vec![Pauli::X, Pauli::I], 0.25),
                    },
                ]),
                "failed case: {case}"
            );
        }
    }

    #[test]
    fn test_rotation_not_merged_across_anticommuting_op() {
        let pg = PauliGraph::new(1).with_ops(vec![
            Op::Rotation {
                data: RotationData::new(vec![Pauli::X], 0.125),
            },
            Op::Rotation {
                data: RotationData::new(vec![Pauli::Z], 0.25),
            },
            Op::Rotation {
                data: RotationData::new(vec![Pauli::X], 0.125),
            },
        ]);

        let merged_pg = rotation_merging(&pg);

        assert_eq!(merged_pg, pg);
    }

    #[test]
    fn test_tableau_propagation_through_supported_ops() {
        let conditional_box = Op::ConditionalBox {
            data: ConditionalBoxData::new(
                vec![
                    Op::Rotation {
                        data: RotationData::new(vec![Pauli::X], 0.25),
                    },
                    Op::Measure {
                        data: MeasureData::new(vec![Pauli::X], false, 4),
                    },
                    Op::Reset {
                        data: ResetData::new(vec![Pauli::Z], vec![Pauli::X], false, true),
                    },
                ],
                vec![4, 5],
                vec![true, false],
            ),
        };
        let pg = PauliGraph::new(1).with_ops(vec![
            s_tableau_op(),
            Op::Measure {
                data: MeasureData::new(vec![Pauli::X], true, 3),
            },
            Op::Reset {
                data: ResetData::new(vec![Pauli::Z], vec![Pauli::X], true, false),
            },
            conditional_box,
        ]);

        let transformed = rotation_merging(&pg);

        assert_eq!(
            transformed,
            PauliGraph::new(1).with_ops(vec![
                Op::Measure {
                    data: MeasureData::new(vec![Pauli::Y], false, 3),
                },
                Op::Reset {
                    data: ResetData::new(vec![Pauli::Z], vec![Pauli::Y], true, true),
                },
                Op::ConditionalBox {
                    data: ConditionalBoxData::new(
                        vec![
                            Op::Rotation {
                                data: RotationData::new(vec![Pauli::Y], -0.25),
                            },
                            Op::Measure {
                                data: MeasureData::new(vec![Pauli::Y], true, 4),
                            },
                            Op::Reset {
                                data: ResetData::new(vec![Pauli::Z], vec![Pauli::Y], false, false,),
                            },
                        ],
                        vec![4, 5],
                        vec![true, false],
                    ),
                },
                s_tableau_op(),
            ])
        );
    }

    #[test]
    fn test_preserving_tableau() {
        let mut pg = PauliGraph::new(2);
        let z_images = vec![
            (vec![Pauli::X, Pauli::X], true),
            (vec![Pauli::Z, Pauli::Z], false),
        ];
        let x_images = vec![
            (vec![Pauli::Z, Pauli::I], true),
            (vec![Pauli::I, Pauli::X], true),
        ];
        pg.add_op(Op::Tableau {
            data: TableauData::new(z_images, x_images),
        });
        let pass = RotationMergingPass::new();
        let transformed = pass.transform(&pg);
        assert!(compare_unitaries_via_tk(&pg, &transformed));
    }

    #[test]
    fn test_preserving_black_box() {
        let mut pg = PauliGraph::new(1);
        pg.add_op(Op::BlackBox {
            data: BlackBoxData::new(vec![0], "payload".into()),
        });
        let transformed = RotationMergingPass::new().transform(&pg);
        assert_eq!(pg, transformed);
    }

    #[test]
    fn test_pending_tableau_emitted_before_black_box() {
        let black_box = Op::BlackBox {
            data: BlackBoxData::new(vec![0], "payload".into()),
        };
        let pg = PauliGraph::new(1).with_ops(vec![
            Op::Rotation {
                data: RotationData::new(vec![Pauli::Z], 0.5),
            },
            black_box.clone(),
        ]);

        let transformed = rotation_merging(&pg);

        assert_eq!(
            transformed,
            PauliGraph::new(1).with_ops(vec![s_tableau_op(), black_box])
        );
    }

    #[test]
    fn test_set_boundaries_removed() {
        let rotation = Op::Rotation {
            data: RotationData::new(vec![Pauli::X], 0.25),
        };
        let pg =
            PauliGraph::new(1).with_ops(vec![Op::SetBoundary, rotation.clone(), Op::SetBoundary]);

        let transformed = rotation_merging(&pg);

        assert_eq!(transformed, PauliGraph::new(1).with_ops(vec![rotation]));
    }

    #[test]
    fn test_negative_angle_rotation_merging() {
        let pg = PauliGraph::new(1).with_ops(vec![
            Op::Rotation {
                data: RotationData::new(vec![Pauli::X], -0.125),
            },
            Op::Rotation {
                data: RotationData::new(vec![Pauli::X], 0.375),
            },
        ]);

        let transformed = rotation_merging(&pg);

        assert_eq!(
            transformed,
            PauliGraph::new(1).with_ops(vec![Op::Rotation {
                data: RotationData::new(vec![Pauli::X], 0.25),
            }])
        );
        assert!(compare_unitaries_via_tk(&pg, &transformed));
    }

    #[test]
    fn test_rotation_cancellation() {
        let pg = PauliGraph::new(1).with_ops(vec![
            Op::Rotation {
                data: RotationData::new(vec![Pauli::X], 0.125),
            },
            Op::Rotation {
                data: RotationData::new(vec![Pauli::X], -0.125),
            },
        ]);

        let transformed = rotation_merging(&pg);

        assert_eq!(
            transformed,
            PauliGraph::new(1).with_ops(vec![Op::Tableau {
                data: Tableau::eye(1).into(),
            }])
        );
        assert!(compare_unitaries_via_tk(&pg, &transformed));
    }

    #[test]
    #[should_panic(expected = "Unsupported op type in `RotationMergingPass`")]
    fn test_gate_panics() {
        let pg = PauliGraph::new(1).with_ops(vec![Op::Gate {
            data: GateData::new(GateType::RX, vec![0]).with_params(vec![0.25]),
        }]);
        RotationMergingPass::new().transform(&pg);
    }

    #[test]
    #[should_panic(expected = "only non-blocking Pauli operations")]
    fn test_unsupported_conditional_op_panics() {
        let pg = PauliGraph::new(1).with_ops(vec![Op::ConditionalBox {
            data: ConditionalBoxData::new(
                vec![Op::BlackBox {
                    data: BlackBoxData::new(vec![0], "payload".into()),
                }],
                vec![0],
                vec![true],
            ),
        }]);
        RotationMergingPass::new().transform(&pg);
    }

    #[test]
    #[should_panic(expected = "No non-identity Pauli found in the string")]
    fn test_all_identity_clifford_rotation_panics() {
        let pg = PauliGraph::new(1).with_ops(vec![Op::Rotation {
            data: RotationData::new(vec![Pauli::I], 0.5),
        }]);
        RotationMergingPass::new().transform(&pg);
    }

    #[test]
    fn test_random_rotation_merging() {
        let strings = [
            vec![Pauli::X, Pauli::I, Pauli::I],
            vec![Pauli::I, Pauli::Y, Pauli::I],
            vec![Pauli::I, Pauli::I, Pauli::Z],
            vec![Pauli::X, Pauli::X, Pauli::I],
            vec![Pauli::Z, Pauli::Z, Pauli::Z],
        ];
        let angles = [-0.125, 0.125, 0.25, 0.5, 0.75, 1.0];
        let mut rng = StdRng::seed_from_u64(7);
        let ops = (0..48)
            .map(|_| Op::Rotation {
                data: RotationData::new(
                    strings[rng.random_range(0..strings.len())].clone(),
                    angles[rng.random_range(0..angles.len())],
                ),
            })
            .collect();
        let pg = PauliGraph::new(3).with_ops(ops);

        let transformed = rotation_merging(&pg);

        assert!(compare_unitaries_via_tk(&pg, &transformed));
    }

    #[test]
    fn test_empty_graph() {
        let pg = PauliGraph::new(2);
        let transformed = RotationMergingPass::new().transform(&pg);
        assert_eq!(pg, transformed);
    }
}