libqasm
library for handling cQASM files
Public Member Functions | Friends | List of all members
cqasm::v1::analyzer::Analyzer Class Reference

Main class used for analyzing cQASM files. More...

Public Member Functions

 Analyzer (const std::string &api_version="1.0")
 Creates a new semantic analyzer. More...
 
 Analyzer (const primitives::Version &api_version)
 Creates a new semantic analyzer. More...
 
void register_mapping (const std::string &name, const values::Value &value)
 Registers an initial mapping from the given name to the given value. More...
 
void register_function (const std::string &name, const types::Types &param_types, const resolver::FunctionImpl &impl)
 Registers a function, usable within expressions. More...
 
void register_function (const std::string &name, const std::string &param_types, const resolver::FunctionImpl &impl)
 Convenience method for registering a function. More...
 
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. More...
 
void register_instruction (const instruction::Instruction &instruction)
 Registers an instruction type. More...
 
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)
 Convenience method for registering an instruction type. More...
 
template<typename T >
void register_instruction_with_annotation (T &&annotation, 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)
 Convenience method for registering an instruction type with a single user-specified annotation. More...
 
void register_error_model (const error_model::ErrorModel &error_model)
 Registers an error model. More...
 
void register_error_model (const std::string &name, const std::string &param_types="")
 Convenience method for registering an error model. More...
 
template<typename T >
void register_error_model_with_annotation (T &&annotation, const std::string &name, const std::string &param_types="")
 Convenience method for registering an error model with a single user-specified annotation. More...
 
AnalysisResult analyze (const ast::Program &program) const
 Analyzes the given program AST node. More...
 
AnalysisResult analyze (const parser::ParseResult &parse_result) const
 Analyzes the given parse result. More...
 
AnalysisResult analyze (const std::function< version::Version()> &version_parser, const std::function< parser::ParseResult()> &file_parser) const
 Parses and analyzes using the given version and file parser closures. More...
 
AnalysisResult analyze (const std::string &filename) const
 Parses and analyzes the given file. More...
 
AnalysisResult analyze (FILE *file, const std::string &filename="<unknown>") const
 Parses and analyzes the given file pointer. More...
 
AnalysisResult analyze_string (const std::string &data, const std::string &filename="<unknown>") const
 Parses and analyzes the given string. More...
 

Friends

class AnalyzerHelper
 

Detailed Description

Main class used for analyzing cQASM files.

Construction of this class is the entry point for libqasm whenever you need to modify the default instruction set, have a different set of supported error models, or want to add additional initial mappings, operators, or functions. The process is simple:

Note that the only state maintained by the Analyzer object is its configuration, and the analyze*() functions never change this state (hence they are const).

Definition at line 101 of file cqasm-v1-analyzer.hpp.

#include <cqasm-v1-analyzer.hpp>

Constructor & Destructor Documentation

◆ Analyzer() [1/2]

cqasm::v1::analyzer::Analyzer::Analyzer ( const std::string &  api_version = "1.0")

Creates a new semantic analyzer.

Definition at line 40 of file cqasm-v1-analyzer.cpp.

◆ Analyzer() [2/2]

cqasm::v1::analyzer::Analyzer::Analyzer ( const primitives::Version api_version)

Creates a new semantic analyzer.

Definition at line 51 of file cqasm-v1-analyzer.cpp.

Member Function Documentation

◆ register_mapping()

void cqasm::v1::analyzer::Analyzer::register_mapping ( const std::string &  name,
const values::Value value 
)

Registers an initial mapping from the given name to the given value.

Definition at line 63 of file cqasm-v1-analyzer.cpp.

◆ register_function() [1/2]

void cqasm::v1::analyzer::Analyzer::register_function ( const std::string &  name,
const types::Types param_types,
const resolver::FunctionImpl impl 
)

Registers a function, usable within expressions.

values::check_const() can be used in the function implementation to assert that the values must be constant when the function can only be used during constant propagation. When the function also (or only) supports dynamic evaluation, the implementation will have to check whether the inputs are const manually (for instance using as_constant()) to determine when to return a dynamic values::Function node instead.

Definition at line 70 of file cqasm-v1-analyzer.cpp.

◆ register_function() [2/2]

void cqasm::v1::analyzer::Analyzer::register_function ( const std::string &  name,
const std::string &  param_types,
const resolver::FunctionImpl impl 
)

Convenience method for registering a function.

The param_types are specified as a string, converted to types::Types for the other overload using types::from_spec.

Definition at line 83 of file cqasm-v1-analyzer.cpp.

