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
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
704
705
706
707
708
709
710
711
712
713
use std::convert::Infallible;

use ascent::{Lattice, lattice::BoundedLattice};

use hugr_core::builder::{CFGBuilder, DataflowHugr, ModuleBuilder, inout_sig};
use hugr_core::ops::{CallIndirect, TailLoop};
use hugr_core::types::{ConstTypeError, TypeRow};
use hugr_core::{Hugr, IncomingPort, Node, Wire};
use hugr_core::{
    HugrView,
    builder::{DFGBuilder, Dataflow, DataflowSubContainer, HugrBuilder, SubContainer, endo_sig},
    extension::prelude::{UnpackTuple, bool_t},
    ops::{DataflowOpTrait, Tag, Value, handle::NodeHandle},
    type_row,
    types::{Signature, SumType, Type},
};
use rstest::{fixture, rstest};

use super::{
    AbstractValue, AsConcrete, ConstLoader, DFContext, LoadedFunction, Machine, PartialValue, Sum,
    TailLoopTermination,
};

// ------- Minimal implementation of DFContext and AbstractValue -------
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
enum Void {}

impl AbstractValue for Void {}

struct TestContext;

impl ConstLoader<Void> for TestContext {
    type Node = Node;
}
impl DFContext<Void> for TestContext {}

// This allows testing creation of tuple/sum Values (only)
impl<N> AsConcrete<Void, N> for Value {
    type ValErr = Infallible;

    type SumErr = ConstTypeError;

    fn from_value(v: Void) -> Result<Self, Infallible> {
        match v {}
    }

    fn from_sum(value: Sum<Self>) -> Result<Self, Self::SumErr> {
        Self::sum(value.tag, value.values, value.st)
    }

    fn from_func(
        func: LoadedFunction<N>,
    ) -> Result<Self, crate::passes::dataflow::LoadedFunction<N>> {
        Err(func)
    }
}

fn pv_false() -> PartialValue<Void> {
    PartialValue::new_variant(0, [])
}

fn pv_true() -> PartialValue<Void> {
    PartialValue::new_variant(1, [])
}

fn pv_true_or_false() -> PartialValue<Void> {
    pv_true().join(pv_false())
}

#[test]
fn test_make_tuple() {
    let mut builder = DFGBuilder::new(endo_sig(vec![])).unwrap();
    let v1 = builder.add_load_value(Value::false_val());
    let v2 = builder.add_load_value(Value::true_val());
    let v3 = builder.make_tuple([v1, v2]).unwrap();
    let hugr = builder.finish_hugr().unwrap();

    let results = Machine::new(&hugr).run_subtree(TestContext, hugr.entrypoint());

    let x: Value = results.try_read_wire_concrete(v3).unwrap();
    assert_eq!(x, Value::tuple([Value::false_val(), Value::true_val()]));
}

#[test]
fn test_unpack_tuple_const() {
    let mut builder = DFGBuilder::new(endo_sig(vec![])).unwrap();
    let v = builder.add_load_value(Value::tuple([Value::false_val(), Value::true_val()]));
    let [o1, o2] = builder
        .add_dataflow_op(UnpackTuple::new(vec![bool_t(); 2].into()), [v])
        .unwrap()
        .outputs_arr();
    let hugr = builder.finish_hugr().unwrap();

    let results = Machine::new(&hugr).run_subtree(TestContext, hugr.module_root());

    let o1_r: Value = results.try_read_wire_concrete(o1).unwrap();
    assert_eq!(o1_r, Value::false_val());
    let o2_r: Value = results.try_read_wire_concrete(o2).unwrap();
    assert_eq!(o2_r, Value::true_val());
}

#[test]
fn test_tail_loop_never_iterates() {
    let mut builder = DFGBuilder::new(Signature::new_endo([])).unwrap();
    let r_v = Value::unit_sum(3, 6).unwrap();
    let r_w = builder.add_load_value(r_v.clone());
    let tag = Tag::new(
        TailLoop::BREAK_TAG,
        vec![type_row![], [r_v.get_type()].into()],
    );
    let tagged = builder.add_dataflow_op(tag, [r_w]).unwrap();

    let tlb = builder
        .tail_loop_builder([], [], vec![r_v.get_type()].into())
        .unwrap();
    let tail_loop = tlb.finish_with_outputs(tagged.out_wire(0), []).unwrap();
    let [tl_o] = tail_loop.outputs_arr();
    let hugr = builder.finish_hugr().unwrap();

    let results = Machine::new(&hugr).run_subtree(TestContext, hugr.entrypoint());

    let o_r: Value = results.try_read_wire_concrete(tl_o).unwrap();
    assert_eq!(o_r, r_v);
    assert_eq!(
        Some(TailLoopTermination::NeverContinues),
        results.tail_loop_terminates(tail_loop.node())
    );
}

