tket2 0.13.0

Quantinuum's TKET2 Quantum Compiler
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
//! This module provides a utility to split a circuit into chunks, and reassemble them afterwards.
//!
//! See [`CircuitChunks`] for more information.

use std::collections::HashMap;
use std::mem;
use std::ops::{Index, IndexMut};

use derive_more::From;
use hugr::builder::{Container, FunctionBuilder};
use hugr::hugr::hugrmut::HugrMut;
use hugr::hugr::views::sibling_subgraph::TopoConvexChecker;
use hugr::hugr::views::SiblingSubgraph;
use hugr::hugr::{HugrError, NodeMetadataMap};
use hugr::ops::handle::DataflowParentID;
use hugr::ops::OpType;
use hugr::types::Signature;
use hugr::{Hugr, HugrView, IncomingPort, Node, OutgoingPort, PortIndex, Wire};
use hugr_core::hugr::internal::{HugrInternals, HugrMutInternals as _};
use itertools::Itertools;
use rayon::iter::{IntoParallelIterator, IntoParallelRefMutIterator, ParallelIterator};
use rayon::slice::ParallelSliceMut;

use crate::Circuit;

use crate::circuit::cost::{CircuitCost, CostDelta};

/// An identifier for the connection between chunks.
///
/// This is based on the wires of the original circuit.
///
/// When reassembling the circuit, the input/output wires of each chunk are
/// re-linked by matching these identifiers.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, From)]
pub struct ChunkConnection(Wire);

/// A chunk of a circuit.
#[derive(Debug, Clone)]
pub struct Chunk {
    /// The extracted circuit.
    pub circ: Circuit,
    /// The original wires connected to the input.
    inputs: Vec<ChunkConnection>,
    /// The original wires connected to the output.
    outputs: Vec<ChunkConnection>,
}

impl Chunk {
    /// Extract a chunk from a circuit.
    ///
    /// The chunk is extracted from the input wires to the output wires.
    pub(self) fn extract(
        circ: &Circuit,
        nodes: impl IntoIterator<Item = Node>,
        checker: &TopoConvexChecker<'_, Hugr>,
    ) -> Self {
        let subgraph = SiblingSubgraph::try_from_nodes_with_checker(
            nodes.into_iter().collect_vec(),
            circ.hugr(),
            checker,
        )
        .expect("Failed to define the chunk subgraph");
        let extracted = subgraph.extract_subgraph(circ.hugr(), "Chunk").into();
        // Transform the subgraph's input/output sets into wires that can be
        // matched between different chunks.
        //
        // This requires finding the `Outgoing` port corresponding to each
        // subgraph input.
        let inputs = subgraph
            .incoming_ports()
            .iter()
            .map(|wires| {
                let (inp_node, inp_port) = wires[0];
                let (out_node, out_port) = circ
                    .hugr()
                    .linked_outputs(inp_node, inp_port)
                    .exactly_one()
                    .ok()
                    .unwrap();
                Wire::new(out_node, out_port).into()
            })
            .collect();
        let outputs = subgraph
            .outgoing_ports()
            .iter()
            .map(|&(node, port)| Wire::new(node, port).into())
            .collect();
        Self {
            circ: extracted,
            inputs,
            outputs,
        }
    }