◆ register_default_functions_and_mappings()

void cqasm::v1::analyzer::Analyzer::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.

Definition at line 96 of file cqasm-v1-analyzer.cpp.

◆ register_instruction() [1/2]

void cqasm::v1::analyzer::Analyzer::register_instruction ( const instruction::Instruction instruction)

Registers an instruction type.

If you never call this, instructions are not resolved (i.e. anything goes name- and operand type-wise). Once you do, only instructions with signatures as added are legal, so anything that doesn't match returns an error.

Definition at line 114 of file cqasm-v1-analyzer.cpp.

◆ register_instruction() [2/2]

void cqasm::v1::analyzer::Analyzer::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 
)

Convenience method for registering an instruction type.

The arguments are passed straight to instruction::Instruction's constructor.

Definition at line 123 of file cqasm-v1-analyzer.cpp.

◆ register_instruction_with_annotation()

template<typename T >
void cqasm::v1::analyzer::Analyzer::register_instruction_with_annotation ( T &&  annotation,
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 
)
inline

Convenience method for registering an instruction type with a single user-specified annotation.

The arguments are passed straight to instruction::Instruction's constructor and set_annotation.

Definition at line 241 of file cqasm-v1-analyzer.hpp.

◆ register_error_model() [1/2]

void cqasm::v1::analyzer::Analyzer::register_error_model ( const error_model::ErrorModel error_model)

Registers an error model.

If you never call this, error models are not resolved (i.e. anything goes name- and operand type-wise). Once you do, only error models with signatures as added are legal, so anything that doesn't match returns an error.

Definition at line 142 of file cqasm-v1-analyzer.cpp.

◆ register_error_model() [2/2]

void cqasm::v1::analyzer::Analyzer::register_error_model ( const std::string &  name,
const std::string &  param_types = "" 
)

Convenience method for registering an error model.

The arguments are passed straight to error_model::ErrorModel's constructor.

Definition at line 151 of file cqasm-v1-analyzer.cpp.

◆ register_error_model_with_annotation()

template<typename T >
void cqasm::v1::analyzer::Analyzer::register_error_model_with_annotation ( T &&  annotation,
const std::string &  name,
const std::string &  param_types = "" 
)
inline

Convenience method for registering an error model with a single user-specified annotation.

The arguments are passed straight to instruction::Instruction's constructor and set_annotation.

Definition at line 285 of file cqasm-v1-analyzer.hpp.

◆ analyze() [1/5]

AnalysisResult cqasm::v1::analyzer::Analyzer::analyze ( const ast::Program program) const

Analyzes the given program AST node.

Analyzes the given AST.

Definition at line 489 of file cqasm-v1-analyzer.cpp.

◆ analyze() [2/5]

AnalysisResult cqasm::v1::analyzer::Analyzer::analyze ( const parser::ParseResult parse_result) const

Analyzes the given parse result.

If there are parse errors, they are copied into the AnalysisResult error list, and the root node will be empty.

Definition at line 502 of file cqasm-v1-analyzer.cpp.

◆ analyze() [3/5]

AnalysisResult cqasm::v1::analyzer::Analyzer::analyze ( const std::function< version::Version()> &  version_parser,
const std::function< parser::ParseResult()> &  file_parser 
) const

Parses and analyzes using the given version and file parser closures.

Definition at line 515 of file cqasm-v1-analyzer.cpp.

◆ analyze() [4/5]

AnalysisResult cqasm::v1::analyzer::Analyzer::analyze ( const std::string &  filename) const

Parses and analyzes the given file.

Definition at line 539 of file cqasm-v1-analyzer.cpp.

◆ analyze() [5/5]

AnalysisResult cqasm::v1::analyzer::Analyzer::analyze ( FILE *  file,
const std::string &  filename = "<unknown>" 
) const

Parses and analyzes the given file pointer.

The optional filename argument will be used only for error messages.

Definition at line 550 of file cqasm-v1-analyzer.cpp.

◆ analyze_string()

AnalysisResult cqasm::v1::analyzer::Analyzer::analyze_string ( const std::string &  data,
const std::string &  filename = "<unknown>" 
) const

Parses and analyzes the given string.

The optional filename argument will be used only for error messages.

Definition at line 561 of file cqasm-v1-analyzer.cpp.

Friends And Related Function Documentation

◆ AnalyzerHelper

friend class AnalyzerHelper
friend

Definition at line 103 of file cqasm-v1-analyzer.hpp.


The documentation for this class was generated from the following files: