Skip to content

Commit

Permalink
refactor: Move tket2 code to a workspace member (#210)
Browse files Browse the repository at this point in the history
Moves the `tket2` crate code to `./tket2/`, and adds it to the
workspace. This is mostly for cleaning up, as part of the python module
cleanup task.

As a drive-by, makes sure that all the `Cargo.toml`s shale the workspace
metadata.
  • Loading branch information
aborgna-q authored Nov 3, 2023
1 parent 9af1bee commit dc03c31
Show file tree
Hide file tree
Showing 46 changed files with 160 additions and 143 deletions.
78 changes: 11 additions & 67 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,78 +1,22 @@
[package]
name = "tket2"
version = "0.1.0"
edition = "2021"

[lib]
bench = false
name = "tket2"
path = "src/lib.rs"

[profile.release]
lto = "thin"

[dependencies]
lazy_static = "1.4.0"
cgmath = "0.18.0"
num-rational = "0.4"
num-complex = { version = "0.4", optional = true }
tket-json-rs = { workspace = true }
rayon = "1.5"
thiserror = "1.0.28"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
downcast-rs = "1.2.0"
priority-queue = "1.3.0"
smol_str = "0.2.0"
typetag = "0.2.8"
itertools = { workspace = true }
petgraph = { version = "0.6.3", default-features = false }
serde_yaml = "0.9.22"
# portmatching = { version = "0.2.0", optional = true, features = ["serde"]}
portmatching = { optional = true, git = "https://github.com/lmondada/portmatching", rev = "738c91c", features = [
"serde",
] }
derive_more = "0.99.17"
quantinuum-hugr = { workspace = true }
portgraph = { workspace = true, features = ["serde"] }
pyo3 = { workspace = true, optional = true, features = ["multiple-pymethods"] }
strum_macros = "0.25.2"
strum = "0.25.0"
fxhash = "0.2.1"
rmp-serde = { version = "1.1.2", optional = true }
delegate = "0.10.0"
csv = { version = "1.2.2" }
chrono = { version = "0.4.30" }
bytemuck = "1.14.0"
stringreader = "0.1.1"
crossbeam-channel = "0.5.8"
tracing = { workspace = true }

[features]
pyo3 = [
"dep:pyo3",
"tket-json-rs/pyo3",
"portgraph/pyo3",
"quantinuum-hugr/pyo3",
]
portmatching = ["dep:portmatching", "dep:rmp-serde"]

[dev-dependencies]
rstest = "0.18.1"
criterion = { version = "0.5.1", features = ["html_reports"] }
webbrowser = "0.8.10"
urlencoding = "2.1.2"

[[bench]]
name = "bench_main"
harness = false

[workspace]
resolver = "2"
members = ["tket2", "pyrs", "compile-rewriter", "taso-optimiser"]
default-members = ["tket2"]

members = ["pyrs", "compile-rewriter", "taso-optimiser"]
[workspace.package]
version = "0.1.0"
rust-version = "1.70"
edition = "2021"
homepage = "https://github.com/CQCL/tket2"
license-file = "LICENCE"
# authors

[workspace.dependencies]

tket2 = { path = "./tket2" }
quantinuum-hugr = { git = "https://github.com/CQCL-DEV/hugr", rev = "0beb165" }
portgraph = { version = "0.10" }
pyo3 = { version = "0.20" }
Expand Down
9 changes: 6 additions & 3 deletions compile-rewriter/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
[package]
name = "compile-matcher"
version = "0.1.0"
edition = "2021"
edition = { workspace = true }
version = { workspace = true }
rust-version = { workspace = true }
homepage = { workspace = true }
license-file = { workspace = true }

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap = { version = "4.4.2", features = ["derive"] }
tket2 = { path = "../", features = ["portmatching"] }
tket2 = { workspace = true, features = ["portmatching"] }
quantinuum-hugr = { workspace = true }
itertools = { workspace = true }
2 changes: 1 addition & 1 deletion pyrs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ name = "pyrs"
crate-type = ["cdylib"]

[dependencies]
tket2 = { path = "../", features = ["pyo3", "portmatching"] }
tket2 = { workspace = true, features = ["pyo3", "portmatching"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
tket-json-rs = { workspace = true, features = ["pyo3"] }
Expand Down
9 changes: 6 additions & 3 deletions taso-optimiser/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
[package]
name = "taso-optimiser"
version = "0.1.0"
edition = "2021"
edition = { workspace = true }
version = { workspace = true }
rust-version = { workspace = true }
homepage = { workspace = true }
license-file = { workspace = true }

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap = { version = "4.4.2", features = ["derive"] }
serde_json = "1.0"
tket2 = { path = "../", features = ["portmatching"] }
tket2 = { workspace = true, features = ["portmatching"] }
quantinuum-hugr = { workspace = true }
itertools = { workspace = true }
tket-json-rs = { workspace = true }
Expand Down
63 changes: 0 additions & 63 deletions tests/taso_termination.rs

This file was deleted.

68 changes: 68 additions & 0 deletions tket2/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
[package]
name = "tket2"
edition = { workspace = true }
version = { workspace = true }
rust-version = { workspace = true }
homepage = { workspace = true }
license-file = { workspace = true }

[lib]
bench = false
name = "tket2"
path = "src/lib.rs"

[features]
pyo3 = [
"dep:pyo3",
"tket-json-rs/pyo3",
"portgraph/pyo3",
"quantinuum-hugr/pyo3",
]
portmatching = ["dep:portmatching", "dep:rmp-serde"]

[dependencies]
lazy_static = "1.4.0"
cgmath = "0.18.0"
num-rational = "0.4"
num-complex = { version = "0.4", optional = true }
tket-json-rs = { workspace = true }
rayon = "1.5"
thiserror = "1.0.28"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
downcast-rs = "1.2.0"
priority-queue = "1.3.0"
smol_str = "0.2.0"
typetag = "0.2.8"
itertools = { workspace = true }
petgraph = { version = "0.6.3", default-features = false }
serde_yaml = "0.9.22"
# portmatching = { version = "0.2.0", optional = true, features = ["serde"]}
portmatching = { optional = true, git = "https://github.com/lmondada/portmatching", rev = "738c91c", features = [
"serde",
] }
derive_more = "0.99.17"
quantinuum-hugr = { workspace = true }
portgraph = { workspace = true, features = ["serde"] }
pyo3 = { workspace = true, optional = true, features = ["multiple-pymethods"] }
strum_macros = "0.25.2"
strum = "0.25.0"
fxhash = "0.2.1"
rmp-serde = { version = "1.1.2", optional = true }
delegate = "0.10.0"
csv = { version = "1.2.2" }
chrono = { version = "0.4.30" }
bytemuck = "1.14.0"
stringreader = "0.1.1"
crossbeam-channel = "0.5.8"
tracing = { workspace = true }

[dev-dependencies]
rstest = "0.18.1"
criterion = { version = "0.5.1", features = ["html_reports"] }
webbrowser = "0.8.10"
urlencoding = "2.1.2"

[[bench]]
name = "bench_main"
harness = false
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/json/tests.rs → tket2/src/json/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ fn json_roundtrip(#[case] circ_s: &str, #[case] num_commands: usize, #[case] num

#[rstest]
#[cfg_attr(miri, ignore)] // Opening files is not supported in (isolated) miri
#[case::barenco_tof_10("test_files/barenco_tof_10.json")]
#[case::barenco_tof_10("../test_files/barenco_tof_10.json")]
fn json_file_roundtrip(#[case] circ: impl AsRef<std::path::Path>) {
let reader = BufReader::new(std::fs::File::open(circ).unwrap());
let ser: circuit_json::SerialCircuit = serde_json::from_reader(reader).unwrap();
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/optimiser/taso.rs → tket2/src/optimiser/taso.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ mod tests {

#[fixture]
fn taso_opt() -> DefaultTasoOptimiser {
TasoOptimiser::default_with_eccs_json_file("test_files/small_eccs.json").unwrap()
TasoOptimiser::default_with_eccs_json_file("../test_files/small_eccs.json").unwrap()
}

#[rstest]
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ mod tests {
#[cfg_attr(miri, ignore)] // Opening files is not supported in (isolated) miri
fn test_read_rep() {
let rep_map: HashMap<String, Circuit> =
load_representative_set("test_files/h_rz_cxrepresentative_set.json");
load_representative_set("../test_files/h_rz_cxrepresentative_set.json");

for c in rep_map.values().take(1) {
println!("{}", c.dot_string());
Expand All @@ -156,7 +156,7 @@ mod tests {
#[cfg_attr(miri, ignore)] // Opening files is not supported in (isolated) miri
fn test_read_complete() {
let _ecc: HashMap<String, Vec<Circuit>> =
load_ecc_set("test_files/h_rz_cxcomplete_ECC_set.json").unwrap();
load_ecc_set("../test_files/h_rz_cxcomplete_ECC_set.json").unwrap();

// ecc.values()
// .flatten()
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ mod tests {
fn ecc_rewriter_from_file() {
// In this example, all circuits are valid patterns, thus
// PatternID == TargetID.
let test_file = "test_files/small_eccs.json";
let test_file = "../test_files/small_eccs.json";
let rewriter = ECCRewriter::try_from_eccs_json_file(test_file).unwrap();
assert_eq!(rewriter.rewrite_rules.len(), rewriter.matcher.n_patterns());
assert_eq!(rewriter.targets.len(), 5 * 4 + 5 * 3);
Expand Down Expand Up @@ -370,7 +370,7 @@ mod tests {
/// obtain convex patterns
#[test]
fn ecc_rewriter_empty_params() {
let test_file = "test_files/cx_cx_eccs.json";
let test_file = "../test_files/cx_cx_eccs.json";
let rewriter = ECCRewriter::try_from_eccs_json_file(test_file).unwrap();

let cx_cx = cx_cx();
Expand Down
File renamed without changes.
File renamed without changes.
62 changes: 62 additions & 0 deletions tket2/tests/taso_termination.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#![cfg(feature = "portmatching")]

use hugr::Hugr;
use rstest::{fixture, rstest};
use tket2::{
json::TKETDecode,
optimiser::{DefaultTasoOptimiser, TasoOptimiser},
Circuit,
};
use tket_json_rs::circuit_json::SerialCircuit;

/// A set of equivalence circuit classes (ECC)
///
/// This is the complete set of ECCs for 2-qubit circuits with up to
/// 4 gates, using the NAM gateset (CX, Rz, H).
#[fixture]
fn nam_4_2() -> DefaultTasoOptimiser {
TasoOptimiser::default_with_eccs_json_file("../test_files/Nam_4_2_complete_ECC_set.json")
.unwrap()
}

/// The following circuit
/// ┌──────────┐ ┌───────────┐
///q_0: ──■──┤ Rz(π/10) ├──■─────────────────────────■────■──┤ Rz(-π/10) ├
/// │ └──────────┘┌─┴─┐┌───┐┌─────────┐┌───┐┌─┴─┐ │ └───────────┘
///q_1: ──┼──────────────┤ X ├┤ H ├┤ Rz(π/5) ├┤ H ├┤ X ├──┼───────────────
/// ┌─┴─┐ └───┘└───┘└─────────┘└───┘└───┘┌─┴─┐
///q_2: ┤ X ├───────────────────────────────────────────┤ X ├─────────────
/// └───┘ └───┘
#[fixture]
fn simple_circ() -> Hugr {
// The TK1 json of the circuit
let json = r#"{
"bits": [],
"commands": [
{"args": [["q", [0]], ["q", [2]]], "op": {"type": "CX"}},
{"args": [["q", [0]]], "op": {"params": ["0.1"], "type": "Rz"}},
{"args": [["q", [0]], ["q", [1]]], "op": {"type": "CX"}},
{"args": [["q", [1]]], "op": {"type": "H"}},
{"args": [["q", [1]]], "op": {"params": ["0.2"], "type": "Rz"}},
{"args": [["q", [1]]], "op": {"type": "H"}},
{"args": [["q", [0]], ["q", [1]]], "op": {"type": "CX"}},
{"args": [["q", [0]], ["q", [2]]], "op": {"type": "CX"}},
{"args": [["q", [0]]], "op": {"params": ["-0.1"], "type": "Rz"}}],
"created_qubits": [],
"discarded_qubits": [],
"implicit_permutation": [
[["q", [0]], ["q", [0]]], [["q", [1]], ["q", [1]]], [["q", [2]], ["q", [2]]]
],
"phase": "0.0",
"qubits": [["q", [0]], ["q", [1]], ["q", [2]]]
}"#;
let ser: SerialCircuit = serde_json::from_str(json).unwrap();
ser.decode().unwrap()
}

#[rstest]
//#[ignore = "Takes 200ms"]
fn taso_termination(simple_circ: Hugr, nam_4_2: DefaultTasoOptimiser) {
let opt_circ = nam_4_2.optimise(&simple_circ, None, 1.try_into().unwrap(), false, 10);
assert_eq!(opt_circ.commands().count(), 11);
}

0 comments on commit dc03c31

Please sign in to comment.