amazon-braket-default-simulator-python icon indicating copy to clipboard operation
amazon-braket-default-simulator-python copied to clipboard

fix: cast node.size to IntegerLiteral for qubit register size

Open rmshaffer opened this issue 1 year ago • 3 comments

Issue #, if available: #240

Description of changes: Starting with the changes from #258 and performing the additional work required to merge to main.

The biggest outstanding items are:

  • [ ] Refactor the new logic in functions.py into a separate helper function, for readability. See this comment thread.
  • [ ] Expand unit tests to ensure correctness of implementation. See this comment thread.
  • [ ] Discuss among maintainers to ensure that the new behavior is actually in line with how we want to support OpenQASM in the default simulator (since the OpenQASM spec isn't entirely clear on all of this).

Testing done: tox

Merge Checklist

Put an x in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your pull request.

General

  • [x] I have read the CONTRIBUTING doc
  • [x] I used the commit message format described in CONTRIBUTING
  • [x] I have updated any necessary documentation, including READMEs and API docs (if appropriate)

Tests

  • [ ] I have added tests that prove my fix is effective or that my feature works (if appropriate)
  • [ ] I have checked that my tests are not configured for a specific region or account (if appropriate)

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

rmshaffer avatar Jun 18 '24 16:06 rmshaffer

@rmshaffer, @DanBlackwell Maybe we should rethink the following case test:

bit[3] b;
qubit["10"] r1;
h r1["01" << "01"];

In this regard the spec explicitly states that shifting, either right or left, is supported by an unsigned integer (https://openqasm.com/language/classical.html#classical-bits-and-registers). Shifting by a bitstring causes a strange behaviour in case its length does not match the length of the specified qbuit array:

bit[3] b;
qubit[3] r1;
h r1["01" << "01"];
IndexError: boolean index did not match indexed array along dimension 0; dimension is 3 but corresponding boolean dimension is 2

Since we are enabling registers sizes to be specified also as bitstring, maybe we should raise an error if the LHS is also a bitstring. This influences the refactoring since, it pertains code regarding shifting operations.

EuGig avatar Jun 19 '24 10:06 EuGig

Maybe slightly related, but I found the following here the other day:

image

I'm not sure whether this is implying that it is valid to cast an array of bits into int and vice versa; but in that case I think that when applying any ops (+ - << >> | & ^ etc) to an 'array of bits' (which a 'bit string literal' is afaik) the best approach is to cast to 'int' rather than reimplement them (and cast back if required). I'm not sure how integer over- and underflows are meant to be handled though.

DanBlackwell avatar Jun 19 '24 10:06 DanBlackwell

Codecov Report

:white_check_mark: All modified and coverable lines are covered by tests. :white_check_mark: Project coverage is 100.00%. Comparing base (95b1b3c) to head (521708d). :warning: Report is 40 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #263   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           48        48           
  Lines         4067      4071    +4     
  Branches       440       441    +1     
=========================================
+ Hits          4067      4071    +4     

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

codecov[bot] avatar Sep 10 '25 18:09 codecov[bot]