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
//! Methods to decode opaque subgraphs from a pytket barrier operation.

use std::sync::Arc;

use hugr::builder::Container;
use hugr::hugr::hugrmut::{HugrMut, InsertedForest};
use hugr::ops::{OpTag, OpTrait};
use hugr::types::Type;
use hugr::{Hugr, HugrView, Node, OutgoingPort, PortIndex, Wire};
use hugr_core::hugr::internal::HugrMutInternals;
use itertools::Itertools;

use crate::extension::rotation::rotation_type;
use crate::serialize::pytket::decoder::{
    DecodeStatus, FoundWire, LoadedParameter, PytketDecoderContext, TrackedBit, TrackedQubit,
};
use crate::serialize::pytket::error::BarrierPayloadError;
use crate::serialize::pytket::extension::RegisterCount;
use crate::serialize::pytket::opaque::{
    EncodedEdgeID, OpaqueSubgraph, OpaqueSubgraphPayload, SubgraphId,
};
use crate::serialize::pytket::{
    PARAMETER_TYPES, PytketDecodeError, PytketDecodeErrorInner, PytketDecoderConfig,
};

impl<'h> PytketDecoderContext<'h> {
    /// Insert a subgraph encoded in the payload of a pytket barrier operation into
    /// the Hugr being decoded.
    pub(in crate::serialize::pytket) fn insert_subgraph_from_payload(
        &mut self,
        qubits: &[TrackedQubit],
        bits: &[TrackedBit],
        payload: &OpaqueSubgraphPayload,
    ) -> Result<DecodeStatus, PytketDecodeError> {
        let mut load_params = |input_params: &[String]| {
            input_params
                .iter()
                .map(|p| self.load_half_turns(p))
                .collect_vec()
        };
        let status = match payload {
            OpaqueSubgraphPayload::External { id, input_params } => {
                let loaded_params = load_params(input_params);
                self.insert_external_subgraph(*id, qubits, bits, &loaded_params)
            }
            OpaqueSubgraphPayload::Inline {
                hugr_envelope,
                inputs,
                outputs,
                input_params,
            } => {
                let loaded_params = load_params(input_params);
                self.insert_inline_subgraph(
                    hugr_envelope,
                    inputs,
                    outputs,
                    qubits,
                    bits,
                    &loaded_params,
                )
            }
        }?;

        // Mark the used qubits and bits as outdated.
        qubits.iter().for_each(|q| {
            self.wire_tracker.mark_qubit_outdated(q.clone());
        });

        Ok(status)
    }

    /// Move the subgraph nodes referenced by an
    /// [`OpaqueSubgraphPayload::External`] into the region being decoded.
    pub(in crate::serialize::pytket) fn insert_external_subgraph(
        &mut self,
        id: SubgraphId,
        qubits: &[TrackedQubit],
        bits: &[TrackedBit],
        params: &[LoadedParameter],
    ) -> Result<DecodeStatus, PytketDecodeError> {
        // TODO: Remove the graphs as we extract them, and give a nice error if subgraph id are reused.
        let Some(subgraph) = self.opaque_subgraphs.and_then(|s| s.get(id)) else {
            return Err(PytketDecodeErrorInner::OpaqueSubgraphNotFound { id }.wrap());
        };

        let old_parent = subgraph.region();
        if !OpTag::DataflowParent.is_superset(self.builder.hugr().get_optype(old_parent).tag()) {
            return Err(PytketDecodeErrorInner::ExternalSubgraphWasModified { id }.wrap());
        }
        let new_parent = self.builder.container_node();

        // Re-parent the nodes in the subgraph.
        for &node in subgraph.nodes() {
            self.builder.hugr_mut().set_parent(node, new_parent);
        }

        self.rewire_external_subgraph_inputs(
            subgraph, qubits, bits, params, old_parent, new_parent,
        )?;

        self.rewire_external_subgraph_outputs(id, subgraph, qubits, bits, old_parent, new_parent)?;

        self.rewire_external_subgraph_io_order_edges(subgraph, old_parent, new_parent)?;

        Ok(DecodeStatus::Success)
    }

