Utilities¶
Module name: blackbird.utils
This module contains utility functions for Blackbird programs, including generating directed acyclic graphs, and pattern matching and Blackbird programs and templates.
Summary¶
Exception class for template related errors |
|
|
Convert a Blackbird program to a directed acyclic graph. |
|
Match a template against a Blackbird program, returning template parameter values. |
Code details¶
- class Command(name, args, kwargs, modes)¶
namedtuple: encapsulate a specific quantum operation
- args¶
Alias for field number 1
- kwargs¶
Alias for field number 2
- modes¶
Alias for field number 3
- name¶
Alias for field number 0
- to_DiGraph(program)[source]¶
Convert a Blackbird program to a directed acyclic graph.
The resulting graph has nodes representing quantum operations, and edges representing dependent/successor operations.
Each node is labelled by an integer; note attributes are used to store information about the node:
'name'
(str): name of the quantum operation (e.g.,'S2gate'
)'args'
(list): positional arguments of the operation'kwargs'
(dict): keyword arguments of the operation'modes'
(tuple[int]): modes the operation acts on
- Parameters
program (blackbird.Program) – a Blackbird program
- Returns
the directed acyclic graph representing the quantum program
- Return type
networkx.DiGraph
- match_template(template, program)[source]¶
Match a template against a Blackbird program, returning template parameter values.
For example, consider the following template and program:
template = blackbird.loads("""\ name prog version 1.0 Dgate(-{r}, 0.45) | 1 Vac | 2 Sgate({r}, 2*{phi}-1) | 0 """) program = blackbird.loads("""\ name prog version 1.0 Sgate(0.543, -1.432*pi) | 0 Dgate(-0.543, 0.45) | 1 Vac | 2 """)
By applying the
match_template
function, we can match the template parameters:>>> res = match_template(template, program) >>> print(res) {'r': 0.543, 'phi': -1.74938033997029}
Verifying this is correct:
>>> print((-1.432*np.pi+1)/2) -1.7493803399702919
Note
The template and the Blackbird program to match must have the same version number and target, otherwise an
TemplateError
will be raised.- Parameters
template (blackbird.Program) – the Blackbird template
program (blackbird.Program) – a Blackbird program to match against the template
- Returns
mapping from the template parameter name to a numerical value.
- Return type
dict[str, Number]