#[test]
fn test_tail_loop_always_iterates() {
    let mut builder = DFGBuilder::new(Signature::new_endo([])).unwrap();
    let r_w = builder.add_load_value(
        Value::sum(
            TailLoop::CONTINUE_TAG,
            [],
            SumType::new([type_row![], [bool_t()].into()]),
        )
        .unwrap(),
    );
    let true_w = builder.add_load_value(Value::true_val());

    let tlb = builder
        .tail_loop_builder([], [(bool_t(), true_w)], vec![bool_t()].into())
        .unwrap();

    // r_w has tag 0, so we always continue;
    // we put true in our "other_output", but we should not propagate this to
    // output because r_w never supports 1.
    let tail_loop = tlb.finish_with_outputs(r_w, [true_w]).unwrap();

    let [tl_o1, tl_o2] = tail_loop.outputs_arr();
    let hugr = builder.finish_hugr().unwrap();

    let results = Machine::new(&hugr).run_subtree(TestContext, hugr.module_root());

    let o_r1 = results.read_out_wire(tl_o1).unwrap();
    assert_eq!(o_r1, PartialValue::bottom());
    let o_r2 = results.read_out_wire(tl_o2).unwrap();
    assert_eq!(o_r2, PartialValue::bottom());
    assert_eq!(
        Some(TailLoopTermination::NeverBreaks),
        results.tail_loop_terminates(tail_loop.node())
    );
    assert_eq!(results.tail_loop_terminates(hugr.entrypoint()), None);
}

#[test]
fn test_tail_loop_two_iters() {
    let mut builder = DFGBuilder::new(Signature::new_endo([])).unwrap();

    let true_w = builder.add_load_value(Value::true_val());
    let false_w = builder.add_load_value(Value::false_val());

    let tlb = builder
        .tail_loop_builder([], [(bool_t(), false_w), (bool_t(), true_w)], type_row![])
        .unwrap();
    assert_eq!(
        tlb.loop_signature().unwrap().signature().as_ref(),
        &Signature::new_endo(vec![bool_t(); 2])
    );
    let [in_w1, in_w2] = tlb.input_wires_arr();
    let tail_loop = tlb.finish_with_outputs(in_w1, [in_w2, in_w1]).unwrap();

    let hugr = builder.finish_hugr().unwrap();
    let [o_w1, o_w2] = tail_loop.outputs_arr();

    let results = Machine::new(&hugr).run_subtree(TestContext, hugr.entrypoint());

    let o_r1 = results.read_out_wire(o_w1).unwrap();
    assert_eq!(o_r1, pv_true_or_false());
    let o_r2 = results.read_out_wire(o_w2).unwrap();
    assert_eq!(o_r2, pv_true_or_false());
    assert_eq!(
        Some(TailLoopTermination::BreaksAndContinues),
        results.tail_loop_terminates(tail_loop.node())
    );
    assert_eq!(results.tail_loop_terminates(hugr.entrypoint()), None);
}