    /// Rewire the inputs of an external subgraph moved to the new region.
    ///
    /// Helper for [`Self::insert_external_subgraph`].
    fn rewire_external_subgraph_inputs(
        &mut self,
        subgraph: &OpaqueSubgraph<Node>,
        mut input_qubits: &[TrackedQubit],
        mut input_bits: &[TrackedBit],
        mut input_params: &[LoadedParameter],
        old_parent: Node,
        new_parent: Node,
    ) -> Result<(), PytketDecodeError> {
        let old_input = self.builder.hugr().get_io(old_parent).unwrap()[0];
        let new_input = self.builder.hugr().get_io(new_parent).unwrap()[0];

        // Reconnect input wires from parts of/nodes in the region that have been encoded into pytket.
        for (ty, (tgt_node, tgt_port)) in subgraph
            .signature()
            .input()
            .iter()
            .zip_eq(subgraph.incoming_ports())
        {
            let found_wire = self.wire_tracker.find_typed_wire(
                &self.config,
                &mut self.builder,
                ty,
                &mut input_qubits,
                &mut input_bits,
                &mut input_params,
                Some(EncodedEdgeID::default()),
            )?;

            let wire = match found_wire {
                FoundWire::Register(wire_data) => wire_data.wire(),
                FoundWire::Parameter(param) => param.wire(),
                FoundWire::Unsupported { .. } => {
                    // Input port with an unsupported type.
                    let Some((neigh, neigh_port)) = self
                        .builder
                        .hugr()
                        .single_linked_output(*tgt_node, *tgt_port)
                    else {
                        // The input was disconnected. We just skip it.
                        // (This is the case for unused other-ports)
                        continue;
                    };
                    if neigh != old_input {
                        // If it was linked to some node, that wasn't the old
                        // region's input, we can keep the link.
                        continue;
                    }
                    // If it was linked to the old circuit's input, we need to
                    // re-wire it to the new region's input.
                    Wire::new(new_input, neigh_port)
                }
            };

            self.builder
                .hugr_mut()
                .connect(wire.node(), wire.source(), *tgt_node, *tgt_port);
        }

        Ok(())
    }

    /// Rewire the outputs of an external subgraph moved to the new region.
    ///
    /// Registers the output wires that should be connected to nodes in the new region.
    /// Re-wires edges to old region's output node to the new one's.
    ///
    /// Helper for [`Self::insert_external_subgraph`].
    fn rewire_external_subgraph_outputs(
        &mut self,
        id: SubgraphId,
        subgraph: &OpaqueSubgraph<Node>,
        qubits: &[TrackedQubit],
        bits: &[TrackedBit],
        old_parent: Node,
        new_parent: Node,
    ) -> Result<(), PytketDecodeError> {
        let old_output = self.builder.hugr().get_io(old_parent).unwrap()[1];
        let new_output = self.builder.hugr().get_io(new_parent).unwrap()[1];

        let mut output_qubits = qubits;
        let mut output_bits = bits;

        for ((out_idx, ty), (src, src_port)) in subgraph
            .signature()
            .output()
            .iter()
            .enumerate()
            .zip_eq(subgraph.outgoing_ports())
        {
            // Output wire from the subgraph. Depending on the type, we may need
            // to track new qubits and bits, re-connect it to some output, or
            // leave it untouched.
            let wire = Wire::new(*src, *src_port);
            if let Some(counts) = self.config().type_to_pytket(ty).filter(|c| c.params == 0) {
                // This port declares new bit/qubit outputs to be tracked by the decoder.

                // Make sure to disconnect the old wire.
                self.builder.hugr_mut().disconnect(*src, *src_port);

                let wire_qubits = output_qubits.split_off(..counts.qubits);
                let wire_bits = output_bits.split_off(..counts.bits);
                if wire_qubits.is_none() || wire_bits.is_none() {
                    return Err(make_unexpected_node_out_error(
                        self.config(),
                        subgraph.signature().output().iter(),
                        qubits.len(),
                        bits.len(),
                    ));
                }
                self.wire_tracker.track_wire(
                    wire,
                    Arc::new(ty.clone()),
                    wire_qubits.unwrap().iter().cloned(),
                    wire_bits.unwrap().iter().cloned(),
                )?;
            } else if PARAMETER_TYPES.contains(ty) {
                let param_name = id.output_parameter(out_idx);
                let param = if ty == &rotation_type() {
                    LoadedParameter::rotation(wire)
                } else {
                    LoadedParameter::float_half_turns(wire)
                };
                self.wire_tracker
                    .register_input_parameter(param, param_name)?;
            } else {
                // This is an unsupported wire. If it was connected to the old
                // region's output, rewire it to the new region's output.
                // Otherwise leave it connected.
                for (tgt, tgt_port) in self
                    .builder
                    .hugr()
                    .linked_inputs(*src, *src_port)
                    .collect_vec()
                {
                    if tgt == old_output {
                        // We only need to disconnect the specific edge here,
                        // but there should only be one incoming value edge.
                        self.builder.hugr_mut().disconnect(old_output, tgt_port);
                        self.builder
                            .hugr_mut()
                            .connect(*src, *src_port, new_output, tgt_port);
                    }
                }
            }
        }

        Ok(())
    }

