3 releases

0.1.3 Feb 26, 2022
0.1.2 Feb 21, 2022
0.1.1 Feb 17, 2022
0.1.0 Feb 17, 2022

#2084 in Parser implementations

Download history 24/week @ 2024-07-21 13/week @ 2024-07-28 90/week @ 2024-08-04 11/week @ 2024-08-11 8/week @ 2024-08-18 17/week @ 2024-08-25 6/week @ 2024-09-01 8/week @ 2024-09-15 18/week @ 2024-09-22 187/week @ 2024-09-29 23/week @ 2024-10-13 25/week @ 2024-10-20 258/week @ 2024-10-27 2/week @ 2024-11-03

308 downloads per month

MIT/Apache

160KB
3.5K SLoC

openqasm-rs

This crate implements a parser, type-checker and translator for OpenQASM 2.0.

Features

  • Full type-checking both before and during translation.
  • Beautiful error messages courtesy of ariadne.
  • Pretty-printing with pretty.
  • Flexible include statement handling for sandboxing.
  • Slightly relaxed from the specification for ease of use, for instance allowing gate definitions out of order and handling include-cycles gracefully.
  • No unsafe code.

Future Roadmap

  • Support OpenQASM 3.0.
  • Provide a syntax highlighting and language server extension.
  • Provide a utility to visualize circuits.
  • Transpile OpenQASM between versions 2.0 to 3.0, or into other languages like Quil.

Examples

Parse a file and pretty print it:

use openqasm as oq;
use oq::GenericError;

fn main() {
    let mut cache = oq::SourceCache::new();
    let mut parser = oq::Parser::new(&mut cache)
        .with_file_policy(oq::parser::FilePolicy::Ignore);
    parser.parse_file("file.qasm");

    let prog = parser.done().to_errors().unwrap();
    println!("{}", prog.to_pretty(70));
}

Typecheck a program:

use openqasm as oq;
use oq::GenericError;

fn example(path: &str, cache: &mut oq::SourceCache) -> Result<(), oq::Errors> {
    let mut parser = oq::Parser::new(cache);
    parser.parse_file(path);
    let program = parser.done().to_errors()?;
    program.type_check().to_errors()?;
    Ok(())
}

fn main() {
    let mut cache = oq::SourceCache::new();
    if let Err(errors) = example("filename.qasm", &mut cache) {
        errors.print(&mut cache).unwrap();
    }
}

More examples are provided in the examples directory.

Dependencies

~4.5–7MB
~91K SLoC