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

Update cirq.contrib.svg to escape < and > characters #6055

Merged
merged 2 commits into from
Apr 5, 2023

Conversation

tanujkhattar
Copy link
Collaborator

If we use the symbols < or > in the circuit diagram of any gate right now, circuit_to_svg would result in a string that leads to a parsing error because the less than / greater than symbols are not escaped correctly.

This PR updates the svg generation logic to correctly escape the < and > characters.

@tanujkhattar tanujkhattar marked this pull request as ready for review April 5, 2023 20:32
@tanujkhattar tanujkhattar requested review from a team, vtomole and cduck as code owners April 5, 2023 20:32
@tanujkhattar tanujkhattar requested a review from pavoljuhas April 5, 2023 20:32
Copy link
Collaborator

@viathor viathor left a comment

Choose a reason for hiding this comment

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

Definitely an improvement, but see below.


circuit = cirq.Circuit(CustomGate().on(*cirq.LineQubit.range(4)))
svg = circuit_to_svg(circuit)
import IPython.display
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we need to add ipython to the requirements file and to mypy config.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Since we are using iPython only in the tests and not in the library code itself; we need it add it to the dev requirements. The dev requirements are defined in https://github.com/quantumlib/Cirq/blob/master/dev_tools/requirements/dev.env.txt which imports https://github.com/quantumlib/Cirq/blob/master/dev_tools/requirements/deps/dev-tools.txt which imports https://github.com/quantumlib/Cirq/blob/master/dev_tools/requirements/deps/notebook.txt which imports
https://github.com/quantumlib/Cirq/blob/master/dev_tools/requirements/deps/ipython.txt

Therefore ipython already exists as part of the dev requirements.

To make mypy happy, I've added a # type: ignore for this specific import.

if '<' in text:
return text.replace('<', '&lt;')
if '>' in text:
return text.replace('>', '&gt;')
Copy link
Collaborator

Choose a reason for hiding this comment

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

This of course raises the question whether we've got them all now :-)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I agree. There are other open issues that track improvements to svg drawing utilities; like #4499 and #2313

I'd say that covering all cases is out of scope for this PR. I ran into this specific case myself and the fix added seems to be enough to unblock me.

@tanujkhattar tanujkhattar enabled auto-merge (squash) April 5, 2023 21:36
@tanujkhattar tanujkhattar merged commit cde65be into quantumlib:master Apr 5, 2023
Comment on lines +26 to +29
if '<' in text:
return text.replace('<', '&lt;')
if '>' in text:
return text.replace('>', '&gt;')
Copy link
Collaborator

Choose a reason for hiding this comment

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

Looks like this replaces only one of the <> characters, we can still get some grief for wire_symbol = '>-<'

How about

diff --git a/cirq-core/cirq/contrib/svg/svg.py b/cirq-core/cirq/contrib/svg/svg.py
index b08c8a71..17e8b4ec 100644
--- a/cirq-core/cirq/contrib/svg/svg.py
+++ b/cirq-core/cirq/contrib/svg/svg.py
@@ -23,10 +23,7 @@ def fixup_text(text: str):
     if '[cirq.VirtualTag()]' in text:
         # https://github.com/quantumlib/Cirq/issues/2905
         return text.replace('[cirq.VirtualTag()]', '')
-    if '<' in text:
-        return text.replace('<', '&lt;')
-    if '>' in text:
-        return text.replace('>', '&gt;')
+    text = text.replace('<', '&lt;').replace('>', '&gt;')
     return text

Copy link
Collaborator

Choose a reason for hiding this comment

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

done in #6056

@mpharrigan
Copy link
Collaborator

I have vague memories of running into this bug before and trying to fix it but simply escaping the offending characters caused problems elsewhere(?) I'll try to remember or find evidence of what was going on

@mpharrigan
Copy link
Collaborator

I was thinking of this #2905 (comment)

If you have a gate or something that uses a wire symbol with <stuff> it will behave differently in: 1) terminal ascii 2) jupyter ascii 3) svg.

So this change probably makes SVG handling of < better than ascii output within a notebook :)

harry-phasecraft pushed a commit to PhaseCraft/Cirq that referenced this pull request Oct 31, 2024
* Update cirq.contrib.svg to escape < and > characters

* Add type: ignore for mypy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants