Skip to content

Commit

Permalink
chore: Small code cleanups (#158)
Browse files Browse the repository at this point in the history
- Remove duplicated tk1 writer fn
- Better .dot debug API
  • Loading branch information
aborgna-q authored Oct 4, 2023
1 parent 8e6692c commit b699553
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
9 changes: 6 additions & 3 deletions src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,21 +129,24 @@ pub fn load_tk1_json_str(json: &str) -> Result<Hugr, TK1ConvertError> {
}

/// Save a circuit to file in TK1 JSON format.
pub fn save_tk1_json_file(path: impl AsRef<Path>, circ: &Hugr) -> Result<(), TK1ConvertError> {
pub fn save_tk1_json_file(
circ: &impl Circuit,
path: impl AsRef<Path>,
) -> Result<(), TK1ConvertError> {
let file = fs::File::create(path)?;
let writer = io::BufWriter::new(file);
save_tk1_json_writer(circ, writer)
}

/// Save a circuit in TK1 JSON format to a writer.
pub fn save_tk1_json_writer(circ: &Hugr, w: impl io::Write) -> Result<(), TK1ConvertError> {
pub fn save_tk1_json_writer(circ: &impl Circuit, w: impl io::Write) -> Result<(), TK1ConvertError> {
let serial_circ = SerialCircuit::encode(circ)?;
serde_json::to_writer(w, &serial_circ)?;
Ok(())
}

/// Save a circuit in TK1 JSON format to a String.
pub fn save_tk1_json_str(circ: &Hugr) -> Result<String, TK1ConvertError> {
pub fn save_tk1_json_str(circ: &impl Circuit) -> Result<String, TK1ConvertError> {
let mut buf = io::BufWriter::new(Vec::new());
save_tk1_json_writer(circ, &mut buf)?;
let bytes = buf.into_inner().unwrap();
Expand Down
17 changes: 15 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,26 @@ pub(crate) fn build_simple_circuit(
#[allow(dead_code)]
#[cfg(test)]
pub(crate) mod test {
#[allow(unused_imports)]
use hugr::HugrView;

/// Open a browser page to render a dot string graph.
///
/// This can be used directly on the output of `Hugr::dot_string`
///
/// Only for use in local testing. Will fail to compile on CI.
#[cfg(not(ci_run))]
pub(crate) fn viz_dotstr(dotstr: &str) {
pub(crate) fn viz_dotstr(dotstr: impl AsRef<str>) {
let mut base: String = "https://dreampuf.github.io/GraphvizOnline/#".into();
base.push_str(&urlencoding::encode(dotstr));
base.push_str(&urlencoding::encode(dotstr.as_ref()));
webbrowser::open(&base).unwrap();
}

/// Open a browser page to render a HugrView's dot string graph.
///
/// Only for use in local testing. Will fail to compile on CI.
#[cfg(not(ci_run))]
pub(crate) fn viz_hugr(hugr: &impl HugrView) {
viz_dotstr(hugr.dot_string());
}
}
14 changes: 2 additions & 12 deletions taso-optimiser/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ use std::path::PathBuf;
use std::process::exit;

use clap::Parser;
use hugr::Hugr;
use tket2::json::{load_tk1_json_file, TKETDecode};
use tket2::json::{load_tk1_json_file, save_tk1_json_file};
use tket2::optimiser::taso::log::TasoLogger;
use tket2::optimiser::TasoOptimiser;
use tket_json_rs::circuit_json::SerialCircuit;

#[cfg(feature = "peak_alloc")]
use peak_alloc::PeakAlloc;
Expand Down Expand Up @@ -82,14 +80,6 @@ struct CmdLineArgs {
n_threads: Option<NonZeroUsize>,
}

fn save_tk1_json_file(path: impl AsRef<Path>, circ: &Hugr) -> Result<(), std::io::Error> {
let file = File::create(path)?;
let writer = BufWriter::new(file);
let serial_circ = SerialCircuit::encode(circ).unwrap();
serde_json::to_writer_pretty(writer, &serial_circ)?;
Ok(())
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
let opts = CmdLineArgs::parse();

Expand Down Expand Up @@ -129,7 +119,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let opt_circ = optimiser.optimise_with_log(&circ, taso_logger, opts.timeout, n_threads);

println!("Saving result");
save_tk1_json_file(output_path, &opt_circ)?;
save_tk1_json_file(&opt_circ, output_path)?;

#[cfg(feature = "peak_alloc")]
println!("Peak memory usage: {} GB", PEAK_ALLOC.peak_usage_as_gb());
Expand Down

0 comments on commit b699553

Please sign in to comment.