tket 0.18.0

Quantinuum's TKET Quantum Compiler
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
//! Scope configuration for a pass.
//!
//! This defines the parts of the HUGR that a pass should be applied to, and
//! which parts is it allowed to modify.
//!
//! See [`PassScope`] for more details.

use hugr_core::ops::{OpType, ValidateOp};
use hugr_core::{HugrView, Visibility};
use itertools::{Either, Itertools};

/// Scope configuration for a pass.
///
/// The scope of a pass defines which parts of a HUGR it should be applied to,
/// and which parts it is allowed to modify.
///
/// Each variant defines three properties: [PassScope::root],
/// [PassScope::preserve_interface], and [PassScope::recursive].
///
/// From these, [PassScope::regions] and [PassScope::in_scope] can be derived.
///
/// A pass will always optimize the entrypoint region, unless the entrypoint
/// is the module root.
//
// This enum should be kept in sync with the `PassScope` enum in `hugr-py`.
#[derive(Debug, Clone, PartialEq, Eq, derive_more::From, Hash, derive_more::Display)]
#[non_exhaustive]
pub enum PassScope {
    /// Run the pass on the entrypoint region.
    ///
    /// If the entrypoint is the module root, does nothing.
    ///
    /// The pass is allowed, but not required, to optimize descendant regions too.
    /// (For passes where it makes sense to distinguish flat from [Self::EntrypointRecursive],
    /// this is encouraged, but for many passes it does not make sense so both EntrypointXXX
    /// variants may behave the same.)
    ///
    /// - `root`: The entrypoint node, if it is not the module root.
    /// - `preserve_interface`: the entrypoint node
    /// - `recursive`: `false`.
    EntrypointFlat,
    /// Run the pass on the entrypoint region and all its descendants.
    ///
    /// For an idempotent pass, this means that immediately rerunning the pass on
    /// any subregion (i.e. with the entrypoint set to any descendant of
    /// the current value), must have no effect.
    ///
    /// If the entrypoint is the module root, does nothing.
    ///
    /// - `root`: The entrypoint node, if it is not the module root.
    /// - `preserve_interface`: the entrypoint node
    /// - `recursive`: `true`.
    EntrypointRecursive,
    /// Run the pass on the whole Hugr, regardless of the entrypoint.
    ///
    /// For lowering passes, signature changes etc. should be applied across the hugr.
    ///
    /// For optimization passes, the inner [Preserve] details which parts must
    /// have their interface preserved.
    ///
    /// - `root`: the [HugrView::module_root]
    /// - `preserve_interface`: according to [Preserve]
    /// - `recursive`: `true`.
    Global(#[from] Preserve),
}

/// Which nodes in the Hugr should have their interface preserved by optimization passes.
///
/// (Interface means signature/value ports, as well as static ports, and their types;
/// also name (if public) for linking; and whether the node is a valid dataflow child
/// or is a [DataflowBlock], [ExitBlock] or [Module]).
///
/// For lowering passes (whose goal is to change the interface!), generally this has no
/// effect.
///
/// [DataflowBlock]: OpType::DataflowBlock
/// [ExitBlock]: OpType::ExitBlock
/// [Module]: OpType::Module
#[derive(Debug, Clone, PartialEq, Eq, Default, Hash, derive_more::Display)]
pub enum Preserve {
    /// Optimization passes must preserve behaviour and interface of every
    /// module child, as well as the entrypoint.
    ///
    /// `preserve_interface`: every child of the module, and the entrypoint.
    All,
    /// Optimization passes must preserve interface and behaviour of all public
    /// functions, as well as the entrypoint.
    ///
    /// Private functions and constant definitions may be modified, including
    /// changing their behaviour or deleting them entirely, so long as this
    /// does not affect behaviour of the public functions (or entrypoint).
    ///
    /// Thus, appropriate for a Hugr that will be linked as a library.
    ///
    /// - `preserve_interface`: every public function defined in the module,
    ///   and the entrypoint.
    #[default]
    Public,
    /// Run the pass on the whole Hugr, but preserving behaviour only of the entrypoint.
    ///
    /// Thus, appropriate for a Hugr that will be run as an executable, with the
    /// entrypoint indicating where execution will begin.
    ///
    /// If the entrypoint is the module root, then the same as [Self::Public].
    ///
    /// - `preserve_interface`: if the entrypoint node is the module root, then all
    ///   children of the module root; otherwise, just the entrypoint node.
    Entrypoint,
}

impl Default for PassScope {
    fn default() -> Self {
        Self::Global(Preserve::default())
    }
}

/// Whether a pass may modify a particular node
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum InScope {
    /// The pass may modify the node arbitrarily, including changing its interface,
    /// behaviour, and/or removing it altogether
    Yes,
    /// The pass may modify the interior of the node - its [OpType], and its descendants -
    /// but must maintain the same ports (including static [Function] ports and [ControlFlow] ports),
    /// function name and [Visibility], and behaviour. For the [Module], this is equivalent
    /// to [InScope::No].
    ///
    /// [Function]: [hugr_core::types::EdgeKind::Function]
    /// [ControlFlow]: [EdgeKind::ControlFlow]
    /// [Module]: OpType::Module
    PreserveInterface,
    /// The pass may not modify this node
    No,
}

impl PassScope {
    /// Returns the root of the subtree that may be optimized by the pass.
    ///
    /// If `None`, the pass may not do anything. (This can be the case for some
    /// entrypoint-specific scopes when the entrypoint is the module root. Use
    /// [PassScope::Global] instead.)
    ///
    /// Otherwise, will be either the module root (for a global pass) or the entrypoint.
    ///
    /// The pass should not touch anything outside this root, must respect
    /// [Self::preserve_interface] within it, and if [`Self::recursive`]
    ///  is `true`, should also optimize the descendant regions of the root.
    pub fn root<'a, H: HugrView>(&'a self, hugr: &'a H) -> Option<H::Node> {
        let ep = hugr.entrypoint();
        match self {
            Self::EntrypointFlat | Self::EntrypointRecursive => {
                (ep != hugr.module_root()).then_some(ep)
            }
            Self::Global(_) => Some(hugr.module_root()),
        }
    }

