libqasm
library for handling cQASM files
cqasm-py.cpp
Go to the documentation of this file.
1 
5 #include "cqasm-py.hpp"
6 #include "cqasm-v1.hpp"
7 
8 namespace v1 = cqasm::v1;
9 
19 V1Analyzer::V1Analyzer(const std::string &max_version, bool without_defaults) {
20  if (without_defaults) {
21  a = std::unique_ptr<v1::analyzer::Analyzer>(
22  new v1::analyzer::Analyzer(max_version)
23  );
24  a->register_default_functions_and_mappings();
25  } else {
26  a = std::unique_ptr<v1::analyzer::Analyzer>(
27  new v1::analyzer::Analyzer(v1::default_analyzer(max_version))
28  );
29  }
30 }
31 
37  const std::string &name,
38  const std::string &param_types,
39  bool allow_conditional,
40  bool allow_parallel,
41  bool allow_reused_qubits,
42  bool allow_different_index_sizes
43 ) {
44  a->register_instruction(
45  name,
46  param_types,
47  allow_conditional,
48  allow_parallel,
49  allow_reused_qubits,
50  allow_different_index_sizes
51  );
52 }
53 
59  const std::string &name,
60  const std::string &param_types
61 ) {
62  a->register_error_model(name, param_types);
63 }
64 
71 std::vector<std::string> V1Analyzer::parse_file(
72  const std::string &filename
73 ) {
74  auto result = v1::parser::parse_file(filename);
75  std::vector<std::string> retval{""};
76  if (result.errors.empty()) {
77  retval[0] = ::tree::base::serialize(result.root);
78  }
79  retval.insert(retval.end(), result.errors.begin(), result.errors.end());
80  return retval;
81 }
82 
87 std::vector<std::string> V1Analyzer::parse_string(
88  const std::string &data,
89  const std::string &filename
90 ) {
91  auto result = v1::parser::parse_string(data, filename);
92  std::vector<std::string> retval{""};
93  if (result.errors.empty()) {
94  retval[0] = ::tree::base::serialize(result.root);
95  }
96  retval.insert(retval.end(), result.errors.begin(), result.errors.end());
97  return retval;
98 }
99 
108 std::vector<std::string> V1Analyzer::analyze_file(
109  const std::string &filename
110 ) const {
111  auto result = a->analyze(filename);
112  std::vector<std::string> retval{""};
113  if (result.errors.empty()) {
114  retval[0] = ::tree::base::serialize(result.root);
115  }
116  retval.insert(retval.end(), result.errors.begin(), result.errors.end());
117  return retval;
118 }
119 
124 std::vector<std::string> V1Analyzer::analyze_string(
125  const std::string &data,
126  const std::string &filename
127 ) const {
128  auto result = a->analyze_string(data, filename);
129  std::vector<std::string> retval{""};
130  if (result.errors.empty()) {
131  retval[0] = ::tree::base::serialize(result.root);
132  }
133  retval.insert(retval.end(), result.errors.begin(), result.errors.end());
134  return retval;
135 }
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
std::vector< std::string > analyze_string(const std::string &data, const std::string &filename="<unknown>") const
Same as analyze_file(), but instead receives the file contents directly.
Definition: cqasm-py.cpp:124
std::vector< std::string > analyze_file(const std::string &filename) const
Parses and analyzes the given file.
Definition: cqasm-py.cpp:108
void serialize(const error_model::ErrorModelRef &obj, ::tree::cbor::MapWriter &map)
Defines SWIG&#39;d things for the Python interface.
Main include file for parsing v1 files.
Namespace for the "new" cQASM 1.x API.
static std::vector< std::string > parse_string(const std::string &data, const std::string &filename="<unknown>")
Same as parse_file(), but instead receives the file contents directly.
Definition: cqasm-py.cpp:87
static std::vector< std::string > parse_file(const std::string &filename)
Only parses the given file.
Definition: cqasm-py.cpp:71
void register_instruction(const std::string &name, const std::string &param_types="", bool allow_conditional=true, bool allow_parallel=true, bool allow_reused_qubits=false, bool allow_different_index_sizes=false)
Registers an instruction type.
Definition: cqasm-py.cpp:36
void register_error_model(const std::string &name, const std::string &param_types="")
Registers an error model.
Definition: cqasm-py.cpp:58
ParseResult parse_string(const std::string &data, const std::string &filename)
Parse the given string.
V1Analyzer(const std::string &max_version="1.0", bool without_defaults=false)
Creates a new 1.x semantic analyzer.
Definition: cqasm-py.cpp:19
ParseResult parse_file(const std::string &filename)
Parse the given file.