Python Blackbird Listener

Module name: blackbird.listener

This module contains the main Blackbird listener, BlackbirdListener. It inherits from the class blackbirdListener contained in the file blackbirdListener.py, which is autogenerated by ANTLR4.

In addition, a small utility function, which automates the parsing of a Blackbird script and returns the completed listener, is included.

Summary

PYTHON_TYPES Mapping from the allowed Blackbird types to the equivalent Python/NumPy types.
BlackbirdListener([cwd]) Listener to run a Blackbird program and extract the program queue and target information.
parse(data[, listener, cwd]) Parse a blackbird data stream.

Code details

PYTHON_TYPES = {'array': <class 'list'>, 'bool': <class 'bool'>, 'complex': <class 'complex'>, 'float': <class 'float'>, 'int': <class 'int'>, 'str': <class 'str'>}

Mapping from the allowed Blackbird types to the equivalent Python/NumPy types.

Type:dict[str->type]
class BlackbirdListener(cwd=None)[source]

Listener to run a Blackbird program and extract the program queue and target information.

On initialization, an empty BlackbirdProgram object is initialized. Over the course of the parsing, the program details are filled in according to the parsed script.

Once parsing is complete, the BlackbirdProgram object can be returned via the program attribute.

__init__(cwd=None)[source]

Initialize self. See help(type(self)) for accurate signature.

program

Returns the parsed blackbird program

exitDeclarename(ctx: blackbird.blackbirdParser.blackbirdParser.DeclarenameContext)[source]

Run after exiting program name metadata.

Parameters:ctx – DeclarenameContext
exitVersion(ctx: blackbird.blackbirdParser.blackbirdParser.VersionContext)[source]

Run after exiting version metadata.

Parameters:ctx – VersionContext
exitTarget(ctx: blackbird.blackbirdParser.blackbirdParser.TargetContext)[source]

Run after exiting target metadata.

Parameters:ctx – TargetContext
exitInclude(ctx: blackbird.blackbirdParser.blackbirdParser.IncludeContext)[source]

Run after exiting include statement.

Parameters:ctx – IncludeContext
exitExpressionvar(ctx: blackbird.blackbirdParser.blackbirdParser.ExpressionvarContext)[source]

Run after exiting an expression variable.

Parameters:ctx – variable context
exitArrayvar(ctx: blackbird.blackbirdParser.blackbirdParser.ArrayvarContext)[source]

Run after exiting an array variable.

Parameters:ctx – array variable context
exitStatement(ctx: blackbird.blackbirdParser.blackbirdParser.StatementContext)[source]

Run after exiting a quantum statement.

Parameters:ctx – statement context
exitProgram(ctx: blackbird.blackbirdParser.blackbirdParser.ProgramContext)[source]

Run after exiting the program block.

Parameters:ctx – program context
parse(data, listener=<class 'blackbird.listener.BlackbirdListener'>, cwd=None)[source]

Parse a blackbird data stream.

Parameters:
  • data (antlr4.InputStream) – ANTLR4 data stream of the Blackbird script
  • Listener (BlackbirdListener) – an Blackbird listener to use to walk the AST. By default, the basic BlackbirdListener defined above is used.
Returns:

returns an instance of the BlackbirdProgram class after parsing the abstract syntax tree

Return type:

BlackbirdProgram