    /// Returns a list of nodes, in the subtree beneath [Self::root], for which
    /// the pass must preserve the observable semantics (ports, execution behaviour,
    /// linking).
    ///
    /// We include the [Module] in this list (if it is [Self::root]) as these
    /// properties must be preserved (this rules out any other changes).
    ///
    /// [Module]: OpType::Module
    pub fn preserve_interface<'a, H: HugrView>(
        &'a self,
        hugr: &'a H,
    ) -> impl Iterator<Item = H::Node> + 'a {
        self.root(hugr).into_iter().flat_map(move |r| {
            let ep = hugr.entrypoint();
            [r, ep]
                .into_iter()
                .unique()
                .chain(hugr.children(hugr.module_root()).filter(move |n| {
                    if *n == ep {
                        return false; // Entrypoint added above
                    };
                    match self {
                        Self::Global(Preserve::All) => return true,
                        Self::Global(Preserve::Public) => (), // fallthrough
                        Self::Global(Preserve::Entrypoint) if ep == hugr.module_root() => (), // fallthough
                        _ => return false,
                    };
                    let vis = match hugr.get_optype(*n) {
                        OpType::FuncDecl(fd) => fd.visibility(),
                        OpType::FuncDefn(fd) => fd.visibility(),
                        _ => return false,
                    };
                    vis == &Visibility::Public
                }))
        })
    }

    /// Return every region (every [dataflow] or [CFG] container - but excluding
    /// [Module]) in the Hugr to be optimized by the pass.
    ///
    /// This computes all the regions to be optimized at once. In general, it is
    /// more efficient to traverse the Hugr incrementally starting from the
    /// [PassScope::root] instead.
    ///
    /// [dataflow]: hugr_core::ops::OpTag::DataflowParent
    /// [CFG]: OpType::CFG
    /// [Module]: OpType::Module
    pub fn regions<'a, H: HugrView>(&'a self, hugr: &'a H) -> impl Iterator<Item = H::Node> {
        self.root(hugr).into_iter().flat_map(|r| {
            if self.recursive() {
                let mut iter = hugr.descendants(r);
                if r == hugr.module_root() {
                    assert_eq!(iter.next(), Some(hugr.module_root())); // skip
                }
                Either::Left(iter.filter(|n| {
                    hugr.get_optype(*n)
                        .validity_flags::<H::Node>()
                        .requires_children
                }))
            } else {
                assert_ne!(r, hugr.module_root());
                Either::Right(std::iter::once(r))
            }
        })
    }

    /// Returns whether the node may be modified by the pass.
    ///
    /// Nodes outside the `root` subtree are never in scope.
    /// Nodes inside the subtree may be either [InScope::Yes] or [InScope::PreserveInterface].
    pub fn in_scope<H: HugrView>(&self, hugr: &H, node: H::Node) -> InScope {
        let Some(r) = self.root(hugr) else {
            return InScope::No;
        };
        'in_subtree: {
            if r != hugr.module_root() {
                let mut anc = Some(node);
                while let Some(n) = anc {
                    if n == r {
                        break 'in_subtree;
                    };
                    anc = hugr.get_parent(n);
                }
                return InScope::No;
            }
        }
        if self.preserve_interface(hugr).contains(&node) {
            InScope::PreserveInterface
        } else {
            InScope::Yes
        }
    }

    /// Returns `true` if the pass should be applied recursively on the
    /// descendants of the root regions.
    pub fn recursive(&self) -> bool {
        !matches!(self, Self::EntrypointFlat)
    }
}

