Skip to content

Commit

Permalink
Escape both <> characters in SVG test labels (#6056)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
pavoljuhas committed Apr 7, 2023
1 parent cde65be commit ae62250
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
15 changes: 4 additions & 11 deletions cirq-core/cirq/contrib/svg/svg.py
Expand Up @@ -16,17 +16,10 @@ def fixup_text(text: str):
# https://github.com/quantumlib/Cirq/issues/4499
# TODO: Visualize Custom MatrixGate
return '?'
if '[<virtual>]' in text:
# https://github.com/quantumlib/Cirq/issues/2905
# TODO: escape angle brackets when you actually want to display tags
return text.replace('[<virtual>]', '') # coverage: ignore
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;')
# https://github.com/quantumlib/Cirq/issues/2905
text = text.replace('[<virtual>]', '')
text = text.replace('[cirq.VirtualTag()]', '')
text = text.replace('<', '&lt;').replace('>', '&gt;')
return text


Expand Down
29 changes: 19 additions & 10 deletions cirq-core/cirq/contrib/svg/svg_test.py
@@ -1,6 +1,7 @@
# pylint: disable=wrong-or-nonexistent-copyright-notice
import pytest
import IPython.display # type: ignore
import numpy as np
import pytest

import cirq
from cirq.contrib.svg import circuit_to_svg
Expand All @@ -20,6 +21,7 @@ def test_svg():
cirq.MatrixGate(np.eye(2)).on(a),
)
)
assert '?' in svg_text
assert '<svg' in svg_text
assert '</svg>' in svg_text

Expand Down Expand Up @@ -59,20 +61,27 @@ 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;'),
('A[<virtual>]B[cirq.VirtualTag()]C>D<E', 'ABC&gt;D&lt;E'),
],
)
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

0 comments on commit ae62250

Please sign in to comment.