#[test]
fn test_tail_loop_containing_conditional() {
    let mut builder = DFGBuilder::new(Signature::new_endo([])).unwrap();
    let control_variants = vec![vec![bool_t(); 2].into(); 2];
    let control_t = Type::new_sum(control_variants.clone());
    let body_out_variants = vec![TypeRow::from([control_t.clone()]), vec![bool_t(); 2].into()];

    let init = builder.add_load_value(
        Value::sum(
            0,
            [Value::false_val(), Value::true_val()],
            SumType::new(control_variants.clone()),
        )
        .unwrap(),
    );

    let mut tlb = builder
        .tail_loop_builder([(control_t, init)], [], vec![bool_t(); 2].into())
        .unwrap();
    let tl = tlb.loop_signature().unwrap().clone();
    let [in_w] = tlb.input_wires_arr();

    // Branch on in_wire, so first iter 0(false, true)...
    let mut cond = tlb
        .conditional_builder(
            (control_variants.clone(), in_w),
            [],
            [Type::new_sum(body_out_variants.clone())].into(),
        )
        .unwrap();
    let mut case0_b = cond.case_builder(0).unwrap();
    let [a, b] = case0_b.input_wires_arr();
    // Builds value for next iter as 1(true, false) by flipping arguments
    let [next_input] = case0_b
        .add_dataflow_op(Tag::new(1, control_variants), [b, a])
        .unwrap()
        .outputs_arr();
    let cont = case0_b.make_continue(tl.clone(), [next_input]).unwrap();
    case0_b.finish_with_outputs([cont]).unwrap();
    // Second iter 1(true, false) => exit with (true, false)
    let mut case1_b = cond.case_builder(1).unwrap();
    let loop_res = case1_b.make_break(tl, case1_b.input_wires()).unwrap();
    case1_b.finish_with_outputs([loop_res]).unwrap();
    let [r] = cond.finish_sub_container().unwrap().outputs_arr();

    let tail_loop = tlb.finish_with_outputs(r, []).unwrap();

    let hugr = builder.finish_hugr().unwrap();
    let [o_w1, o_w2] = tail_loop.outputs_arr();

    let results = Machine::new(&hugr).run_subtree(TestContext, hugr.module_root());

    let o_r1 = results.read_out_wire(o_w1).unwrap();
    assert_eq!(o_r1, pv_true());
    let o_r2 = results.read_out_wire(o_w2).unwrap();
    assert_eq!(o_r2, pv_false());
    assert_eq!(
        Some(TailLoopTermination::BreaksAndContinues),
        results.tail_loop_terminates(tail_loop.node())
    );
    assert_eq!(results.tail_loop_terminates(hugr.entrypoint()), None);
}

#[test]
fn test_conditional() {
    let variants = vec![type_row![], type_row![], [bool_t()].into()];
    let cond_t = Type::new_sum(variants.clone());
    let mut builder = DFGBuilder::new(Signature::new([cond_t], type_row![])).unwrap();
    let [arg_w] = builder.input_wires_arr();

    let true_w = builder.add_load_value(Value::true_val());
    let false_w = builder.add_load_value(Value::false_val());

    let mut cond_builder = builder
        .conditional_builder(
            (variants, arg_w),
            [(bool_t(), true_w)],
            vec![bool_t(); 2].into(),
        )
        .unwrap();
    // will be unreachable
    let case1_b = cond_builder.case_builder(0).unwrap();
    let case1 = case1_b.finish_with_outputs([false_w, false_w]).unwrap();

    let case2_b = cond_builder.case_builder(1).unwrap();
    let [c2a] = case2_b.input_wires_arr();
    let case2 = case2_b.finish_with_outputs([false_w, c2a]).unwrap();

    let case3_b = cond_builder.case_builder(2).unwrap();
    let [c3_1, _c3_2] = case3_b.input_wires_arr();
    let case3 = case3_b.finish_with_outputs([c3_1, false_w]).unwrap();

    let cond = cond_builder.finish_sub_container().unwrap();

    let [cond_o1, cond_o2] = cond.outputs_arr();

    let hugr = builder.finish_hugr().unwrap();

    let arg_pv = PartialValue::new_variant(1, []).join(PartialValue::new_variant(
        2,
        [PartialValue::new_variant(0, [])],
    ));
    let results = {
        let mut m = Machine::new(&hugr);
        m.prepopulate_inputs(hugr.entrypoint(), [(0.into(), arg_pv)])
            .unwrap();
        m.run_subtree(TestContext, hugr.entrypoint())
    };

    let cond_r1: Value = results.try_read_wire_concrete(cond_o1).unwrap();
    assert_eq!(cond_r1, Value::false_val());
    assert!(results.try_read_wire_concrete::<Value>(cond_o2).is_err());

    assert_eq!(results.case_reachable(case1.node()), Some(false)); // arg_pv is variant 1 or 2 only
    assert_eq!(results.case_reachable(case2.node()), Some(true));
    assert_eq!(results.case_reachable(case3.node()), Some(true));
    assert_eq!(results.case_reachable(cond.node()), None);
}

