-
Notifications
You must be signed in to change notification settings - Fork 11
feat(pytket-decoder): Decode unsupported subgraphs from barriers #1182
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
b6b0bb7
48c25f3
6d86bbd
abd2a91
a79e44d
4bbcf80
2821b53
003b830
c921d3a
088c0b3
e178c9a
2130309
689bf6c
eada88e
58819ea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,13 +4,17 @@ use std::collections::{HashMap, VecDeque}; | |
| use std::ops::{Index, IndexMut}; | ||
|
|
||
| use hugr::core::HugrNode; | ||
| use hugr::hugr::hugrmut::HugrMut; | ||
| use hugr::ops::handle::NodeHandle; | ||
| use hugr::ops::{OpTag, OpTrait}; | ||
| use hugr::{Hugr, HugrView}; | ||
| use hugr::{Hugr, HugrView, Node}; | ||
| use rayon::iter::{IntoParallelIterator, IntoParallelRefIterator, IntoParallelRefMutIterator}; | ||
| use tket_json_rs::circuit_json::{Command as PytketCommand, SerialCircuit}; | ||
|
|
||
| use crate::serialize::pytket::decoder::PytketDecoderContext; | ||
| use crate::serialize::pytket::{ | ||
| default_encoder_config, EncodeOptions, PytketEncodeError, PytketEncoderContext, | ||
| default_encoder_config, DecodeInsertionTarget, DecodeOptions, EncodeOptions, PytketDecodeError, | ||
| PytketDecodeErrorInner, PytketEncodeError, PytketEncoderContext, | ||
| }; | ||
| use crate::Circuit; | ||
|
|
||
|
|
@@ -149,6 +153,86 @@ impl<'a, H: HugrView> EncodedCircuit<'a, H> { | |
| Ok(()) | ||
| } | ||
|
|
||
| /// Reassemble the encoded circuits into a new [`Hugr`], containing a | ||
| /// function defining the [`Self::head_region`] and expanding any opaque | ||
| /// hugrs in pytket barrier operations back into Hugr subgraphs. | ||
| /// | ||
| /// Functions called by the internal hugrs may be added to the hugr module | ||
| /// as well. | ||
| /// | ||
| /// # Arguments | ||
| /// | ||
| /// - `fn_name`: The name of the function to create. If `None`, we will use | ||
| /// the name of the circuit, or "main" if the circuit has no name. | ||
| /// - `options`: The options for the decoder. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns a [`PytketDecodeErrorInner::NonDataflowHeadRegion`] error if | ||
| /// [`Self::head_region`] is not a dataflow container in the hugr. | ||
| /// | ||
| /// Returns an error if a circuit being decoded is invalid. See | ||
| /// [`PytketDecodeErrorInner`][super::error::PytketDecodeErrorInner] for | ||
| /// more details. | ||
| pub fn reassemble( | ||
| &self, | ||
| fn_name: Option<String>, | ||
| options: DecodeOptions<H>, | ||
| ) -> Result<Hugr, PytketDecodeError> { | ||
| let mut hugr = Hugr::new(); | ||
| let main_func = self.reassemble_inline( | ||
| &mut hugr, | ||
| DecodeInsertionTarget::Function { fn_name }, | ||
| options, | ||
| )?; | ||
| hugr.set_entrypoint(main_func); | ||
| Ok(hugr) | ||
| } | ||
|
|
||
| /// Reassemble the encoded circuits inside an existing [`Hugr`], containing | ||
| /// the [`Self::head_region`] at the given insertion target. | ||
| /// | ||
| /// Functions called by the internal hugrs may be added to the hugr module | ||
| /// as well. | ||
| /// | ||
| /// # Arguments | ||
| /// | ||
| /// - `hugr`: The [`Hugr`] to reassemble the circuits in. | ||
| /// - `target`: The target to insert the function at. | ||
| /// - `options`: The options for the decoder. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns a [`PytketDecodeErrorInner::NonDataflowHeadRegion`] error if | ||
| /// [`Self::head_region`] is not a dataflow container in the hugr. | ||
| /// | ||
| /// Returns an error if a circuit being decoded is invalid. See | ||
| /// [`PytketDecodeErrorInner`][super::error::PytketDecodeErrorInner] for | ||
| /// more details. | ||
| pub fn reassemble_inline( | ||
| &self, | ||
| hugr: &mut Hugr, | ||
| target: DecodeInsertionTarget, | ||
| options: DecodeOptions<H>, | ||
| ) -> Result<Node, PytketDecodeError> { | ||
| if !self.check_dataflow_head_region() { | ||
| let head_op = self.hugr.get_optype(self.head_region).to_string(); | ||
| return Err(PytketDecodeErrorInner::NonDataflowHeadRegion { head_op }.wrap()); | ||
| }; | ||
| let serial_circuit = &self[self.head_region]; | ||
|
|
||
| if self.len() > 1 { | ||
| unimplemented!( | ||
| "Reassembling an `EncodedCircuit` with nested subcircuits is not yet implemented." | ||
| ); | ||
| }; | ||
|
|
||
| let mut decoder = PytketDecoderContext::<H>::new(serial_circuit, hugr, target, options)?; | ||
| decoder.register_opaque_subgraphs(&self.opaque_subgraphs, self.hugr); | ||
|
||
| decoder.run_decoder(&serial_circuit.commands)?; | ||
| Ok(decoder.finish()?.node()) | ||
| } | ||
|
|
||
| /// Extract the top-level pytket circuit as a standalone definition | ||
| /// containing the whole original HUGR. | ||
| /// | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.