//! Run a circuit (JSON lines, one gate per line: {"q":[a,b],"m":[re,im,…]}) on //! the dense state vector and print the final state, or time it. //! //! ```text //! cargo run --release --example sv_run --features quantum_sv -- CIRCUIT N WIDTH [--state | --time] //! ``` use wai_quantum::gates::{complex, Gate}; use wai_quantum::quantum_sv::StateVector; fn main() { let a: Vec = std::env::args().collect(); let text = std::fs::read_to_string(&a[1]).expect("read circuit"); let n: u32 = a[2].parse().unwrap(); let width: usize = a[3].parse().unwrap(); let gates: Vec = text .lines() .filter(|l| !l.trim().is_empty()) .map(|l| { let v: serde_json::Value = serde_json::from_str(l).unwrap(); let qubits = v["q"].as_array().unwrap().iter().map(|x| x.as_u64().unwrap() as u32).collect(); let m: Vec = v["m"].as_array().unwrap().iter().map(|x| x.as_f64().unwrap()).collect(); Gate { qubits, matrix: m.chunks(2).map(|c| complex(c[0], c[1])).collect() } }) .collect(); let mut sv = StateVector::zero(n).unwrap(); let t0 = std::time::Instant::now(); sv.run(&gates, width).unwrap(); let dt = t0.elapsed().as_secs_f64(); if a.iter().any(|x| x == "--state") { let amps: Vec = (0..sv.re.len()).map(|x| format!("[{:e},{:e}]", sv.re[x], sv.im[x])).collect(); println!("[{}]", amps.join(",")); } else { println!("{n} qubits, {} gates, width {width}: {dt:.3} s, norm {:.15}", gates.len(), sv.norm2()); } }