// A Hugr being a function on bools: (x, y) => (x XOR y, x AND y)
#[fixture]
fn xor_and_cfg() -> Hugr {
    //        Entry       branch on first arg, passes arguments on unchanged
    //       /T   F\
    //      A --T-> B     A(x=true, y) branch on second arg, passing (first arg == true, false)
    //       \F    /      B(w,v) => X(v,w)
    //        > X <
    // Inputs received:
    // Entry    A       B       X
    // F,F      -       F,F     F,F
    // F,T      -       F,T     T,F
    // T,F      T,F     -       T,F
    // T,T      T,T     T,F     F,T
    let mut builder =
        CFGBuilder::new(Signature::new(vec![bool_t(); 2], vec![bool_t(); 2])).unwrap();

    // entry (x, y) => (if x then A else B)(x=true, y)
    let entry = builder
        .entry_builder(vec![type_row![]; 2], vec![bool_t(); 2].into())
        .unwrap();
    let [in_x, in_y] = entry.input_wires_arr();
    let entry = entry.finish_with_outputs(in_x, [in_x, in_y]).unwrap();

    // A(x==true, y) => (if y then B else X)(x, false)
    let mut a = builder
        .block_builder(
            vec![bool_t(); 2].into(),
            vec![type_row![]; 2],
            vec![bool_t(); 2].into(),
        )
        .unwrap();
    let [in_x, in_y] = a.input_wires_arr();
    let false_w1 = a.add_load_value(Value::false_val());
    let a = a.finish_with_outputs(in_y, [in_x, false_w1]).unwrap();

    // B(w, v) => X(v, w)
    let mut b = builder
        .block_builder(
            vec![bool_t(); 2].into(),
            [type_row![]],
            vec![bool_t(); 2].into(),
        )
        .unwrap();
    let [in_w, in_v] = b.input_wires_arr();
    let [control] = b
        .add_dataflow_op(Tag::new(0, vec![type_row![]]), [])
        .unwrap()
        .outputs_arr();
    let b = b.finish_with_outputs(control, [in_v, in_w]).unwrap();

    let x = builder.exit_block();

    let [fals, tru]: [usize; 2] = [0, 1];
    builder.branch(&entry, tru, &a).unwrap(); // if true
    builder.branch(&entry, fals, &b).unwrap(); // if false
    builder.branch(&a, tru, &b).unwrap(); // if true
    builder.branch(&a, fals, &x).unwrap(); // if false
    builder.branch(&b, 0, &x).unwrap();
    builder.finish_hugr().unwrap()
}

#[rstest]
#[case(pv_true(), pv_true(), pv_false(), pv_true())]
#[case(pv_true(), pv_false(), pv_true(), pv_false())]
#[case(pv_true(), pv_true_or_false(), pv_true_or_false(), pv_true_or_false())]
#[case(pv_true(), PartialValue::Top, pv_true_or_false(), pv_true_or_false())]
#[case(pv_false(), pv_true(), pv_true(), pv_false())]
#[case(pv_false(), pv_false(), pv_false(), pv_false())]
#[case(pv_false(), pv_true_or_false(), pv_true_or_false(), pv_false())]
#[case(pv_false(), PartialValue::Top, PartialValue::Top, pv_false())] // if !inp0 then out0=inp1
#[case(pv_true_or_false(), pv_true(), pv_true_or_false(), pv_true_or_false())]
#[case(pv_true_or_false(), pv_false(), pv_true_or_false(), pv_true_or_false())]
#[case(PartialValue::Top, pv_true(), pv_true_or_false(), PartialValue::Top)]
#[case(PartialValue::Top, pv_false(), PartialValue::Top, PartialValue::Top)]
fn test_cfg(
    #[case] inp0: PartialValue<Void>,
    #[case] inp1: PartialValue<Void>,
    #[case] out0: PartialValue<Void>,
    #[case] out1: PartialValue<Void>,
    xor_and_cfg: Hugr,
) {
    let root = xor_and_cfg.entrypoint();
    let mut m = Machine::new(&xor_and_cfg);
    m.prepopulate_inputs(
        xor_and_cfg.entrypoint(),
        [(0.into(), inp0), (1.into(), inp1)],
    )
    .unwrap();
    let results = m.run_subtree(TestContext, xor_and_cfg.entrypoint());

    assert_eq!(results.read_out_wire(Wire::new(root, 0)).unwrap(), out0);
    assert_eq!(results.read_out_wire(Wire::new(root, 1)).unwrap(), out1);
}

