Crates.io | qvnt-qasm |
lib.rs | qvnt-qasm |
version | 0.2.0 |
source | src |
created_at | 2022-05-01 16:58:59.580116 |
updated_at | 2022-05-01 16:58:59.580116 |
description | A OPENQASM 2.0 Parser |
homepage | https://github.com/QCGPU/qasm-rust |
repository | |
max_upload_size | |
id | 578668 |
size | 63,199 |
An OPENQASM 2.0 parser written in Rust 🌵
The usage of qasm
is fully given in the docs. A brief example is given here:
Here is an example that reads a file test.qasm
, processes it and then prints the AST.
OPENQASM 2.0;
// Clifford gate: Hadamard
gate h a { u2(0,pi) a; }
qreg q[2];
creg c[1];
h q[0];
CX q[0], q[1];
measure q[1] -> c[1];
extern crate qasm;
use std::env;
use std::fs::File;
use std::io::prelude::*;
use qasm::{process, lex, parse};
fn main() {
let cwd = env::current_dir().unwrap();
let mut source = String::new();
let mut f = File::open("test.qasm").expect("cannot find source file 'test.qasm'");
f.read_to_string(&mut source).expect("couldn't read file 'test.qasm'");
let processed_source = process(&source, &cwd);
let mut tokens = lex(&processed_source);
let ast = parse(&mut tokens);
println!("{:?}", ast);
}
Ok([
Gate("h", ["a"], [], [ApplyGate("u2", [Register("a")], [" 0 ", " pi "])]),
QReg("q", 2),
CReg("c", 1),
ApplyGate("h", [Qubit("q", 0)], []),
ApplyGate("CX", [Qubit("q", 0), Qubit("q", 1)], []),
Measure(Qubit("q", 1), Qubit("c", 1))
])
MIT