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

Fix measure with condition display in mpl and latex drawers #6822

Merged
merged 32 commits into from
Sep 3, 2021

Conversation

enavarro51
Copy link
Contributor

@enavarro51 enavarro51 commented Jul 27, 2021

Summary

Fixes #6804

Details and comments

This PR fixes a problem where a measure with condition will sometimes display incorrectly in the 'mpl' and 'latex' drawers. The corrected displays are below.

qr = QuantumRegister(2, "qr")
cr1 = ClassicalRegister(2, "cr1")
cr2 = ClassicalRegister(2, "cr2")
qc = QuantumCircuit(qr, cr1, cr2)
qc.h(0)
qc.h(1)
qc.measure(0, cr1[0])
qc.measure(1, cr2[0]).c_if(cr1, 1)
qc.draw('mpl')

image
image

@TharrmashasthaPV
Copy link
Contributor

Hi @enavarro51 . Nice one! I think supporting measure with condition in latex drawer has some issues. For instance, when cregbundle=False, then the same circuit given above returns as below:

latex_6822

You can notice here that it is hard to distinguish between the conditional bits and the bit on which the measurement is stored. I guess we would be able to overcome this issue if we had different notations for the conditional bits and the storage bit. I understand that it is a completely different issue that what this PR is for. But any opinions on that?

@enavarro51
Copy link
Contributor Author

Good point. It would be nice if there were some kind of arrow so it mimics the mpl drawer. Not sure how that works in Latex.
image

@enavarro51
Copy link
Contributor Author

@TharrmashasthaPV How does this look? Not perfect, but I think it solves your issue.

image

@TharrmashasthaPV
Copy link
Contributor

@TharrmashasthaPV How does this look? Not perfect, but I think it solves your issue.

image

@enavarro51 This certainly is better than having just dots. But I was thinking we could do something like

latex_6822

I guess replacing \\cwx[#1] with \\ar @{<=} [#1,0] should do the trick if I am not wrong. Also in #6447 we were trying to get a minimal latex source code with minimal headers. So, I guess this way we can also be in line with its goal.😃 What do you think?

@enavarro51
Copy link
Contributor Author

@TharrmashasthaPV Very nice! Thought you might have a trick to use. I'll drop it in. Seems to work in all the cases I've tried. I added it to the cregbundle True as well. Is there a way to add a bullet in addition to the '= 1' at the condition creg?

image

@TharrmashasthaPV
Copy link
Contributor

@TharrmashasthaPV Very nice! Thought you might have a trick to use. I'll drop it in. Seems to work in all the cases I've tried. I added it to the cregbundle True as well. Is there a way to add a bullet in addition to the '= 1' at the condition creg?

image

Awesome. For having both the bullet and =1, I think currently we use \dstick{_{_{\hspace{1.5em}=1}}} \cw \cwx[-1] which just gives =1. But alternatively we can use something like \control \cw^(-0.25){^{=1}} \cwx[-1] to have both. The parameter that has been set to -0.25 can be adjusted as per the need I guess.

@enavarro51
Copy link
Contributor Author

Thanks a lot, @TharrmashasthaPV. Sometimes it's great to have someone else write your code for you. 😊 Anyway, I've extended this so the mpl and latex drawers should now show the same information for measures and conditionals. I'm not sure why mpl uses hex for the condition and the other 2 drawers don't. Maybe a discussion for another time. You may need to incorporate these changes in #6248 and #6259 at some point.
image
image
image
image

@javabster
Copy link
Contributor

Not a great reason. After a discussion with Luciano, I added the line onto the previous empty plot to make it more realistic. Not too important either way.

Ok, but how is it relevant to the work in this PR?

@javabster javabster added the Changelog: Bugfix Include in the "Fixed" section of the changelog label Aug 13, 2021
@enavarro51
Copy link
Contributor Author

Uh, not really. Would you like it removed?

@javabster
Copy link
Contributor

Also, could you add a release note please 😄

@javabster
Copy link
Contributor

Uh, not really. Would you like it removed?

Ah ok. Generally it's preferable to keep the content of PRs as relevant as possible to the original issue, and open new issues to cover extra things, but in this case it's quite small and not worth opening a whole new PR for it (and there is already scope creep with the switching to hex conditionals as well anyway) so I think just leave it

@enavarro51
Copy link
Contributor Author

Oh, 'scope creep'. I like that. 😊 Will do the release note.

@enavarro51
Copy link
Contributor Author

Release note added in 056eb8b.

javabster
javabster previously approved these changes Aug 18, 2021
Copy link
Contributor

@javabster javabster left a comment

Choose a reason for hiding this comment

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

This lgtm! Thanks for working on this, just a few merge conflicts left and I think it'll be good to go

@enavarro51
Copy link
Contributor Author

@javabster Merge conflicts fixed in 3bd0c3e.

javabster
javabster previously approved these changes Sep 1, 2021
Copy link
Contributor

@javabster javabster left a comment

Choose a reason for hiding this comment

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

LGTM 🚀

# Add (hex condition value) below the last cwire
control = "\\control" if if_value[creg_size - 1] == "1" else "\\controlo"
self._latex[creg_size + cwire - 1][col] = (
f"{control}" + " \\cw^(%s){^{%s}} \\cwx[-%s]"
Copy link
Contributor

Choose a reason for hiding this comment

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

The fix looks great! I was just curious to know if we could make a small aesthetic fix. In line 627, when we print the hex, it actually gets printed as a string inside a math environment. So it looks like
image
If we could just change this line to

f"{control}" + " \\cw^(%s){^{\\mathtt{%s}}} \\cwx[-%s]"

then we can get something like
image
which I personally think looks more like a hexadecimal. Any thoughts on this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed. That x was a bit too fancy, and the new form is more compact as well. Will make the change.

Copy link
Contributor

@TharrmashasthaPV TharrmashasthaPV left a comment

Choose a reason for hiding this comment

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

Perfect. LGTM.

Copy link
Member

@1ucian0 1ucian0 left a comment

Choose a reason for hiding this comment

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

Thanks!

@mergify mergify bot merged commit 808e66d into Qiskit:main Sep 3, 2021
sakibguy added a commit to sakibguy/qiskit-sdk-py that referenced this pull request Sep 4, 2021
Fix measure with condition display in mpl and latex drawers (Qiskit#6822)
@kdk kdk added this to the 0.19 milestone Nov 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: Bugfix Include in the "Fixed" section of the changelog
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Problem in drawing circuit for conditional measurement using multiple classical registers
5 participants