From 8edcbe343cd93a4a37515c17f3835380487c0b6c Mon Sep 17 00:00:00 2001 From: Douglas Wilson Date: Wed, 29 May 2024 08:39:00 +0100 Subject: [PATCH] feat: init tket2-ngrte --- Cargo.lock | 83 +++++++++++++++++++++++++++++++++------ Cargo.toml | 7 ++-- tket2-hseries/Cargo.toml | 33 ++++++++++++++++ tket2-hseries/README.md | 4 ++ tket2-hseries/src/cli.rs | 50 +++++++++++++++++++++++ tket2-hseries/src/lib.rs | 14 +++++++ tket2-hseries/src/main.rs | 16 ++++++++ 7 files changed, 192 insertions(+), 15 deletions(-) create mode 100644 tket2-hseries/Cargo.toml create mode 100644 tket2-hseries/README.md create mode 100644 tket2-hseries/src/cli.rs create mode 100644 tket2-hseries/src/lib.rs create mode 100644 tket2-hseries/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index 7d30e502b..b4483e36f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -257,6 +257,25 @@ dependencies = [ "clap_derive", ] +[[package]] +name = "clap-stdin" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc126d12a0930c94c3548581294d5f19360ac02e408600b4d7619d7234e8b505" +dependencies = [ + "thiserror", +] + +[[package]] +name = "clap-verbosity-flag" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb9b20c0dd58e4c2e991c8d203bbeb76c11304d1011659686b5b644bc29aa478" +dependencies = [ + "clap", + "log", +] + [[package]] name = "clap_builder" version = "4.5.2" @@ -707,9 +726,33 @@ dependencies = [ [[package]] name = "hugr" -version = "0.4.0" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ebf436d9d4d0239fcb511a1f7d745548bbdc6316baf50bb2e944f294542ea24" +dependencies = [ + "hugr-core", + "hugr-passes", +] + +[[package]] +name = "hugr-cli" +version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e8e15eaffd64f1cceac13429e5ceaf20017691e647cc56b8b7c53d73ad9f8714" +checksum = "bfb044591f0f794712a8a390859bc1d4ae181062ada7f1ed0fc9f7fdb835fe10" +dependencies = [ + "clap", + "clap-stdin", + "clap-verbosity-flag", + "hugr-core", + "serde_json", + "thiserror", +] + +[[package]] +name = "hugr-core" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f2b8cfdccedf45a563e526a4a2c7443025ece0e2518bca1c3f227318607de44" dependencies = [ "bitvec", "cgmath", @@ -719,7 +762,7 @@ dependencies = [ "downcast-rs", "enum_dispatch", "html-escape", - "itertools 0.12.1", + "itertools 0.13.0", "lazy_static", "num-rational", "paste", @@ -736,6 +779,19 @@ dependencies = [ "typetag", ] +[[package]] +name = "hugr-passes" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08c1ae428e41786736cc92cbc3ae11ad39f178591d36a9ac3d1c8990e1a9bf76" +dependencies = [ + "hugr-core", + "itertools 0.13.0", + "lazy_static", + "paste", + "thiserror", +] + [[package]] name = "iana-time-zone" version = "0.1.60" @@ -817,15 +873,6 @@ dependencies = [ "either", ] -[[package]] -name = "itertools" -version = "0.12.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ba291022dbbd398a455acf126c1e341954079855bc60dfdda641363bd6922569" -dependencies = [ - "either", -] - [[package]] name = "itertools" version = "0.13.0" @@ -1776,6 +1823,18 @@ dependencies = [ "webbrowser", ] +[[package]] +name = "tket2-hseries" +version = "0.1.0" +dependencies = [ + "clap", + "hugr", + "hugr-cli", + "lazy_static", + "serde_json", + "tket2", +] + [[package]] name = "tket2-py" version = "0.0.0" diff --git a/Cargo.toml b/Cargo.toml index f44a9778a..c2cdb9315 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,8 +3,8 @@ lto = "thin" [workspace] resolver = "2" -members = ["tket2", "tket2-py", "compile-rewriter", "badger-optimiser"] -default-members = ["tket2"] +members = ["tket2", "tket2-py", "compile-rewriter", "badger-optimiser", "tket2-hseries"] +default-members = ["tket2", "tket2-hseries"] [workspace.package] rust-version = "1.75" @@ -19,7 +19,8 @@ missing_docs = "warn" [workspace.dependencies] tket2 = { path = "./tket2" } -hugr = "0.4.0" +hugr = "0.5.0" +hugr-cli = "0.1.0" portgraph = "0.12" pyo3 = "0.21.2" itertools = "0.13.0" diff --git a/tket2-hseries/Cargo.toml b/tket2-hseries/Cargo.toml new file mode 100644 index 000000000..6ea1d373d --- /dev/null +++ b/tket2-hseries/Cargo.toml @@ -0,0 +1,33 @@ +[package] +name = "tket2-hseries" +version = "0.1.0" +edition.workspace = true +rust-version.workspace = true + +license.workspace = true +readme = "README.md" +documentation = "https://docs.rs/tket2-hseries" +homepage.workspace = true +repository.workspace = true +description = "TKET2 tool for preparing and validating `Hugr`s for compilation targeting Quantinuum H-series quantum computers" +keywords = ["Quantum", "Quantinuum"] +categories = ["compilers"] + +[features] +default = ["cli"] +cli = ["dep:hugr-cli", "dep:clap"] + +[dependencies] +hugr.workspace = true +hugr-cli = { workspace = true, optional = true } +clap = { workspace = true, optional = true, features = ["derive"] } +tket2.workspace = true +serde_json.workspace = true +lazy_static.workspace = true + +[lints] +workspace = true + +[[bin]] +name = "tket2-hseries" +required-features = ["cli"] diff --git a/tket2-hseries/README.md b/tket2-hseries/README.md new file mode 100644 index 000000000..82c79f2f0 --- /dev/null +++ b/tket2-hseries/README.md @@ -0,0 +1,4 @@ +# tket2-hseries + +A TKET2 tool for preparing and validating `Hugr`s for compilation targeting +Quantinuum H-series quantum computers. diff --git a/tket2-hseries/src/cli.rs b/tket2-hseries/src/cli.rs new file mode 100644 index 000000000..30b60f195 --- /dev/null +++ b/tket2-hseries/src/cli.rs @@ -0,0 +1,50 @@ +//! Provides a command line interface to tket2-hseries +use clap::Parser; +use hugr::std_extensions::arithmetic::{ + conversions::EXTENSION as CONVERSIONS_EXTENSION, float_ops::EXTENSION as FLOAT_OPS_EXTENSION, + float_types::EXTENSION as FLOAT_TYPES_EXTENSION, int_ops::EXTENSION as INT_OPS_EXTENSION, + int_types::EXTENSION as INT_TYPES_EXTENSION, +}; +use hugr::std_extensions::logic::EXTENSION as LOGICS_EXTENSION; + +use hugr::extension::{ExtensionRegistry, PRELUDE}; +use lazy_static::lazy_static; + +lazy_static! { + /// A registry suitable for passing to `run`. Use this unless you have a + /// good reason not to do so. + pub static ref REGISTRY: ExtensionRegistry = ExtensionRegistry::try_new([ + PRELUDE.to_owned(), + INT_OPS_EXTENSION.to_owned(), + INT_TYPES_EXTENSION.to_owned(), + CONVERSIONS_EXTENSION.to_owned(), + FLOAT_OPS_EXTENSION.to_owned(), + FLOAT_TYPES_EXTENSION.to_owned(), + LOGICS_EXTENSION.to_owned(), + ]) + .unwrap(); +} + +/// Arguments for `run`. +#[derive(Parser, Debug)] +#[command(version, about)] +pub struct CmdLineArgs { + #[command(flatten)] + base: hugr_cli::CmdLineArgs, +} + +impl CmdLineArgs { + /// Run the ngrte preparation and validation workflow with the given + /// registry. + pub fn run(&self, registry: &ExtensionRegistry) -> Result<(), hugr_cli::CliError> { + let mut hugr = self.base.run(registry)?; + crate::prepare_ngrte(&mut hugr).unwrap(); + serde_json::to_writer_pretty(std::io::stdout(), &hugr)?; + Ok(()) + } + + /// Test whether a `level` message should be output. + pub fn verbosity(&self, level: hugr_cli::Level) -> bool { + self.base.verbosity(level) + } +} diff --git a/tket2-hseries/src/lib.rs b/tket2-hseries/src/lib.rs new file mode 100644 index 000000000..03612aaa4 --- /dev/null +++ b/tket2-hseries/src/lib.rs @@ -0,0 +1,14 @@ +//! Provides a preparation and validation workflow for Hugrs targeting +//! Quantinuum H-series quantum computers. + +use hugr::Hugr; + +#[cfg(feature = "cli")] +pub mod cli; + +/// Modify a [Hugr] into a form that is acceptable for input into ngrte. +/// +/// Returns an error if this cannot be done. +pub fn prepare_ngrte(#[allow(unused)] hugr: &mut Hugr) -> Result<(), Box> { + Ok(()) +} diff --git a/tket2-hseries/src/main.rs b/tket2-hseries/src/main.rs new file mode 100644 index 000000000..a27ccb6f8 --- /dev/null +++ b/tket2-hseries/src/main.rs @@ -0,0 +1,16 @@ +//! A command line interface to tket2-hseries +use tket2_hseries::cli; +use hugr_cli::{Parser as _, Level}; + +fn main() { + let opts = cli::CmdLineArgs::parse(); + let registry = &cli::REGISTRY; + + // validate with all std extensions + if let Err(e) = opts.run(registry) { + if opts.verbosity(Level::Error) { + eprintln!("{}", e); + } + std::process::exit(1); + } +}