Skip to content

Commit 9bad845

Browse files
authored
Escape both <> characters in SVG test labels (quantumlib#6056)
* Test escaping both `<>` symbols in SVG Also parametrize test_gate_with_less_greater_str so it is easier to add checks for more wire symbols. * Escape both `<>` symbols when used as symbols in SVG * Skip typecheck at import of IPython.display * Test short-circuit fixup of a multi-line SVG text Strings with `\n` are replaced with '?'. * Test SVG replacements are all in effect * Apply all fixups to SVG string labels
1 parent fdd9698 commit 9bad845

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

cirq/contrib/svg/svg.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,10 @@ def fixup_text(text: str):
1616
# https://github.com/quantumlib/Cirq/issues/4499
1717
# TODO: Visualize Custom MatrixGate
1818
return '?'
19-
if '[<virtual>]' in text:
20-
# https://github.com/quantumlib/Cirq/issues/2905
21-
# TODO: escape angle brackets when you actually want to display tags
22-
return text.replace('[<virtual>]', '') # coverage: ignore
23-
if '[cirq.VirtualTag()]' in text:
24-
# https://github.com/quantumlib/Cirq/issues/2905
25-
return text.replace('[cirq.VirtualTag()]', '')
26-
if '<' in text:
27-
return text.replace('<', '&lt;')
28-
if '>' in text:
29-
return text.replace('>', '&gt;')
19+
# https://github.com/quantumlib/Cirq/issues/2905
20+
text = text.replace('[<virtual>]', '')
21+
text = text.replace('[cirq.VirtualTag()]', '')
22+
text = text.replace('<', '&lt;').replace('>', '&gt;')
3023
return text
3124

3225

cirq/contrib/svg/svg_test.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# pylint: disable=wrong-or-nonexistent-copyright-notice
2-
import pytest
2+
import IPython.display # type: ignore
33
import numpy as np
4+
import pytest
45

56
import cirq
67
from cirq.contrib.svg import circuit_to_svg
@@ -20,6 +21,7 @@ def test_svg():
2021
cirq.MatrixGate(np.eye(2)).on(a),
2122
)
2223
)
24+
assert '?' in svg_text
2325
assert '<svg' in svg_text
2426
assert '</svg>' in svg_text
2527

@@ -59,20 +61,27 @@ def test_empty_moments():
5961
assert '</svg>' in svg_2
6062

6163

62-
def test_gate_with_less_greater_str():
64+
@pytest.mark.parametrize(
65+
'symbol,svg_symbol',
66+
[
67+
('<a', '&lt;a'),
68+
('<=b', '&lt;=b'),
69+
('>c', '&gt;c'),
70+
('>=d', '&gt;=d'),
71+
('>e<', '&gt;e&lt;'),
72+
('A[<virtual>]B[cirq.VirtualTag()]C>D<E', 'ABC&gt;D&lt;E'),
73+
],
74+
)
75+
def test_gate_with_less_greater_str(symbol, svg_symbol):
6376
class CustomGate(cirq.Gate):
6477
def _num_qubits_(self) -> int:
65-
return 4
78+
return 1
6679

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

70-
circuit = cirq.Circuit(CustomGate().on(*cirq.LineQubit.range(4)))
83+
circuit = cirq.Circuit(CustomGate().on(cirq.LineQubit(0)))
7184
svg = circuit_to_svg(circuit)
72-
import IPython.display # type: ignore
7385

7486
_ = IPython.display.SVG(svg)
75-
assert '&lt;a' in svg
76-
assert '&lt;=b' in svg
77-
assert '&gt;c' in svg
78-
assert '&gt;=d' in svg
87+
assert svg_symbol in svg

0 commit comments

Comments
 (0)