@@ -34,14 +34,22 @@ use crate::extension::rotation::rotation_type;
3434use crate :: serialize:: pytket:: config:: PytketDecoderConfig ;
3535use crate :: serialize:: pytket:: decoder:: wires:: WireTracker ;
3636use crate :: serialize:: pytket:: extension:: { build_opaque_tket_op, RegisterCount } ;
37+ use crate :: serialize:: pytket:: opaque:: OpaqueSubgraphs ;
3738use crate :: serialize:: pytket:: { DecodeInsertionTarget , PytketDecodeErrorInner } ;
3839use crate :: TketOp ;
3940
4041/// State of the tket circuit being decoded.
4142///
42- /// The state of an in-progress [`FunctionBuilder`] being built from a [`SerialCircuit`].
43+ /// The state of an in-progress [`FunctionBuilder`] being built from a
44+ /// [`SerialCircuit`].
45+ ///
46+ /// The generic parameter `H` is the HugrView type of the Hugr that was encoded
47+ /// into the circuit, if any. This is required when the encoded pytket circuit
48+ /// contains opaque barriers that reference subgraphs in the original HUGR. See
49+ /// [`UnsupportedSubgraphPayload`][super::unsupported::UnsupportedSubgraphPayload]
50+ /// for more details.
4351#[ derive( Debug ) ]
44- pub struct PytketDecoderContext < ' h > {
52+ pub struct PytketDecoderContext < ' h , H : HugrView > {
4553 /// The Hugr being built.
4654 pub builder : DFGBuilder < & ' h mut Hugr > ,
4755 /// A tracker keeping track of the generated wires and their corresponding types.
@@ -50,10 +58,15 @@ pub struct PytketDecoderContext<'h> {
5058 ///
5159 /// Contains custom operation decoders, that define translation of legacy tket
5260 /// commands into HUGR operations.
53- config : Arc < PytketDecoderConfig > ,
61+ config : Arc < PytketDecoderConfig < H > > ,
62+ /// The HugrView type of the Hugr that originated the circuit, if any.
63+ original_hugr : Option < & ' h H > ,
64+ /// A registry of unsupported subgraphs from `original_hugr`, that are referenced by opaque barriers in the pytket circuit
65+ /// via their [`SubgraphId`].
66+ opaque_subgraphs : Option < & ' h OpaqueSubgraphs < H :: Node > > ,
5467}
5568
56- impl < ' h > PytketDecoderContext < ' h > {
69+ impl < ' h , H : HugrView > PytketDecoderContext < ' h , H > {
5770 /// Initialize a new [`PytketDecoderContext`], using the metadata from a
5871 /// [`SerialCircuit`].
5972 ///
@@ -94,12 +107,11 @@ impl<'h> PytketDecoderContext<'h> {
94107 serialcirc : & SerialCircuit ,
95108 hugr : & ' h mut Hugr ,
96109 target : DecodeInsertionTarget ,
97- fn_name : Option < String > ,
98110 signature : Option < Signature > ,
99111 input_params : impl IntoIterator < Item = String > ,
100- config : impl Into < Arc < PytketDecoderConfig > > ,
112+ config : impl Into < Arc < PytketDecoderConfig < H > > > ,
101113 ) -> Result < Self , PytketDecodeError > {
102- let config: Arc < PytketDecoderConfig > = config. into ( ) ;
114+ let config: Arc < PytketDecoderConfig < H > > = config. into ( ) ;
103115 let signature = signature. unwrap_or_else ( || {
104116 let num_qubits = serialcirc. qubits . len ( ) ;
105117 let num_bits = serialcirc. bits . len ( ) ;
@@ -108,11 +120,11 @@ impl<'h> PytketDecoderContext<'h> {
108120 . into ( ) ;
109121 Signature :: new ( types. clone ( ) , types)
110122 } ) ;
111- let name = fn_name
112- . or_else ( || serialcirc. name . clone ( ) )
113- . unwrap_or_default ( ) ;
114123 let mut dfg: DFGBuilder < & mut Hugr > = match target {
115- DecodeInsertionTarget :: Function => {
124+ DecodeInsertionTarget :: Function { fn_name } => {
125+ let name = fn_name
126+ . or_else ( || serialcirc. name . clone ( ) )
127+ . unwrap_or_default ( ) ;
116128 FunctionBuilder :: with_hugr ( hugr, name, signature. clone ( ) )
117129 . unwrap ( )
118130 . into_dfg_builder ( )
@@ -146,6 +158,8 @@ impl<'h> PytketDecoderContext<'h> {
146158 builder : dfg,
147159 wire_tracker,
148160 config,
161+ original_hugr : None ,
162+ opaque_subgraphs : None ,
149163 } )
150164 }
151165
@@ -177,7 +191,7 @@ impl<'h> PytketDecoderContext<'h> {
177191 dfg : & mut DFGBuilder < & mut Hugr > ,
178192 input_types : & TypeRow ,
179193 input_params : impl IntoIterator < Item = String > ,
180- config : & PytketDecoderConfig ,
194+ config : & PytketDecoderConfig < H > ,
181195 ) -> Result < WireTracker , PytketDecodeError > {
182196 let num_qubits = serialcirc. qubits . len ( ) ;
183197 let num_bits = serialcirc. bits . len ( ) ;
@@ -332,6 +346,24 @@ impl<'h> PytketDecoderContext<'h> {
332346 . node ( ) )
333347 }
334348
349+ /// Register the set of unsupported subgraphs that were present in the original HUGR,
350+ /// along with a reference to the original HugrView.
351+ ///
352+ /// # Arguments
353+ /// - `opaque_subgraphs`: A registry of opaque subgraphs from
354+ /// `original_hugr`, that are referenced by opaque barriers in the pytket
355+ /// circuit via their [`SubgraphId`].
356+ /// - `original_hugr`: The [`Hugr`] that originated the circuit, if any.
357+ #[ expect( unused) ]
358+ pub ( super ) fn register_unsupported_subgraphs (
359+ & mut self ,
360+ opaque_subgraphs : & ' h OpaqueSubgraphs < H :: Node > ,
361+ original_hugr : & ' h H ,
362+ ) {
363+ self . opaque_subgraphs = Some ( opaque_subgraphs) ;
364+ self . original_hugr = Some ( original_hugr) ;
365+ }
366+
335367 /// Decode a list of pytket commands.
336368 pub ( super ) fn run_decoder (
337369 & mut self ,
@@ -351,7 +383,7 @@ impl<'h> PytketDecoderContext<'h> {
351383 pub ( super ) fn process_command (
352384 & mut self ,
353385 command : & circuit_json:: Command ,
354- config : & PytketDecoderConfig ,
386+ config : & PytketDecoderConfig < H > ,
355387 ) -> Result < ( ) , PytketDecodeError > {
356388 let circuit_json:: Command { op, args, opgroup } = command;
357389
@@ -380,13 +412,13 @@ impl<'h> PytketDecoderContext<'h> {
380412 }
381413
382414 /// Returns the configuration used by the decoder.
383- pub fn config ( & self ) -> & Arc < PytketDecoderConfig > {
415+ pub fn config ( & self ) -> & Arc < PytketDecoderConfig < H > > {
384416 & self . config
385417 }
386418}
387419
388420/// Public API, used by the [`PytketDecoder`][super::extension::PytketDecoder] implementers.
389- impl < ' h > PytketDecoderContext < ' h > {
421+ impl < ' h , H : HugrView > PytketDecoderContext < ' h , H > {
390422 /// Returns a new set of [TrackedWires] for a list of [`TrackedQubit`]s,
391423 /// [`TrackedBit`]s, and [`LoadedParameter`]s following the required types.
392424 ///
@@ -723,7 +755,7 @@ pub enum DecodeStatus {
723755
724756/// Helper to continue exhausting the iterators in [`PytketDecoderContext::register_node_outputs`] until we have the total number of elements to report.
725757fn make_unexpected_node_out_error < ' ty > (
726- config : & PytketDecoderConfig ,
758+ config : & PytketDecoderConfig < impl HugrView > ,
727759 port_types : impl IntoIterator < Item = ( OutgoingPort , & ' ty Type ) > ,
728760 mut partial_count : RegisterCount ,
729761 expected_qubits : usize ,
0 commit comments