From 5bc8a7e9243cc07138acda6a3b3fbc2ad382dc08 Mon Sep 17 00:00:00 2001 From: Agustin Borgna Date: Fri, 7 Jun 2024 11:26:13 +0100 Subject: [PATCH] refactor!: Rename `tket2::json` into `tket2::serialize::pytket` --- badger-optimiser/src/main.rs | 2 +- tket2-py/src/circuit.rs | 4 ++-- tket2-py/src/circuit/convert.rs | 2 +- tket2-py/src/circuit/tk2circuit.rs | 2 +- tket2/src/circuit.rs | 5 +++-- tket2/src/circuit/hash.rs | 2 +- tket2/src/extension.rs | 2 +- tket2/src/lib.rs | 4 ++-- tket2/src/optimiser/badger.rs | 2 +- tket2/src/serialize.rs | 9 +++++++++ tket2/src/{json.rs => serialize/pytket.rs} | 6 ++++-- tket2/src/{json => serialize/pytket}/decoder.rs | 2 +- tket2/src/{json => serialize/pytket}/encoder.rs | 0 tket2/src/{json => serialize/pytket}/op.rs | 2 +- tket2/src/{json => serialize/pytket}/tests.rs | 2 +- tket2/tests/badger_termination.rs | 8 +++----- 16 files changed, 32 insertions(+), 22 deletions(-) create mode 100644 tket2/src/serialize.rs rename tket2/src/{json.rs => serialize/pytket.rs} (98%) rename tket2/src/{json => serialize/pytket}/decoder.rs (99%) rename tket2/src/{json => serialize/pytket}/encoder.rs (100%) rename tket2/src/{json => serialize/pytket}/op.rs (98%) rename tket2/src/{json => serialize/pytket}/tests.rs (99%) diff --git a/badger-optimiser/src/main.rs b/badger-optimiser/src/main.rs index 08aa4d8e..eccc5782 100644 --- a/badger-optimiser/src/main.rs +++ b/badger-optimiser/src/main.rs @@ -11,10 +11,10 @@ use std::path::PathBuf; use std::process::exit; use clap::Parser; -use tket2::json::{load_tk1_json_file, save_tk1_json_file}; use tket2::optimiser::badger::log::BadgerLogger; use tket2::optimiser::badger::BadgerOptions; use tket2::optimiser::{BadgerOptimiser, DefaultBadgerOptimiser}; +use tket2::serialize::{load_tk1_json_file, save_tk1_json_file}; #[cfg(feature = "peak_alloc")] #[global_allocator] diff --git a/tket2-py/src/circuit.rs b/tket2-py/src/circuit.rs index df03266b..674e7cba 100644 --- a/tket2-py/src/circuit.rs +++ b/tket2-py/src/circuit.rs @@ -16,8 +16,8 @@ use std::fmt; use hugr::{type_row, Hugr, HugrView, PortIndex}; use tket2::extension::{LINEAR_BIT, REGISTRY}; -use tket2::json::TKETDecode; use tket2::rewrite::CircuitRewrite; +use tket2::serialize::TKETDecode; use tket_json_rs::circuit_json::SerialCircuit; use crate::utils::create_py_exception; @@ -83,7 +83,7 @@ create_py_exception!( ); create_py_exception!( - tket2::json::OpConvertError, + tket2::serialize::pytket::OpConvertError, PyOpConvertError, "Error type for the conversion between tket2 and tket1 operations." ); diff --git a/tket2-py/src/circuit/convert.rs b/tket2-py/src/circuit/convert.rs index c8f70d63..f46df25a 100644 --- a/tket2-py/src/circuit/convert.rs +++ b/tket2-py/src/circuit/convert.rs @@ -20,8 +20,8 @@ use hugr::{Hugr, HugrView, Wire}; use serde::Serialize; use tket2::circuit::CircuitHash; use tket2::extension::REGISTRY; -use tket2::json::TKETDecode; use tket2::passes::CircuitChunks; +use tket2::serialize::TKETDecode; use tket2::{Circuit, Tk2Op}; use tket_json_rs::circuit_json::SerialCircuit; diff --git a/tket2-py/src/circuit/tk2circuit.rs b/tket2-py/src/circuit/tk2circuit.rs index 024a9c0f..65cfe7e1 100644 --- a/tket2-py/src/circuit/tk2circuit.rs +++ b/tket2-py/src/circuit/tk2circuit.rs @@ -20,8 +20,8 @@ use hugr::{Hugr, HugrView, Wire}; use serde::Serialize; use tket2::circuit::CircuitHash; use tket2::extension::REGISTRY; -use tket2::json::TKETDecode; use tket2::passes::CircuitChunks; +use tket2::serialize::TKETDecode; use tket2::{Circuit, Tk2Op}; use tket_json_rs::circuit_json::SerialCircuit; diff --git a/tket2/src/circuit.rs b/tket2/src/circuit.rs index 8958342c..5d3afff8 100644 --- a/tket2/src/circuit.rs +++ b/tket2/src/circuit.rs @@ -497,8 +497,9 @@ mod tests { }; use super::*; - use crate::utils::build_module_with_circuit; - use crate::{json::load_tk1_json_str, utils::build_simple_circuit, Tk2Op}; + use crate::serialize::load_tk1_json_str; + use crate::utils::{build_module_with_circuit, build_simple_circuit}; + use crate::Tk2Op; #[fixture] fn tk1_circuit() -> Circuit { diff --git a/tket2/src/circuit/hash.rs b/tket2/src/circuit/hash.rs index 84806874..cbc1a7a7 100644 --- a/tket2/src/circuit/hash.rs +++ b/tket2/src/circuit/hash.rs @@ -153,7 +153,7 @@ pub enum HashError { mod test { use tket_json_rs::circuit_json; - use crate::json::TKETDecode; + use crate::serialize::TKETDecode; use crate::utils::build_simple_circuit; use crate::Tk2Op; diff --git a/tket2/src/extension.rs b/tket2/src/extension.rs index e53b6b95..3e048c40 100644 --- a/tket2/src/extension.rs +++ b/tket2/src/extension.rs @@ -2,7 +2,7 @@ //! //! This includes a extension for the opaque TKET1 operations. -use super::json::op::JsonOp; +use super::serialize::pytket::JsonOp; use crate::Tk2Op; use hugr::extension::prelude::PRELUDE; use hugr::extension::simple_op::MakeOpDef; diff --git a/tket2/src/lib.rs b/tket2/src/lib.rs index a11de7ea..b107c0bf 100644 --- a/tket2/src/lib.rs +++ b/tket2/src/lib.rs @@ -24,7 +24,7 @@ //! use hugr::HugrView; //! //! // Load a tket1 circuit. -//! let mut circ: Circuit = tket2::json::load_tk1_json_file("../test_files/barenco_tof_5.json").unwrap(); +//! let mut circ: Circuit = tket2::serialize::load_tk1_json_file("../test_files/barenco_tof_5.json").unwrap(); //! //! assert_eq!(circ.qubit_count(), 9); //! assert_eq!(circ.num_operations(), 170); @@ -45,11 +45,11 @@ pub mod circuit; pub mod extension; -pub mod json; pub(crate) mod ops; pub mod optimiser; pub mod passes; pub mod rewrite; +pub mod serialize; #[cfg(feature = "portmatching")] pub mod portmatching; diff --git a/tket2/src/optimiser/badger.rs b/tket2/src/optimiser/badger.rs index 9aa6bafa..5d632f94 100644 --- a/tket2/src/optimiser/badger.rs +++ b/tket2/src/optimiser/badger.rs @@ -504,8 +504,8 @@ mod tests { }; use rstest::{fixture, rstest}; - use crate::json::load_tk1_json_str; use crate::optimiser::badger::BadgerOptions; + use crate::serialize::load_tk1_json_str; use crate::{extension::REGISTRY, Circuit, Tk2Op}; use super::{BadgerOptimiser, DefaultBadgerOptimiser}; diff --git a/tket2/src/serialize.rs b/tket2/src/serialize.rs new file mode 100644 index 00000000..fc0dc3e6 --- /dev/null +++ b/tket2/src/serialize.rs @@ -0,0 +1,9 @@ +//! Utilities for serializing circuits. +//! +//! See [`crate::serialize::pytket`] for serialization to and from the legacy pytket format. +pub mod pytket; + +pub use pytket::{ + load_tk1_json_file, load_tk1_json_reader, load_tk1_json_str, save_tk1_json_file, + save_tk1_json_str, save_tk1_json_writer, TKETDecode, +}; diff --git a/tket2/src/json.rs b/tket2/src/serialize/pytket.rs similarity index 98% rename from tket2/src/json.rs rename to tket2/src/serialize/pytket.rs index 34c71089..f870f145 100644 --- a/tket2/src/json.rs +++ b/tket2/src/serialize/pytket.rs @@ -1,8 +1,10 @@ -//! Json serialization and deserialization. +//! Serialization and deserialization of circuits using the `pytket` JSON format. mod decoder; mod encoder; -pub mod op; +mod op; + +pub(crate) use op::JsonOp; #[cfg(test)] mod tests; diff --git a/tket2/src/json/decoder.rs b/tket2/src/serialize/pytket/decoder.rs similarity index 99% rename from tket2/src/json/decoder.rs rename to tket2/src/serialize/pytket/decoder.rs index 7cac7360..e17391f2 100644 --- a/tket2/src/json/decoder.rs +++ b/tket2/src/serialize/pytket/decoder.rs @@ -18,8 +18,8 @@ use tket_json_rs::circuit_json::SerialCircuit; use super::op::JsonOp; use super::{try_param_to_constant, METADATA_IMPLICIT_PERM, METADATA_PHASE}; +use super::{METADATA_B_REGISTERS, METADATA_Q_REGISTERS}; use crate::extension::{LINEAR_BIT, REGISTRY}; -use crate::json::{METADATA_B_REGISTERS, METADATA_Q_REGISTERS}; use crate::symbolic_constant_op; /// The state of an in-progress [`DFGBuilder`] being built from a [`SerialCircuit`]. diff --git a/tket2/src/json/encoder.rs b/tket2/src/serialize/pytket/encoder.rs similarity index 100% rename from tket2/src/json/encoder.rs rename to tket2/src/serialize/pytket/encoder.rs diff --git a/tket2/src/json/op.rs b/tket2/src/serialize/pytket/op.rs similarity index 98% rename from tket2/src/json/op.rs rename to tket2/src/serialize/pytket/op.rs index 2ba84f69..5dc898a1 100644 --- a/tket2/src/json/op.rs +++ b/tket2/src/serialize/pytket/op.rs @@ -23,7 +23,7 @@ use crate::Tk2Op; /// A serialized operation, containing the operation type and all its attributes. /// -/// Wrapper around [`circuit_json::Operation`] with cached number of qubits and bits. +/// Wrapper around [`tket_json_rs::circuit_json::Operation`] with cached number of qubits and bits. /// /// The `Operation` contained by this struct is guaranteed to have a signature. #[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)] diff --git a/tket2/src/json/tests.rs b/tket2/src/serialize/pytket/tests.rs similarity index 99% rename from tket2/src/json/tests.rs rename to tket2/src/serialize/pytket/tests.rs index fde3e641..868b5183 100644 --- a/tket2/src/json/tests.rs +++ b/tket2/src/serialize/pytket/tests.rs @@ -11,9 +11,9 @@ use rstest::{fixture, rstest}; use tket_json_rs::circuit_json::{self, SerialCircuit}; use tket_json_rs::optype; +use super::TKETDecode; use crate::circuit::Circuit; use crate::extension::REGISTRY; -use crate::json::TKETDecode; use crate::Tk2Op; const SIMPLE_JSON: &str = r#"{ diff --git a/tket2/tests/badger_termination.rs b/tket2/tests/badger_termination.rs index b79d46f1..57ec12d1 100644 --- a/tket2/tests/badger_termination.rs +++ b/tket2/tests/badger_termination.rs @@ -2,11 +2,9 @@ use rstest::{fixture, rstest}; use tket2::optimiser::badger::BadgerOptions; -use tket2::{ - json::TKETDecode, - optimiser::{BadgerOptimiser, DefaultBadgerOptimiser}, - Circuit, -}; +use tket2::optimiser::{BadgerOptimiser, DefaultBadgerOptimiser}; +use tket2::serialize::TKETDecode; +use tket2::Circuit; use tket_json_rs::circuit_json::SerialCircuit; /// A set of equivalence circuit classes (ECC)