    /// Insert the chunk back into a circuit.
    pub(self) fn insert(
        &self,
        circ: &mut impl HugrMut<Node = Node>,
        root: Node,
    ) -> ChunkInsertResult {
        let chunk = self.circ.hugr();
        let chunk_root = chunk.entrypoint();
        if chunk.children(self.circ.parent()).nth(2).is_none() {
            // The chunk is empty. We don't need to insert anything.
            return self.empty_chunk_insert_result();
        }

        let [chunk_inp, chunk_out] = chunk.get_io(chunk_root).unwrap();
        // Insert the chunk circuit into the original circuit.
        let subgraph =
            SiblingSubgraph::<Node>::try_new_dataflow_subgraph::<_, DataflowParentID>(&chunk)
                .unwrap_or_else(|e| panic!("The chunk circuit is no longer a dataflow graph: {e}"));
        let node_map = circ.insert_subgraph(root, &chunk, &subgraph);

        let mut input_map = HashMap::with_capacity(self.inputs.len());
        let mut output_map = HashMap::with_capacity(self.outputs.len());

        // Translate each connection from the chunk input into a [`ConnectionTarget`].
        //
        // Connections to an inserted node are translated into a [`ConnectionTarget::InsertedNode`].
        // Connections from the input directly into the output become a [`ConnectionTarget::TransitiveConnection`].
        for (&connection, chunk_inp_port) in self.inputs.iter().zip(chunk.node_outputs(chunk_inp)) {
            let connection_targets: Vec<ConnectionTarget> = chunk
                .linked_inputs(chunk_inp, chunk_inp_port)
                .map(|(node, port)| {
                    if node == chunk_out {
                        // This was a direct wire from the chunk input to the output. Use the output's [`ChunkConnection`].
                        let output_connection = self.outputs[port.index()];
                        ConnectionTarget::TransitiveConnection(output_connection)
                    } else {
                        // Translate the original chunk node into the inserted node.
                        ConnectionTarget::InsertedInput(*node_map.get(&node).unwrap(), port)
                    }
                })
                .collect();
            input_map.insert(connection, connection_targets);
        }

        for (&wire, chunk_out_port) in self.outputs.iter().zip(chunk.node_inputs(chunk_out)) {
            let (node, port) = chunk
                .linked_outputs(chunk_out, chunk_out_port)
                .exactly_one()
                .ok()
                .unwrap();
            let target = if node == chunk_inp {
                // This was a direct wire from the chunk output to the input. Use the input's [`ChunkConnection`].
                let input_connection = self.inputs[port.index()];
                ConnectionTarget::TransitiveConnection(input_connection)
            } else {
                // Translate the original chunk node into the inserted node.
                ConnectionTarget::InsertedOutput(*node_map.get(&node).unwrap(), port)
            };
            output_map.insert(wire, target);
        }

        ChunkInsertResult {
            incoming_connections: input_map,
            outgoing_connections: output_map,
        }
    }

    /// Compute the return value for `insert` when the chunk is empty (Subgraph would throw an error in this case).
    ///
    /// TODO: Support empty Subgraphs in Hugr?
    fn empty_chunk_insert_result(&self) -> ChunkInsertResult {
        let hugr = self.circ.hugr();
        let [chunk_inp, chunk_out] = self.circ.io_nodes();
        let mut input_map = HashMap::with_capacity(self.inputs.len());
        let mut output_map = HashMap::with_capacity(self.outputs.len());

        for (&connection, chunk_inp_port) in self.inputs.iter().zip(hugr.node_outputs(chunk_inp)) {
            let connection_targets: Vec<ConnectionTarget> = hugr
                .linked_ports(chunk_inp, chunk_inp_port)
                .map(|(node, port)| {
                    assert_eq!(node, chunk_out);
                    let output_connection = self.outputs[port.index()];
                    ConnectionTarget::TransitiveConnection(output_connection)
                })
                .collect();
            input_map.insert(connection, connection_targets);
        }

        for (&wire, chunk_out_port) in self.outputs.iter().zip(hugr.node_inputs(chunk_out)) {
            let (node, port) = hugr
                .linked_ports(chunk_out, chunk_out_port)
                .exactly_one()
                .ok()
                .unwrap();
            assert_eq!(node, chunk_inp);
            let input_connection = self.inputs[port.index()];
            output_map.insert(
                wire,
                ConnectionTarget::TransitiveConnection(input_connection),
            );
        }

        ChunkInsertResult {
            incoming_connections: input_map,
            outgoing_connections: output_map,
        }
    }
}

/// A map from the original input/output [`ChunkConnection`]s to an inserted chunk's inputs and outputs.
#[derive(Debug, Clone)]
struct ChunkInsertResult {
    /// A map from incoming connections to a chunk, to the new node and incoming port targets.
    ///
    /// A chunk may specify multiple targets to be connected to a single incoming `ChunkConnection`.
    pub incoming_connections: HashMap<ChunkConnection, Vec<ConnectionTarget>>,
    /// A map from outgoing connections from a chunk, to the new node and outgoing port target.
    pub outgoing_connections: HashMap<ChunkConnection, ConnectionTarget>,
}

