diff --git a/Cargo.toml b/Cargo.toml index 29ce91aa..fc07a6dd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,7 @@ license-file = "LICENCE" [workspace.dependencies] tket2 = { path = "./tket2" } -quantinuum-hugr = { git = "https://github.com/CQCL-DEV/hugr", rev = "1fce927" } +quantinuum-hugr = { git = "https://github.com/CQCL-DEV/hugr", rev = "b71cae6" } portgraph = { version = "0.10" } pyo3 = { version = "0.20" } itertools = { version = "0.11.0" } diff --git a/tket2/src/circuit.rs b/tket2/src/circuit.rs index 7e46563d..d8e55577 100644 --- a/tket2/src/circuit.rs +++ b/tket2/src/circuit.rs @@ -38,8 +38,7 @@ pub trait Circuit: HugrView { /// Return the name of the circuit #[inline] fn name(&self) -> Option<&str> { - let meta = self.get_metadata(self.root()).as_object()?; - meta.get("name")?.as_str() + self.get_metadata(self.root(), "name")?.as_str() } /// Returns the function type of the circuit. diff --git a/tket2/src/json/decoder.rs b/tket2/src/json/decoder.rs index 6a02e297..2721dfb6 100644 --- a/tket2/src/json/decoder.rs +++ b/tket2/src/json/decoder.rs @@ -73,14 +73,14 @@ impl JsonDecoder { // Metadata. The circuit requires "name", and we store other things that // should pass through the serialization roundtrip. - let metadata = json!({ - "name": serialcirc.name, - METADATA_PHASE: serialcirc.phase, - METADATA_IMPLICIT_PERM: serialcirc.implicit_permutation, - METADATA_Q_REGISTERS: serialcirc.qubits, - METADATA_B_REGISTERS: serialcirc.bits, - }); - dfg.set_metadata(metadata); + dfg.set_metadata("name", json!(serialcirc.name)); + dfg.set_metadata(METADATA_PHASE, json!(serialcirc.phase)); + dfg.set_metadata( + METADATA_IMPLICIT_PERM, + json!(serialcirc.implicit_permutation), + ); + dfg.set_metadata(METADATA_Q_REGISTERS, json!(serialcirc.qubits)); + dfg.set_metadata(METADATA_B_REGISTERS, json!(serialcirc.bits)); let dangling_wires = dfg.input_wires().collect::>(); JsonDecoder { diff --git a/tket2/src/json/encoder.rs b/tket2/src/json/encoder.rs index e6a0d136..f3ea1273 100644 --- a/tket2/src/json/encoder.rs +++ b/tket2/src/json/encoder.rs @@ -57,21 +57,19 @@ impl JsonEncoder { let mut implicit_permutation = vec![]; // Recover other parameters stored in the metadata - if let Some(meta) = circ.get_metadata(circ.root()).as_object() { - if let Some(p) = meta.get(METADATA_PHASE) { - // TODO: Check for invalid encoded metadata - phase = p.as_str().unwrap().to_string(); - } - if let Some(perm) = meta.get(METADATA_IMPLICIT_PERM) { - // TODO: Check for invalid encoded metadata - implicit_permutation = serde_json::from_value(perm.clone()).unwrap(); - } - if let Some(q_regs) = meta.get(METADATA_Q_REGISTERS) { - qubit_registers = serde_json::from_value(q_regs.clone()).unwrap(); - } - if let Some(b_regs) = meta.get(METADATA_B_REGISTERS) { - bit_registers = serde_json::from_value(b_regs.clone()).unwrap(); - } + // TODO: Check for invalid encoded metadata + let root = circ.root(); + if let Some(p) = circ.get_metadata(root, METADATA_PHASE) { + phase = p.as_str().unwrap().to_string(); + } + if let Some(perm) = circ.get_metadata(root, METADATA_IMPLICIT_PERM) { + implicit_permutation = serde_json::from_value(perm.clone()).unwrap(); + } + if let Some(q_regs) = circ.get_metadata(root, METADATA_Q_REGISTERS) { + qubit_registers = serde_json::from_value(q_regs.clone()).unwrap(); + } + if let Some(b_regs) = circ.get_metadata(root, METADATA_B_REGISTERS) { + bit_registers = serde_json::from_value(b_regs.clone()).unwrap(); } // Map the Hugr units to tket1 register names. diff --git a/tket2/src/passes/chunks.rs b/tket2/src/passes/chunks.rs index e15a6da5..bad25839 100644 --- a/tket2/src/passes/chunks.rs +++ b/tket2/src/passes/chunks.rs @@ -12,7 +12,7 @@ use hugr::extension::ExtensionSet; use hugr::hugr::hugrmut::HugrMut; use hugr::hugr::views::sibling_subgraph::ConvexChecker; use hugr::hugr::views::{HierarchyView, SiblingGraph, SiblingSubgraph}; -use hugr::hugr::{HugrError, NodeMetadata}; +use hugr::hugr::{HugrError, NodeMetadataMap}; use hugr::ops::handle::DataflowParentID; use hugr::ops::OpType; use hugr::types::{FunctionType, Signature}; @@ -247,7 +247,7 @@ pub struct CircuitChunks { signature: FunctionType, /// The original circuit's root metadata. - root_meta: NodeMetadata, + root_meta: Option, /// The original circuit's inputs. input_connections: Vec, @@ -275,7 +275,7 @@ impl CircuitChunks { max_cost: C, op_cost: impl Fn(&OpType) -> C, ) -> Self { - let root_meta = circ.get_metadata(circ.root()).clone(); + let root_meta = circ.get_node_metadata(circ.root()).cloned(); let signature = circ.circuit_signature().clone(); let [circ_input, circ_output] = circ.get_io(circ.root()).unwrap(); @@ -318,8 +318,9 @@ impl CircuitChunks { pub fn reassemble(self) -> Result { let name = self .root_meta - .get("name") - .and_then(|v| v.as_str()) + .as_ref() + .and_then(|map| map.get("name")) + .and_then(|s| s.as_str()) .unwrap_or(""); let signature = Signature { signature: self.signature, @@ -437,7 +438,7 @@ impl CircuitChunks { } } - reassembled.set_metadata(root, self.root_meta)?; + reassembled.overwrite_node_metadata(root, self.root_meta)?; Ok(reassembled) }