C++
libQASM C++ API is defined in include/v3x/cqasm_python.hpp
.
The only exported class is V3xAnalyzer
.
This is actually a C++ class that works as a binding for accessing C++ code from Python.
class V3xAnalyzer;
Main class for parsing and analyzing cQASM v3.0 files.
This class works as a binding for accessing C++ code from Python.
The parsing methods are static because they do not change the status of the analyzer. Instead, they just invoke free functions that create a temporary instance of a parser.
None of the parsing or the analyzing methods perform any version check.
parse_file
, parse_string
, analyze_file
, and analyze_string
:
Return a vector of strings. The first string is reserved for the CBOR serialization of the syntactic/semantic (in the case of parsing/analyzing) Abstract Syntax Tree (AST) of the input program. Any additional strings represent error messages. Notice that the AST and error messages will not be available at the same time.
parse_file_to_json
, parse_string_to_json
, analyze_file_to_json
, and analyze_string_to_json
:
Return a string in JSON format. If the parsing was successful, that string contains a JSON representation of the AST of the input program. Otherwise, it will contain a list of errors. The JSON representation of each error follows the Language Server Protocol (LSP) specification. Every error is mapped to an LSP Diagnostic structure:
severity
is hardcoded to 1 at the moment (value corresponding to an Error level).
parse_string
, parse_string_to_json
, analyze_string, and
analyze_string_to_json`:
have an optional second argument:
file_name
. It is only used when reporting errors.
Example:
auto result = analyze_file("grover.cq");
Example: auto program = std::string{ R"(
version 3.0
qubit[2] q
bit[2] b
H q[0]
CNOT q[0], q[1]
b = measure q
)" };
auto result = parse_string_to_json(program, "bell.cq");
static std::vector<std::string> parse_file(const std::string& file_name);
Parses a file containing a cQASM v3.0 program.
static std::string parse_file_to_json(const std::string& file_name);
Parses a file containing a cQASM v3.0 program.
static std::vector<std::string> parse_string(const std::string& data, const std::string& file_name);
Parses a string containing a cQASM v3.0 program.
file_name
is optional. It is only used when reporting errors.
static std::string parse_string_to_json(const std::string& data, const std::string& file_name);
Parses a string containing a cQASM v3.0 program.
file_name
is optional. It is only used when reporting errors.
V3xAnalyzer(const std::string& max_version, bool without_defaults);
Creates a new cQASM v3.0 semantic analyzer.
max_version
is optional. It has a default value of 3.0
. The maximum cQASM version supported by the analyzer.
without_defaults
is optional. If set, the default instruction set is not loaded into the instruction table, so you have to specify the entire instruction set using register_instruction()
. Otherwise, those functions only add to the defaults.
The initial mappings and functions are not configurable. The defaults for these are always used.
~V3xAnalyzer();
Default destructor.
void register_instruction(const std::string& name, const std::optional<std::string>& param_types);
Registers an instruction type.
The param_types
can be:
Q
= qubit.V
= qubit array.B
= bit.W
= bit array.i
= int.f
= float.
Example:
register_instruction("H", "Q");
std::vector<std::string> analyze_file(const std::string& file_name);
Parses and analyzes a file containing a cQASM v3.0 program.
std::string analyze_file_to_json(const std::string& file_name);
Parses and analyzes a file containing a cQASM v3.0 program.
std::vector<std::string> analyze_string(const std::string& data, const std::string& file_name);
Parses and analyzes a string containing a cQASM v3.0 program.
file_name
is optional. It is only used when reporting errors.
std::string analyze_string_to_json(const std::string& data, const std::string& file_name);
Parses and analyzes a string containing a cQASM v3.0 program.
file_name
is optional. It is only used when reporting errors.