Skip to content

Commit

Permalink
fix misalignment
Browse files Browse the repository at this point in the history
  • Loading branch information
jcjaskula-aws committed Dec 28, 2023
1 parent 44f3e20 commit 3515dba
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 34 deletions.
24 changes: 13 additions & 11 deletions src/braket/circuits/ascii_circuit_diagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
class AsciiCircuitDiagram(CircuitDiagram):
"""Builds ASCII string circuit diagrams."""

vdelim = "|"
_vdelim = "|"
_qubit_line_char = "-"
_add_empty_line = True
_buffer_size = 0

Expand Down Expand Up @@ -123,13 +124,13 @@ def _prepare_diagram_vars(
# Y Axis Column
y_axis_width = len(str(int(max(circuit_qubits))))
y_axis_str = "{0:{width}} : {vdelim}\n".format(
"T", width=y_axis_width + 1, vdelim=cls.vdelim
"T", width=y_axis_width + 1, vdelim=cls._vdelim
)

global_phase = None
if any(m.moment_type == MomentType.GLOBAL_PHASE for m in circuit._moments):
y_axis_str += "{0:{width}} : {vdelim}\n".format(
"GP", width=y_axis_width, vdelim=cls.vdelim
"GP", width=y_axis_width, vdelim=cls._vdelim
)
global_phase = 0

Expand Down Expand Up @@ -296,19 +297,20 @@ def _ascii_diagram_column_set(
if symbols_width < col_title_width:
diff = col_title_width - symbols_width
for i in range(len(lines) - 1):
if lines[i].endswith("-"):
lines[i] += "-" * diff
if lines[i].endswith(cls._qubit_line_char):
lines[i] += cls._qubit_line_char * diff
else:
lines[i] += " "

first_line = "{:^{width}}{vdelim}\n".format(
col_title, width=len(lines[0]) - 1, vdelim=cls.vdelim
col_title, width=len(lines[0]) - 1, vdelim=cls._vdelim
)

return first_line + "\n".join(lines)

@staticmethod
@classmethod
def _ascii_diagram_column(
cls,
circuit_qubits: QubitSet,
items: list[Union[Instruction, ResultType]],
global_phase: float | None = None,
Expand All @@ -324,7 +326,7 @@ def _ascii_diagram_column(
Returns:
str: an ASCII string diagram for the specified moment in time for a column.
"""
symbols = {qubit: "-" for qubit in circuit_qubits}
symbols = {qubit: cls._qubit_line_char for qubit in circuit_qubits}
margins = {qubit: " " for qubit in circuit_qubits}

for item in items:
Expand Down Expand Up @@ -353,7 +355,7 @@ def _ascii_diagram_column(
control_qubits = QubitSet()
target_and_control = QubitSet()
qubits = circuit_qubits
ascii_symbols = "-" * len(circuit_qubits)
ascii_symbols = cls._qubit_line_char * len(circuit_qubits)
else:
if isinstance(item.target, list):
target_qubits = reduce(QubitSet.union, map(QubitSet, item.target), QubitSet())
Expand Down Expand Up @@ -422,7 +424,7 @@ def _create_output(
)
symbols_width = max([symbols_width, len(global_phase_str)])
output += "{0:{fill}{align}{width}}{vdelim}\n".format(
global_phase_str, fill=" ", align="^", width=symbols_width, vdelim=cls.vdelim
global_phase_str, fill=" ", align="^", width=symbols_width, vdelim=cls._vdelim
)

for qubit in qubits:
Expand All @@ -433,7 +435,7 @@ def _create_output(
def _draw_symbol(cls, symbol: str, symbols_width: int, connection: str) -> str:
output = "{0:{width}}\n".format(connection, width=symbols_width + 1)
output += "{0:{fill}{align}{width}}\n".format(
symbol, fill="-", align="<", width=symbols_width + 1
symbol, fill=cls._qubit_line_char, align="<", width=symbols_width + 1
)
return output

Expand Down
34 changes: 22 additions & 12 deletions src/braket/circuits/box_drawing_circuit_diagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
class BoxDrawingCircuitDiagram(AsciiCircuitDiagram):
"""Builds ASCII string circuit diagrams using box-drawing characters."""

vdelim = "│"
_vdelim = "│"
_qubit_line_char = "─"
_add_empty_line = False
_buffer_size = 4

Expand All @@ -56,7 +57,11 @@ def _create_qubit_layout(qubit: Qubit, y_axis_width: int) -> None:
y_axis_width (int): Width of the y axis.
"""
y_axis_str = "{0:{width}}\n".format(" ", width=y_axis_width + 5)
y_axis_str += "q{0:{width}} : ─\n".format(str(int(qubit)), width=y_axis_width)
y_axis_str += "q{0:{width}} : {qubit_line_char}\n".format(
str(int(qubit)),
width=y_axis_width,
qubit_line_char=BoxDrawingCircuitDiagram._qubit_line_char,
)
y_axis_str += "{0:{width}}\n".format(" ", width=y_axis_width + 5)
return y_axis_str

Expand All @@ -82,7 +87,7 @@ def _build_parameters(
target_qubits = circuit_qubits
control_qubits = QubitSet()
qubits = circuit_qubits
ascii_symbols = "─" * len(circuit_qubits)
ascii_symbols = BoxDrawingCircuitDiagram._qubit_line_char * len(circuit_qubits)
else:
if isinstance(item.target, list):
target_qubits = reduce(QubitSet.union, map(QubitSet, item.target), QubitSet())
Expand Down Expand Up @@ -114,8 +119,9 @@ def _update_connections(qubits: QubitSet, connections: dict[Qubit, str]) -> None
connections[qubits[-1]] = "above"
connections[qubits[0]] = "below"

@staticmethod
@classmethod
def _ascii_diagram_column(
cls,
circuit_qubits: QubitSet,
items: list[Instruction | ResultType],
global_phase: float | None = None,
Expand All @@ -131,7 +137,7 @@ def _ascii_diagram_column(
Returns:
str: an ASCII string diagram for the specified moment in time for a column.
"""
symbols = {qubit: "─" for qubit in circuit_qubits}
symbols = {qubit: cls._qubit_line_char for qubit in circuit_qubits}
connections = {qubit: "none" for qubit in circuit_qubits}

for item in items:
Expand All @@ -142,7 +148,7 @@ def _ascii_diagram_column(
connections,
ascii_symbols,
map_control_qubit_states,
) = BoxDrawingCircuitDiagram._build_parameters(circuit_qubits, item, connections)
) = cls._build_parameters(circuit_qubits, item, connections)

for qubit in qubits:
# Determine if the qubit is part of the item or in the middle of a
Expand Down Expand Up @@ -174,7 +180,7 @@ def _ascii_diagram_column(
else:
symbols[qubit] = "┼"

output = BoxDrawingCircuitDiagram._create_output(
output = cls._create_output(
symbols, connections, circuit_qubits, global_phase
)
return output
Expand All @@ -185,7 +191,7 @@ def _fill_symbol(symbol: str, filler: str, width: int | None = None) -> str:
symbol,
fill=filler,
align="^",
width=width if width is not None else len(symbol) + 1,
width=width if width is not None else len(symbol),
)

@classmethod
Expand Down Expand Up @@ -217,9 +223,11 @@ def _draw_symbol(
else:
top, symbol, bottom = BoxDrawingCircuitDiagram._build_box(symbol, connection)

output = fill_symbol(top, " ", symbols_width + 1) + "\n"
output += fill_symbol(symbol, "─", symbols_width + 1) + "\n"
output += fill_symbol(bottom, " ", symbols_width + 1) + "\n"
output = f"{fill_symbol(top, ' ', symbols_width)} \n"
output += (
f"{fill_symbol(symbol, cls._qubit_line_char, symbols_width)}{cls._qubit_line_char}\n"
)
output += f"{fill_symbol(bottom, ' ', symbols_width)} \n"
return output

@staticmethod
Expand Down Expand Up @@ -253,7 +261,9 @@ def _build_verbatim_box(
top = "║"
symbol = "╨"
top = BoxDrawingCircuitDiagram._fill_symbol(top, " ")
symbol = BoxDrawingCircuitDiagram._fill_symbol(
symbol, BoxDrawingCircuitDiagram._qubit_line_char
)
bottom = BoxDrawingCircuitDiagram._fill_symbol(bottom, " ")
symbol = BoxDrawingCircuitDiagram._fill_symbol(symbol, "─")

return top, symbol, bottom
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ def test_qubit_width():
_assert_correct_diagram(circ, expected)


@pytest.mark.xfail
def test_different_size_boxes():
circ = Circuit().cnot(0, 1).rx(2, 0.3)
expected = (
Expand Down Expand Up @@ -699,9 +698,9 @@ def test_multiple_result_types():
)
expected = (
"T : │ 0 │ 1 │ Result Types │",
" ┌───┐ ┌─────────────┐ ┌───────────┐ ",
"q0 : ───●─────────┤ H ├──┤ Variance(Y) ├──┤ Sample(Y) ├─",
" │ └───┘ └─────────────┘ └─────┬─────┘ ",
" ┌───┐ ┌─────────────┐ ┌───────────┐ ",
"q0 : ───●─────────┤ H ├──┤ Variance(Y) ├──┤ Sample(Y) ├─",
" │ └───┘ └─────────────┘ └─────┬─────┘ ",
" │ ┌─────┴─────┐ ",
"q1 : ───┼─────●────────────────────────────┤ Sample(Y) ├─",
" │ │ └─────┬─────┘ ",
Expand Down Expand Up @@ -730,9 +729,9 @@ def test_multiple_result_types_with_state_vector_amplitude():
)
expected = (
"T : │ 0 │ 1 │ Result Types │",
" ┌───┐ ┌─────────────┐ ",
"q0 : ───●─────────┤ H ├──────┤ Variance(Y) ├──────",
" │ └───┘ └─────────────┘ ",
" ┌───┐ ┌─────────────┐ ",
"q0 : ───●─────────┤ H ├──────┤ Variance(Y) ├──────",
" │ └───┘ └─────────────┘ ",
" │ ┌────────────────────────┐ ",
"q1 : ───┼─────●─────────┤ Expectation(Hermitian) ├─",
" │ │ └────────────────────────┘ ",
Expand Down Expand Up @@ -777,9 +776,9 @@ def test_multiple_result_types_with_custom_hermitian_ascii_symbol():
" ┌─┴─┐ │ ┌──────────┴──────────┐ ",
"q2 : ─┤ X ├───┼─────────┤ Expectation(MyHerm) ├─",
" └───┘ │ └─────────────────────┘ ",
" ┌─┴─┐ ┌────────────────┐ ",
"q3 : ───────┤ X ├─────────┤ Expectation(Y) ├───",
" └───┘ └────────────────┘ ",
" ┌─┴─┐ ┌────────────────┐ ",
"q3 : ───────┤ X ├─────────┤ Expectation(Y) ├───",
" └───┘ └────────────────┘ ",
"T : │ 0 │ 1 │ Result Types │",
)
_assert_correct_diagram(circ, expected)
Expand Down Expand Up @@ -979,7 +978,6 @@ def __init__(self):
_assert_correct_diagram(circ, expected)


@pytest.mark.xfail
def test_unbalanced_ascii_symbols():
class FooFoo(Gate):
def __init__(self):
Expand Down

0 comments on commit 3515dba

Please sign in to comment.