    /// Rewire any order edges betwen the input/output nodes of the old region and the external subgraph,
    /// connecting them to the new input/output nodes instead.
    fn rewire_external_subgraph_io_order_edges(
        &mut self,
        subgraph: &OpaqueSubgraph<Node>,
        old_parent: Node,
        new_parent: Node,
    ) -> Result<(), PytketDecodeError> {
        let [old_input, old_output] = self.builder.hugr().get_io(old_parent).expect("IO nodes");
        let [new_input, new_output] = self.builder.hugr().get_io(new_parent).expect("IO nodes");

        // Find the order edge ports.
        // Since we are in a dataflow region, nodes are guaranteed to have an order-edge `other_port`s.
        let order_out = |node: Node| {
            self.builder
                .hugr()
                .get_optype(node)
                .other_output_port()
                .expect("Order edge")
        };
        let order_in = |node: Node| {
            self.builder
                .hugr()
                .get_optype(node)
                .other_input_port()
                .expect("Order edge")
        };
        let old_input_order = order_out(old_input);
        let new_input_order = order_out(new_input);
        let old_output_order = order_in(old_output);
        let new_output_order = order_in(new_output);

        for (tgt_node, tgt_port) in self
            .builder
            .hugr()
            .linked_inputs(old_input, old_input_order)
            .filter(|(node, _)| subgraph.nodes().contains(node))
            .collect_vec()
        {
            self.builder
                .hugr_mut()
                .connect(new_input, new_input_order, tgt_node, tgt_port);
            self.builder
                .hugr_mut()
                .disconnect_edge(old_input, old_input_order, tgt_node, tgt_port);
        }

        for (src_node, src_port) in self
            .builder
            .hugr()
            .linked_outputs(old_output, old_output_order)
            .filter(|(node, _)| subgraph.nodes().contains(node))
            .collect_vec()
        {
            self.builder
                .hugr_mut()
                .connect(src_node, src_port, new_output, new_output_order);
            self.builder.hugr_mut().disconnect_edge(
                src_node,
                src_port,
                old_output,
                old_output_order,
            );
        }

        Ok(())
    }

