Skip to content

Commit

Permalink
fixed top-bottom control rend. bug
Browse files Browse the repository at this point in the history
  • Loading branch information
gadhvirushiraj committed Aug 27, 2024
1 parent 3004909 commit 68b8215
Showing 1 changed file with 46 additions and 29 deletions.
75 changes: 46 additions & 29 deletions src/qutip_qip/circuit/text_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,20 @@ def _draw_multiq_gate(self, gate: Gate, gate_text: str):

# Adjust top_frame or bottom if there is a control wire
if gate.controls:
sorted_controls = sorted(gate.controls)
sorted_targets = sorted(gate.targets)

mid_index = len(bot_frame) // 2
if gate.controls[0] < gate.targets[0]:
bot_frame = (
bot_frame[:mid_index] + "┬" + bot_frame[mid_index + 1 :]
)
else:
top_frame = (
top_frame[:mid_index] + "┴" + top_frame[mid_index + 1 :]
)
top_frame = (
(top_frame[:mid_index] + "┴" + top_frame[mid_index + 1 :])
if sorted_controls[-1] > sorted_targets[0]
else top_frame
)
bot_frame = (
(bot_frame[:mid_index] + "┬" + bot_frame[mid_index + 1 :])
if sorted_controls[0] < sorted_targets[-1]
else bot_frame
)

# check for equal part lengths
assert (
Expand Down Expand Up @@ -295,7 +300,7 @@ def _update_qbridge(
gate: Gate,
wire_list_control: List[int],
width: int,
is_top_closer: bool,
is_top: bool,
):
"""
Update the render strings for part of the multi qubit gate drawn on the control wires.
Expand Down Expand Up @@ -333,12 +338,10 @@ def _update_qbridge(
):
self._render_strs["mid_frame"][wire] += node_conn
self._render_strs["top_frame"][wire] += (
bar_conn if is_top_closer else " " * len(bar_conn)
bar_conn if not is_top else " " * len(bar_conn)
)
self._render_strs["bot_frame"][wire] += (
bar_conn
if not is_top_closer
else " " * len(bar_conn)
bar_conn if is_top else " " * len(bar_conn)
)
else:
self._render_strs["top_frame"][wire] += bar_conn
Expand Down Expand Up @@ -431,22 +434,36 @@ def layout(self):

if gate.controls:
sorted_controls = sorted(gate.controls)
# check if the top of the gate is closer to the first control wire
is_top_closer = wire_list[0] >= sorted_controls[0]
closest_pos = (
wire_list[0] if not is_top_closer else wire_list[-1]
)
self._update_qbridge(
gate,
list(
range(
min(sorted_controls[0], closest_pos),
max(sorted_controls[-1], closest_pos) + 1,
)
),
width,
is_top_closer,
)

# check if there is control wire above the gate top
is_top = sorted_controls[-1] > sorted_targets[0]
is_bot = sorted_controls[0] < sorted_targets[-1]

if is_top:
self._update_qbridge(
gate,
list(
range(
sorted_targets[0],
sorted_controls[-1] + 1,
)
),
width,
is_top,
)

if is_bot:
self._update_qbridge(
gate,
list(
range(
sorted_controls[0],
sorted_targets[-1] + 1,
)
),
width,
not is_bot,
)

max_layer_len = max(sum(layer) for layer in self._layer_list)
self._adjust_layer_pad(
Expand Down

0 comments on commit 68b8215

Please sign in to comment.