XanaduAI / blackbird

Blackbird is a quantum assembly language for continuous-variable quantum computation, that can be used to program Xanadu's quantum photonics hardware and Strawberry Fields simulator.
https://quantum-blackbird.readthedocs.io
Apache License 2.0
69 stars 26 forks source link

Adds parametrized template support #13

Closed josh146 closed 5 years ago

josh146 commented 5 years ago

This PR adds support for:

Templates

This PR adds support for creating Blackbird 'templates', that can be loaded and used as parametrized functions. Template parameters use the syntax {parameter_name}, and can be placed within any arithmetic expression. This syntax is inspired by the Python templating library Jinja.

For example, consider the following template file ('teleport.xbt'), with two template parameters {alpha} and {sq}.

name StateTeleportation
version 1.0

# state to be teleported:
Coherent({alpha}) | 0

# teleportation algorithm
Squeezed(-{sq}) | 1
Squeezed({sq}) | 2
BSgate(pi/4, 0) | (1, 2)
BSgate(pi/4, 0) | (0, 1)
MeasureX | 0
MeasureP | 1
Xgate(sqrt(2)*q0) | 2
Zgate(sqrt(2)*q1) | 2

Deserializing the template:

>>> prog = blackbird.load("teleportation.xbt")
>>> prog.is_template()
True
>>> prog.parameters
{'alpha', 'sq'}

We can also use the template to generate static blackbird programs:

>>> prog_new = prog(alpha=0.54, sq=4)

Benefits

Support for templated Blackbird scripts will be advantageous for:

Future work

codecov[bot] commented 5 years ago

Codecov Report

Merging #13 into master will increase coverage by 0.94%. The diff coverage is 100%.

@@            Coverage Diff             @@
##           master      #13      +/-   ##
==========================================
+ Coverage    95.6%   96.54%   +0.94%     
==========================================
  Files          10       12       +2     
  Lines        1182     1475     +293     
==========================================
+ Hits         1130     1424     +294     
+ Misses         52       51       -1
Impacted Files Coverage Δ
blackbird_python/blackbird/listener.py 100% <100%> (ø) :arrow_up:
blackbird_python/blackbird/auxiliary.py 100% <100%> (ø) :arrow_up:
blackbird_python/blackbird/tests/test_utils.py 100% <100%> (ø)
blackbird_python/blackbird/utils.py 100% <100%> (ø)
blackbird_python/blackbird/tests/test_program.py 100% <100%> (ø) :arrow_up:
blackbird_python/blackbird/program.py 100% <100%> (ø) :arrow_up:
blackbird_python/blackbird/tests/test_listener.py 100% <100%> (+0.52%) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update fea01a4...3b8fca5. Read the comment docs.

co9olguy commented 5 years ago

maybe we could overload the select argument as a way to pass placeholders?

co9olguy commented 5 years ago

Please add the "Future work" items to backlog

josh146 commented 5 years ago

All suggested changes applied.