libqasm
library for handling cQASM files
cqasm-v1.cpp
Go to the documentation of this file.
1 
9 #include "cqasm-v1.hpp"
10 
11 namespace cqasm {
12 namespace v1 {
13 
19  const std::string &filename,
20  const std::string &api_version
21 ) {
22  return default_analyzer(api_version).analyze(filename).unwrap();
23 }
24 
31  FILE *file,
32  const std::string &filename,
33  const std::string &api_version
34 ) {
35  return default_analyzer(api_version).analyze(file, filename).unwrap();
36 }
37 
44  const std::string &data,
45  const std::string &filename,
46  const std::string &api_version
47 ) {
48  return default_analyzer(api_version).analyze_string(data, filename).unwrap();
49 }
50 
55 analyzer::Analyzer default_analyzer(const std::string &api_version) {
56  analyzer::Analyzer analyzer{api_version};
57 
58  // Register the default mappings (true, false, pi, x, y, z, etc.) and
59  // functions (operators, things like trigonometric functions, etc.).
61 
62  // Register the error models. Originally, depolarizing_channel accepted any
63  // number of floating point arguments, but the new parser doesn't support
64  // that, so we just brute-force the first 50 into it and call it a day.
65  std::ostringstream args;
66  for (int i = 0; i <= 50; i++) {
67  analyzer.register_error_model("depolarizing_channel", args.str());
68  args << "r";
69  }
70 
71  // Register the cQASM 1.0 instruction set.
72  analyzer.register_instruction("measure_all", "", false, false);
73  analyzer.register_instruction("measure_parity", "QaQa", false, false, false, true);
74  analyzer.register_instruction("x", "Q");
75  analyzer.register_instruction("y", "Q");
76  analyzer.register_instruction("z", "Q");
77  analyzer.register_instruction("i", "Q");
78  analyzer.register_instruction("h", "Q");
79  analyzer.register_instruction("x90", "Q");
80  analyzer.register_instruction("y90", "Q");
81  analyzer.register_instruction("mx90", "Q");
82  analyzer.register_instruction("my90", "Q");
83  analyzer.register_instruction("s", "Q");
84  analyzer.register_instruction("sdag", "Q");
85  analyzer.register_instruction("t", "Q");
86  analyzer.register_instruction("tdag", "Q");
87  analyzer.register_instruction("u", "Qu");
88  analyzer.register_instruction("prep", "Q", false);
89  analyzer.register_instruction("prep_x", "Q", false);
90  analyzer.register_instruction("prep_y", "Q", false);
91  analyzer.register_instruction("prep_z", "Q", false);
92  analyzer.register_instruction("measure", "Q", false);
93  analyzer.register_instruction("measure_x", "Q", false);
94  analyzer.register_instruction("measure_y", "Q", false);
95  analyzer.register_instruction("measure_z", "Q", false);
96  analyzer.register_instruction("rx", "Qr");
97  analyzer.register_instruction("ry", "Qr");
98  analyzer.register_instruction("rz", "Qr");
99  analyzer.register_instruction("cnot", "QQ");
100  analyzer.register_instruction("cz", "QQ");
101  analyzer.register_instruction("swap", "QQ");
102  analyzer.register_instruction("cr", "QQr");
103  analyzer.register_instruction("crk", "QQi");
104  analyzer.register_instruction("toffoli", "QQQ");
105  analyzer.register_instruction("not", "B");
106  analyzer.register_instruction("display", "", false, false);
107  analyzer.register_instruction("display", "B", false, false);
108  analyzer.register_instruction("display_binary", "", false, false);
109  analyzer.register_instruction("display_binary", "B", false, false);
110  analyzer.register_instruction("skip", "i", false, false);
111  analyzer.register_instruction("wait", "i", false, false);
112  analyzer.register_instruction("reset-averaging", "", false, false);
113  analyzer.register_instruction("reset-averaging", "Q", false, false);
114  analyzer.register_instruction("load_state", "s", false, false);
115 
116  return analyzer;
117 }
118 
119 } // namespace v1
120 } // namespace cqasm
Main class used for analyzing cQASM files.
tree::One< semantic::Program > analyze_string(const std::string &data, const std::string &filename, const std::string &api_version)
Parses and analyzes the given string with the default analyzer, dumping error messages to stderr and ...
Definition: cqasm-v1.cpp:43
analyzer::Analyzer default_analyzer(const std::string &api_version)
Constructs an Analyzer object with the defaults for cQASM 1.0 already loaded into it...
Definition: cqasm-v1.cpp:55
AnalysisResult analyze(const ast::Program &program) const
Analyzes the given program AST node.
Toplevel namespace with entry points for the new API.
tree::One< semantic::Program > analyze(const std::string &filename, const std::string &api_version)
Parses and analyzes the given file with the default analyzer, dumping error messages to stderr and th...
Definition: cqasm-v1.cpp:18
Main include file for parsing v1 files.
Namespace for the "new" cQASM 1.x API.
void register_default_functions_and_mappings()
Registers a number of default functions and mappings, such as the operator functions, the usual trigonometric functions, mappings for pi, eu (aka e, 2.718...), im (imaginary unit) and so on.
::tree::base::One< T > One
Definition: cqasm-tree.hpp:26
AnalysisResult analyze_string(const std::string &data, const std::string &filename="<unknown>") const
Parses and analyzes the given string.
ast::One< semantic::Program > unwrap(std::ostream &out=std::cerr) const
"Unwraps" the result (as you would in Rust) to get the program node or an exception.