XanaduAI / strawberryfields

Strawberry Fields is a full-stack Python library for designing, simulating, and optimizing continuous variable (CV) quantum optical circuits.
https://strawberryfields.ai
Apache License 2.0
747 stars 186 forks source link

Keep symbolic expressions for Blackbird programs #596

Closed thisac closed 3 years ago

thisac commented 3 years ago

Context: There are a few issues when attempting to convert an SF circuit with feed-forwarding to Blackbird. If any parameter is found to be symbolic, it's cast as a string rather than keeping it as a blackbird.RegRefTransform as it would be if loaded directly from a script to Blackbird. For example, the following SF circuit

with prog.context as q:
    Coherent(math.sqrt(5/4), math.atan(1/2)) | q[0]
    Sgate(2, 0) | q[2]
    Sgate(-2, 0) | q[1]
    BSgate(0.7854, 0) | (q[1], q[2])
    BSgate(0.7854, 0) | (q[0], q[1])
    MeasureP | q[1]
    MeasureX | q[0]
    Xgate(q[0].par*1.41421356237000) | q[2]
    Zgate(q[1].par*1.41421356237000) | q[2]

will, when transformed into a Blackbird program, converting q[0].par*1.41421356237000 and q[1].par*1.41421356237000 into the strings "1.41421356237000*q0" and "1.41421356237000*q1", rather than, as it should, into blackbird.RegRefTransform objects.

If attempting to load the following blackbird script

name Teleportation
version 1.0
target gaussian

Coherent(1.118033988749895, 0.4636476090008061) | 0
Sgate(2, 0) | 2
Sgate(-2, 0) | 1
BSgate(0.7854, 0) | [1, 2]
BSgate(0.7854, 0) | [0, 1]
MeasureHomodyne(phi=1.5707963267948966) | 1
MeasureHomodyne(phi=0) | 0
Xgate(1.41421356237*q0) | 2
Zgate(1.41421356237*q1) | 2

both 1.41421356237*q0 and 1.41421356237*q0 will be stored as blackbird.RegRefTransform objects.

Description of the Change: The strawberryfields.to_blackbird function is changed so that if an SF parameter contains a regref (e.g. q0 or q1, as in the string representation of the symbolic expression in SF) it stores it as a blackbird.RegRefTransform instead of as a string.

When converting from a Blackbird program to an SF program, the symbolic expression needs to be extracted from the RegRefTransform, so that SF can understand it. This is being added in this Blackbird PR #46.

Benefits: SF circuits with feed-forwarding can now be transformed and serialized into Blackbird and back, without any issues or loss of information.

Possible Drawbacks: None

Related GitHub Issues: Closes #595

codecov[bot] commented 3 years ago

Codecov Report

Merging #596 (1e7d629) into master (6b7b5c5) will increase coverage by 0.01%. The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master     #596      +/-   ##
==========================================
+ Coverage   98.44%   98.45%   +0.01%     
==========================================
  Files          76       76              
  Lines        8752     8758       +6     
==========================================
+ Hits         8616     8623       +7     
+ Misses        136      135       -1     
Impacted Files Coverage Δ
strawberryfields/io.py 96.90% <100.00%> (+0.04%) :arrow_up:
strawberryfields/parameters.py 100.00% <100.00%> (+0.89%) :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 6b7b5c5...1e7d629. Read the comment docs.