#[cfg(test)]
mod test {
    use std::collections::HashSet;

    use hugr_core::hugr::hugrmut::HugrMut;
    use rstest::{fixture, rstest};

    use hugr_core::builder::{Container, Dataflow, HugrBuilder, ModuleBuilder, SubContainer};
    use hugr_core::ops::Value;
    use hugr_core::ops::handle::NodeHandle;
    use hugr_core::std_extensions::arithmetic::int_types::ConstInt;
    use hugr_core::types::Signature;
    use hugr_core::{Hugr, Node};

    use super::*;

    #[derive(Debug)]
    struct TestHugr {
        hugr: Hugr,
        module_root: Node,
        public_func: Node,
        public_func_nested: Node,
        private_func: Node,
        public_func_decl: Node,
        private_func_decl: Node,
        const_def: Node,
    }

    #[fixture]
    fn th() -> TestHugr {
        let mut h = ModuleBuilder::new();
        let module_root = h.container_node();

        let (public_func, public_func_nested) = {
            let mut pub_f = h
                .define_function_vis(
                    "public_func",
                    Signature::new_endo(vec![]),
                    Visibility::Public,
                )
                .unwrap();

            let public_func_nested = {
                let pub_f_nested = pub_f.dfg_builder(Signature::new_endo(vec![]), []).unwrap();
                pub_f_nested.finish_sub_container().unwrap().node()
            };

            (
                pub_f.finish_sub_container().unwrap().node(),
                public_func_nested,
            )
        };

        let private_func = {
            let priv_f = h
                .define_function_vis(
                    "private_func",
                    Signature::new_endo(vec![]),
                    Visibility::Private,
                )
                .unwrap();
            priv_f.finish_sub_container().unwrap().node()
        };

        let public_func_decl = h
            .declare_vis(
                "public_func_decl",
                Signature::new_endo(vec![]).into(),
                Visibility::Public,
            )
            .unwrap()
            .node();

        let private_func_decl = h
            .declare_vis(
                "private_func_decl",
                Signature::new_endo(vec![]).into(),
                Visibility::Private,
            )
            .unwrap()
            .node();

        let const_def = h
            .add_constant(Value::from(ConstInt::new_u(5, 7).unwrap()))
            .node();

        TestHugr {
            hugr: h.finish_hugr().unwrap(),
            module_root,
            public_func,
            public_func_nested,
            private_func,
            public_func_decl,
            private_func_decl,
            const_def,
        }
    }

