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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cirq-core/cirq/contrib/svg/svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ 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;')
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.

Comment on lines +26 to +29
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

return text


Expand Down
19 changes: 19 additions & 0 deletions cirq-core/cirq/contrib/svg/svg_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,22 @@ def test_empty_moments():
svg_2 = circuit_to_svg(cirq.Circuit(cirq.Moment()))
assert '<svg' in svg_2
assert '</svg>' in svg_2


def test_gate_with_less_greater_str():
class CustomGate(cirq.Gate):
def _num_qubits_(self) -> int:
return 4

def _circuit_diagram_info_(self, _) -> cirq.CircuitDiagramInfo:
return cirq.CircuitDiagramInfo(wire_symbols=['<a', '<=b', '>c', '>=d'])

circuit = cirq.Circuit(CustomGate().on(*cirq.LineQubit.range(4)))
svg = circuit_to_svg(circuit)
import IPython.display # type: ignore

_ = IPython.display.SVG(svg)
assert '&lt;a' in svg
assert '&lt;=b' in svg
assert '&gt;c' in svg
assert '&gt;=d' in svg