/// The target of a chunk connection in a reassembled circuit.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
enum ConnectionTarget {
    /// The target is a chunk node's input.
    InsertedInput(Node, IncomingPort),
    /// The target is a chunk node's output.
    InsertedOutput(Node, OutgoingPort),
    /// The link goes directly to the opposite boundary, without an intermediary
    /// node.
    TransitiveConnection(ChunkConnection),
}

/// An utility for splitting a circuit into chunks, and reassembling them
/// afterwards.
///
/// Circuits can be split into [`CircuitChunks`] with [`CircuitChunks::split`]
/// or [`CircuitChunks::split_with_cost`], and reassembled with
/// [`CircuitChunks::reassemble`].
#[derive(Debug, Clone)]
pub struct CircuitChunks {
    /// The original circuit's signature.
    signature: Signature,

    /// The original circuit's root metadata.
    root_meta: Option<NodeMetadataMap>,

    /// The original circuit's inputs.
    input_connections: Vec<ChunkConnection>,

    /// The original circuit's outputs.
    output_connections: Vec<ChunkConnection>,

    /// The split circuits.
    chunks: Vec<Chunk>,
}

impl CircuitChunks {
    /// Split a circuit into chunks.
    ///
    /// The circuit is split into chunks of at most `max_size` gates.
    pub fn split(circ: &Circuit, max_size: usize) -> Self {
        Self::split_with_cost(circ, max_size.saturating_sub(1), |_| 1)
    }

    /// Split a circuit into chunks.
    ///
    /// The circuit is split into chunks of at most `max_cost`, using the provided cost function.
    pub fn split_with_cost<C: CircuitCost>(
        circ: &Circuit,
        max_cost: C,
        op_cost: impl Fn(&OpType) -> C,
    ) -> Self {
        let hugr = circ.hugr();
        let root_meta = hugr.node_metadata_map(circ.parent()).clone();
        let signature = circ.circuit_signature().clone();

        let [circ_input, circ_output] = circ.io_nodes();
        let input_connections = hugr
            .node_outputs(circ_input)
            .map(|port| Wire::new(circ_input, port).into())
            .collect();
        let output_connections = hugr
            .node_inputs(circ_output)
            .flat_map(|p| hugr.linked_outputs(circ_output, p))
            .map(|(n, p)| Wire::new(n, p).into())
            .collect();

        let mut chunks = Vec::new();
        let convex_checker = TopoConvexChecker::new(circ.hugr(), circ.parent());
        let mut running_cost = C::default();
        let mut current_group = 0;
        for (_, commands) in &circ.commands().map(|cmd| cmd.node()).chunk_by(|&node| {
            let new_cost = running_cost.clone() + op_cost(hugr.get_optype(node));
            if new_cost.sub_cost(&max_cost).as_isize() > 0 {
                running_cost = C::default();
                current_group += 1;
            } else {
                running_cost = new_cost;
            }
            current_group
        }) {
            chunks.push(Chunk::extract(circ, commands, &convex_checker));
        }
        Self {
            signature: signature.into_owned(),
            root_meta: Some(root_meta),
            input_connections,
            output_connections,
            chunks,
        }
    }