#[rstest]
#[case(pv_true(), pv_true(), pv_true())]
#[case(pv_false(), pv_false(), pv_false())]
#[case(pv_true(), pv_false(), pv_true_or_false())] // Two calls alias
fn test_call(
    #[case] inp0: PartialValue<Void>,
    #[case] inp1: PartialValue<Void>,
    #[case] out: PartialValue<Void>,
) {
    let mut builder = DFGBuilder::new(Signature::new_endo(vec![bool_t(); 2])).unwrap();
    let func_defn = {
        let mut mb = builder.module_root_builder();
        let func_bldr = mb
            .define_function("id", Signature::new_endo([bool_t()]))
            .unwrap();
        let [v] = func_bldr.input_wires_arr();
        func_bldr.finish_with_outputs([v]).unwrap()
    };
    let [a, b] = builder.input_wires_arr();
    let [a2] = builder
        .call(func_defn.handle(), &[], [a])
        .unwrap()
        .outputs_arr();
    let [b2] = builder
        .call(func_defn.handle(), &[], [b])
        .unwrap()
        .outputs_arr();
    let hugr = builder.finish_hugr_with_outputs([a2, b2]).unwrap();

    let results = {
        let mut m = Machine::new(&hugr);
        m.prepopulate_inputs(hugr.entrypoint(), [(0.into(), inp0), (1.into(), inp1)])
            .unwrap();
        m.run_subtree(TestContext, hugr.module_root())
    };

    let [res0, res1] = [0, 1].map(|i| {
        results
            .read_out_wire(Wire::new(hugr.entrypoint(), i))
            .unwrap()
    });
    // The two calls alias so both results will be the same:
    assert_eq!(res0, out);
    assert_eq!(res1, out);
}

#[test]
fn test_region() {
    let mut builder = DFGBuilder::new(Signature::new(vec![bool_t()], vec![bool_t(); 2])).unwrap();
    let [in_w] = builder.input_wires_arr();
    let cst_w = builder.add_load_const(Value::false_val());
    // Create a nested DFG which gets in_w passed as an input, but has a nonlocal edge
    // from the LoadConstant
    let nested = builder
        .dfg_builder(Signature::new([bool_t()], vec![bool_t(); 2]), [in_w])
        .unwrap();
    let [nested_in] = nested.input_wires_arr();
    let nested = nested.finish_with_outputs([nested_in, cst_w]).unwrap();
    let hugr = builder.finish_hugr_with_outputs(nested.outputs()).unwrap();
    let [nested_input, _] = hugr.get_io(nested.node()).unwrap();
    let whole_hugr_results = {
        let mut m = Machine::new(&hugr);
        m.prepopulate_inputs(hugr.entrypoint(), [(0.into(), pv_true())])
            .unwrap();
        m.run_subtree(TestContext, hugr.entrypoint())
    };
    let sub_hugr_results = {
        let mut m = Machine::new(&hugr);
        m.prepopulate_inputs(nested.node(), [(0.into(), pv_true())])
            .unwrap();
        m.run_subtree(TestContext, nested.node())
    };
    for (wire, val) in [
        (Wire::new(nested_input, 0), Some(pv_true())),
        (Wire::new(nested.node(), 0), Some(pv_true())),
        (Wire::new(nested.node(), 1), Some(pv_false())),
    ] {
        assert_eq!(whole_hugr_results.read_out_wire(wire), val);
        assert_eq!(sub_hugr_results.read_out_wire(wire), val);
    }

    for (wire, val) in [
        (cst_w, pv_false()),
        (Wire::new(hugr.entrypoint(), 0), pv_true()),
        (Wire::new(hugr.entrypoint(), 1), pv_false()),
    ] {
        assert_eq!(whole_hugr_results.read_out_wire(wire), Some(val));
        assert_eq!(sub_hugr_results.read_out_wire(wire), None);
    }
}

