qasm 1.0.0

A OPENQASM 2.0 Parser
Documentation
  • Coverage
  • 88.89%
    56 out of 63 items documented5 out of 5 items with examples
  • Size
  • Source code size: 53.56 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 3.23 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 18s Average build duration of successful builds.
  • all releases: 18s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • adamisntdead

QASM-rust

Build Status crates icon

An OPENQASM 2.0 parser written in Rust 🌵

Features

  • Passes the official OpenQASM conformance test suite
  • As described in the OpenQASM specification
  • Get tokens for a given source file
  • Resolve include statements
  • Remove comments
  • Build Abstract Syntax Tree of a list of tokens

Usage

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.

test.qasm

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];

main.rs

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);
}

Output

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))
])

License

MIT