    /// Reassemble the chunks into a circuit.
    pub fn reassemble(self) -> Result<Circuit, HugrError> {
        let name = self
            .root_meta
            .as_ref()
            .and_then(|map| map.get("name"))
            .and_then(|s| s.as_str())
            .unwrap_or("");

        let mut builder = FunctionBuilder::new(name, self.signature).unwrap();
        // Take the unfinished Hugr from the builder, to avoid unnecessary
        // validation checks that require connecting the inputs an outputs.
        let mut reassembled = mem::take(builder.hugr_mut());
        let root = reassembled.entrypoint();
        let [reassembled_input, reassembled_output] = reassembled.get_io(root).unwrap();

        // The chunks input and outputs are each identified with a
        // [`ChunkConnection`]. We collect both sides first, and rewire them
        // after the chunks have been inserted.
        let mut sources: HashMap<ChunkConnection, (Node, OutgoingPort)> = HashMap::new();
        let mut targets: HashMap<ChunkConnection, Vec<(Node, IncomingPort)>> = HashMap::new();

        // A map for `ChunkConnection`s that have been merged into another (due
        // to identity wires in the updated chunks).
        //
        // Maps each `ChunkConnection` to the `ChunkConnection` it has been
        // merged into.
        //
        // This is a poor man's Union Find. Since we traverse the chunks in
        // order, we can assume that already seen connections will not be merged
        // again.
        let mut transitive_connections: HashMap<ChunkConnection, ChunkConnection> = HashMap::new();
        let get_merged_connection = |transitive_connections: &HashMap<_, _>, connection| {
            transitive_connections
                .get(&connection)
                .copied()
                .unwrap_or(connection)
        };

        // Register the source ports for the `ChunkConnections` in the circuit input.
        for (&connection, port) in self
            .input_connections
            .iter()
            .zip(reassembled.node_outputs(reassembled_input))
        {
            sources.insert(connection, (reassembled_input, port));
        }

        for chunk in self.chunks {
            // Insert the chunk circuit without its input/output nodes.
            let ChunkInsertResult {
                incoming_connections,
                outgoing_connections,
            } = chunk.insert(&mut reassembled, root);
            // Associate the chunk's inserted inputs and outputs to the
            // `ChunkConnection` identifiers, so we can re-connect everything
            // afterwards.
            //
            // The chunk may return `ConnectionTarget::TransitiveConnection`s to
            // indicate that a `ChunkConnection` has been merged into another
            // (due to an identity wire).
            for (connection, conn_target) in outgoing_connections {
                match conn_target {
                    ConnectionTarget::InsertedOutput(node, port) => {
                        // The output of a chunk always has fresh `ChunkConnection`s.
                        sources.insert(connection, (node, port));
                    }
                    ConnectionTarget::TransitiveConnection(merged_connection) => {
                        // The output's `ChunkConnection` has been merged into one of the input's.
                        let merged_connection =
                            get_merged_connection(&transitive_connections, merged_connection);
                        transitive_connections.insert(connection, merged_connection);
                    }
                    _ => panic!("Unexpected connection target"),
                }
            }
            for (connection, conn_targets) in incoming_connections {
                // The connection in the chunk's input may have been merged into a earlier one.
                let connection = get_merged_connection(&transitive_connections, connection);
                for tgt in conn_targets {
                    match tgt {
                        ConnectionTarget::InsertedInput(node, port) => {
                            targets.entry(connection).or_default().push((node, port));
                        }
                        ConnectionTarget::TransitiveConnection(_merged_connection) => {
                            // The merge has been registered when scanning the
                            // outgoing_connections, so we don't need to do
                            // anything here.
                        }
                        _ => panic!("Unexpected connection target"),
                    }
                }
            }
        }

        // Register the target ports for the `ChunkConnections` into the circuit output.
        for (&connection, port) in self
            .output_connections
            .iter()
            .zip(reassembled.node_inputs(reassembled_output))
        {
            // The connection in the chunk's input may have been merged into a earlier one.
            let connection = get_merged_connection(&transitive_connections, connection);
            targets
                .entry(connection)
                .or_default()
                .push((reassembled_output, port));
        }

        // Reconnect the different chunks.
        for (connection, (source, source_port)) in sources {
            let Some(tgts) = targets.remove(&connection) else {
                continue;
            };
            for (target, target_port) in tgts {
                reassembled.connect(source, source_port, target, target_port);
            }
        }

        *reassembled.node_metadata_map_mut(root) = self.root_meta.unwrap_or_default();
        Ok(reassembled.into())
    }

    /// Returns a list of references to the split circuits.
    pub fn iter(&self) -> impl Iterator<Item = &Circuit> {
        self.chunks.iter().map(|chunk| &chunk.circ)
    }

