Crates.io | openqasm |
lib.rs | openqasm |
version | 0.1.3 |
source | src |
created_at | 2022-02-17 15:07:50.228265 |
updated_at | 2022-02-26 13:38:11.883519 |
description | Parser and translator for OpenQASM 2.0 |
homepage | https://github.com/tuomas56/openqasm-rs |
repository | https://github.com/tuomas56/openqasm-rs |
max_upload_size | |
id | 534172 |
size | 186,008 |
This crate implements a parser, type-checker and translator for OpenQASM 2.0.
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.