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
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
impl OpenQASMProgram
Sourcepub fn from_ast(ast_node: &MainProgram) -> Result<Self, SemanticError>
pub fn from_ast(ast_node: &MainProgram) -> Result<Self, SemanticError>
The main function for running semantic analysis on an AST.
Source§impl OpenQASMProgram
impl OpenQASMProgram
Sourcepub fn get_basic_operations(&self) -> Vec<(Option<Condition>, BasicOp)>
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§
impl Freeze for OpenQASMProgram
impl !RefUnwindSafe for OpenQASMProgram
impl !Send for OpenQASMProgram
impl !Sync for OpenQASMProgram
impl Unpin for OpenQASMProgram
impl !UnwindSafe for OpenQASMProgram
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more