|
| 1 | +# Copyright 2018 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from typing import Any, Callable, Optional |
| 16 | + |
| 17 | +from cirq import abc, circuits, extension, ops |
| 18 | +from cirq.contrib.qcircuit_diagrammable_gate import ( |
| 19 | + QCircuitDiagrammableGate, |
| 20 | + fallback_qcircuit_extensions, |
| 21 | +) |
| 22 | + |
| 23 | + |
| 24 | +class _QCircuitQubit(ops.QubitId): |
| 25 | + def __init__(self, sub: ops.QubitId) -> None: |
| 26 | + self.sub = sub |
| 27 | + |
| 28 | + def __str__(self): |
| 29 | + # TODO: If qubit name ends with digits, turn them into subscripts. |
| 30 | + return '\\lstick{\\text{' + str(self.sub) + '}}&' |
| 31 | + |
| 32 | + def __eq__(self, other): |
| 33 | + if not isinstance(other, _QCircuitQubit): |
| 34 | + return NotImplemented |
| 35 | + return self.sub == other.sub |
| 36 | + |
| 37 | + def __ne__(self, other): |
| 38 | + return not self == other |
| 39 | + |
| 40 | + def __hash__(self): |
| 41 | + return hash((_QCircuitQubit, self.sub)) |
| 42 | + |
| 43 | + |
| 44 | +class _QCircuitGate(ops.TextDiagrammableGate, metaclass=abc.ABCMeta): |
| 45 | + def __init__(self, sub: QCircuitDiagrammableGate) -> None: |
| 46 | + self.sub = sub |
| 47 | + |
| 48 | + def text_diagram_exponent(self): |
| 49 | + return 1 |
| 50 | + |
| 51 | + def text_diagram_wire_symbols(self, |
| 52 | + qubit_count: Optional[int] = None, |
| 53 | + use_unicode_characters: bool = True): |
| 54 | + return self.sub.qcircuit_wire_symbols(qubit_count) |
| 55 | + |
| 56 | + |
| 57 | +def _render(diagram: circuits.TextDiagramDrawer) -> str: |
| 58 | + w = diagram.width() |
| 59 | + h = diagram.height() |
| 60 | + |
| 61 | + qwx = {(x, y + 1) |
| 62 | + for x, y1, y2 in diagram.vertical_lines |
| 63 | + for y in range(y1, y2)} |
| 64 | + |
| 65 | + qw = {(x, y) |
| 66 | + for y, x1, x2 in diagram.horizontal_lines |
| 67 | + for x in range(x1, x2)} |
| 68 | + |
| 69 | + rows = [] |
| 70 | + for y in range(h): |
| 71 | + row = [] |
| 72 | + for x in range(w): |
| 73 | + cell = [] |
| 74 | + key = (x, y) |
| 75 | + v = diagram.entries.get(key) |
| 76 | + if v is not None: |
| 77 | + cell.append(' ' + v + ' ') |
| 78 | + if key in qw: |
| 79 | + cell.append('\\qw ') |
| 80 | + if key in qwx: |
| 81 | + cell.append('\\qwx ') |
| 82 | + row.append(''.join(cell)) |
| 83 | + rows.append('&'.join(row) + '\qw') |
| 84 | + |
| 85 | + grid = '\\\\\n'.join(rows) |
| 86 | + |
| 87 | + output = '\Qcircuit @R=1em @C=0.75em { \\\\ \n' + grid + ' \\\\ \n \\\\ }' |
| 88 | + |
| 89 | + return output |
| 90 | + |
| 91 | + |
| 92 | +def _wrap_operation(op: ops.Operation, |
| 93 | + ext: extension.Extensions) -> ops.Operation: |
| 94 | + new_qubits = [_QCircuitQubit(e) for e in op.qubits] |
| 95 | + new_gate = ext.try_cast(op.gate, QCircuitDiagrammableGate) |
| 96 | + if new_gate is None: |
| 97 | + new_gate = fallback_qcircuit_extensions.cast(op.gate, |
| 98 | + QCircuitDiagrammableGate) |
| 99 | + return ops.Operation(_QCircuitGate(new_gate), new_qubits) |
| 100 | + |
| 101 | + |
| 102 | +def _wrap_moment(moment: circuits.Moment, |
| 103 | + ext: extension.Extensions) -> circuits.Moment: |
| 104 | + return circuits.Moment(_wrap_operation(op, ext) |
| 105 | + for op in moment.operations) |
| 106 | + |
| 107 | + |
| 108 | +def _wrap_circuit(circuit: circuits.Circuit, |
| 109 | + ext: extension.Extensions) -> circuits.Circuit: |
| 110 | + return circuits.Circuit(_wrap_moment(moment, ext) |
| 111 | + for moment in circuit.moments) |
| 112 | + |
| 113 | + |
| 114 | +def circuit_to_latex_using_qcircuit( |
| 115 | + circuit: circuits.Circuit, |
| 116 | + ext: extension.Extensions = None, |
| 117 | + qubit_order_key: Callable[[ops.QubitId], Any] = None) -> str: |
| 118 | + """Returns a QCircuit-based latex diagram of the given circuit. |
| 119 | +
|
| 120 | + Args: |
| 121 | + circuit: The circuit to represent in latex. |
| 122 | + ext: Extensions used when attempting to cast gates into |
| 123 | + QCircuitDiagrammableGate instances (before falling back to the |
| 124 | + default wrapping methods). |
| 125 | + qubit_order_key: Determines the order of qubit wires in the diagram. |
| 126 | +
|
| 127 | + Returns: |
| 128 | + Latex code for the diagram. |
| 129 | + """ |
| 130 | + if ext is None: |
| 131 | + ext = extension.Extensions() |
| 132 | + qcircuit = _wrap_circuit(circuit, ext) |
| 133 | + diagram = qcircuit.to_text_diagram_drawer( |
| 134 | + ext, |
| 135 | + qubit_name_suffix='', |
| 136 | + qubit_order_key=(None |
| 137 | + if qubit_order_key is None |
| 138 | + else lambda e: qubit_order_key(e.sub))) |
| 139 | + return _render(diagram) |
0 commit comments