1use std::path::Path;
2
3use crate::openqasm::{
4 parse_openqasm,
5 semantic_analysis::{GateOperation, Operation},
6};
7use openqasm_parser::openqasm::{self, BasicOp};
8
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}