예제 #1
0
    def __str__(self) -> str:
        # If all qubits are grid qubits, render an appropriate text diagram.
        if all(isinstance(q, devices.GridQubit) for q in self.qubits):
            diagram = circuits.TextDiagramDrawer()

            qubits = cast(List['cirq.GridQubit'], self.qubits)

            # Don't print out extras newlines if the row/col doesn't start at 0
            min_col = min(q.col for q in qubits)
            min_row = min(q.row for q in qubits)

            for q in qubits:
                diagram.write(q.col - min_col, q.row - min_row, str(q))

            # Find pairs that are connected by two-qubit gates.
            Pair = Tuple['cirq.GridQubit', 'cirq.GridQubit']
            pairs = {
                cast(Pair, pair)
                for gate_defs in self.gate_definitions.values()
                for gate_def in gate_defs if gate_def.number_of_qubits == 2
                for pair in gate_def.target_set if len(pair) == 2
            }

            # Draw lines between connected pairs. Limit to horizontal/vertical
            # lines since that is all the diagram drawer can handle.
            for q1, q2 in sorted(pairs):
                if q1.row == q2.row or q1.col == q2.col:
                    diagram.grid_line(q1.col - min_col, q1.row - min_row,
                                      q2.col - min_col, q2.row - min_row)

            return diagram.render(horizontal_spacing=3,
                                  vertical_spacing=2,
                                  use_unicode_characters=True)

        return super().__str__()
예제 #2
0
def _render(diagram: circuits.TextDiagramDrawer) -> str:
    w = diagram.width()
    h = diagram.height()

    qwx = {(x, y + 1)
           for x, y1, y2, _ in diagram.vertical_lines for y in range(y1, y2)}

    qw = {(x, y)
          for y, x1, x2, _ in diagram.horizontal_lines for x in range(x1, x2)}

    diagram2 = circuits.TextDiagramDrawer()
    for y in range(h):
        for x in range(max(0, w - 1)):
            key = (x, y)
            diagram_text = diagram.entries.get(key)
            v = '&' + (diagram_text.text if diagram_text else '') + ' '
            diagram2.write(2 * x + 1, y, v)
            post1 = '\\qw' if key in qw else ''
            post2 = '\\qwx' if key in qwx else ''
            diagram2.write(2 * x + 2, y, post1 + post2)
        diagram2.write(2 * w - 1, y, '&\\qw\\\\')
    grid = diagram2.render(horizontal_spacing=0, vertical_spacing=0)

    output = '\Qcircuit @R=1em @C=0.75em {\n \\\\\n' + grid + '\n \\\\\n}'

    return output
예제 #3
0
    def __str__(self) -> str:
        diagram = circuits.TextDiagramDrawer()

        for q in self.qubits:
            diagram.write(q.x, 0, str(q))
            for q2 in self.neighbors_of(q):
                diagram.grid_line(q.x, 0, q2.x, 0)

        return diagram.render(horizontal_spacing=3, vertical_spacing=2, use_unicode_characters=True)