Struct OpenQASMProgram

Source
pub struct OpenQASMProgram {
    pub gates: HashMap<String, Gate>,
    pub qregs: HashMap<String, usize>,
    pub cregs: HashMap<String, usize>,
    pub operations: Vec<(Option<Condition>, Operation)>,
}
Expand description

The type returned after performing semantic analysis on an AST.

It contains the quantum and classical registers and the gates defined in the program. I also contains a list of the Operations to perform in order. The Operations are optionally paired with a Condition for running. the operation.

Fields§

§gates: HashMap<String, Gate>

The custom gates defined in the openqasm program. Maps the gate names to Gates

§qregs: HashMap<String, usize>

The quantum registers defined in the openqasm program. Maps the register names to the size of the register.

§cregs: HashMap<String, usize>

The classical registers defined in the openqasm program. Maps the register names to the size of the register.

§operations: Vec<(Option<Condition>, Operation)>

The Operations to execute when running the openqasm program. The operations are optionally paired with a Condition which controls whether to run the operation or not.

Implementations§

Source§

impl OpenQASMProgram

Source

pub fn from_ast(ast_node: &MainProgram) -> Result<Self, SemanticError>

The main function for running semantic analysis on an AST.

Source§

impl OpenQASMProgram

Source

pub fn get_basic_operations(&self) -> Vec<(Option<Condition>, BasicOp)>

Retrieves a list of BasicOps from an OpenQASMProgram so that the operations can be for example perfomed by a simulator.

Each BasicOp can optionally be paired with a Condition, if the operation was applied in an ‘if’ statement.

Examples found in repository?
examples/example.rs (line 62)
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}

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.