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

Add post_selection and feed_forward properties to program. #570

Closed felipeoyarce closed 3 years ago

felipeoyarce commented 3 years ago

Context: As the related issue #302 states

there is no nice way of checking whether a circuit has feed-forwarding or post-selection.

Description of the Change:

New properties added to Program that return True/False for when feed-forwarding and/or post-selection is used. These properties are post-selection and feed-forward.

Benefits:

Improves the developer experience because now you can just you use these new properties instead of having to check each operation in the circuit each time you want to run it.

Sample old code for checking this using try-except:

for c in program.circuit:
    try:
        if c.op.select:
            print("post-selection is used")
    except AttributeError:
        pass

    if c.op.measurement_deps:
        print("feed-forwarding is used")

Possible Drawbacks: None

Related GitHub Issues: Issue #302

felipeoyarce commented 3 years ago

Hey @thisac feel free to test these properties combining more operations, they should work properly :hugs:

codecov[bot] commented 3 years ago

Codecov Report

Merging #570 (0eefe17) into master (bcffdee) will increase coverage by 0.00%. The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master     #570   +/-   ##
=======================================
  Coverage   98.38%   98.38%           
=======================================
  Files          75       75           
  Lines        8399     8411   +12     
=======================================
+ Hits         8263     8275   +12     
  Misses        136      136           
Impacted Files Coverage Δ
strawberryfields/program.py 100.00% <100.00%> (ø)

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 bcffdee...0eefe17. Read the comment docs.

felipeoyarce commented 3 years ago

Thanks @felipeoyarce. It seems like a lot of the tests have changed slightly (perhaps due to running black on the tests files). It would be good to refrain from changing these here and only focus on the functionality you've added.

Otherwise it looks good, apart from a few changes in some docstrings, e.g. code being represented by double backticks (`) instead of single () ones due to us using Sphinx for the docs.

Thanks @thisac!

josh146 commented 3 years ago

Hi @felipeoyarce, thanks for this PR! We have a SF feature freeze at the moment, but will be able to merge this PR in early next week :)

felipeoyarce commented 3 years ago

Sure! Thanks @josh146 and @thisac :hugs:

co9olguy commented 3 years ago

Thanks for the contrib @felipeoyarce! Don't forget to update the changelog with your name and contribution :slightly_smiling_face:

felipeoyarce commented 3 years ago

Thanks for the contrib @felipeoyarce! Don't forget to update the changelog with your name and contribution

Oh, thanks @co9olguy :smile:

co9olguy commented 3 years ago

@felipeoyarce @thisac Another reminder to add this contribution to the changelog and take credit for your nice contribution :slightly_smiling_face: