Function parse_openqasm

Source
pub fn parse_openqasm(
    file_path: &Path,
) -> Result<OpenQASMProgram, OpenQASMError>
Expand description

The most importan function exposed by the library. This function reads a file containing openqasm 2.0 code and parses it. If the file include statments they are replaced by the contents of the included file before being parsed.

Returns an OpenQASMProgram if parsing was successful. This datatype contains information about the different quantum and classical registers, custom gates and operations performed.

If parsing was unsuccuessful an OpenQASMError is returned.

ยงExample

use openqasm_parser::openqasm;
use std::path::Path;

let program = openqasm::parse_openqasm(Path::new("openqasmfile.qasm"));
Examples found in repository?
examples/example.rs (line 13)
9fn main() {
10    let example_dir = Path::new(file!()).parent().unwrap();
11    let qasm_path = example_dir.join(Path::new("example.qasm"));
12    println!("{:?}", qasm_path);
13    let program = parse_openqasm(&qasm_path).unwrap();
14
15    println!("Quantum registers:");
16    for (name, reg_size) in program.qregs.iter() {
17        println!("    {}[{}]", name, reg_size);
18    }
19
20    println!("Classical registers:");
21    for (name, reg_size) in program.cregs.iter() {
22        println!("    {}[{}]", name, reg_size);
23    }
24
25    println!("Gates:");
26    for (name, gate) in program.gates.iter() {
27        println!(
28            "    {} | parameters: {}, arguments: {}",
29            name, gate.num_arguments, gate.num_targets
30        );
31
32        for op in gate.operations.iter() {
33            let gate_name = match op {
34                GateOperation::U(_, _, _, _) => "U".to_string(),
35                GateOperation::CX(_, _) => "CX".to_string(),
36                GateOperation::Custom(gate_name, _, _) => gate_name.clone(),
37            };
38            println!("        {}", gate_name);
39        }
40    }
41
42    println!("Operations:");
43    for (cond, operation) in program.operations.iter() {
44        print!("    ");
45
46        if let Some(cond) = cond {
47            print!("if({} == {}) ", cond.0, cond.1);
48        }
49
50        match operation {
51            Operation::U(p1, p2, p3, qbit) => {
52                println!("U({},{},{}) {}[{}]", p1, p2, p3, qbit.0, qbit.1)
53            }
54            Operation::CX(q1, q2) => println!("CX {}[{}], {}[{}]", q1.0, q1.1, q2.0, q2.1),
55            Operation::Custom(name, ps, qs) => println!("{}({:?}) {:?}", name, ps, qs),
56            Operation::Measure(q, c) => println!("measure {}[{}] -> {}[{}]", q.0, q.1, c.0, c.1),
57            Operation::ResetQ(q) => println!("reset {}[{}]", q.0, q.1),
58            Operation::ResetC(c) => println!("reset {}[{}]", c.0, c.1),
59        }
60    }
61    println!("Basic operations:");
62    for (cond, operation) in program.get_basic_operations().iter() {
63        print!("    ");
64
65        if let Some(cond) = cond {
66            print!("if({} == {}) ", cond.0, cond.1);
67        }
68
69        match operation {
70            BasicOp::U(p1, p2, p3, qbit) => {
71                println!("U({},{},{}) {}[{}]", p1, p2, p3, qbit.0, qbit.1)
72            }
73            BasicOp::CX(q1, q2) => println!("CX {}[{}], {}[{}]", q1.0, q1.1, q2.0, q2.1),
74            BasicOp::Measure(q, c) => println!("measure {}[{}] -> {}[{}]", q.0, q.1, c.0, c.1),
75            BasicOp::ResetQ(q) => println!("reset {}[{}]", q.0, q.1),
76            BasicOp::ResetC(c) => println!("reset {}[{}]", c.0, c.1),
77        }
78    }
79
80}