Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds parametrized template support #13

Merged
merged 9 commits into from
May 22, 2019
Merged

Adds parametrized template support #13

merged 9 commits into from
May 22, 2019

Conversation

josh146
Copy link
Member

@josh146 josh146 commented May 19, 2019

This PR adds support for:

  • Parametrized Blackbird templates

  • Pattern matching of template parameters (blackbird.utils.match_template())

  • a utility function for converting a Blackbird program to a NetworkX directed acyclic graph (blackbird.utils.to_DiGraph())

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:

  • Variational quantum circuits, where the circuit need only be compiled once, but certain parameters can be updated independently

  • Serializing/deserializing parametrized quantum programs in Strawberry Fields

  • Communicating template parameters (for example, large or sparse graphs) separately from the blackbird template.

Future work

  • Support for including templates in Blackbird scripts will be added. Example:

    name coherent_teleport
    version 1.0
    # include the template file
    include "state_teleportation.xbt"
    target gaussian (shots=1000)
    
    # state to be teleported:
    Coherent(0.543) | 0
    # call the template, using its 'name', and passing
    # required values of template parameters
    StateTeleportation(sq=4) | [0, 1, 2]
    MeasureHeterodyne() | 2
  • Load a template into Strawberry Fields, to create a 'program factory':

    template = sf.load('state_teleportation.xbt')
    eng.run(template(sq=2), backend='fock')
  • Save a parametrized program from Strawberry Fields, to create a template. This will necessitate adding some sort of 'placeholder' variable class to Strawberry Fields.

    import strawberryfields as sf
    from strawberryfields import tparam
    
    prog = sf.Program(2)
    
    with prog.context as q:
      Dgate(0.54) | q[0]
      Sgate(tparam('r'), 0.543) | q[0]
    
    # can run the program
    eng.run(prog(r=0.1), backend='gaussian')
    # or save it as a template
    sf.save('new_template.xbt', prog)

@josh146 josh146 requested a review from co9olguy May 19, 2019 02:01
@codecov
Copy link

codecov bot commented May 19, 2019

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%> (ø) ⬆️
blackbird_python/blackbird/auxiliary.py 100% <100%> (ø) ⬆️
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%> (ø) ⬆️
blackbird_python/blackbird/program.py 100% <100%> (ø) ⬆️
blackbird_python/blackbird/tests/test_listener.py 100% <100%> (+0.52%) ⬆️

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.

@josh146 josh146 requested review from trbromley and smite May 19, 2019 02:19
@co9olguy
Copy link
Member

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

blackbird_python/blackbird/listener.py Outdated Show resolved Hide resolved
blackbird_python/blackbird/listener.py Show resolved Hide resolved
blackbird_python/blackbird/program.py Outdated Show resolved Hide resolved
blackbird_python/blackbird/program.py Outdated Show resolved Hide resolved
blackbird_python/blackbird/program.py Show resolved Hide resolved
blackbird_python/blackbird/utils.py Show resolved Hide resolved
blackbird_python/blackbird/utils.py Outdated Show resolved Hide resolved
blackbird_python/blackbird/utils.py Outdated Show resolved Hide resolved
blackbird_python/blackbird/utils.py Show resolved Hide resolved
blackbird_python/blackbird/utils.py Outdated Show resolved Hide resolved
@co9olguy
Copy link
Member

Please add the "Future work" items to backlog

josh146 and others added 3 commits May 20, 2019 14:28
Co-Authored-By: Nathan Killoran <co9olguy@users.noreply.github.com>
@josh146
Copy link
Member Author

josh146 commented May 20, 2019

All suggested changes applied.

@josh146 josh146 requested a review from co9olguy May 20, 2019 22:12
doc/syntax.rst Outdated Show resolved Hide resolved
Co-Authored-By: Nathan Killoran <co9olguy@users.noreply.github.com>
co9olguy
co9olguy previously approved these changes May 22, 2019
Copy link
Member

@co9olguy co9olguy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

left 2 more minor changes. Otherwise good to go!

@josh146 josh146 dismissed stale reviews from co9olguy via 3b8fca5 May 22, 2019 01:30
@josh146 josh146 merged commit d55236d into master May 22, 2019
@josh146 josh146 deleted the match_template branch May 22, 2019 01:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants