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
//! Utilities for inserting element extraction and reconstruction functions for
//! container types like array and tuple.

pub mod op_function_map;
pub mod type_unpack;

pub use op_function_map::OpFunctionMap;
pub use type_unpack::TypeUnpacker;

use hugr::{
    Wire,
    builder::{BuildError, Dataflow},
    extension::{
        Extension,
        prelude::{UnpackTuple, UnwrapBuilder, option_type},
    },
    ops::{ExtensionOp, OpName},
    std_extensions::collections::{
        array::{Array, ArrayKind, op_builder::GenericArrayOpBuilder},
        borrow_array::BorrowArray,
    },
    types::{
        FuncValueType, PolyFuncTypeRV, SumType, Type, TypeArg, TypeBound, TypeRV,
        type_param::TypeParam,
    },
};
use std::sync::{Arc, LazyLock};

use type_unpack::{array_args, is_opt_of};

/// Invert the signature of a function type.
fn invert_sig(sig: &PolyFuncTypeRV) -> PolyFuncTypeRV {
    let body = FuncValueType::new(sig.body().output().clone(), sig.body().input().clone());
    PolyFuncTypeRV::new(sig.params(), body)
}

fn generic_array_unpack_sig<AK: ArrayKind>() -> PolyFuncTypeRV {
    PolyFuncTypeRV::new(
        vec![
            TypeParam::max_nat_type(),
            TypeParam::RuntimeType(TypeBound::Linear),
            TypeParam::new_list_type(TypeBound::Linear),
        ],
        FuncValueType::new(
            [AK::ty_parametric(
                TypeArg::new_var_use(0, TypeParam::max_nat_type()),
                Type::new_var_use(1, TypeBound::Linear),
            )
            .unwrap()],
            [TypeRV::new_row_var_use(2, TypeBound::Linear)],
        ),
    )
}

/// Helper function to add array operations for any ArrayKind to the extension
fn add_array_ops<AK: ArrayKind>(
    ext: &mut Extension,
    ext_ref: &std::sync::Weak<Extension>,
    unpack_name: OpName,
    repack_name: OpName,
) -> Result<(), hugr::extension::ExtensionBuildError> {
    let array_unpack_sig = generic_array_unpack_sig::<AK>();
    // pack some wires into an array
    ext.add_op(
        repack_name,
        Default::default(),
        invert_sig(&array_unpack_sig),
        ext_ref,
    )?;
    // unpack an array into some wires
    ext.add_op(unpack_name, Default::default(), array_unpack_sig, ext_ref)?;
    Ok(())
}

/// Temporary extension name for barrier-specific operations.
pub const TEMP_UNPACK_EXT_NAME: hugr::hugr::IdentList =
    hugr::hugr::IdentList::new_static_unchecked("__tket.barrier.temp");

// Temporary operation names.
const UNPACK_OPT: OpName = OpName::new_static("option_unwrap");
const REPACK_OPT: OpName = OpName::new_static("option_tag");
const ARRAY_UNPACK: OpName = OpName::new_static("array_unpack");
const ARRAY_REPACK: OpName = OpName::new_static("array_repack");
const BARRAY_UNPACK: OpName = OpName::new_static("barray_unpack");
const BARRAY_REPACK: OpName = OpName::new_static("barray_repack");
const TUPLE_UNPACK: OpName = OpName::new_static("tuple_unpack");
const TUPLE_REPACK: OpName = OpName::new_static("tuple_repack");

static TEMP_UNPACK_EXT: LazyLock<Arc<Extension>> = LazyLock::new(|| {
    Extension::new_arc(
        TEMP_UNPACK_EXT_NAME,
        hugr::extension::Version::new(0, 0, 0),
        |ext, ext_ref| {
            // Generic option unwrap/tag operations
            let opt_unwrap_sig = PolyFuncTypeRV::new(
                vec![TypeParam::RuntimeType(TypeBound::Linear)],
                FuncValueType::new(
                    hugr::types::TypeRow::from(vec![Type::from(
                        hugr::extension::prelude::option_type([Type::new_var_use(
                            0,
                            TypeBound::Linear,
                        )]),
                    )]),
                    hugr::types::TypeRow::from(vec![Type::new_var_use(0, TypeBound::Linear)]),
                ),
            );
            // produce option of element
            ext.add_op(
                REPACK_OPT,
                Default::default(),
                invert_sig(&opt_unwrap_sig),
                ext_ref,
            )
            .unwrap();
            // unwrap option of element
            ext.add_op(UNPACK_OPT, Default::default(), opt_unwrap_sig, ext_ref)
                .unwrap();

            // Add array operations for all ArrayKind types
            add_array_ops::<Array>(ext, ext_ref, ARRAY_UNPACK, ARRAY_REPACK).unwrap();
            add_array_ops::<BorrowArray>(ext, ext_ref, BARRAY_UNPACK, BARRAY_REPACK).unwrap();

            let tuple_unpack_sig = PolyFuncTypeRV::new(
                vec![
                    // incoming tuple row
                    TypeParam::new_list_type(TypeBound::Linear),
                    // unpacked row
                    TypeParam::new_list_type(TypeBound::Linear),
                ],
                FuncValueType::new(
                    [Type::new_tuple([TypeRV::new_row_var_use(
                        0,
                        TypeBound::Linear,
                    )])],
                    [TypeRV::new_row_var_use(1, TypeBound::Linear)],
                ),
            );
            // pack some wires into a tuple
            ext.add_op(
                TUPLE_REPACK,
                Default::default(),
                invert_sig(&tuple_unpack_sig),
                ext_ref,
            )
            .unwrap();
            // unpack a tuple into some wires
            ext.add_op(TUPLE_UNPACK, Default::default(), tuple_unpack_sig, ext_ref)
                .unwrap();
        },
    )
});

/// Factory for creating and caching container unpack/repack operations.
///
/// This factory provides a generic framework for unpacking and repacking container types
/// such as arrays, tuples, and option types. It uses lazy operation caching to avoid
/// regenerating the same function definitions multiple times.
#[derive(Clone)]
pub struct UnpackContainerBuilder {
    /// Function definitions for each instance of the operations.
    func_map: OpFunctionMap,
    /// Type analyzer for determining which types to unpack
    type_analyzer: TypeUnpacker,
}

impl UnpackContainerBuilder {
    /// Create a new instance with a custom type analyzer.
    pub fn new(type_analyzer: TypeUnpacker) -> Self {
        Self {
            func_map: OpFunctionMap::new(),
            type_analyzer,
        }
    }

    /// Consume and return the internal operation-to-function mapping.
    pub fn into_function_map(self) -> OpFunctionMap {
        self.func_map
    }

    /// Gets a reference to the internal type analyzer
    pub fn type_analyzer(&mut self) -> &mut TypeUnpacker {
        &mut self.type_analyzer
    }

    /// Get an operation from the extension.
    pub fn get_op(&self, name: &OpName, args: impl Into<Vec<TypeArg>>) -> Option<ExtensionOp> {
        ExtensionOp::new(TEMP_UNPACK_EXT.get_op(name)?.clone(), args).ok()
    }

    /// Insert an option unwrap operation for a given element type.
    pub fn unpack_option(
        &self,
        builder: &mut impl Dataflow,
        opt_wire: Wire,
        elem_ty: &Type,
    ) -> Result<Wire, BuildError> {
        let args = [elem_ty.clone().into()];
        let op = self.get_op(&UNPACK_OPT, args.clone()).expect("known op");
        self.func_map
            .insert_with(&op, &[elem_ty.clone().into()], |func_b| {
                let [in_wire] = func_b.input_wires_arr();
                let [out_wire] =
                    func_b.build_expect_sum(1, option_type([elem_ty.clone()]), in_wire, |_| {
                        format!("Value of type Option<{elem_ty}> is None so cannot unpack.")
                    })?;
                Ok(vec![out_wire])
            })?;
        Ok(builder
            .add_dataflow_op(op, [opt_wire])?
            .outputs()
            .next()
            .expect("one output"))
    }

    /// Insert an option construction operation for a given element type.
    pub fn repack_option(
        &self,
        builder: &mut impl Dataflow,
        wire: Wire,
        elem_ty: &Type,
    ) -> Result<Wire, BuildError> {
        let args = [elem_ty.clone().into()];
        let op = self.get_op(&REPACK_OPT, args.clone()).expect("known op");
        self.func_map.insert_with(&op, &[], |func_b| {
            let [in_wire] = func_b.input_wires_arr();
            let out_wire = func_b.make_sum(
                1,
                vec![hugr::type_row![], vec![elem_ty.clone()].into()],
                [in_wire],
            )?;
            Ok(vec![out_wire])
        })?;
        Ok(builder
            .add_dataflow_op(op, [wire])?
            .outputs()
            .next()
            .expect("one output"))
    }

    /// Generic array unpacking using the ArrayKind trait
    fn unpack_array<AK: ArrayKind>(
        &self,
        builder: &mut impl Dataflow,
        array_wire: Wire,
        size: u64,
        elem_ty: &Type,
        op_name: &OpName,
    ) -> Result<Vec<Wire>, BuildError> {
        let args = match self.array_args::<AK>(size, elem_ty) {
            Some(args) => args,
            None => return Ok(vec![array_wire]), // Not a type we should unpack
        };

        let op = self.get_op(op_name, args.clone()).expect("known op");

        self.func_map.insert_with(&op, &args[..2], |func_b| {
            let w = func_b.input().out_wire(0);
            let elems = func_b.add_generic_array_unpack::<AK>(elem_ty.clone(), size, w)?;

            let result: Vec<_> = elems
                .into_iter()
                .map(|wire| self.unpack_container(func_b, elem_ty, wire))
                .collect::<Result<Vec<_>, _>>()?
                .concat();
            Ok(result)
        })?;

        Ok(builder
            .add_dataflow_op(op, [array_wire])?
            .outputs()
            .collect())
    }

    /// Helper function for array arguments
    fn array_args<AK: ArrayKind>(&self, size: u64, elem_ty: &Type) -> Option<[TypeArg; 3]> {
        let row = self
            .type_analyzer
            .unpack_type(&AK::ty(size, elem_ty.clone()))?;
        let args = [
            size.into(),
            elem_ty.clone().into(),
            TypeArg::List(row.into_iter().map(Into::into).collect()),
        ];
        Some(args)
    }

    /// Generic array repacking using the ArrayKind trait
    fn repack_array<AK: ArrayKind>(
        &self,
        builder: &mut impl Dataflow,
        elem_wires: impl IntoIterator<Item = Wire>,
        size: u64,
        elem_ty: &Type,
        op_name: &OpName,
    ) -> Result<Wire, BuildError> {
        let args = match self.array_args::<AK>(size, elem_ty) {
            Some(args) => args,
            None => {
                return Ok(elem_wires
                    .into_iter()
                    .next()
                    .expect("Non-unpackable container should only have one wire."));
            }
        };

        let inner_row_len = self.type_analyzer.num_unpacked_wires(elem_ty);
        let op = self.get_op(op_name, args.clone()).expect("known op");

        self.func_map.insert_with(&op, &args[..2], |func_b| {
            let input = func_b.input();
            // SAFETY: We guarantee no aliasing by only using this pointer in this closure.
            let elems: Result<Vec<_>, _> = input
                .outputs()
                .collect::<Vec<_>>()
                .chunks(inner_row_len)
                .map(|chunk| self.repack_container(func_b, elem_ty, chunk.to_vec()))
                .collect();

            let array_wire = func_b.add_new_generic_array::<AK>(elem_ty.clone(), elems?)?;
            Ok(vec![array_wire])
        })?;
        Ok(builder
            .add_dataflow_op(op, elem_wires)?
            .outputs()
            .next()
            .expect("one output"))
    }

