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 all 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
15 changes: 4 additions & 11 deletions cirq-core/cirq/contrib/svg/svg.py
Original file line number Diff line number Diff line change
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
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 # 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