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 dual of mpl mathtext syntax for latex drawer labels #3224

Merged
merged 18 commits into from
Oct 28, 2019

Conversation

mtreinish
Copy link
Member

Summary

This commit adds a new feature to the latex drawer that adds support for
a similar syntax to matplotlib's mathtext to labels on the latex drawer.
In matplotlib if you specify a string between two dollar signs (for
example $\gamma$) that string will be parsed with mpl's native TeX
expression parser and that will be used to layout and render the string
per the TeX syntax. This uses the same syntax as mathtext, and enables
users to write raw latex syntax as gate labels between a pair of dollar
signs. All characters outside of the dollar signs are treated as utf8
and get encoded by pylatexenc to create valid latex, however all text
inside the dollar signs get passed verbatim to the output latex. This
enables users to mix and match having their unicode encoded for them and
manually passing latex

Details and comments

Fixes #3171

This commit adds a new feature to the latex drawer that adds support for
a similar syntax to matplotlib's mathtext to labels on the latex drawer.
In matplotlib if you specify a string between two dollar signs (for
example $\gamma$) that string will be parsed with mpl's native TeX
expression parser and that will be used to layout and render the string
per the TeX syntax. This uses the same syntax as mathtext, and enables
users to write raw latex syntax as gate labels between a pair of dollar
signs. All characters outside of the dollar signs are treated as utf8
and get encoded by pylatexenc to create valid latex, however all text
inside the dollar signs get passed verbatim to the output latex. This
enables users to mix and match having their unicode encoded for them and
manually passing latex

Fixes Qiskit#3171
@mtreinish mtreinish requested a review from ajavadia October 8, 2019 18:22
@mtreinish mtreinish added the Changelog: New Feature Include in the "Added" section of the changelog label Oct 8, 2019
ajavadia
ajavadia previously approved these changes Oct 8, 2019
Copy link
Member

@ajavadia ajavadia left a comment

Choose a reason for hiding this comment

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

awesome thanks

@mtreinish
Copy link
Member Author

Just for everyone's reference taking the example the release notes and running it locally will yield:

foo

@mtreinish mtreinish requested a review from kdk as a code owner October 10, 2019 13:58
@1ucian0 1ucian0 merged commit fb87e7c into Qiskit:master Oct 28, 2019
@mtreinish mtreinish deleted the mpl-latexmode-for-latex-drawer branch October 28, 2019 17:55
mtreinish added a commit to mtreinish/qiskit-core that referenced this pull request Nov 14, 2019
In Qiskit#3459 the latex circuit drawer recently received support for drawing
with symbolic parameters. However, the review of that PR it was raised
that the latex drawer does not currently have support for drawing
parameters with unicode characters in them. For example you would have
to write out theta instead of using the chracter θ. This limits how
people could use the latex drawer for representing parameteric circuits.
This commit fixes this issue by checking in the latex drawer if the
parameter is symbolic or numeric and then passing that value to either
generate_latex_label() or pi_check() respectively. This will let the
strings representing symbolic parameters either use unicode characters
and have that work in the output latex, or use the math mode for labels
introduced in Qiskit#3224 as the symbol name to use arbitrary latex.
mergify bot pushed a commit that referenced this pull request Nov 20, 2019
* Handle unicode parameters in latex circuit drawer

In #3459 the latex circuit drawer recently received support for drawing
with symbolic parameters. However, the review of that PR it was raised
that the latex drawer does not currently have support for drawing
parameters with unicode characters in them. For example you would have
to write out theta instead of using the chracter θ. This limits how
people could use the latex drawer for representing parameteric circuits.
This commit fixes this issue by checking in the latex drawer if the
parameter is symbolic or numeric and then passing that value to either
generate_latex_label() or pi_check() respectively. This will let the
strings representing symbolic parameters either use unicode characters
and have that work in the output latex, or use the math mode for labels
introduced in #3224 as the symbol name to use arbitrary latex.

* Fix lint
mtreinish added a commit to mtreinish/qiskit-core that referenced this pull request Jul 8, 2020
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
mergify bot pushed a commit that referenced this pull request Jul 28, 2020
* Stop forcing mathtext mode for all strings

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.

Fixes #4667

[1] https://matplotlib.org/3.2.2/tutorials/text/usetex.html

* Also fix parameters

* Add explicit math mode to default gate names where necessary

* Remove conversions assuming hard coded mathtext mode

* Don't slant letters for standard gates

* Stop hard coding latex characters and use pylatexenc

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.

* Use mathtext for U_* standard gates too

* Add pylatexenc to binder build

* Add release note

* Don't slant standard gate subscript and ALL CAPS

* Fix spacing with mathmode labels

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.

* Use \psi \rangle for initialize label

* Fix oversight in mathmode handling

* new references

Co-authored-by: Luciano Bello <luciano.bello@ibm.com>
Co-authored-by: Julien Gacon <jules.gacon@googlemail.com>
faisaldebouni pushed a commit to faisaldebouni/qiskit-terra that referenced this pull request Aug 5, 2020
* Add dual of mpl mathtext syntax for latex drawer labels

This commit adds a new feature to the latex drawer that adds support for
a similar syntax to matplotlib's mathtext to labels on the latex drawer.
In matplotlib if you specify a string between two dollar signs (for
example $\gamma$) that string will be parsed with mpl's native TeX
expression parser and that will be used to layout and render the string
per the TeX syntax. This uses the same syntax as mathtext, and enables
users to write raw latex syntax as gate labels between a pair of dollar
signs. All characters outside of the dollar signs are treated as utf8
and get encoded by pylatexenc to create valid latex, however all text
inside the dollar signs get passed verbatim to the output latex. This
enables users to mix and match having their unicode encoded for them and
manually passing latex

Fixes Qiskit#3171

* Fix lint

* Use jupyter-execute instead of describing diagram

* Revert "Use jupyter-execute instead of describing diagram"

This reverts commit ab7bc18. Running
the code example in jupyter-execute will require a latex distribution
with qcircuit to be installed. That adds a lot of run time to the doc
build and a non-obvious dependency. To avoid this, we'll just rely on
the code example and a description of the output.
faisaldebouni pushed a commit to faisaldebouni/qiskit-terra that referenced this pull request Aug 5, 2020
* Handle unicode parameters in latex circuit drawer

In Qiskit#3459 the latex circuit drawer recently received support for drawing
with symbolic parameters. However, the review of that PR it was raised
that the latex drawer does not currently have support for drawing
parameters with unicode characters in them. For example you would have
to write out theta instead of using the chracter θ. This limits how
people could use the latex drawer for representing parameteric circuits.
This commit fixes this issue by checking in the latex drawer if the
parameter is symbolic or numeric and then passing that value to either
generate_latex_label() or pi_check() respectively. This will let the
strings representing symbolic parameters either use unicode characters
and have that work in the output latex, or use the math mode for labels
introduced in Qiskit#3224 as the symbol name to use arbitrary latex.

* Fix lint
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: New Feature Include in the "Added" section of the changelog
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow formatting in names of custom gates
3 participants