    /// Generate tuple arguments
    fn tuple_args(&self, tuple_row: &[Type]) -> Option<[TypeArg; 2]> {
        let unpacked_row = self
            .type_analyzer
            .unpack_type(&Type::new_tuple(tuple_row.to_vec()))?;

        let args = [
            TypeArg::List(tuple_row.iter().cloned().map(Into::into).collect()),
            TypeArg::List(unpacked_row.into_iter().map(Into::into).collect()),
        ];

        Some(args)
    }

    /// Unpack a row of types into a flat list of wires containing all elements matching the analyzer
    pub fn unpack_row(
        &self,
        builder: &mut impl Dataflow,
        types: &[Type],
        wires: impl IntoIterator<Item = Wire>,
    ) -> Result<Vec<Wire>, BuildError> {
        // Process each type in the row with its corresponding wire
        let unpacked: Result<Vec<_>, _> = types
            .iter()
            .zip(wires)
            .map(|(ty, wire)| self.unpack_container(builder, ty, wire))
            .collect();

        // Flatten the nested vector of wires
        Ok(unpacked?.concat())
    }

    /// Repack a flat list of wires into a row of structured types
    pub fn repack_row(
        &self,
        builder: &mut impl Dataflow,
        types: &[Type],
        wires: impl IntoIterator<Item = Wire>,
    ) -> Result<Vec<Wire>, BuildError> {
        let mut wires = wires.into_iter();
        types
            .iter()
            .map(|ty| {
                let wire_count = self.type_analyzer.num_unpacked_wires(ty);
                let type_wires = wires.by_ref().take(wire_count).collect();
                self.repack_container(builder, ty, type_wires)
            })
            .collect()
    }

    /// Unpack a tuple into individual wires
    pub fn unpack_tuple(
        &self,
        builder: &mut impl Dataflow,
        tuple_wire: Wire,
        tuple_row: &[Type],
    ) -> Result<Vec<Wire>, BuildError> {
        let tuple_row = tuple_row.to_vec();
        let args = match self.tuple_args(&tuple_row) {
            Some(args) => args,
            None => return Ok(vec![tuple_wire]), // Not a tuple we should unpack
        };
        let op = self.get_op(&TUPLE_UNPACK, args.clone()).expect("known op");

        self.func_map.insert_with(&op, &args[..1], |func_b| {
            let w = func_b.input().out_wire(0);
            let unpacked_tuple_wires = func_b
                .add_dataflow_op(UnpackTuple::new(tuple_row.clone().into()), [w])?
                .outputs()
                .collect::<Vec<_>>();

            let unpacked = self.unpack_row(func_b, &tuple_row, unpacked_tuple_wires)?;
            Ok(unpacked)
        })?;

        Ok(builder
            .add_dataflow_op(op, [tuple_wire])?
            .outputs()
            .collect())
    }

    /// Repack wires into a tuple
    pub fn repack_tuple(
        &self,
        builder: &mut impl Dataflow,
        elem_wires: impl IntoIterator<Item = Wire>,
        tuple_row: &[Type],
    ) -> Result<Wire, BuildError> {
        let tuple_row = tuple_row.to_vec();
        let args = match self.tuple_args(&tuple_row) {
            Some(args) => args,
            None => {
                return Ok(elem_wires
                    .into_iter()
                    .next()
                    .expect("Non-unpackable container should only have one wire."));
            }
        };

        let op = self.get_op(&TUPLE_REPACK, args.clone()).expect("known op");

        self.func_map.insert_with(&op, &args[..1], |func_b| {
            let in_wires = func_b.input().outputs().collect::<Vec<_>>();

            let repacked_elem_wires = self.repack_row(func_b, &tuple_row, in_wires)?;
            let tuple_wire = func_b.make_tuple(repacked_elem_wires)?;

            Ok(vec![tuple_wire])
        })?;

        Ok(builder
            .add_dataflow_op(op, elem_wires)?
            .outputs()
            .next()
            .expect("one output"))
    }

