diff --git a/Cargo.toml b/Cargo.toml index a362d2ca..d04c60f4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,8 +20,8 @@ missing_docs = "warn" [workspace.dependencies] tket2 = { path = "./tket2" } -quantinuum-hugr = { git = "https://github.com/CQCL/hugr", rev = "2efcfb3" } -portgraph = { version = "0.10" } +quantinuum-hugr = { git = "https://github.com/CQCL/hugr", rev = "4b11a24" } +portgraph = { version = "0.11" } pyo3 = { version = "0.20" } itertools = { version = "0.12.0" } tket-json-rs = { version = "0.3.0" } diff --git a/tket2/src/extension/angle.rs b/tket2/src/extension/angle.rs index 5aedb1a7..3ed48b30 100644 --- a/tket2/src/extension/angle.rs +++ b/tket2/src/extension/angle.rs @@ -1,5 +1,6 @@ use std::{cmp::max, num::NonZeroU64}; +use hugr::extension::ExtensionSet; use hugr::{ extension::{prelude::ERROR_TYPE, SignatureError, SignatureFromArgs, TypeDef}, types::{ @@ -14,6 +15,8 @@ use itertools::Itertools; use smol_str::SmolStr; use std::f64::consts::TAU; +use super::TKET2_EXTENSION_ID; + /// Identifier for the angle type. const ANGLE_TYPE_ID: SmolStr = SmolStr::new_inline("angle"); @@ -125,6 +128,9 @@ impl CustomConst for ConstAngle { fn equal_consts(&self, other: &dyn CustomConst) -> bool { hugr::values::downcast_equal_consts(self, other) } + fn extension_reqs(&self) -> ExtensionSet { + ExtensionSet::singleton(&TKET2_EXTENSION_ID) + } } /// Collect a vector into an array. diff --git a/tket2/src/json/decoder.rs b/tket2/src/json/decoder.rs index 3c048a2d..dbb2a37f 100644 --- a/tket2/src/json/decoder.rs +++ b/tket2/src/json/decoder.rs @@ -6,8 +6,7 @@ use std::hash::{Hash, Hasher}; use std::mem; use hugr::builder::{CircuitBuilder, Container, DFGBuilder, Dataflow, DataflowHugr}; -use hugr::extension::prelude::{PRELUDE_ID, QB_T}; -use hugr::extension::ExtensionSet; +use hugr::extension::prelude::QB_T; use hugr::ops::Const; use hugr::std_extensions::arithmetic::float_types::FLOAT64_TYPE; @@ -147,9 +146,7 @@ impl JsonDecoder { Some(c) => { let const_type = FLOAT64_TYPE; let const_op = Const::new(c, const_type).unwrap(); - self.hugr - .add_load_const(const_op, ExtensionSet::singleton(&PRELUDE_ID)) - .unwrap() + self.hugr.add_load_const(const_op).unwrap() } None => { // store string in custom op. diff --git a/tket2/src/json/tests.rs b/tket2/src/json/tests.rs index 04befe4f..f518bbf6 100644 --- a/tket2/src/json/tests.rs +++ b/tket2/src/json/tests.rs @@ -4,7 +4,7 @@ use std::io::BufReader; use hugr::builder::{DFGBuilder, Dataflow, DataflowHugr}; use hugr::extension::prelude::QB_T; -use hugr::extension::ExtensionSet; + use hugr::std_extensions::arithmetic::float_types::{ConstF64, FLOAT64_TYPE}; use hugr::types::FunctionType; use hugr::Hugr; @@ -108,14 +108,9 @@ fn circ_add_angles_constants() -> Hugr { let mut h = DFGBuilder::new(FunctionType::new(qb_row.clone(), qb_row)).unwrap(); let qb = h.input_wires().next().unwrap(); - let f64_ext = hugr::std_extensions::arithmetic::float_types::EXTENSION_ID; - - let point2 = h - .add_load_const(ConstF64::new(0.2).into(), ExtensionSet::singleton(&f64_ext)) - .unwrap(); - let point3 = h - .add_load_const(ConstF64::new(0.3).into(), ExtensionSet::singleton(&f64_ext)) - .unwrap(); + + let point2 = h.add_load_const(ConstF64::new(0.2).into()).unwrap(); + let point3 = h.add_load_const(ConstF64::new(0.3).into()).unwrap(); let point5 = h .add_dataflow_op(Tk2Op::AngleAdd, [point2, point3]) .unwrap() diff --git a/tket2/src/passes/chunks.rs b/tket2/src/passes/chunks.rs index b89171e4..278df47e 100644 --- a/tket2/src/passes/chunks.rs +++ b/tket2/src/passes/chunks.rs @@ -9,7 +9,7 @@ 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::ConvexChecker; +use hugr::hugr::views::sibling_subgraph::TopoConvexChecker; use hugr::hugr::views::{HierarchyView, SiblingGraph, SiblingSubgraph}; use hugr::hugr::{HugrError, NodeMetadataMap}; use hugr::ops::handle::DataflowParentID; @@ -17,6 +17,7 @@ use hugr::ops::OpType; use hugr::types::FunctionType; use hugr::{Hugr, HugrView, IncomingPort, Node, OutgoingPort, PortIndex, Wire}; use itertools::Itertools; +use portgraph::algorithms::ConvexChecker; use crate::Circuit; @@ -46,10 +47,10 @@ impl Chunk { /// Extract a chunk from a circuit. /// /// The chunk is extracted from the input wires to the output wires. - pub(self) fn extract<'h, H: HugrView>( - circ: &'h H, + pub(self) fn extract( + circ: &H, nodes: impl IntoIterator, - checker: &ConvexChecker<'h, H>, + checker: &impl ConvexChecker, ) -> Self { let subgraph = SiblingSubgraph::try_from_nodes_with_checker( nodes.into_iter().collect_vec(), @@ -281,7 +282,7 @@ impl CircuitChunks { .collect(); let mut chunks = Vec::new(); - let convex_checker = ConvexChecker::new(circ); + let convex_checker = TopoConvexChecker::new(circ); let mut running_cost = C::default(); let mut current_group = 0; for (_, commands) in &circ.commands().map(|cmd| cmd.node()).group_by(|&node| { diff --git a/tket2/src/portmatching/matcher.rs b/tket2/src/portmatching/matcher.rs index 2d979dd9..4a555487 100644 --- a/tket2/src/portmatching/matcher.rs +++ b/tket2/src/portmatching/matcher.rs @@ -9,12 +9,13 @@ use std::{ use super::{CircuitPattern, NodeID, PEdge, PNode}; use hugr::hugr::views::sibling_subgraph::{ - ConvexChecker, InvalidReplacement, InvalidSubgraph, InvalidSubgraphBoundary, + InvalidReplacement, InvalidSubgraph, InvalidSubgraphBoundary, TopoConvexChecker, }; use hugr::hugr::views::SiblingSubgraph; use hugr::ops::OpType; use hugr::{Hugr, IncomingPort, Node, OutgoingPort, Port, PortIndex}; use itertools::Itertools; +use portgraph::algorithms::ConvexChecker; use portmatching::{ automaton::{LineBuilder, ScopeAutomaton}, EdgeProperty, PatternID, @@ -108,7 +109,7 @@ impl PatternMatch { circ: &impl Circuit, matcher: &PatternMatcher, ) -> Result { - let checker = ConvexChecker::new(circ); + let checker = TopoConvexChecker::new(circ); Self::try_from_root_match_with_checker(root, pattern, circ, matcher, &checker) } @@ -118,12 +119,12 @@ impl PatternMatch { /// checker object to speed up convexity checking. /// /// See [`PatternMatch::try_from_root_match`] for more details. - pub fn try_from_root_match_with_checker<'c, C: Circuit>( + pub fn try_from_root_match_with_checker( root: Node, pattern: PatternID, - circ: &'c C, + circ: &C, matcher: &PatternMatcher, - checker: &ConvexChecker<'c, C>, + checker: &impl ConvexChecker, ) -> Result { let pattern_ref = matcher .get_pattern(pattern) @@ -166,7 +167,7 @@ impl PatternMatch { inputs: Vec>, outputs: Vec<(Node, OutgoingPort)>, ) -> Result { - let checker = ConvexChecker::new(circ); + let checker = TopoConvexChecker::new(circ); Self::try_from_io_with_checker(root, pattern, circ, inputs, outputs, &checker) } @@ -178,13 +179,13 @@ impl PatternMatch { /// /// This checks at construction time that the match is convex. This will /// have runtime linear in the size of the circuit. - pub fn try_from_io_with_checker<'c, C: Circuit>( + pub fn try_from_io_with_checker( root: Node, pattern: PatternID, - circ: &'c C, + circ: &C, inputs: Vec>, outputs: Vec<(Node, OutgoingPort)>, - checker: &ConvexChecker<'c, C>, + checker: &impl ConvexChecker, ) -> Result { let subgraph = SiblingSubgraph::try_new_with_checker(inputs, outputs, circ, checker)?; Ok(Self { @@ -258,7 +259,7 @@ impl PatternMatcher { &'a self, circuit: &'c C, ) -> impl Iterator + 'a { - let checker = ConvexChecker::new(circuit); + let checker = TopoConvexChecker::new(circuit); circuit .commands() .flat_map(move |cmd| self.find_rooted_matches(circuit, cmd.node(), &checker)) @@ -270,11 +271,11 @@ impl PatternMatcher { } /// Find all convex pattern matches in a circuit rooted at a given node. - fn find_rooted_matches<'c, C: Circuit + Clone>( + fn find_rooted_matches( &self, - circ: &'c C, + circ: &C, root: Node, - checker: &ConvexChecker<'c, C>, + checker: &impl ConvexChecker, ) -> Vec { self.automaton .run(