//! What the benchmark run and its test share: how a compile is broken, and //! what a receipt keeps of each verdict. #![allow(dead_code)] use serde_json::{Value, json}; use wai_quantum::quantum_toolchain::native::{self, NativeGate}; /// The text with one gate removed (the `pick`-th gate line, counted among /// the gate lines outside definitions) or one rotation's first angle turned /// by π/4. pub fn break_text(text: &str, how: &str, pick: u64) -> String { let lines: Vec<&str> = text.lines().collect(); let mut depth = 0i32; let mut gates = Vec::new(); for (i, l) in lines.iter().enumerate() { let t = l.trim(); let opens = t.matches('{').count() as i32 - t.matches('}').count() as i32; if depth == 0 && t.ends_with(';') && !["OPENQASM", "include", "qubit", "bit", "creg", "qreg", "barrier", "gate", "input", "output"].iter().any(|k| t.starts_with(k)) && !t.contains("measure") { let angled = t.contains('('); if how == "drop" || angled { gates.push(i); } } depth += opens; } if gates.is_empty() { return text.to_string(); } let at = gates[(pick % gates.len() as u64) as usize]; let mut out: Vec = lines.iter().map(|s| s.to_string()).collect(); if how == "drop" { out.remove(at); } else { out[at] = out[at].replacen('(', "(pi/4 + ", 1); } out.join("\n") + "\n" } pub fn break_native(p: &native::NativeProgram, how: &str, pick: u64) -> native::NativeProgram { let mut q = p.clone(); let eighth = native::Dyadic::new(1, 3).expect("an eighth"); let idx: Vec = q .gates .iter() .enumerate() .filter(|(_, g)| if how == "drop" { true } else { matches!(g, NativeGate::Rz(..) | NativeGate::Prx(..) | NativeGate::Gpi(..) | NativeGate::Gpi2(..)) }) .map(|(i, _)| i) .collect(); if idx.is_empty() { return q; } let at = idx[(pick % idx.len() as u64) as usize]; if how == "drop" { q.gates.remove(at); } else { q.gates[at] = match q.gates[at] { NativeGate::Rz(x, t) => NativeGate::Rz(x, t + eighth), NativeGate::Prx(x, t, f) => NativeGate::Prx(x, t + eighth, f), NativeGate::Gpi(x, f) => NativeGate::Gpi(x, f + eighth), NativeGate::Gpi2(x, f) => NativeGate::Gpi2(x, f + eighth), g => g, }; } q } /// A verdict without its long members: what the receipt keeps of each check /// (each route's outcome and its counts, and its reason only where it did /// not decide). It leaves out the verdict's `tolerance` and a diagram /// route's `near_identity` statement: in a receipt, a diagram route proved in /// doubles with a `largest_difference` is one the near-identity rule proved /// (every entry within the dense route's tolerance), and one without it a /// diagram whose nodes merged with the identity's within the doubles' own. pub fn brief(v: &Value) -> Value { let routes: Vec = v["routes"] .as_array() .into_iter() .flatten() .map(|r| { let mut o = json!({ "route": r["route"], "outcome": r["outcome"] }); for k in ["arithmetic", "strategy", "exchanges_found", "runs_matched", "peak_nodes", "steps", "stimuli", "rewrites", "spiders_left", "contracted", "work", "largest_difference", "equal_up_to_phases_before_measurement"] { if let Some(x) = r.get(k) { o[k] = x.clone(); } } if !matches!(r["outcome"].as_str(), Some("proved" | "refuted" | "no_difference")) { for k in ["why", "note"] { if let Some(x) = r.get(k) { o[k] = x.clone(); } } } o }) .collect(); let mut b = json!({ "verdict": v["verdict"], "notion": v["notion"], "routes": routes, "wires": v["wires"]["wires"], "ancillas": v["wires"]["ancillas"].as_array().map_or(0, Vec::len), "gates": v["gates"] }); for k in ["proved_by", "refuted_by", "arithmetic"] { if let Some(x) = v.get(k) { b[k] = x.clone(); } } if let Some(f) = v["routes"].as_array().into_iter().flatten().find_map(|r| r.get("field")) { b["field"] = f.clone(); } if let Some(c) = v.get("counterexample") { b["counterexample"] = json!(c["found_by"]); // How large the difference is, where the route that found it measures one. for k in ["size", "difference", "fidelity", "total_variation"] { if let Some(x) = c.get(k) { b[format!("counterexample_{k}")] = x.clone(); } } } if v["verdict"] == "undecided" { b["why"] = v["why"].clone(); } b } /// Where a check of one unitary refuted the pair on an input whose outcome /// distributions already differ, the statement up to phases before the /// measurements is refuted by that input too: that finding, without a second /// check. `None` where the second check must decide it. pub fn measured_by_the_same_input(v: &Value) -> Option { let t = v["counterexample"]["total_variation"].as_f64()?; (v["verdict"] != "proved" && t > 1e-8).then(|| json!({ "verdict": "refuted", "notion": "measured", "by": "the same input: its outcome distributions differ", "total_variation": v["counterexample"]["total_variation"] })) }