    /// Unpack a container type to extract wires matching the analyzer criteria.
    pub fn unpack_container(
        &self,
        builder: &mut impl Dataflow,
        ty: &Type,
        container_wire: Wire,
    ) -> Result<Vec<Wire>, BuildError> {
        let elem_ty = self.type_analyzer.element_type();
        // If the type is a qubit, return it directly
        if ty == elem_ty {
            return Ok(vec![container_wire]);
        }

        // Check for option of qubit
        if is_opt_of(ty, &hugr::extension::prelude::qb_t()) {
            return Ok(vec![self.unpack_option(
                builder,
                container_wire,
                elem_ty,
            )?]);
        }

        macro_rules! handle_array_type {
            ($array_kind:ty, $unpack_op:expr) => {
                if let Some((n, elem_ty)) = ty.as_extension().and_then(array_args::<$array_kind>) {
                    return self.unpack_array::<$array_kind>(
                        builder,
                        container_wire,
                        n,
                        elem_ty,
                        &$unpack_op,
                    );
                }
            };
        }

        handle_array_type!(Array, ARRAY_UNPACK);
        handle_array_type!(BorrowArray, BARRAY_UNPACK);

        if let Some(row) = ty.as_sum().and_then(SumType::as_tuple) {
            let row: hugr::types::TypeRow =
                row.clone().try_into().expect("unexpected row variable.");
            return self.unpack_tuple(builder, container_wire, &row);
        }

        // No need to unpack if the type doesn't match our analyzer criteria
        Ok(vec![container_wire])
    }

