mod ast;
mod check;
mod diag;
mod interp;
mod lexer;
mod msg;
mod parser;
#[cfg(test)]
mod tests;
use std::collections::HashMap;
use qxide::{Backend, Shot, SimError, DEFAULT_MEMORY_LIMIT};
use diag::{Code, Diagnostic, SourceMap};
use interp::{BitsFmt, Elaborated, OutPiece};
pub struct Failure(pub String);
fn front(src: &str) -> Result<ast::Program, Vec<Diagnostic>> {
let toks = lexer::lex(src).map_err(|d| vec![d])?;
let program = parser::parse(toks).map_err(|d| vec![d])?;
let errors = check::check(&program);
if errors.is_empty() {
Ok(program)
} else {
Err(errors)
}
}
fn build(src: &str) -> Result<(ast::Program, Elaborated), Vec<Diagnostic>> {
let program = front(src)?;
let elaborated = interp::elaborate(&program).map_err(|d| vec![d])?;
Ok((program, elaborated))
}
fn stabilizer_bytes(n: usize) -> u128 {
let (n, rows) = (n as u128, 2 * n as u128 + 1);
let words = n.div_ceil(64).max(1);
2 * rows * words * 8 + rows
}
fn gib(bytes: u128) -> String {
format!("{:.1} GiB", bytes as f64 / f64::from(1u32 << 30))
}
fn sim_error(err: SimError, at: diag::Span) -> Diagnostic {
match err {
SimError::OutOfMemory {
qubits,
required_bytes,
limit_bytes,
} => {
let reason =
msg::state_vector_too_large(qubits, &gib(required_bytes), &gib(limit_bytes));
Diagnostic::new(Code::Simulation, at, msg::simulation_failed(&reason))
.with_help(msg::help_too_large())
}
other => Diagnostic::new(
Code::Simulation,
at,
msg::simulation_failed(&other.to_string()),
),
}
}
fn render(name: &str, src: &str, errors: &[Diagnostic]) -> Failure {
let map = SourceMap::new(name, src);
Failure(
errors
.iter()
.map(|d| map.render(d))
.collect::<Vec<_>>()
.join("\n"),
)
}
fn strip_bom(src: &str) -> &str {
src.strip_prefix('\u{feff}').unwrap_or(src)
}
pub fn check(name: &str, src: &str) -> Result<(), Failure> {
let src = strip_bom(src);
front(src).map(|_| ()).map_err(|e| render(name, src, &e))
}
pub fn qasm(name: &str, src: &str) -> Result<String, Failure> {
let src = strip_bom(src);
let (_, e) = build(src).map_err(|e| render(name, src, &e))?;
Ok(e.circuit.to_qasm())
}
fn fmt_array(bits: &[bool]) -> String {
let items: Vec<&str> = bits.iter().map(|&b| if b { "1" } else { "0" }).collect();
format!("[{}]", items.join(", "))
}
fn fmt_binary(bits: &[bool]) -> String {
if bits.is_empty() {
return "0b0".into();
}
let digits: String = bits
.iter()
.rev()
.map(|&b| if b { '1' } else { '0' })
.collect();
format!("0b{digits}")
}
fn bits_value(bits: &[bool]) -> Option<u64> {
(bits.len() <= 64).then(|| {
bits.iter()
.enumerate()
.fold(0u64, |v, (i, &b)| v | (u64::from(b) << i))
})
}
fn render_output(out: &[OutPiece], shot: Option<&Shot>) -> String {
let bit = |b: qxide::Bit| shot.is_some_and(|s| s.get(b));
let mut s = String::new();
for p in out {
match p {
OutPiece::Text(t) => s.push_str(t),
OutPiece::Bit(b) => s.push(if bit(*b) { '1' } else { '0' }),
OutPiece::Bits(v, fmt) => {
let bits: Vec<bool> = v.iter().map(|&b| bit(b)).collect();
s.push_str(&match fmt {
BitsFmt::Array => fmt_array(&bits),
BitsFmt::Binary => fmt_binary(&bits),
BitsFmt::Int => bits_value(&bits).unwrap_or_default().to_string(),
});
}
}
}
s
}
fn table_row(bits: &[bool], count: u64) -> String {
let value = bits_value(bits).map_or(String::new(), |v| format!(" = {v}"));
format!("{} {}{value} ×{count}", fmt_array(bits), fmt_binary(bits))
}
pub fn run(name: &str, src: &str, shots: u64, seed: u64) -> Result<String, Failure> {
let src = strip_bom(src);
let (program, e) = build(src).map_err(|e| render(name, src, &e))?;
let prints_bits = e
.output
.iter()
.any(|p| matches!(p, OutPiece::Bit(_) | OutPiece::Bits(..)));
let needs_sim = if shots == 1 {
prints_bits
} else {
!e.measured.is_empty()
};
if !needs_sim {
return Ok(render_output(&e.output, None));
}
let main_span = program
.fns
.iter()
.find(|f| f.name.name == "main")
.map_or(diag::Span::default(), |f| f.name.span);
let tableau = stabilizer_bytes(e.slots);
if e.circuit.select_backend() == Backend::Stabilizer
&& tableau > u128::from(DEFAULT_MEMORY_LIMIT)
{
let reason =
msg::stabilizer_too_large(e.slots, &gib(tableau), &gib(DEFAULT_MEMORY_LIMIT.into()));
return Err(render(
name,
src,
&[
Diagnostic::new(Code::Simulation, main_span, msg::simulation_failed(&reason))
.with_help(msg::help_too_large()),
],
));
}
let simulate = |k: u64| {
e.circuit
.simulate_with(Backend::Auto, seed.wrapping_add(k))
.map_err(|err| render(name, src, &[sim_error(err, main_span)]))
};
if shots == 1 {
return Ok(render_output(&e.output, Some(&simulate(0)?)));
}
let mut groups: HashMap<Vec<bool>, (u64, String)> = HashMap::new();
for k in 0..shots {
let shot = simulate(k)?;
let key: Vec<bool> = e.measured.iter().map(|&b| shot.get(b)).collect();
groups
.entry(key)
.or_insert_with(|| (0, render_output(&e.output, Some(&shot))))
.0 += 1;
}
let mut rows: Vec<(Vec<bool>, (u64, String))> = groups.into_iter().collect();
rows.sort_by(|a, b| b.1 .0.cmp(&a.1 .0).then_with(|| a.0.cmp(&b.0)));
let mut s = String::new();
for (bits, (count, text)) in rows {
s.push_str(&table_row(&bits, count));
s.push('\n');
for line in text.lines() {
s.push_str(" ");
s.push_str(line);
s.push('\n');
}
}
Ok(s)
}