-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
142 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use std::fs; | ||
use std::path::Path; | ||
use std::process::exit; | ||
use std::time::Instant; | ||
|
||
use clap::Parser; | ||
|
||
use tket2::rewrite::ECCRewriter; | ||
|
||
/// Program to precompile patterns from files into a PatternMatcher stored as binary file. | ||
#[derive(Parser, Debug)] | ||
#[clap(version = "1.0", long_about = None)] | ||
#[clap( | ||
about = "Precompiles ECC sets into a TKET2 Rewriter. The resulting binary files can be loaded into TKET2 for circuit optimisation." | ||
)] | ||
struct CmdLineArgs { | ||
// TODO: Differentiate between TK1 input and ECC input | ||
/// Name of input file/folder | ||
#[arg( | ||
short, | ||
long, | ||
value_name = "FILE", | ||
help = "Sets the input file to use. It must be a JSON file of ECC sets in the Quartz format." | ||
)] | ||
input: String, | ||
/// Name of output file/folder | ||
#[arg( | ||
short, | ||
long, | ||
value_name = "FILE", | ||
default_value = ".", | ||
help = "Sets the output file or folder. Defaults to \"matcher.rwr\" if no file name is provided. The extension of the file name will always be set or amended to be `.rwr`." | ||
)] | ||
output: String, | ||
} | ||
|
||
fn main() { | ||
let opts = CmdLineArgs::parse(); | ||
|
||
let input_path = Path::new(&opts.input); | ||
let output_path = Path::new(&opts.output); | ||
|
||
if !input_path.is_file() || input_path.extension().unwrap() != "json" { | ||
panic!("Input must be a JSON file"); | ||
}; | ||
let start_time = Instant::now(); | ||
println!("Compiling rewriter..."); | ||
let Ok(rewriter) = ECCRewriter::try_from_eccs_json_file(input_path) else { | ||
eprintln!( | ||
"Unable to load ECC file {:?}. Is it a JSON file of Quartz-generated ECCs?", | ||
input_path | ||
); | ||
exit(1); | ||
}; | ||
println!("Saving to file..."); | ||
let output_file = if output_path.is_dir() { | ||
output_path.join("matcher.rwr") | ||
} else { | ||
output_path.to_path_buf() | ||
}; | ||
let output_file = rewriter.save_binary(output_file.to_str().unwrap()).unwrap(); | ||
println!("Written rewriter to {:?}", output_file); | ||
|
||
// Print the file size of output_file in megabytes | ||
if let Ok(metadata) = fs::metadata(&output_file) { | ||
let file_size = metadata.len() as f64 / (1024.0 * 1024.0); | ||
println!("File size: {:.2} MB", file_size); | ||
} | ||
let elapsed = start_time.elapsed(); | ||
println!( | ||
"Done in {}.{:03} seconds", | ||
elapsed.as_secs(), | ||
elapsed.subsec_millis() | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters