Skip to content

Commit

Permalink
chore: Update hugr dependency (#223)
Browse files Browse the repository at this point in the history
Fixes the metadata API changes from
CQCL/hugr#661.
  • Loading branch information
aborgna-q authored Nov 8, 2023
1 parent 15a36fb commit 036091d
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 32 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
3 changes: 1 addition & 2 deletions tket2/src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 8 additions & 8 deletions tket2/src/json/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>();
JsonDecoder {
Expand Down
28 changes: 13 additions & 15 deletions tket2/src/json/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 7 additions & 6 deletions tket2/src/passes/chunks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -247,7 +247,7 @@ pub struct CircuitChunks {
signature: FunctionType,

/// The original circuit's root metadata.
root_meta: NodeMetadata,
root_meta: Option<NodeMetadataMap>,

/// The original circuit's inputs.
input_connections: Vec<ChunkConnection>,
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -318,8 +318,9 @@ impl CircuitChunks {
pub fn reassemble(self) -> Result<Hugr, HugrError> {
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,
Expand Down Expand Up @@ -437,7 +438,7 @@ impl CircuitChunks {
}
}

reassembled.set_metadata(root, self.root_meta)?;
reassembled.overwrite_node_metadata(root, self.root_meta)?;

Ok(reassembled)
}
Expand Down

0 comments on commit 036091d

Please sign in to comment.