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

[SVG] Noise hack and font fix #3076

Merged
merged 7 commits into from
Jun 9, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions cirq/contrib/svg/svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,25 @@
import cirq

QBLUE = '#1967d2'
FONT = "Arial"


def fixup_text(text: str):
if '\n' in text:
return '?'
if '[<virtual>]' in text:
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems awfully specific.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We can wait for #2984 (comment) and then it will be unnecessary but I'm using this patch currently

Copy link
Collaborator

Choose a reason for hiding this comment

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

This is contrib, so I'd just make sure we have an issue to fix this before moving to cirq, and maybe a link from that issue.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

there's a comment linking to the issue; I'll put a backref on the issue itself

# 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()]', '')
return text


def _get_text_width(t: str) -> float:
if '\n' in t:
return _get_text_width('?')
tp = matplotlib.textpath.TextPath((0, 0), t, size=14, prop='Arial')
t = fixup_text(t)
tp = matplotlib.textpath.TextPath((0, 0), t, size=14, prop=FONT)
bb = tp.get_extents()
return bb.width + 10

Expand All @@ -30,7 +43,8 @@ def _rect(x: float,
def _text(x: float, y: float, text: str, fontsize: int = 14):
"""Draw SVG <text> text."""
return f'<text x="{x}" y="{y}" dominant-baseline="middle" ' \
f'text-anchor="middle" font-size="{fontsize}px">{text}</text>'
f'text-anchor="middle" font-size="{fontsize}px" ' \
f'font-family="{FONT}">{text}</text>'


def _fit_horizontal(tdd: 'cirq.TextDiagramDrawer',
Expand Down Expand Up @@ -208,13 +222,10 @@ def tdd_to_svg(
if v.text == '×':
t += _text(x, y + 3, '×', fontsize=40)
continue
if '\n' in v.text:
t += _rect(boxx, boxy, boxwidth, boxheight)
t += _text(x, y, '?', fontsize=18)
continue

v_text = fixup_text(v.text)
t += _rect(boxx, boxy, boxwidth, boxheight)
t += _text(x, y, v.text, fontsize=14 if len(v.text) > 1 else 18)
t += _text(x, y, v_text, fontsize=14 if len(v_text) > 1 else 18)

t += '</svg>'
return t
Expand Down
9 changes: 9 additions & 0 deletions cirq/contrib/svg/svg_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ def test_svg():
assert '</svg>' in svg_text


def test_svg_noise():
noise_model = cirq.ConstantQubitNoiseModel(cirq.DepolarizingChannel(p=1e-3))
q = cirq.LineQubit(0)
circuit = cirq.Circuit(cirq.X(q))
circuit = cirq.Circuit(noise_model.noisy_moments(circuit.moments, [q]))
svg = circuit_to_svg(circuit)
assert '>D(0.001)</text>' in svg


def test_validation():
with pytest.raises(ValueError):
circuit_to_svg(cirq.Circuit())
Expand Down