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

Add support for lists in kwargs (in operations) #23

Merged
merged 7 commits into from
May 25, 2020
Merged

Conversation

thisac
Copy link
Contributor

@thisac thisac commented May 23, 2020

Context: Currently Blackbird doesn't support lists in keyword arguments in operations which causes problems with e.g. the select and dark_counts; for example, MeasureHomodyne(select=[0, 1]) or MeasureFock(dark_counts=[1, 0.5, 3]).

Description: A new parser rule called vallist is added, and the kwarg parser rule is updated to support it. _get_arguments in auxiliary.py is also updated to support the handling of lists as keyword arguments.

Benefits: Lists can now be used in operations as values for keyword arguments. This allows e.g. the loading and parsing of Blackbird scripts containing this.

Drawbacks: N/A

@thisac thisac requested a review from josh146 May 23, 2020 01:56
@thisac thisac self-assigned this May 23, 2020
@thisac thisac added the enhancement New feature or request label May 23, 2020
@codecov
Copy link

codecov bot commented May 23, 2020

Codecov Report

Merging #23 into master will increase coverage by 0.10%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master      #23      +/-   ##
==========================================
+ Coverage   96.74%   96.84%   +0.10%     
==========================================
  Files          12       12              
  Lines        1596     1615      +19     
==========================================
+ Hits         1544     1564      +20     
+ Misses         52       51       -1     
Impacted Files Coverage Δ
blackbird_python/blackbird/auxiliary.py 100.00% <100.00%> (ø)
blackbird_python/blackbird/tests/test_listener.py 100.00% <100.00%> (+0.35%) ⬆️

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 763e0eb...44506b8. Read the comment docs.

@@ -1,10 +1,11 @@
# Generated from blackbird.g4 by ANTLR 4.7.1
# Generated from src/blackbird.g4 by ANTLR 4.8
Copy link
Member

Choose a reason for hiding this comment

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

Out of curiosity, does this affect the version of antlr4-python3-runtime we are currently pinning in setup.py and requirements.txt? We're currently pinning 4.7.1, but this might require us to upgrade the requirements

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I updated this in the requirements since I happened to use antlr4-python3-runtime=4.8 while generating the files. Could there be any other issues with this that I'm missing?

Copy link
Member

Choose a reason for hiding this comment

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

Nope, should be fine! I shouldn't have commented until I read the PR to the end 😆

Copy link
Member

@josh146 josh146 left a comment

Choose a reason for hiding this comment

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

Nice work @thisac! ANTLR is not the easiest library to get installed, let alone work with.

Your solution looks clear and concise as well 👍 This doesn't allow lists for args, but I don't think that's too much of an issue (I don't think there is any case where an non-named argument will be passed as a list).

Couple of requests, however:

  • Don't forget to update setup.py with the new antlr4-python3-runtime version!

  • Might be worth also testing the case dark_counts=[1], to ensure that it works correctly for a list of length 1

  • In addition, it would be good to test an operation that contains a combination of args, kwargs, and kwargs with lists. If none exists, I think it's fine just to make one up for the test.

Comment on lines 259 to 264
def test_operation_kwarglist(self, parse_input_mocked_metadata):
"""Test that an operation with keyword arguments is correctly parsed"""
bb = parse_input_mocked_metadata("MeasureFock(dark_counts=[1, 3]) | [0, 1]\n")
assert bb.operations == [
{"modes": [0, 1], "op": "MeasureFock", "args": [], "kwargs": {"dark_counts": [1, 3]}}
]
Copy link
Member

Choose a reason for hiding this comment

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

🔥

@@ -1,4 +1,4 @@
antlr4-python3-runtime==4.7.2
antlr4-python3-runtime==4.8
Copy link
Member

Choose a reason for hiding this comment

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

Answered my question from above 🙂

@thisac
Copy link
Contributor Author

thisac commented May 24, 2020

Your solution looks clear and concise as well This doesn't allow lists for args, but I don't think that's too much of an issue (I don't think there is any case where an non-named argument will be passed as a list).

Thanks @josh146. I deliberately chose not to implement list support for args, since I'm not sure if that's currently needed or wanted. I chose to focus on the specific problem with the select and dark_counts kwargs. It should be very quick to implement in the future though if needed.

Couple of requests, however:

  • Don't forget to update setup.py with the new antlr4-python3-runtime version!
  • Might be worth also testing the case dark_counts=[1], to ensure that it works correctly for a list of length 1
  • In addition, it would be good to test an operation that contains a combination of args, kwargs, and kwargs with lists. If none exists, I think it's fine just to make one up for the test.

Done! I expanded the tests to include testing single element lists and empty lists, as well as combinations of args, kwargs and kwargs with lists.

@thisac thisac requested a review from josh146 May 24, 2020 22:20
josh146
josh146 previously approved these changes May 25, 2020
Copy link
Member

@josh146 josh146 left a comment

Choose a reason for hiding this comment

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

New tests look awesome 💯

@josh146
Copy link
Member

josh146 commented May 25, 2020

@thisac: I had a look at the coverage report. My initial instinct was that Codecov was messing up, but it looks like these two lines are not covered:

elif v.nonnumeric():
    kwargslist.append(_literal(v.nonnumeric()))

It should be sufficient to simply add lists that contain bools or strings to your parametrized dc data. Once done, happy for this to be merged in!

@thisac
Copy link
Contributor Author

thisac commented May 25, 2020

Thanks @josh146 for finding the testing issue. I looked at it quickly before and also thought it was Codecov doing weird things again. Added a small variety of cases with strings and bools in the lists. Seems to pass now! 😄

Copy link
Member

@josh146 josh146 left a comment

Choose a reason for hiding this comment

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

Perfect!

@thisac thisac merged commit 688c900 into master May 25, 2020
@thisac thisac deleted the list_kwarg_support branch May 25, 2020 19:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants