def test_draw_entries_and_lines_with_options(): d = TextDiagramDrawer() d.write(0, 0, '!') d.write(6, 2, 'span') d.horizontal_line(y=3, x1=2, x2=8) d.vertical_line(x=7, y1=1, y2=4) assert d.render().strip() == """ ! │ │ span │ │ ─────────────┼─ │ """.strip() assert d.render(use_unicode_characters=False).strip() == """ ! | | span | | -------------+- | """.strip() assert d.render(crossing_char='@').strip() == """ ! │ │ span │ │ ─────────────@─ │ """.strip() assert d.render(horizontal_spacing=0).strip() == """ ! │ │ span│ │ ────────┼ │ """.strip() assert d.render(vertical_spacing=0).strip() == """ ! │ span │ ─────────────┼─ """.strip()
def test_draw_entries_and_lines_with_options(): d = TextDiagramDrawer() d.write(0, 0, '!') d.write(6, 2, 'span') d.horizontal_line(y=3, x1=2, x2=8) d.vertical_line(x=7, y1=1, y2=4) assert d.render().strip() == """ ! │ │ span │ │ ─────────────┼─ │ """.strip() assert d.render(use_unicode_characters=False).strip() == """ ! | | span | | -------------+- | """.strip() assert d.render(crossing_char='@').strip() == """ ! │ │ span │ │ ─────────────@─ │ """.strip() assert d.render(horizontal_spacing=0).strip() == """ ! │ │ span│ │ ────────┼ │ """.strip() assert d.render(vertical_spacing=0).strip() == """ ! │ span │ ─────────────┼─ """.strip()
def test_draw_entries_and_lines_with_emphasize(): d = TextDiagramDrawer() d.write(0, 0, '!') d.write(6, 2, 'span') d.horizontal_line(y=3, x1=2, x2=8, emphasize=True) d.horizontal_line(y=5, x1=2, x2=9, emphasize=False) d.vertical_line(x=7, y1=1, y2=6, emphasize=True) d.vertical_line(x=5, y1=1, y2=7, emphasize=False) _assert_same_diagram( d.render().strip(), """ ! ╷ ╻ │ ┃ │ span ┃ │ ┃ ╺━━━━━┿━━━━━━╋━╸ │ ┃ │ ┃ │ ┃ ╶─────┼──────╂─── │ ┃ │ ╹ │ """.strip())
def assert_has_rendering(actual: TextDiagramDrawer, desired: str, **kwargs) -> None: """Determines if a given diagram has the desired rendering. Args: actual: The text diagram. desired: The desired rendering as a string. **kwargs: Keyword arguments to be passed to actual.render. """ actual_diagram = actual.render(**kwargs) desired_diagram = desired assert actual_diagram == desired_diagram, ( "Diagram's rendering differs from the desired rendering.\n" '\n' 'Actual rendering:\n' '{}\n' '\n' 'Desired rendering:\n' '{}\n' '\n' 'Highlighted differences:\n' '{}\n'.format( actual_diagram, desired_diagram, ct.highlight_text_differences(actual_diagram, desired_diagram), ))
def test_draw_entries_and_lines_with_emphasize(): d = TextDiagramDrawer() d.write(0, 0, '!') d.write(6, 2, 'span') d.grid_line(2, 3, 8, 3, True) d.grid_line(7, 1, 7, 4, True) print(d.render().strip()) assert d.render().strip() == """ ! ┃ ┃ span ┃ ┃ ━━━━━━━━━━━━━━━ ┃ """.strip()
def test_multiline_entries(): d = TextDiagramDrawer() d.write(0, 0, 'hello\nthere') d.write(0, 1, 'next') d.write(5, 1, '1\n2\n3') d.write(5, 2, '4n') d.vertical_line(x=5, y1=1, y2=2) d.horizontal_line(y=1, x1=0, x2=8) _assert_same_diagram( d.render().strip(), """ hello there 1 next──────────2────── 3 │ 4n """.strip(), ) d = TextDiagramDrawer() d.vertical_line(x=0, y1=0, y2=3) d.vertical_line(x=1, y1=0, y2=3) d.vertical_line(x=2, y1=0, y2=3) d.vertical_line(x=3, y1=0, y2=3) d.write(0, 0, 'long line\nshort') d.write(2, 2, 'short\nlong line') _assert_same_diagram( d.render().strip(), """ long line ╷ ╷ ╷ short │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ short │ │ │ long line │ │ │ │ │ """.strip(), )
def __str__(self): diagram = TextDiagramDrawer() for q in self.qubits: diagram.write(q.col, q.row, str(q)) for q2 in self.neighbors_of(q): diagram.grid_line(q.col, q.row, q2.col, q2.row) return diagram.render(horizontal_spacing=3, vertical_spacing=2, use_unicode_characters=True)
def __str__(self): diagram = TextDiagramDrawer() for q in self.qubits: diagram.write(q.col, q.row, str(q)) for q2 in self.neighbors_of(q): diagram.grid_line(q.col, q.row, q2.col, q2.row) return diagram.render( horizontal_spacing=3, vertical_spacing=2, use_unicode_characters=True)
def __str__(self) -> str: diagram = TextDiagramDrawer() dx = min(q.col for q in self) dy = min(q.row for q in self) for q in self: diagram.write(q.col - dx, q.row - dy, str(q)) for q1, q2 in zip(self, self[1:]): diagram.grid_line(q1.col - dx, q1.row - dy, q2.col - dx, q2.row - dy, True) return diagram.render(horizontal_spacing=2, vertical_spacing=1, use_unicode_characters=True)
def __str__(self): diagram = TextDiagramDrawer() dx = min(q.col for q in self) dy = min(q.row for q in self) for q in self: diagram.write(q.col - dx, q.row - dy, str(q)) for q1, q2 in zip(self, self[1:]): diagram.grid_line(q1.col - dx, q1.row - dy, q2.col - dx, q2.row - dy, True) return diagram.render(horizontal_spacing=2, vertical_spacing=1, use_unicode_characters=True)
def _describe_bad_arithmetic_changed_const(registers: Sequence[Union[ int, Sequence['cirq.Qid']]], inputs: List[int], outputs: List[int]) -> str: from cirq.circuits import TextDiagramDrawer drawer = TextDiagramDrawer() drawer.write(0, 0, 'Register Data') drawer.write(1, 0, 'Register Type') drawer.write(2, 0, 'Input Value') drawer.write(3, 0, 'Output Value') for i in range(len(registers)): drawer.write(0, i + 1, str(registers[i])) drawer.write(1, i + 1, 'constant' if isinstance(registers[i], int) else 'qureg') drawer.write(2, i + 1, str(inputs[i])) drawer.write(3, i + 1, str(outputs[i])) return ( "A register cannot be set to an int (a classical constant) unless its " "value is not affected by the operation.\n" "\nExample case where a constant changed:\n" + drawer.render(horizontal_spacing=1, vertical_spacing=0))
def test_draw_entries_and_lines_with_emphasize(): d = TextDiagramDrawer() d.write(0, 0, '!') d.write(6, 2, 'span') d.horizontal_line(y=3, x1=2, x2=8, emphasize=True) d.horizontal_line(y=5, x1=2, x2=9, emphasize=False) d.vertical_line(x=7, y1=1, y2=6, emphasize=True) d.vertical_line(x=5, y1=1, y2=7, emphasize=False) assert d.render().strip() == """ ! │ ┃ │ ┃ │ span ┃ │ ┃ ━━━━━━┿━━━━━━╋━ │ ┃ │ ┃ │ ┃ ──────┼──────╂─── │ ┃ │ │ """.strip()