#[test]
fn test_module() {
    let mut modb = ModuleBuilder::new();
    let leaf_fn = modb
        .define_function("leaf", Signature::new_endo(vec![bool_t(); 2]))
        .unwrap();
    let outs = leaf_fn.input_wires();
    let leaf_fn = leaf_fn.finish_with_outputs(outs).unwrap();

    let mut f2 = modb
        .define_function("f2", Signature::new([bool_t()], vec![bool_t(); 2]))
        .unwrap();
    let [inp] = f2.input_wires_arr();
    let cst_true = f2.add_load_value(Value::true_val());
    let f2_call = f2.call(leaf_fn.handle(), &[], [inp, cst_true]).unwrap();
    let f2 = f2.finish_with_outputs(f2_call.outputs()).unwrap();

    let mut main = modb
        .define_function("main", Signature::new([bool_t()], vec![bool_t(); 2]))
        .unwrap();
    let [inp] = main.input_wires_arr();
    let cst_false = main.add_load_value(Value::false_val());
    let main_call = main.call(leaf_fn.handle(), &[], [inp, cst_false]).unwrap();
    let main = main.finish_with_outputs(main_call.outputs()).unwrap();
    let hugr = modb.finish_hugr().unwrap();
    let [f2_inp, _] = hugr.get_io(f2.node()).unwrap();

    let results_just_main = {
        let mut mach = Machine::new(&hugr);
        mach.prepopulate_inputs(main.node(), [(0.into(), pv_true())])
            .unwrap();
        mach.run_subtree(TestContext, hugr.module_root())
    };
    assert_eq!(
        results_just_main.read_out_wire(Wire::new(f2_inp, 0)),
        Some(PartialValue::Bottom)
    );
    for call in [f2_call, main_call] {
        // The first output of the Call comes from `main` because no value was fed in from f2
        assert_eq!(
            results_just_main.read_out_wire(Wire::new(call.node(), 0)),
            Some(pv_true())
        );
        // (Without reachability) the second output of the Call is the join of the two constant inputs from the two calls
        assert_eq!(
            results_just_main.read_out_wire(Wire::new(call.node(), 1)),
            Some(pv_true_or_false())
        );
    }

    let results_two_calls = {
        let mut m = Machine::new(&hugr);
        m.prepopulate_inputs(f2.node(), [(0.into(), pv_true())])
            .unwrap();
        m.prepopulate_inputs(main.node(), [(0.into(), pv_false())])
            .unwrap();
        m.run_subtree(TestContext, hugr.entrypoint())
    };

    for call in [f2_call, main_call] {
        assert_eq!(
            results_two_calls.read_out_wire(Wire::new(call.node(), 0)),
            Some(pv_true_or_false())
        );
        assert_eq!(
            results_two_calls.read_out_wire(Wire::new(call.node(), 1)),
            Some(pv_true_or_false())
        );
    }
}