    /// Returns a list of references to the split circuits.
    pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut Circuit> {
        self.chunks.iter_mut().map(|chunk| &mut chunk.circ)
    }

    /// Returns the number of chunks.
    pub fn len(&self) -> usize {
        self.chunks.len()
    }

    /// Returns `true` if there are no chunks.
    pub fn is_empty(&self) -> bool {
        self.chunks.is_empty()
    }

    /// Supports implementation of rayon::iter::IntoParallelRefMutIterator
    fn par_iter_mut(
        &mut self,
    ) -> rayon::iter::Map<
        rayon::slice::IterMut<'_, Chunk>,
        for<'a> fn(&'a mut Chunk) -> &'a mut Circuit,
    > {
        self.chunks
            .as_parallel_slice_mut()
            .into_par_iter()
            .map(|chunk| &mut chunk.circ)
    }
}

impl Index<usize> for CircuitChunks {
    type Output = Circuit;

    fn index(&self, index: usize) -> &Self::Output {
        &self.chunks[index].circ
    }
}

impl IndexMut<usize> for CircuitChunks {
    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
        &mut self.chunks[index].circ
    }
}

impl<'data> IntoParallelRefMutIterator<'data> for CircuitChunks {
    type Item = &'data mut Circuit;
    type Iter = rayon::iter::Map<
        rayon::slice::IterMut<'data, Chunk>,
        for<'a> fn(&'a mut Chunk) -> &'a mut Circuit,
    >;

    fn par_iter_mut(&'data mut self) -> Self::Iter {
        self.par_iter_mut()
    }
}

#[cfg(test)]
mod test {
    use crate::circuit::CircuitHash;

    use crate::utils::build_simple_circuit;
    use crate::Tk2Op;

    use super::*;

    #[test]
    fn split_reassemble() {
        let circ = build_simple_circuit(2, |circ| {
            circ.append(Tk2Op::H, [0])?;
            circ.append(Tk2Op::CX, [0, 1])?;
            circ.append(Tk2Op::T, [1])?;
            circ.append(Tk2Op::H, [0])?;
            circ.append(Tk2Op::CX, [0, 1])?;
            circ.append(Tk2Op::H, [0])?;
            circ.append(Tk2Op::CX, [0, 1])?;
            Ok(())
        })
        .unwrap();

        let chunks = CircuitChunks::split(&circ, 3);

        assert_eq!(chunks.len(), 3);

        let mut reassembled = chunks.reassemble().unwrap();

        reassembled.hugr_mut().validate().unwrap();
        assert_eq!(
            circ.circuit_hash(circ.parent()),
            reassembled.circuit_hash(reassembled.parent())
        );
    }

    #[test]
    fn reassemble_empty() {
        let circ = build_simple_circuit(3, |circ| {
            circ.append(Tk2Op::CX, [0, 1])?;
            circ.append(Tk2Op::H, [0])?;
            circ.append(Tk2Op::H, [1])?;
            Ok(())
        })
        .unwrap();

        let circ_1q_id = build_simple_circuit(1, |_| Ok(())).unwrap();
        let circ_2q_id_h = build_simple_circuit(2, |circ| {
            circ.append(Tk2Op::H, [0])?;
            Ok(())
        })
        .unwrap();

        let mut chunks = CircuitChunks::split(&circ, 1);

        // Replace the Hs with identities, and the CX with an identity and an H gate.
        chunks[0] = circ_2q_id_h.clone();
        chunks[1] = circ_1q_id.clone();
        chunks[2] = circ_1q_id.clone();

        let mut reassembled = chunks.reassemble().unwrap();

        reassembled.hugr_mut().validate().unwrap();

        assert_eq!(reassembled.commands().count(), 1);
        let h = reassembled.commands().next().unwrap().node();

        let [inp, out] = reassembled.io_nodes();
        assert_eq!(
            &reassembled.hugr().output_neighbours(inp).collect_vec(),
            &[h, out, out]
        );
    }
}