Skip to content

Pure-Rust parser, type-checker and translator for OpenQASM 2.0

Notifications You must be signed in to change notification settings

tuomas56/openqasm-rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Pure-Rust parser, type-checker and translator for OpenQASM 2.0

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published