Skip to content

Commit

Permalink
Remove duplicated best circuit output
Browse files Browse the repository at this point in the history
  • Loading branch information
aborgna-q committed Sep 22, 2023
1 parent 8028140 commit 0bd3a72
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 30 deletions.
8 changes: 2 additions & 6 deletions src/optimiser/taso.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,7 @@ where
}
}

logger.log_processing_end(circ_cnt, false, timeout_flag);
logger.log_final(&best_circ, best_circ_cost);

logger.log_processing_end(circ_cnt, best_circ_cost, false, timeout_flag);
best_circ
}

Expand Down Expand Up @@ -281,14 +279,12 @@ where
}
}

logger.log_processing_end(circ_cnt, true, timeout_flag);
logger.log_processing_end(circ_cnt, best_circ_cost, true, timeout_flag);

// Drop the channel so the threads know to stop.
drop(tx_work);
joins.into_iter().for_each(|j| j.join().unwrap());

logger.log_final(&best_circ, best_circ_cost);

best_circ
}
}
Expand Down
31 changes: 9 additions & 22 deletions src/optimiser/taso/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@
use std::io;

use hugr::Hugr;

use crate::json::save_tk1_json_writer;

/// Logging configuration for the TASO optimiser.
#[derive(Default)]
pub struct TasoLogger<'w> {
final_circ_writer: Option<Box<dyn io::Write + 'w>>,
circ_candidates_csv: Option<csv::Writer<Box<dyn io::Write + 'w>>>,
}

Expand All @@ -24,22 +19,16 @@ impl<'w> TasoLogger<'w> {
/// Create a new logging configuration.
///
/// Two writer objects must be provided:
/// - best_circ_json_writer: for the final optimised circuit, in TK1 JSON
/// format,
/// - best_progress_csv_writer: for a log of the successive best candidate
/// circuits,
///
/// Regular events are logged with [`tracing`], with targets [`LOG_TARGET`]
/// or [`PROGRESS_TARGET`].
///
/// [`log`]: <https://docs.rs/log/latest/log/>
pub fn new(
best_circ_json_writer: impl io::Write + 'w,
best_progress_csv_writer: impl io::Write + 'w,
) -> Self {
pub fn new(best_progress_csv_writer: impl io::Write + 'w) -> Self {
let boxed_candidates_writer: Box<dyn io::Write> = Box::new(best_progress_csv_writer);
Self {
final_circ_writer: Some(Box::new(best_circ_json_writer)),
circ_candidates_csv: Some(csv::Writer::from_writer(boxed_candidates_writer)),
}
}
Expand All @@ -56,12 +45,19 @@ impl<'w> TasoLogger<'w> {

/// Log the final optimised circuit
#[inline]
pub fn log_processing_end(&self, circuit_count: usize, needs_joining: bool, timeout: bool) {
pub fn log_processing_end(
&self,
circuit_count: usize,
best_cost: usize,
needs_joining: bool,
timeout: bool,
) {
if timeout {
self.log("Timeout");
}
self.log("Optimisation finished");
self.log(format!("Tried {circuit_count} circuits"));
self.log(format!("END RESULT: {}", best_cost));
if needs_joining {
self.log("Joining worker threads");
}
Expand All @@ -84,15 +80,6 @@ impl<'w> TasoLogger<'w> {
}
}

/// Log the final optimised circuit.
#[inline]
pub fn log_final(&mut self, best_circ: &Hugr, best_cost: usize) {
self.log(format!("END RESULT: {}", best_cost));
if let Some(circ_writer) = self.final_circ_writer.as_mut() {
save_tk1_json_writer(best_circ, circ_writer).unwrap();
}
}

/// Internal function to log general events, normally printed to stdout.
#[inline]
fn log(&self, msg: impl AsRef<str>) {
Expand Down
5 changes: 3 additions & 2 deletions taso-optimiser/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let output_path = Path::new(&opts.output);
let ecc_path = Path::new(&opts.eccs);

let final_circ_json = fs::File::create("final_circ.json")?;
// TODO: Remove this from the Logger, and use tracing events instead.
let circ_candidates_csv = fs::File::create("best_circs.csv")?;
let taso_logger = TasoLogger::new(final_circ_json, circ_candidates_csv);

let taso_logger = TasoLogger::new(circ_candidates_csv);

let circ = load_tk1_json_file(input_path)?;

Expand Down

0 comments on commit 0bd3a72

Please sign in to comment.