use clap::{Args, Parser};
use std::fs;
use std::path::PathBuf;
use crate::circuit::Circuit;
use crate::extract::ToCircuit;
use crate::simplify;
use crate::vec_graph::Graph;
use super::CliError;
#[derive(Parser, Debug)]
pub struct OptArgs {
input: PathBuf,
#[arg(long, short)]
out: Option<PathBuf>,
#[command(flatten)]
method: Option<OptMethod>,
}
impl OptArgs {
pub fn run(self) -> Result<(), CliError> {
let circ = Circuit::from_file(self.input.to_str().unwrap())?;
let mut g = circ.to_graph();
self.method.unwrap_or_default().simp(&mut g);
let qasm = g
.to_circuit()
.expect("Extraction should succeed since we start from a circuit")
.to_qasm();
if let Some(out_path) = self.out {
fs::write(out_path, qasm)?;
} else {
println!("{qasm}");
}
Ok(())
}
}
#[derive(Args, Debug)]
#[group(multiple = false)]
pub struct OptMethod {
#[arg(long)]
full: bool,
#[arg(long)]
flow: bool,
#[arg(long)]
clifford: bool,
}
impl Default for OptMethod {
fn default() -> Self {
OptMethod {
full: true,
flow: false,
clifford: false,
}
}
}
impl OptMethod {
fn simp(&self, g: &mut Graph) {
if self.full {
simplify::full_simp(g);
} else if self.flow {
simplify::flow_simp(g);
} else if self.clifford {
simplify::clifford_simp(g);
}
}
}