Skip to content

Commit

Permalink
feat!: Compress binary ECCs using zlib (#498)
Browse files Browse the repository at this point in the history
Breaking change for the compiled `.rwr` ECC sets. The format is now
compressed using `zstd`.

Comparative figures for the `nam_6_3.rwr` included in the python
package:

- File size reduced from `69MB` to `4.4MB`.
- Load time on my machine reduced from `~560ms` to `~420ms`.
- (offline) ECC compilation time increased from `2.4s` to `3s`.
This is using the highest compression rate. Faster encodings produce
files of around `5MB`.

Closes #488

BREAKING CHANGE: `.rwr` ECC files generated with older versions are no
longer supported. Please recompile them, or compress the file with
`zstd`.
  • Loading branch information
aborgna-q authored Jul 22, 2024
1 parent 8c7c338 commit d9a713c
Show file tree
Hide file tree
Showing 14 changed files with 135 additions and 41 deletions.
98 changes: 73 additions & 25 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,4 @@ typetag = "0.2.8"
urlencoding = "2.1.2"
webbrowser = "1.0.0"
cool_asserts = "2.0.3"
zstd = "0.13.2"
6 changes: 5 additions & 1 deletion badger-optimiser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ publish = false
[dependencies]
clap = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
tket2 = { path = "../tket2", features = ["portmatching", "rewrite-tracing"] }
tket2 = { path = "../tket2", features = [
"portmatching",
"rewrite-tracing",
"binary-eccs",
] }
hugr = { workspace = true }
itertools = { workspace = true }
tket-json-rs = { workspace = true }
Expand Down
6 changes: 5 additions & 1 deletion badger-optimiser/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,15 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
circ.enable_rewrite_tracing();
}

println!("Loading optimiser...");
print!("Loading optimiser...");
let load_ecc_start = std::time::Instant::now();
let Ok(optimiser) = load_optimiser(ecc_path) else {
println!();
eprintln!("Unable to load ECC file {ecc_path:?}. Is it a JSON file of Quartz-generated ECCs? Or a pre-compiled `.rwr` ECC set?");
exit(1);
};
println!(" done in {:?}", load_ecc_start.elapsed());

println!(
"Using {n_threads} threads. Queue size is {}.",
opts.queue_size
Expand Down
2 changes: 1 addition & 1 deletion compile-rewriter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ publish = false

[dependencies]
clap = { workspace = true, features = ["derive"] }
tket2 = { path = "../tket2", features = ["portmatching"] }
tket2 = { path = "../tket2", features = ["portmatching", "binary-eccs"] }
hugr = { workspace = true }
itertools = { workspace = true }
5 changes: 4 additions & 1 deletion compile-rewriter/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,16 @@ fn main() {
);
exit(1);
};
println!("Saving to file...");
print!("Saving to file...");
let output_file = if output_path.is_dir() {
output_path.join("matcher.rwr")
} else {
output_path.to_path_buf()
};
let write_time = Instant::now();
let output_file = rewriter.save_binary(output_file).unwrap();
println!(" done in {:?}", write_time.elapsed());

println!("Written rewriter to {:?}", output_file);

// Print the file size of output_file in megabytes
Expand Down
Binary file added test_files/nam_4_2.rwr
Binary file not shown.
Binary file modified test_files/nam_6_3.rwr
Binary file not shown.
Binary file modified test_files/small_eccs.rwr
Binary file not shown.
1 change: 1 addition & 0 deletions tket2-py/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ bench = false
[dependencies]
tket2 = { path = "../tket2", version = "0.1.0-alpha.1", features = [
"portmatching",
"binary-eccs",
] }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
Expand Down
Binary file modified tket2-py/tket2/data/nam_6_3.rwr
Binary file not shown.
6 changes: 5 additions & 1 deletion tket2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ portmatching = ["dep:portmatching", "dep:rmp-serde"]
# Stores a trace of the applied rewrites
rewrite-tracing = []

default = []
# Support compressed binary encoded ECC files
binary-eccs = ["dep:zstd"]

default = ["binary-eccs"]

[dependencies]
lazy_static = { workspace = true }
Expand Down Expand Up @@ -62,6 +65,7 @@ chrono = { workspace = true }
bytemuck = { workspace = true }
crossbeam-channel = { workspace = true }
tracing = { workspace = true }
zstd = { workspace = true, optional = true }

[dev-dependencies]
rstest = { workspace = true }
Expand Down
1 change: 1 addition & 0 deletions tket2/src/optimiser/badger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@ mod badger_default {
}

/// A sane default optimiser using a precompiled binary rewriter.
#[cfg(feature = "binary-eccs")]
pub fn default_with_rewriter_binary(
rewriter_path: impl AsRef<Path>,
) -> Result<Self, RewriterSerialisationError> {
Expand Down
Loading

0 comments on commit d9a713c

Please sign in to comment.