    /// Repack a container type from its unpacked wires.
    pub fn repack_container(
        &self,
        builder: &mut impl Dataflow,
        ty: &Type,
        unpacked_wires: Vec<Wire>,
    ) -> Result<Wire, BuildError> {
        let elem_ty = self.type_analyzer.element_type();
        // If the type is a qubit, return the wire directly
        if ty == elem_ty {
            debug_assert!(unpacked_wires.len() == 1);
            return Ok(unpacked_wires[0]);
        }

        // Check for option of qubit
        if is_opt_of(ty, elem_ty) {
            debug_assert!(unpacked_wires.len() == 1);
            return self.repack_option(builder, unpacked_wires[0], elem_ty);
        }

        macro_rules! handle_array_type {
            ($array_kind:ty, $repack_op:expr) => {
                if let Some((n, elem_ty)) = ty.as_extension().and_then(array_args::<$array_kind>) {
                    return self.repack_array::<$array_kind>(
                        builder,
                        unpacked_wires,
                        n,
                        elem_ty,
                        &$repack_op,
                    );
                }
            };
        }

        handle_array_type!(Array, ARRAY_REPACK);
        handle_array_type!(BorrowArray, BARRAY_REPACK);

        if let Some(row) = ty.as_sum().and_then(SumType::as_tuple) {
            let row: hugr::types::TypeRow =
                row.clone().try_into().expect("unexpected row variable.");
            return self.repack_tuple(builder, unpacked_wires, &row);
        }

        debug_assert!(unpacked_wires.len() == 1);
        Ok(unpacked_wires[0])
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use hugr::{
        HugrView,
        builder::{DFGBuilder, DataflowHugr as _},
        extension::prelude::{bool_t, option_type, qb_t, usize_t},
        std_extensions::collections::array::array_type,
        types::Signature,
    };
    use rstest::rstest;

    #[test]
    fn test_container_factory_creation() {
        let analyzer = TypeUnpacker::for_qubits();
        let factory = UnpackContainerBuilder::new(analyzer);
        assert_eq!(factory.func_map.len(), 0);
    }

    #[test]
    fn test_option_unwrap_wrap() -> Result<(), BuildError> {
        let analyzer = TypeUnpacker::for_qubits();
        let factory = UnpackContainerBuilder::new(analyzer);
        let option_qb_type = Type::from(option_type([qb_t()]));
        let mut builder = DFGBuilder::new(Signature::new_endo(vec![option_qb_type]))?;

        let input = builder.input().out_wire(0);
        let unwrapped = factory.unpack_option(&mut builder, input, &qb_t())?;
        let wrapped = factory.repack_option(&mut builder, unwrapped, &qb_t())?;

        let hugr = builder.finish_hugr_with_outputs([wrapped])?;
        assert!(hugr.validate().is_ok());
        Ok(())
    }

    #[rstest]
    #[case::array(Array, ARRAY_UNPACK, ARRAY_REPACK)]
    #[case::borrow_array(BorrowArray, BARRAY_UNPACK, BARRAY_REPACK)]
    fn test_array_unpack_repack<AK: ArrayKind>(
        #[case] _kind: AK,
        #[case] unpack_op: OpName,
        #[case] repack_op: OpName,
    ) -> Result<(), BuildError> {
        let analyzer = TypeUnpacker::for_qubits();
        let factory = UnpackContainerBuilder::new(analyzer);
        let array_size = 2;

        // Create the specific array type
        let array_type = AK::ty(array_size, qb_t());

        // Build a dataflow graph that unpacks and repacks the array
        let mut builder = DFGBuilder::new(Signature::new_endo([array_type]))?;
        let input = builder.input().out_wire(0);

        // Unpack the array
        let unpacked =
            factory.unpack_array::<AK>(&mut builder, input, array_size, &qb_t(), &unpack_op)?;

        // Repack the array
        let repacked =
            factory.repack_array::<AK>(&mut builder, unpacked, array_size, &qb_t(), &repack_op)?;

        let hugr = builder.finish_hugr_with_outputs([repacked])?;
        assert!(hugr.validate().is_ok());

        Ok(())
    }

    #[test]
    fn test_tuple_unpack_repack() -> Result<(), BuildError> {
        let analyzer = TypeUnpacker::for_qubits();
        let factory = UnpackContainerBuilder::new(analyzer);
        let tuple_row = vec![qb_t(), bool_t()];
        let tuple_type = Type::new_tuple(tuple_row.clone());

        let mut builder = DFGBuilder::new(Signature::new_endo([tuple_type]))?;

        let input = builder.input().out_wire(0);
        let unpacked = factory.unpack_tuple(&mut builder, input, &tuple_row)?;
        assert_eq!(unpacked.len(), tuple_row.len());

        let repacked = factory.repack_tuple(&mut builder, unpacked, &tuple_row)?;
        let hugr = builder.finish_hugr_with_outputs([repacked])?;
        assert!(hugr.validate().is_ok());
        Ok(())
    }

    #[test]
    fn test_unpack_repack_row() -> Result<(), BuildError> {
        let analyzer = TypeUnpacker::for_qubits();
        let factory = UnpackContainerBuilder::new(analyzer);
        let types = vec![qb_t(), bool_t(), array_type(2, qb_t())];
        let mut builder = DFGBuilder::new(hugr::types::Signature::new_endo(types.clone()))?;

        let inputs = builder.input().outputs().collect::<Vec<_>>();
        let unpacked = factory.unpack_row(&mut builder, &types, inputs)?;
        let repacked = factory.repack_row(&mut builder, &types, unpacked)?;

        let hugr = builder.finish_hugr_with_outputs(repacked)?;
        assert!(hugr.validate().is_ok());
        Ok(())
    }

    #[test]
    fn test_unpack_repack_row_non_qubit() -> Result<(), BuildError> {
        // Use a TypeUnpacker that targets bools, not qubits
        let analyzer = TypeUnpacker::new(bool_t());
        let factory = UnpackContainerBuilder::new(analyzer);
        let types = vec![bool_t(), usize_t(), Array::ty(2, bool_t())];
        let mut builder = DFGBuilder::new(hugr::types::Signature::new_endo(types.clone()))?;

        let inputs = builder.input().outputs().collect::<Vec<_>>();
        let unpacked = factory.unpack_row(&mut builder, &types, inputs)?;
        // Should unpack all bools and array of bools (array size 2)
        assert_eq!(unpacked.len(), 4, "Bool row should be fully unpacked");

        let repacked = factory.repack_row(&mut builder, &types, unpacked)?;
        assert_eq!(
            repacked.len(),
            3,
            "Repacked row should match original length"
        );

        let hugr = builder.finish_hugr_with_outputs(repacked)?;
        assert!(hugr.validate().is_ok());
        Ok(())
    }
}