    /// Insert an [`OpaqueSubgraphPayload::Inline`] into the Hugr being decoded.
    fn insert_inline_subgraph(
        &mut self,
        hugr_envelope: &str,
        payload_inputs: &[(Type, EncodedEdgeID)],
        payload_outputs: &[(Type, EncodedEdgeID)],
        qubits: &[TrackedQubit],
        bits: &[TrackedBit],
        params: &[LoadedParameter],
    ) -> Result<DecodeStatus, PytketDecodeError> {
        let to_insert_hugr = Hugr::load_str(hugr_envelope, Some(self.extension_registry()))
            .map_err(|e| BarrierPayloadError::HugrRead(e).wrap())?;
        let to_insert_signature = to_insert_hugr.inner_function_type().unwrap();

        let module = self.builder.hugr().module_root();
        let region = self.builder.container_node();

        // Collect the non-IO nodes in the hugr we plan to insert.
        let Some([to_insert_input, to_insert_output]) =
            to_insert_hugr.get_io(to_insert_hugr.entrypoint())
        else {
            return Err(PytketDecodeError::custom(
                "Opaque subgraph payload has a non-dataflow parent as entrypoint",
            ));
        };
        let entrypoint_children = to_insert_hugr
            .children(to_insert_hugr.entrypoint())
            // Ignore the input and output nodes.
            .skip(2)
            .map(|c| (c, region));

        // Compute the inputs and output ports of the subgraph.
        // Since we insert the nodes inside the region directly (without the I/O nodes),
        // we need to do some bookkeeping to match the ports.
        //
        // `to_insert_inputs` is a vector of vectors, where each first-level entry corresponds to
        // an input to the subgraph / element in `payload.inputs`, and the second-level
        // list is all the target ports that connect to that input.
        //
        // `to_insert_outputs` is just a vector of node+outgoing ports.
        let to_insert_inputs: hugr::hugr::views::sibling_subgraph::IncomingPorts<Node> =
            to_insert_signature
                .input_ports()
                .map(|p| {
                    to_insert_hugr
                        .linked_inputs(to_insert_input, p.index())
                        .collect_vec()
                })
                .collect_vec();
        let to_insert_outputs: Vec<(Node, OutgoingPort)> = to_insert_signature
            .output_ports()
            .map(|p| {
                to_insert_hugr
                    .single_linked_output(to_insert_output, p.index())
                    .unwrap()
            })
            .collect_vec();

        // Collect any module child that does not contain the entrypoint function.
        //
        // These are global functions or constant definitions.
        let entrypoint_function =
            std::iter::successors(Some(to_insert_hugr.entrypoint()), |&node| {
                let parent = to_insert_hugr.get_parent(node)?;
                if parent == module { None } else { Some(parent) }
            })
            .last()
            .unwrap();
        let module_children = to_insert_hugr
            .children(to_insert_hugr.module_root())
            .filter(|c| *c != entrypoint_function)
            .map(|c| (c, module));

        // Insert the hugr's entrypoint region directly into the region being built,
        // and any other function in the HUGR module into the module being built.
        let insertion_roots = entrypoint_children.chain(module_children).collect_vec();
        let insertion_result = self
            .builder
            .hugr_mut()
            .insert_forest(to_insert_hugr, insertion_roots)
            .unwrap_or_else(|e| panic!("Invalid `insertion_roots`. {e}"));

        self.wire_inline_subgraph_inputs(
            qubits,
            bits,
            params,
            payload_inputs,
            to_insert_inputs,
            &insertion_result,
        )?;

        self.wire_inline_subgraph_outputs(
            qubits,
            bits,
            payload_outputs,
            to_insert_outputs,
            &insertion_result,
        )?;

        Ok(DecodeStatus::Success)
    }

