Skip to content

Commit

Permalink
chore: Add script to recompile the ECC sets (#514)
Browse files Browse the repository at this point in the history
- Moves the loose ECC files into `test_files/eccs`
- Adds a `just recompile-eccs` command / script to regenerate the `.rwr`
files.

Note that the compilation is not deterministic, so we should avoid
running it unless needed to avoid bloating the git history.

---------

Co-authored-by: Douglas Wilson <douglas.wilson@quantinuum.com>
  • Loading branch information
aborgna-q and doug-q committed Jul 29, 2024
1 parent 54564fc commit e272cf7
Show file tree
Hide file tree
Showing 13 changed files with 32,426 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

# Compiled ECC files
*.rwr binary
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ missing_docs = "warn"

[workspace.dependencies]

# Make sure to run `just recompile-eccs` if the hugr serialisation format changes.
hugr = "0.9.1"
hugr-cli = "0.2.1"
hugr-core = "0.6.1"
Expand Down
4 changes: 4 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ coverage language="[rust|python]": (_run_lang language \
shell:
poetry shell

# Runs `compile-rewriter` on the ECCs in `test_files/eccs`
recompile-eccs:
scripts/compile-test-eccs.sh


# Runs a rust and a python command, depending on the `language` variable.
#
Expand Down
33 changes: 33 additions & 0 deletions scripts/compile-test-eccs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env bash

# This script calls `compile-rewriter` to recompile all ECC sets in the
# `test_files/eccs/` directory.
#
# Note that the output is not deterministic, so the resulting `.rwr` will always
# be different even if the input and the compiler are the same.
set -euo pipefail

# Get the directory of this script
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
ECCS_DIR="$DIR/../test_files/eccs"

# Command to call compile-rewriter with cargo
COMPILE_REWRITER="cargo run --release -p compile-rewriter --"

# Compile all json files into `.rwr`
for ecc in $ECCS_DIR/*.json; do
echo "-------- Compiling $ecc"
$COMPILE_REWRITER -i "$ecc" -o "${ecc%.json}.rwr"
done

# Additional hard-coded step:
# The python package contains a copy of the nam_6_3 ecc,
# which must be manually copied.
PY_ECCS_DIR="$DIR/../tket2-py/tket2/data"
nam_6_3="$ECCS_DIR/nam_6_3.rwr"
PY_NAM_6_3="$PY_ECCS_DIR/nam_6_3.rwr"

echo "Copying $nam_6_3 to $PY_NAM_6_3"
cp -f "$nam_6_3" "$PY_NAM_6_3"

echo "Done!"
File renamed without changes.
File renamed without changes.
32,378 changes: 32,378 additions & 0 deletions test_files/eccs/nam_6_3.json

Large diffs are not rendered by default.

File renamed without changes.
File renamed without changes.
File renamed without changes.
9 changes: 5 additions & 4 deletions tket2/src/optimiser/badger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -607,13 +607,13 @@ mod tests {
/// A badger optimiser using a reduced set of rewrite rules.
#[fixture]
fn badger_opt_json() -> DefaultBadgerOptimiser {
BadgerOptimiser::default_with_eccs_json_file("../test_files/small_eccs.json").unwrap()
BadgerOptimiser::default_with_eccs_json_file("../test_files/eccs/small_eccs.json").unwrap()
}

/// A badger optimiser using a reduced set of rewrite rules.
#[fixture]
fn badger_opt_compiled() -> DefaultBadgerOptimiser {
BadgerOptimiser::default_with_rewriter_binary("../test_files/small_eccs.rwr").unwrap()
BadgerOptimiser::default_with_rewriter_binary("../test_files/eccs/small_eccs.rwr").unwrap()
}

/// A badger optimiser using the complete nam_6_3 rewrite set.
Expand All @@ -622,7 +622,7 @@ mod tests {
/// Use [`badger_opt`] if possible.
#[fixture]
fn badger_opt_full() -> DefaultBadgerOptimiser {
BadgerOptimiser::default_with_rewriter_binary("../test_files/nam_6_3.rwr").unwrap()
BadgerOptimiser::default_with_rewriter_binary("../test_files/eccs/nam_6_3.rwr").unwrap()
}

#[rstest]
Expand Down Expand Up @@ -697,7 +697,8 @@ mod tests {

#[test]
fn load_precompiled_bin() {
let opt = BadgerOptimiser::default_with_rewriter_binary("../test_files/small_eccs.rwr");
let opt =
BadgerOptimiser::default_with_rewriter_binary("../test_files/eccs/small_eccs.rwr");
opt.unwrap();
}
}
2 changes: 1 addition & 1 deletion tket2/src/rewrite/ecc_rewriter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,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/eccs/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
3 changes: 1 addition & 2 deletions tket2/tests/badger_termination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ use tket_json_rs::circuit_json::SerialCircuit;
/// 4 gates, using the NAM gateset (CX, Rz, H).
#[fixture]
fn nam_4_2() -> DefaultBadgerOptimiser {
BadgerOptimiser::default_with_eccs_json_file("../test_files/Nam_4_2_complete_ECC_set.json")
.unwrap()
BadgerOptimiser::default_with_eccs_json_file("../test_files/eccs/nam_4_2.json").unwrap()
}

/// The following circuit
Expand Down

0 comments on commit e272cf7

Please sign in to comment.