From c40e1a9c50f10e6c9ff8d2def8cc7d76ccc46895 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Agust=C3=ADn=20Borgna?= <121866228+aborgna-q@users.noreply.github.com> Date: Mon, 8 Jul 2024 14:37:22 +0100 Subject: [PATCH] chore: Update to hugr 0.6.0, fix breaking changes (#464) Closes #463 --- Cargo.lock | 16 ++++++++-------- Cargo.toml | 6 +++--- tket2/src/circuit.rs | 17 +++++------------ tket2/src/circuit/command.rs | 8 +------- tket2/src/circuit/extract_dfg.rs | 20 +++----------------- tket2/src/passes/chunks.rs | 2 +- tket2/src/passes/pytket.rs | 3 +-- tket2/src/serialize/pytket/decoder.rs | 2 +- tket2/src/utils.rs | 4 ++-- 9 files changed, 25 insertions(+), 53 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a4a84b95..2d2bb8fd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -735,9 +735,9 @@ dependencies = [ [[package]] name = "hugr" -version = "0.5.1" +version = "0.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a20246e5f1a0aae160b80b71bc4d5c1dcccc5605dbe6458ac6f9af73137339c3" +checksum = "79e9896b1c2f2760f71e05e8e56492e98f828e4abb17055055f851752c54720a" dependencies = [ "hugr-core", "hugr-passes", @@ -745,9 +745,9 @@ dependencies = [ [[package]] name = "hugr-cli" -version = "0.1.1" +version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f3d973149618f24c966ea90cfaf5955c184b670d38b18f3114a66a20dc5b712b" +checksum = "cce7564fd98a98a3e6576b627f18294319e02664aa4765924defdc789062e5f6" dependencies = [ "clap", "clap-stdin", @@ -759,9 +759,9 @@ dependencies = [ [[package]] name = "hugr-core" -version = "0.2.0" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bd64c708ba21cedae58e37f3557d139ee6eaeaaa7df0ab9541385249c1ee6065" +checksum = "86da0c1e47982cb16d20911c33af448cae7b4aa36f1f5ff44753c38dbb152aae" dependencies = [ "bitvec", "cgmath", @@ -790,9 +790,9 @@ dependencies = [ [[package]] name = "hugr-passes" -version = "0.2.0" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7522d3a5299a49ee453f6d0e3c3a6e8e7ed08a38cbd11e1d19163b97098ce9be" +checksum = "f4ae0241e13fc66d944faaa7c25b4062336274e6d533a0da05b55fa9b8b601df" dependencies = [ "hugr-core", "itertools 0.13.0", diff --git a/Cargo.toml b/Cargo.toml index ffde4770..e80ea55f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,9 +25,9 @@ missing_docs = "warn" [workspace.dependencies] tket2 = { path = "./tket2" } -hugr = "0.5.1" -hugr-cli = "0.1.1" -hugr-core = "0.2.0" +hugr = "0.6.1" +hugr-cli = "0.1.2" +hugr-core = "0.3.1" portgraph = "0.12" pyo3 = "0.21.2" itertools = "0.13.0" diff --git a/tket2/src/circuit.rs b/tket2/src/circuit.rs index cfe3da4f..6770870d 100644 --- a/tket2/src/circuit.rs +++ b/tket2/src/circuit.rs @@ -15,7 +15,6 @@ use hugr_core::hugr::internal::HugrMutInternals; use itertools::Either::{Left, Right}; use hugr::hugr::hugrmut::HugrMut; -use hugr::hugr::NodeType; use hugr::ops::dataflow::IOTrait; use hugr::ops::{Input, NamedOp, OpParent, OpTag, OpTrait, Output}; use hugr::types::{FunctionType, PolyFuncType}; @@ -528,10 +527,7 @@ fn update_signature( types.remove(in_index); types.into() }; - let new_inp_op = Input::new(inp_types.clone()); - let inp_exts = hugr.get_nodetype(inp).input_extensions().cloned(); - hugr.replace_op(inp, NodeType::new(new_inp_op, inp_exts)) - .unwrap(); + hugr.replace_op(inp, Input::new(inp_types.clone())).unwrap(); // Update output node if necessary. let out_types = out_index.map(|out_index| { @@ -544,17 +540,14 @@ fn update_signature( types.remove(out_index); types.into() }; - let new_out_op = Output::new(out_types.clone()); - let inp_exts = hugr.get_nodetype(out).input_extensions().cloned(); - hugr.replace_op(out, NodeType::new(new_out_op, inp_exts)) + hugr.replace_op(out, Output::new(out_types.clone())) .unwrap(); out_types }); // Update the parent's signature - let nodetype = hugr.get_nodetype(parent).clone(); - let input_extensions = nodetype.input_extensions().cloned(); - let mut optype = nodetype.into_op(); + let mut optype = hugr.get_optype(parent).clone(); + // Replace the parent node operation with the right operation type // This must be able to process all implementers of `DataflowParent`. match &mut optype { @@ -594,7 +587,7 @@ fn update_signature( })?, } - hugr.replace_op(parent, NodeType::new(optype, input_extensions))?; + hugr.replace_op(parent, optype)?; Ok(()) } diff --git a/tket2/src/circuit/command.rs b/tket2/src/circuit/command.rs index d1cd8223..88de1e7e 100644 --- a/tket2/src/circuit/command.rs +++ b/tket2/src/circuit/command.rs @@ -7,7 +7,7 @@ use std::collections::{HashMap, HashSet}; use std::iter::FusedIterator; use hugr::hugr::views::{HierarchyView, SiblingGraph}; -use hugr::hugr::{NodeMetadata, NodeType}; +use hugr::hugr::NodeMetadata; use hugr::ops::{OpTag, OpTrait}; use hugr::{HugrView, IncomingPort, OutgoingPort}; use itertools::Either::{self, Left, Right}; @@ -40,12 +40,6 @@ impl<'circ, T: HugrView> Command<'circ, T> { self.node } - /// Returns the [`NodeType`] of the command. - #[inline] - pub fn nodetype(&self) -> &NodeType { - self.circ.hugr().get_nodetype(self.node) - } - /// Returns the [`OpType`] of the command. #[inline] pub fn optype(&self) -> &OpType { diff --git a/tket2/src/circuit/extract_dfg.rs b/tket2/src/circuit/extract_dfg.rs index 17c10ed2..c5b5bb72 100644 --- a/tket2/src/circuit/extract_dfg.rs +++ b/tket2/src/circuit/extract_dfg.rs @@ -1,7 +1,6 @@ //! Internal implementation of `Circuit::extract_dfg`. use hugr::hugr::hugrmut::HugrMut; -use hugr::hugr::NodeType; use hugr::ops::{OpTrait, OpType, Output, DFG}; use hugr::types::{FunctionType, SumType, TypeEnum}; use hugr::HugrView; @@ -26,10 +25,7 @@ pub(super) fn rewrite_into_dfg(circ: &mut Circuit) -> Result<(), CircuitMutError }; let dfg = DFG { signature }; - let nodetype = circ.hugr.get_nodetype(circ.parent()); - let input_extensions = nodetype.input_extensions().cloned(); - let nodetype = NodeType::new(OpType::DFG(dfg), input_extensions); - circ.hugr.replace_op(circ.parent(), nodetype)?; + circ.hugr.replace_op(circ.parent(), OpType::DFG(dfg))?; Ok(()) } @@ -50,8 +46,7 @@ fn remove_cfg_empty_output_tuple( let input_node = circ.input_node(); let output_node = circ.output_node(); - let output_nodetype = circ.hugr.get_nodetype(output_node).clone(); - let output_op = output_nodetype.op(); + let output_op = circ.hugr.get_optype(output_node).clone(); let output_sig = output_op .dataflow_signature() @@ -89,16 +84,7 @@ fn remove_cfg_empty_output_tuple( let new_op = Output { types: new_types.clone().into(), }; - let new_node = hugr.add_node_after( - input_node, - NodeType::new( - new_op, - output_nodetype - .input_extensions() - .cloned() - .unwrap_or_default(), - ), - ); + let new_node = hugr.add_node_after(input_node, new_op); // Reconnect the outputs. for (i, (neigh, port)) in input_neighs.into_iter().enumerate() { diff --git a/tket2/src/passes/chunks.rs b/tket2/src/passes/chunks.rs index f1a40715..fba83be9 100644 --- a/tket2/src/passes/chunks.rs +++ b/tket2/src/passes/chunks.rs @@ -308,7 +308,7 @@ impl CircuitChunks { .and_then(|s| s.as_str()) .unwrap_or(""); - let mut builder = FunctionBuilder::new(name, self.signature.into()).unwrap(); + 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()); diff --git a/tket2/src/passes/pytket.rs b/tket2/src/passes/pytket.rs index 9def01fa..b9f1d1fb 100644 --- a/tket2/src/passes/pytket.rs +++ b/tket2/src/passes/pytket.rs @@ -70,13 +70,12 @@ mod test { let mut builder = ModuleBuilder::new(); let _func = { - let mut func = builder.define_function("main", circ_signature.into())?; + let mut func = builder.define_function("main", circ_signature)?; let [q1, q2] = func.input_wires_arr(); let cfg = { let mut cfg = func.cfg_builder( [(QB_T, q1), (QB_T, q2)], - None, two_qbs.clone(), ExtensionSet::new(), )?; diff --git a/tket2/src/serialize/pytket/decoder.rs b/tket2/src/serialize/pytket/decoder.rs index b1f2eeb5..8c70249b 100644 --- a/tket2/src/serialize/pytket/decoder.rs +++ b/tket2/src/serialize/pytket/decoder.rs @@ -48,7 +48,7 @@ impl Tk1Decoder { .with_extension_delta(TKET1_EXTENSION_ID); let name = serialcirc.name.clone().unwrap_or_default(); - let mut dfg = FunctionBuilder::new(name, sig.into()).unwrap(); + let mut dfg = FunctionBuilder::new(name, sig).unwrap(); let dangling_wires = dfg.input_wires().collect::>(); // Metadata. The circuit requires "name", and we store other things that diff --git a/tket2/src/utils.rs b/tket2/src/utils.rs index e6578c47..55af83a9 100644 --- a/tket2/src/utils.rs +++ b/tket2/src/utils.rs @@ -28,7 +28,7 @@ where let qb_row = vec![QB_T; num_qubits]; let signature = FunctionType::new(qb_row.clone(), qb_row).with_extension_delta(float_types::EXTENSION_ID); - let mut h = FunctionBuilder::new("main", signature.into())?; + let mut h = FunctionBuilder::new("main", signature)?; let qbs = h.input_wires(); @@ -53,7 +53,7 @@ where let circ = { let qb_row = vec![QB_T; num_qubits]; let circ_signature = FunctionType::new(qb_row.clone(), qb_row); - let mut dfg = builder.define_function("main", circ_signature.into())?; + let mut dfg = builder.define_function("main", circ_signature)?; let mut circ = dfg.as_circuit(dfg.input_wires()); f(&mut circ)?; let qbs = circ.finish();