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 test_drawer_superimposed(): empty_drawer = TextDiagramDrawer() assert not empty_drawer drawer_with_something = TextDiagramDrawer() drawer_with_something.write(0, 0, 'A') assert drawer_with_something superimposed_drawer = empty_drawer.superimposed(drawer_with_something) assert superimposed_drawer == drawer_with_something assert not empty_drawer
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 __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 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 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()
def test_drawer_eq(): assert TextDiagramDrawer().__eq__(23) == NotImplemented eq = ct.EqualsTester() d = TextDiagramDrawer() d.write(0, 0, 'A') d.write(1, 0, 'B') d.write(1, 1, 'C') alt_d = TextDiagramDrawer() alt_d.write(0, 0, 'A') alt_d.write(1, 0, 'B') alt_d.write(1, 1, 'C') eq.add_equality_group(d, alt_d) dd = TextDiagramDrawer() dd.write(0, 0, 'D') dd.write(0, 1, 'E') dd.write(1, 1, 'F') eq.add_equality_group(dd)
def test_drawer_stack(): d = TextDiagramDrawer() d.write(0, 0, 'A') d.write(1, 0, 'B') d.write(1, 1, 'C') dd = TextDiagramDrawer() dd.write(0, 0, 'D') dd.write(0, 1, 'E') dd.write(1, 1, 'F') vstacked = TextDiagramDrawer.vstack((dd, d)) expected = """ D E F A B C """.strip() assert_has_rendering(vstacked, expected) hstacked = TextDiagramDrawer.hstack((d, dd)) expected = """ A B D C E F """.strip() assert_has_rendering(hstacked, expected) d.force_horizontal_padding_after(0, 0) with pytest.raises(ValueError): TextDiagramDrawer.vstack((dd, d)) dd.force_horizontal_padding_after(0, 0) expected = """ D EF AB C """.strip() vstacked = TextDiagramDrawer.vstack((dd, d)) assert_has_rendering(vstacked, expected) d.force_vertical_padding_after(0, 0) with pytest.raises(ValueError): print(d.vertical_padding) print(dd.vertical_padding) TextDiagramDrawer.hstack((d, dd)) dd.force_vertical_padding_after(0, 0) expected = """ AB D C EF """.strip() hstacked = TextDiagramDrawer.hstack((d, dd)) assert_has_rendering(hstacked, expected) d.force_horizontal_padding_after(0, 0) dd.force_horizontal_padding_after(0, 2) d.force_vertical_padding_after(0, 1) dd.force_vertical_padding_after(0, 3) with pytest.raises(ValueError): TextDiagramDrawer.vstack((d, dd)) vstacked = TextDiagramDrawer.vstack((dd, d), padding_resolver=max) expected = """ D E F A B C """.strip() assert_has_rendering(vstacked, expected) hstacked = TextDiagramDrawer.hstack((d, dd), padding_resolver=max) expected = """ AB D C E F """.strip() assert_has_rendering(hstacked, expected) vstacked_min = TextDiagramDrawer.vstack((dd, d), padding_resolver=min) expected = """ D EF AB C """.strip() assert_has_rendering(vstacked_min, expected) hstacked_min = TextDiagramDrawer.hstack((d, dd), padding_resolver=min) expected = """ AB D C E F """.strip() assert_has_rendering(hstacked_min, expected)
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))