-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Conversation
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.
Definitely an improvement, but see below.
|
||
circuit = cirq.Circuit(CustomGate().on(*cirq.LineQubit.range(4))) | ||
svg = circuit_to_svg(circuit) | ||
import IPython.display |
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.
I think we need to add ipython to the requirements file and to mypy config.
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.
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('<', '<') | ||
if '>' in text: | ||
return text.replace('>', '>') |
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.
This of course raises the question whether we've got them all now :-)
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.
if '<' in text: | ||
return text.replace('<', '<') | ||
if '>' in text: | ||
return text.replace('>', '>') |
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.
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('<', '<')
- if '>' in text:
- return text.replace('>', '>')
+ text = text.replace('<', '<').replace('>', '>')
return text
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.
done in #6056
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 |
I was thinking of this #2905 (comment) If you have a gate or something that uses a wire symbol with So this change probably makes SVG handling of |
* Update cirq.contrib.svg to escape < and > characters * Add type: ignore for mypy
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.