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.

NUMPY_TYPES

Mapping from the allowed Blackbird array types to the equivalent NumPy data types.

RegRefTransform(expr)

Class to represent a classical register transform.

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 'numpy.ndarray'>, '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]

NUMPY_TYPES = {'bool': <class 'numpy.bool_'>, 'complex': <class 'numpy.complex128'>, 'float': <class 'numpy.float64'>, 'int': <class 'numpy.int64'>, 'str': <class 'numpy.str_'>}

Mapping from the allowed Blackbird array types to the equivalent NumPy data types.

Type

dict[str->type]

is_ptype(p)[source]

Checks whether a parameter is in the form p0, p1, etc.

class RegRefTransform(expr)[source]

Class to represent a classical register transform.

Parameters

expr (sympy.Expr) – a SymPy expression representing the RegRef transform

__init__(expr)[source]

After initialization, the RegRefTransform has four attributes which may be inspected to translate the Blackbird program to a simulator or quantum hardware:

expr

The input symbolic expression.

Type

sympy.Expr

func

Scalar function that takes one or more values corresponding to measurement results, and outputs a single numeric value.

Type

function

regrefs

List of integers corresponding to the modes that are measured and act as inputs to func. Note that the order of this list corresponds to the order that the measured mode results should be passed to the function.

Type

list[int]

func_str

String representation of the RegRefTransform function.

Type

str

__str__()[source]

Print formatting

__repr__()

Print formatting

__weakref__

list of weak references to the object (if defined)

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]
property 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

enterProgram(ctx: blackbird.blackbirdParser.blackbirdParser.ProgramContext)[source]

Run after entering 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