    /// Wire the inputs of a newly inserted inline subgraph.
    ///
    /// Helper for [`Self::insert_inline_subgraph`].
    fn wire_inline_subgraph_inputs(
        &mut self,
        mut input_qubits: &[TrackedQubit],
        mut input_bits: &[TrackedBit],
        mut input_params: &[LoadedParameter],
        payload_inputs: &[(Type, EncodedEdgeID)],
        to_insert_inputs: hugr::hugr::views::sibling_subgraph::IncomingPorts<Node>,
        insertion_result: &InsertedForest,
    ) -> Result<(), PytketDecodeError> {
        // A list of incoming ports corresponding to [`EncodedEdgeID`]s that must be
        // connected once the outgoing port is created.
        //
        // This handles the case where unsupported subgraphs in opaque barriers on
        // the pytket circuit get reordered and input ports are seen before their
        // outputs.
        for ((ty, edge_id), targets) in payload_inputs.iter().zip_eq(to_insert_inputs) {
            let found_wire = self.wire_tracker.find_typed_wire(
                &self.config,
                &mut self.builder,
                ty,
                &mut input_qubits,
                &mut input_bits,
                &mut input_params,
                Some(*edge_id),
            )?;

            let wire = match found_wire {
                FoundWire::Register(wire_data) => wire_data.wire(),
                FoundWire::Parameter(param) => param.wire(),
                FoundWire::Unsupported { id } => {
                    self.wire_tracker.connect_unsupported_wire_targets(
                        id,
                        targets,
                        self.builder.hugr_mut(),
                    );
                    continue;
                }
            };

            for (to_insert_node, port) in targets {
                let node = *insertion_result.node_map.get(&to_insert_node).unwrap();
                self.builder
                    .hugr_mut()
                    .connect(wire.node(), wire.source(), node, port);
            }
        }

        Ok(())
    }

    /// Wire the outputs of a newly inserted inline subgraph.
    ///
    /// Helper for [`Self::insert_inline_subgraph`].
    fn wire_inline_subgraph_outputs(
        &mut self,
        qubits: &[TrackedQubit],
        bits: &[TrackedBit],
        payload_outputs: &[(Type, EncodedEdgeID)],
        to_insert_outputs: Vec<(Node, OutgoingPort)>,
        insertion_result: &InsertedForest,
    ) -> Result<(), PytketDecodeError> {
        // Register the subgraph outputs in the wire tracker.
        let mut output_qubits = qubits;
        let mut output_bits = bits;
        for ((ty, edge_id), (to_insert_node, port)) in
            payload_outputs.iter().zip_eq(to_insert_outputs)
        {
            let node = *insertion_result.node_map.get(&to_insert_node).unwrap();
            let wire = Wire::new(node, port);
            match self.config().type_to_pytket(ty).filter(|c| c.params == 0) {
                Some(counts) => {
                    // Track the registers in the subgraph output wires.
                    // Output parameters from a subgraph are always marked as not supported (they don't map to any pytket argument variable).
                    // We only track qubit/bit wires here.
                    let wire_qubits = output_qubits.split_off(..counts.qubits);
                    let wire_bits = output_bits.split_off(..counts.bits);
                    if wire_qubits.is_none() || wire_bits.is_none() {
                        return Err(make_unexpected_node_out_error(
                            self.config(),
                            payload_outputs.iter().map(|(ty, _)| ty),
                            qubits.len(),
                            bits.len(),
                        ));
                    }
                    self.wire_tracker.track_wire(
                        wire,
                        Arc::new(ty.clone()),
                        wire_qubits.unwrap().iter().cloned(),
                        wire_bits.unwrap().iter().cloned(),
                    )?;
                }
                None => {
                    // This is an unsupported wire, so we let the wire tracker manage the connections.
                    self.wire_tracker.connect_unsupported_wire_source(
                        *edge_id,
                        wire,
                        self.builder.hugr_mut(),
                    );
                }
            }
        }
        Ok(())
    }
}

/// Helper to compute the expected register counts before generating a
/// [`PytketDecodeErrorInner::UnexpectedNodeOutput`] error when registering the
/// outputs of an unsupported subgraph.
///
/// Processes all the output types to compute the number of qubits and bits we
/// required to have available.
fn make_unexpected_node_out_error<'ty>(
    config: &PytketDecoderConfig,
    output_types: impl IntoIterator<Item = &'ty Type>,
    available_qubits: usize,
    available_bits: usize,
) -> PytketDecodeError {
    let mut expected_count = RegisterCount::default();
    for ty in output_types {
        expected_count += config.type_to_pytket(ty).unwrap_or_default();
    }
    PytketDecodeErrorInner::UnexpectedNodeOutput {
        expected_qubits: expected_count.qubits,
        expected_bits: expected_count.bits,
        circ_qubits: available_qubits,
        circ_bits: available_bits,
    }
    .wrap()
}