#[rstest]
#[case(pv_false(), pv_false())]
#[case(pv_false(), pv_true())]
#[case(pv_true(), pv_false())]
#[case(pv_true(), pv_true())]
fn call_indirect(#[case] inp1: PartialValue<Void>, #[case] inp2: PartialValue<Void>) {
    let b2b = || Signature::new_endo([bool_t()]);
    let mut dfb = DFGBuilder::new(inout_sig(vec![bool_t(); 3], vec![bool_t(); 2])).unwrap();

    let [id1, id2] = ["id1", "[id2]"].map(|name| {
        let mut mb = dfb.module_root_builder();
        let fb = mb.define_function(name, b2b()).unwrap();
        let [inp] = fb.input_wires_arr();
        fb.finish_with_outputs([inp]).unwrap()
    });

    let [inp_direct, which, inp_indirect] = dfb.input_wires_arr();
    let [res1] = dfb
        .call(id1.handle(), &[], [inp_direct])
        .unwrap()
        .outputs_arr();

    // We'll unconditionally load both functions, to demonstrate that it's
    // the CallIndirect that matters, not just which functions are loaded.
    let lf1 = dfb.load_func(id1.handle(), &[]).unwrap();
    let lf2 = dfb.load_func(id2.handle(), &[]).unwrap();
    let bool_func = || Type::new_function(b2b());
    let mut cond = dfb
        .conditional_builder(
            (vec![type_row![]; 2], which),
            [(bool_func(), lf1), (bool_func(), lf2)],
            [bool_func()].into(),
        )
        .unwrap();
    let case_false = cond.case_builder(0).unwrap();
    let [f0, _f1] = case_false.input_wires_arr();
    case_false.finish_with_outputs([f0]).unwrap();
    let case_true = cond.case_builder(1).unwrap();
    let [_f0, f1] = case_true.input_wires_arr();
    case_true.finish_with_outputs([f1]).unwrap();
    let [tgt] = cond.finish_sub_container().unwrap().outputs_arr();
    let [res2] = dfb
        .add_dataflow_op(CallIndirect { signature: b2b() }, [tgt, inp_indirect])
        .unwrap()
        .outputs_arr();
    let h = dfb.finish_hugr_with_outputs([res1, res2]).unwrap();

    let run = |which| {
        let mut m = Machine::new(&h);
        m.prepopulate_inputs(
            h.entrypoint(),
            [
                (0.into(), inp1.clone()),
                (1.into(), which),
                (2.into(), inp2.clone()),
            ],
        )
        .unwrap();
        m.run_subtree(TestContext, h.entrypoint())
    };
    let (w1, w2) = (Wire::new(h.entrypoint(), 0), Wire::new(h.entrypoint(), 1));

    // 1. Test with `which` unknown -> second output unknown
    let results = run(PartialValue::Top);
    assert_eq!(results.read_out_wire(w1), Some(inp1.clone()));
    assert_eq!(results.read_out_wire(w2), Some(PartialValue::Top));

    // 2. Test with `which` selecting second function -> both passthrough
    let results = run(pv_true());
    assert_eq!(results.read_out_wire(w1), Some(inp1.clone()));
    assert_eq!(results.read_out_wire(w2), Some(inp2.clone()));

    //3. Test with `which` selecting first function -> alias
    let results = run(pv_false());
    let out = Some(inp1.join(inp2));
    assert_eq!(results.read_out_wire(w1), out);
    assert_eq!(results.read_out_wire(w2), out);
}

#[rstest]
fn func_set_input(#[values(false, true)] pre_pop_wire: bool) {
    let mut mb = ModuleBuilder::new();
    let func = mb
        .define_function("f", Signature::new_endo(vec![bool_t()]))
        .unwrap();
    let [inp] = func.input_wires_arr();
    let func = func.finish_with_outputs([inp]).unwrap();
    let hugr = mb.finish_hugr().unwrap();

    let [inp, _] = hugr.get_io(func.node()).unwrap();
    let mut m = Machine::<_, Void>::new(&hugr);
    let inp_w = Wire::new(inp, 0);
    if pre_pop_wire {
        m.prepopulate_wire(inp_w, pv_true());
    } else {
        m.prepopulate_inputs(func.node(), [(IncomingPort::from(0), pv_true())])
            .unwrap();
    }
    let results = m.run_subtree(TestContext, hugr.module_root());
    assert_eq!(results.read_out_wire(inp_w), Some(pv_true()));
}

#[rstest]
fn func_override_input_top(#[values(false, true)] pre_pop_wire: bool) {
    let mut mb = ModuleBuilder::new();

    let callee = mb
        .define_function("id", Signature::new_endo([bool_t()]))
        .unwrap();
    let [b] = callee.input_wires_arr();
    let callee = callee.finish_with_outputs([b]).unwrap();

    let mut main = mb
        .define_function("main", Signature::new([], [bool_t()]))
        .unwrap();
    let tr = main.add_load_value(Value::true_val());
    let call = main.call(callee.handle(), &[], [tr]).unwrap();
    main.finish_with_outputs(call.outputs()).unwrap();

    let hugr = mb.finish_hugr().unwrap();

    let [callee_inp, _] = hugr.get_io(callee.node()).unwrap();
    let callee_inp = Wire::new(callee_inp, 0);
    let ep = hugr.entrypoint();
    let mut machine = Machine::<_, Void>::new(hugr);

    if pre_pop_wire {
        machine.prepopulate_wire(callee_inp, PartialValue::Top);
    } else {
        machine
            .prepopulate_inputs(callee.node(), [(IncomingPort::from(0), PartialValue::Top)])
            .unwrap();
    }
    let results = machine.run_subtree(TestContext, ep);
    // `true` value from main should be overridden by the `Top` we fed in:
    assert_eq!(results.read_out_wire(callee_inp), Some(PartialValue::Top));
}