    #[rstest]
    #[case(PassScope::EntrypointFlat, false)]
    #[case(PassScope::EntrypointRecursive, true)]
    fn scope_entrypoint(mut th: TestHugr, #[case] scope: PassScope, #[case] recursive: bool) {
        assert_eq!(scope.recursive(), recursive);

        // When the entrypoint is the module root,
        // the pass should not be applied to any regions.
        th.hugr.set_entrypoint(th.module_root);
        assert_eq!(scope.root(&th.hugr), None);
        assert_eq!(scope.regions(&th.hugr).next(), None);
        for n in th.hugr.nodes() {
            assert_eq!(scope.in_scope(&th.hugr, n), InScope::No);
        }

        // Public function with a nested DFG
        th.hugr.set_entrypoint(th.public_func);
        assert_eq!(scope.root(&th.hugr), Some(th.public_func));
        let expected_regions = match recursive {
            true => vec![th.public_func, th.public_func_nested],
            false => vec![th.public_func],
        };
        assert_eq!(scope.regions(&th.hugr).collect_vec(), expected_regions);

        assert_eq!(scope.in_scope(&th.hugr, th.module_root), InScope::No);
        assert_eq!(
            scope.in_scope(&th.hugr, th.public_func),
            InScope::PreserveInterface
        );
        assert_eq!(
            scope.in_scope(&th.hugr, th.public_func_nested),
            InScope::Yes
        );
        for n in [
            th.module_root,
            th.private_func,
            th.public_func_decl,
            th.private_func_decl,
            th.const_def,
        ] {
            assert_eq!(scope.in_scope(&th.hugr, n), InScope::No);
        }

        // DFG inside a function
        th.hugr.set_entrypoint(th.public_func_nested);
        assert_eq!(scope.root(&th.hugr), Some(th.public_func_nested));
        assert_eq!(
            scope.regions(&th.hugr).collect_vec(),
            [th.public_func_nested]
        );
        for n in [
            th.module_root,
            th.public_func,
            th.private_func,
            th.public_func_decl,
            th.private_func_decl,
            th.const_def,
        ] {
            assert_eq!(scope.in_scope(&th.hugr, n), InScope::No)
        }
        assert_eq!(
            scope.in_scope(&th.hugr, th.public_func_nested),
            InScope::PreserveInterface
        );
    }

    #[rstest]
    fn preserve_all(th: TestHugr) {
        let preserve = [
            th.public_func,
            th.private_func,
            th.public_func_decl,
            th.private_func_decl,
            th.const_def,
        ];
        check_preserve(&th, Preserve::All, preserve)
    }

    fn check_preserve(
        th: &TestHugr,
        preserve: Preserve,
        expected_chs: impl IntoIterator<Item = Node>,
    ) {
        let scope = PassScope::Global(preserve);
        assert!(scope.recursive());
        let expected_chs = expected_chs.into_iter().collect::<HashSet<_>>();
        assert_eq!(scope.root(&th.hugr), Some(th.module_root));
        assert_eq!(
            scope.regions(&th.hugr).collect_vec(),
            [th.public_func, th.private_func, th.public_func_nested]
        );
        assert_eq!(
            scope.in_scope(&th.hugr, th.module_root),
            InScope::PreserveInterface
        );
        for n in [
            th.public_func,
            th.private_func,
            th.public_func_decl,
            th.public_func_nested,
            th.private_func_decl,
            th.const_def,
        ] {
            let expected = if expected_chs.contains(&n) {
                InScope::PreserveInterface
            } else {
                InScope::Yes
            };
            assert_eq!(
                scope.in_scope(&th.hugr, n),
                expected,
                "{:?} among {:?}",
                n,
                th
            );
        }
        let mut preserve = expected_chs;
        preserve.insert(th.module_root);
        assert_eq!(preserve, scope.preserve_interface(&th.hugr).collect());
    }

    #[rstest]
    fn preserve_public(th: TestHugr) {
        let preserve = [th.public_func, th.public_func_decl];
        check_preserve(&th, Preserve::Public, preserve)
    }

    #[rstest]
    fn preserve_entrypoint(mut th: TestHugr) {
        th.hugr.set_entrypoint(th.hugr.module_root());
        let preserve = [th.public_func, th.public_func_decl];
        check_preserve(&th, Preserve::Entrypoint, preserve);

        th.hugr.set_entrypoint(th.public_func_nested);
        let preserve = [th.public_func_nested];
        check_preserve(&th, Preserve::Entrypoint, preserve)
    }
}