Class Visitor

Inheritance Relationships

Base Type

  • public blackbirdBaseVisitor

Class Documentation

class Visitor : public blackbirdBaseVisitor

The main Blackbird visitor. This visitor is invoked implicitly via the parse function to parse the Blackbird script.

The methods defined in this class describe how to traverse the abstract syntax tree describing the blackbird program, how to extract and evaluate arrays, variables, expressions, and quantum programs.

Public Functions

template<class O>
O *_create_operation(blackbirdParser::ArgumentsContext *ctx, intvec modes)

Factory function to create Blackbird operations corresponding to those provided in the Blackbird script.

Return
the operation
Parameters
  • ctx: ArgumentsContext
  • modes: std::vector<int> containing the modes the operation is applied to

antlrcpp::Any visitNumber(blackbirdParser::NumberContext *ctx)

Defines what the visitor does when a NumberContext is visited.

Here, it gets the string representing the number, and calls various helper functions to convert Blackbird complex, float, and int to the C++ equivalent.

Return
returns the number literal
Parameters
  • ctx: NumberContext

antlrcpp::Any visitExpressionvar(blackbirdParser::ExpressionvarContext *ctx)

Defines what the visitor does when an ExpressionvarContext is visited.

Here, it gets the variable name and the variable type, and starts the process of extracting the variable based on the declared type.

Return
returns 0 to indicate successful visitation
Parameters
  • ctx: ExpressionvarContext

antlrcpp::Any visitArrayvar(blackbirdParser::ArrayvarContext *ctx)

Defines what the visitor does when a ArrayvarContext is visited.

Here, it gets the name and type of the array variable, initializes a vector corresponding to the declared type, and stores it either in the complexmat_vars, floatmat_vars, or intmat_vars dictionary.

Parameters
  • ctx: NumberContext

antlrcpp::Any visitArrayrow(blackbirdParser::ArrayrowContext *ctx)

Defines what the visitor does when a ArrayrowContext is visited.

Here, it extracts the column element values, and populates the row by manually visiting each child element.

Return
the array row vector
Parameters
  • ctx: ArrayrowContext

antlrcpp::Any visitStatement(blackbirdParser::StatementContext *ctx)

Defines what to do as each quantum operation in the device context is visited.

In general, this includes:

  • Getting the number of modes the operation is applied to using split_string_to_ints
  • If the statement is an operation:
    • Get the number of arguments/types of the arguments
    • Evaluate the arguments
    • Use _create_operation template to create the new operation object, acting

      on the specified number of modes, with the specified arguments

    • Add this operation to the program->operations vector.
  • Repeat the previous steps for any measurements.

Return
0 to specify correct visitation.
Parameters
  • ctx: StatementContext

antlrcpp::Any visitProgram(blackbirdParser::ProgramContext *ctx)

Defines what to do as each quantum operation in the device context is visited.

In general, this includes:

  • Getting the number of modes the operation is applied to using split_string_to_ints
  • If the statement is an operation:
    • Get the number of arguments/types of the arguments
    • Evaluate the arguments
    • Use _create_operation template to create the new operation object, acting

      on the specified number of modes, with the specified arguments

    • Add this operation to the program->operations vector.
  • Repeat the previous steps for any measurements.

Return
0 to specify correct visitation.
Parameters
  • ctx: StatementContext

antlrcpp::Any visitStart(blackbirdParser::StartContext *ctx)

Starts the visitation process of parsing the Blackbird code.

Return
returns the blackbird program, containing device information, metadata, and the quantum program to run
Parameters
  • ctx: StartContext

Public Members

std::unordered_map<std::string, std::complex<double>> complex_vars

Unordered map mapping from blackbird complex variable names to std::complex<double> values.

std::unordered_map<std::string, double> float_vars

Unordered map mapping from blackbird float variable names to double values

std::unordered_map<std::string, int> int_vars

Unordered map mapping from blackbird int variable names to int values

std::unordered_map<std::string, std::string> str_vars

Unordered map mapping from blackbird string variable names to std::string values

std::unordered_map<std::string, bool> bool_vars

Unordered map mapping from blackbird bool variable names to bool values

std::unordered_map<std::string, complexmat> complexmat_vars

Unordered map mapping from blackbird complex array variable names to complexmat values.

std::unordered_map<std::string, floatmat> floatmat_vars

Unordered map mapping from blackbird float array variable names to floatmat values.

std::unordered_map<std::string, intmat> intmat_vars

Unordered map mapping from blackbird int array variable names to intmat values.

Program *program

quantum Program that is populated by the Visitor