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

Escape both <> characters in SVG test labels #6056

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 1 addition & 4 deletions cirq-core/cirq/contrib/svg/svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()]', '')
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we change the if statements above as well so that we don't return as soon as a single if matches?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sure, of course! Done. I have left the first short-circuit if as it does not matter when the string is dropped.

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


Expand Down
21 changes: 11 additions & 10 deletions cirq-core/cirq/contrib/svg/svg_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# pylint: disable=wrong-or-nonexistent-copyright-notice
import pytest
import IPython.display
import numpy as np
import pytest

import cirq
from cirq.contrib.svg import circuit_to_svg
Expand Down Expand Up @@ -59,20 +60,20 @@ def test_empty_moments():
assert '</svg>' in svg_2


def test_gate_with_less_greater_str():
@pytest.mark.parametrize(
'symbol,svg_symbol',
[('<a', '&lt;a'), ('<=b', '&lt;=b'), ('>c', '&gt;c'), ('>=d', '&gt;=d'), ('>e<', '&gt;e&lt;')],
)
def test_gate_with_less_greater_str(symbol, svg_symbol):
class CustomGate(cirq.Gate):
def _num_qubits_(self) -> int:
return 4
return 1

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

circuit = cirq.Circuit(CustomGate().on(*cirq.LineQubit.range(4)))
circuit = cirq.Circuit(CustomGate().on(cirq.LineQubit(0)))
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
assert svg_symbol in svg