Skip to content

Commit

Permalink
Update cirq.contrib.svg to escape < and > characters (#6055)
Browse files Browse the repository at this point in the history
* Update cirq.contrib.svg to escape < and > characters

* Add type: ignore for mypy
  • Loading branch information
tanujkhattar committed Apr 5, 2023
1 parent 6a97cca commit cde65be
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cirq-core/cirq/contrib/svg/svg.py
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;')
return text


Expand Down
19 changes: 19 additions & 0 deletions cirq-core/cirq/contrib/svg/svg_test.py
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

0 comments on commit cde65be

Please sign in to comment.