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

Hotfix for minus sign in DRAG shape #869

Merged
merged 1 commit into from
Apr 15, 2024
Merged

Hotfix for minus sign in DRAG shape #869

merged 1 commit into from
Apr 15, 2024

Conversation

stavros11
Copy link
Member

Fixes #826. We were originally planning to postpone this to 0.2, where it is automatically fixed, however since it is relevant for some experiments done now (see qiboteam/qibocal#689 (comment)) and the fix was easy, it would be good to merge it now.

I repeated the experiments from qiboteam/qibocal#689 (review) using Sergi's runcards from the reports uploaded in that comment before and after DRAG calibration. Here are my reports after the sign correction:

Before: http://login.qrccluster.com:9000/2LXRRtnlTV-dnJXpxoN7BQ==
After: http://login.qrccluster.com:9000/Ov8k7evxSfuy7GburXvBiQ==

The difference is small, but I believe DRAG now works more consistently over all qubits (in contrast to before that was only working for qubit 0 for which beta > 0). @igres26 @andrea-pasquale you can also confirm if you like.

(@alecandido maybe you can double check the fix - it is only one line but it's in regex which, as you know, is my favorite!)

@stavros11 stavros11 added bug Something isn't working urgent labels Apr 6, 2024
Copy link

codecov bot commented Apr 6, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 66.67%. Comparing base (4ae5445) to head (c6945e6).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #869   +/-   ##
=======================================
  Coverage   66.67%   66.67%           
=======================================
  Files          55       55           
  Lines        5902     5902           
=======================================
  Hits         3935     3935           
  Misses       1967     1967           
Flag Coverage Δ
unittests 66.67% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -218,7 +218,7 @@ def eval(value: str) -> "PulseShape":
shape_name = re.findall(r"(\w+)", value)[0]
if shape_name not in globals():
raise ValueError(f"shape {value} not found")
shape_parameters = re.findall(r"[\w+\d\.\d]+", value)[1:]
shape_parameters = re.findall(r"[-\w+\d\.\d]+", value)[1:]
Copy link
Member

Choose a reason for hiding this comment

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

Premise: this is in eval that is already replaced in 0.2, so if it's passing, I wouldn't care that much.

[abcd] matches any character within (once), and [abcd]+ matches one or more of occurrences of any characters in the list.
Thus having twice \d (a digit) makes no sense.

I assume [...] has been used in place of (...), that is just a group (so the elements inside have to be matched in the order they are written).

\w is a word character (i.e. a digit or letter or _), and I don't see how you need that for the parameters (unless you were writing them as kwargs, but then you'd need also an =), so it's essentially matching just digits, and we're lucky that [...] is simply making it ignored (since \d twice and \w just means \w, inside [], and it's going to match only digits).

Copy link
Member

Choose a reason for hiding this comment

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

I believe that the pattern was meant to match numbers, and not ,, that are used as a separator.

Thus the pattern should have been:

r"[+-]?\d+(\.\d*)?([eE][+-]?\d+)?"

I.e.:

  • either + or - or none of them (but not both)
  • then one or more digits
  • possibly a . followed by zero or more digits
  • possibly a group, led by an e (upper or lower case), with either + or - or none of them (but not both) and then one or more digits

I didn't test it, but if you want to confirm you can look for a float pattern, or just test yourself.

Copy link
Member

Choose a reason for hiding this comment

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

However, since the idea is just parsing comma-separated float (or integers, that are float compatible), I'd suggest just the following:

shape_parameters = [float(p) for p in value.split(",")]

Unless there are also booleans involved (for which you should try one or the other).

In any case, I'd convert to a programmatic parse (leveraging standard utilities) rather than using regexes...

Copy link
Member Author

@stavros11 stavros11 Apr 7, 2024

Choose a reason for hiding this comment

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

Thanks for the summary @alecandido. I actually learnt several things from these comments (which I could most likely also find online, but thanks for summarizing). Generally the idea behind this PR was exactly

Premise: this is in eval that is already replaced in 0.2, so if it's passing, I wouldn't care that much.

so I did a fix that required the minimal effort (and time) and since both pytests and the hardware check seemed to work, I didn't care much more.

Also, note

# TODO: create multiple tests to prove regex working correctly

which probably means that it is known that the current approach is not robust. Also, none of these approaches will work for the Custom shape.

shape_parameters = [float(p) for p in value.split(",")]

Indeed this is cleaner, however I think some additional manipulations are needed, because value also contains the name of the shape. For example value may be Drag(5, 0.02), so we would first need to extract 5, 0.02 (using regex or some other string gymnastics) and then split.

Again, given that this will be irrelevant pretty soon (and also none of these approaches is acceptable for Custom), I would leave as it is, and merge if someone else confirms it fixes the issue in the qibocal discussion.

Copy link
Member

Choose a reason for hiding this comment

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

I thought that value was only the inside of parentheses, since parentheses otherwise you would match the name as well (but, well, maybe that's what it was desired).

In that case, you could split it in two, and match the name and parameters, and then parse the parameters separately.
Something like:

match_ = re.fullmatch(r"(\w)\(.*\)", value)
name = match_[1]
shape_parameters = [float(p) for p in match_[2].split(",")]

(to be tested, I just wrote it in GitHub).

And yes, not even this one would work for the Custom, but definitely, I wouldn't solve the problem at this level now. And if it's working for what you need, just keep it as it is.

@alecandido
Copy link
Member

Given that this is a temporary patch, and only thought to work for the cases we have at hand, I will leave the approval to actual "users" (i.e. @igres26 and @andrea-pasquale)

Copy link
Contributor

@andrea-pasquale andrea-pasquale left a comment

Choose a reason for hiding this comment

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

Thank you for the quick fix @stavros11.
The fix was as expected: http://login.qrccluster.com:9000/YTEAvL9UQ5-dG7mHgjGLNQ==

Copy link
Contributor

@igres26 igres26 left a comment

Choose a reason for hiding this comment

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

Seems to work well for what I can see. After the comments from @alecandido we can merge.

@alecandido
Copy link
Member

Thanks @andrea-pasquale and @igres26, I have no further comment.

@stavros11, you can proceed merging!

@stavros11 stavros11 merged commit 1db988e into main Apr 15, 2024
30 checks passed
@stavros11 stavros11 deleted the fixdrag branch April 15, 2024 12:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working urgent
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Sign of pulse parameters is not deserialized properly
4 participants