-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Stop forcing mathtext mode for all strings #4669
Stop forcing mathtext mode for all strings #4669
Conversation
In Qiskit#4616 the gate title and labels were changed to always use matplotlib's mathtext mode. [1] This was a breaking change and will result in users having weirdly formatted names and labels. For example places in qiskit itself set gate names like 'Controlled-Evolution^1_dg' which will not get rendered as expected in mathtext mode. The documented behavior for labels and gate names before was to rely on matplotlib's rendering behavior to expliclictly add '$' to the label string for sections that should be rendered in mathtext mode. For example, this behavior was ported to the latex circuit drawer in Qiskit#3224 so that users could take advantage of the same flexibility in how text was rendered by the drawer. This commit removes the forced addition of '$' around all the label strings to go back to the expected behavior. Fixes Qiskit#4667 [1] https://matplotlib.org/3.2.2/tutorials/text/usetex.html
Previously the manual text width detection had a hard coded subset of latex special characters to try and convert a latex string to a text string. This however is quite error prone because it's missing a large portion of valid latex commands. For example, in the previous commit when we added \\mathrm to stop slanting text in the standard gates was missed by this. Instead of maintaing a manual list of commands this commit just switches to use pylatexenc, which is already a visualization requirement for the latex drawers, to do the conversion from latex to unicode. This is much less error prone and will give a true width of the strings even for user supplied gate names which can use any latex commands they want.
Could we change the subscripts to be uppercase and also non-slanted? Looks a bit weird now that in Rx, the x is cursive. Also if we use uppercase it'll be the same as the new IQX (which will hopefully be out soon). |
Done in: ea4735e |
qiskit/visualization/qcstyle.py
Outdated
'rzx': '$\\mathrm{R}_{\\mathrm{ZX}}$', | ||
'rzz': '$\\mathrm{R}_{\\mathrm{ZZ}}$', | ||
'reset': '$\\left|0\\right\\rangle$', | ||
'initialize': '$|psi>$' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'initialize': '$|psi>$' | |
'initialize': '$|\\psi\\rangle$' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mtreinish @Cryoris The latex_to_text method leaves underscores in the output. That's why the multi-rotation Pauli's are wider above. The text width calc thinks there are 4 chars instead of 3. This code at line 268 will take care of it.
It produces, |
On the cu1/zz issue dropping the '$$' from line 760, |
This comit fixes a few edge case with mathmode text spacing. The first is relying on pylatexenc to convert latex to text leaves '_' in for subscripts in math mode strings. This fixes this by finding manually removing those from the output string used for finding label width. The other 2 fixes are from review comments about spacing for cui1/rzz and a minus sign width.
@mtreinish Don't know if you want to do this here or put in a separate issue. I was going through the textbook and found this,
with
|
Until the bot gets back on its feet. Here is the link for the tests! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, I couldn't find any corner cases anymore!
Summary
In #4616 the gate title and labels were changed to always use
matplotlib's mathtext mode. [1] This was a breaking change and will result
in users having weirdly formatted names and labels. For example places in
qiskit itself set gate names like 'Controlled-Evolution^1_dg' which will
not get rendered as expected in mathtext mode. The documented behavior for
labels and gate names before was to rely on matplotlib's rendering
behavior to expliclictly add '$' to the label string for sections that
should be rendered in mathtext mode. For example, this behavior was ported
to the latex circuit drawer in #3224 so that users could take advantage
of the same flexibility in how text was rendered by the drawer. This
commit removes the forced addition of '$' around all the label strings
to go back to the expected behavior.
Details and comments
Fixes #4667
[1] https://matplotlib.org/3.2.2/tutorials/text/mathtext.html