From 61e7535dc909faf72d10967001725dc3a52bde08 Mon Sep 17 00:00:00 2001 From: doug-q <141026920+doug-q@users.noreply.github.com> Date: Fri, 14 Jun 2024 07:27:39 +0100 Subject: [PATCH] feat!: init tket2-hseries (#368) BREAKING CHANGE: require `hugr-0.5.0` --- Cargo.lock | 80 +++++++++++++++++++++++++++++++++++++- Cargo.toml | 5 ++- release-plz.toml | 9 +++++ tket2-hseries/CHANGELOG.md | 0 tket2-hseries/Cargo.toml | 33 ++++++++++++++++ tket2-hseries/README.md | 34 ++++++++++++++++ tket2-hseries/src/cli.rs | 50 ++++++++++++++++++++++++ tket2-hseries/src/lib.rs | 14 +++++++ tket2-hseries/src/main.rs | 16 ++++++++ tket2-py/Cargo.toml | 2 +- 10 files changed, 238 insertions(+), 5 deletions(-) create mode 100644 tket2-hseries/CHANGELOG.md 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 2d33ad2e..e30e2800 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.6" @@ -720,10 +739,55 @@ version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a20246e5f1a0aae160b80b71bc4d5c1dcccc5605dbe6458ac6f9af73137339c3" dependencies = [ - "hugr-core", + "hugr-core 0.2.0", "hugr-passes", ] +[[package]] +name = "hugr-cli" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3d973149618f24c966ea90cfaf5955c184b670d38b18f3114a66a20dc5b712b" +dependencies = [ + "clap", + "clap-stdin", + "clap-verbosity-flag", + "hugr-core 0.1.0", + "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", + "context-iterators", + "delegate 0.12.0", + "derive_more", + "downcast-rs", + "enum_dispatch", + "html-escape", + "itertools 0.13.0", + "lazy_static", + "num-rational", + "paste", + "petgraph", + "portgraph 0.12.1", + "regex", + "serde", + "serde_json", + "serde_yaml", + "smol_str", + "strum", + "strum_macros", + "thiserror", + "typetag", +] + [[package]] name = "hugr-core" version = "0.2.0" @@ -761,7 +825,7 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7522d3a5299a49ee453f6d0e3c3a6e8e7ed08a38cbd11e1d19163b97098ce9be" dependencies = [ - "hugr-core", + "hugr-core 0.2.0", "itertools 0.13.0", "lazy_static", "paste", @@ -1799,6 +1863,18 @@ dependencies = [ "webbrowser", ] +[[package]] +name = "tket2-hseries" +version = "0.1.0-alpha.1" +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 210cb6f3..910a3512 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" @@ -20,6 +20,7 @@ missing_docs = "warn" tket2 = { path = "./tket2" } hugr = "0.5.1" +hugr-cli = "0.1.1" portgraph = "0.12" pyo3 = "0.21.2" itertools = "0.13.0" diff --git a/release-plz.toml b/release-plz.toml index f565a37b..f4596042 100644 --- a/release-plz.toml +++ b/release-plz.toml @@ -31,3 +31,12 @@ commit_parsers = [ [[package]] name = "tket2" release = true + +[[package]] +name = "tket2-hseries" +release = true + +# Disabled until the first version is manually published +publish = false +git_tag_enable = false +git_release_enable = false diff --git a/tket2-hseries/CHANGELOG.md b/tket2-hseries/CHANGELOG.md new file mode 100644 index 00000000..e69de29b diff --git a/tket2-hseries/Cargo.toml b/tket2-hseries/Cargo.toml new file mode 100644 index 00000000..3cc08261 --- /dev/null +++ b/tket2-hseries/Cargo.toml @@ -0,0 +1,33 @@ +[package] +name = "tket2-hseries" +version = "0.1.0-alpha.1" +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 = { path = "../tket2", version = "0.1.0-alpha.1" } +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 00000000..e195e8e4 --- /dev/null +++ b/tket2-hseries/README.md @@ -0,0 +1,34 @@ +# tket2-hseries + +![msrv][] + +A TKET2 tool for preparing and validating `Hugr`s for compilation targeting +Quantinuum H-series quantum computers. + +## Usage + +Install using `cargo`: + +```bash +cargo install tket2-hseries +``` + +This will install the `tket2-hseries` binary. + +## Recent Changes + +See [CHANGELOG][] for a list of changes. The minimum supported rust +version will only change on major releases. + +## Development + +See [DEVELOPMENT.md][] for instructions on setting up the development environment. + +## License + +This project is licensed under Apache License, Version 2.0 ([LICENSE][] or http://www.apache.org/licenses/LICENSE-2.0). + + [msrv]: https://img.shields.io/badge/rust-1.75.0%2B-blue.svg + [LICENSE]: https://github.com/CQCL/tket2/blob/main/LICENCE + [CHANGELOG]: https://github.com/CQCL/tket2/blob/main/tket2-hseries/CHANGELOG.mdd + [DEVELOPMENT.md]: https://github.com/CQCL/tket2/blob/main/DEVELOPMENT.md diff --git a/tket2-hseries/src/cli.rs b/tket2-hseries/src/cli.rs new file mode 100644 index 00000000..30b60f19 --- /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 00000000..36aa948e --- /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 ingress into an H-series. +/// +/// 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 00000000..9d9cb2cb --- /dev/null +++ b/tket2-hseries/src/main.rs @@ -0,0 +1,16 @@ +//! A command line interface to tket2-hseries +use hugr_cli::{Level, Parser as _}; +use tket2_hseries::cli; + +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); + } +} diff --git a/tket2-py/Cargo.toml b/tket2-py/Cargo.toml index ac2dff56..f268d321 100644 --- a/tket2-py/Cargo.toml +++ b/tket2-py/Cargo.toml @@ -19,7 +19,7 @@ test = false bench = false [dependencies] -tket2 = { workspace = true, features = ["portmatching"] } +tket2 = { path = "../tket2", version = "0.1.0-alpha.1", features = ["portmatching"] } serde = { workspace = true, features = ["derive"] } serde_json = { workspace = true } tket-json-rs = { workspace = true, features = ["pyo3"] }