Preventing printing overflow for long circuits in terminal with `qml.draw_circuit()`
See original GitHub issueFeature details
Calling qml.drawcircuit and rendering the output to the terminal results in illegible circuits if the circuit width (i.e. the number of text columns used) is greater than the column width of the terminal window. This issue is due to the overflow of text used to visualize the circuit onto the next line. For example:
Current Behavior:
|------------------terminal width ------------------|
0: βHβRZ(1.0)βββCβββββββββββXβRX(3.14)ββCββββββββββ
βXβRX(3.14)βββββββ€ β¨Zβ©
1: βHβRZ(0.1)βββ°XββCββββββββββRX(3.14)ββ°XββCβββββββ
βββRX(3.14)βββββββ€
2: βHβRZ(0.2)ββββββ°XββCβββββββRX(3.14)βββββ°XββCββββ
βββRX(3.14)βββββββ€
3: βHβRZ(0.3)βββββββββ°XββCββββRX(3.14)ββββββββ°XββCβ
βββRX(3.14)βββββββ€
4: βHβRZ(0.4)ββββββββββββ°Xββ°CβRX(3.14)βββββββββββ°Xβ
β°CβRX(3.14)βββββββ€
A possible solution would be to detect the width of the terminal prior to printing the circuit. With the width detected, the printing of lines could occur with the circuit length modulo the terminal width to facilitate line skipping as needed. This skipping would be repeated to allow the full circuit to be cleanly rendered in the terminal without the overflow issues. For example:
Alternative Behavior:
|------------------terminal width ------------------|
0: βHβRZ(1.0)βββCβββββββββββXβRX(3.14)ββCββββββββββ
1: βHβRZ(0.1)βββ°XββCββββββββββRX(3.14)ββ°XββCβββββββ
2: βHβRZ(0.2)ββββββ°XββCβββββββRX(3.14)βββββ°XββCββββ
3: βHβRZ(0.3)βββββββββ°XββCββββRX(3.14)ββββββββ°XββCβ
4: βHβRZ(0.4)ββββββββββββ°Xββ°CβRX(3.14)βββββββββββ°Xβ
0: ββXβRX(3.14)βββββββ€β¨Zβ©
1: ββββRX(3.14)βββββββ€
2: ββββRX(3.14)βββββββ€
3: ββββRX(3.14)βββββββ€
4: ββ°CβRX(3.14)βββββββ€
Provided thereβs interest, Iβd be happy to send a pull request with this feature (although I may need assistance testing it on Linux and Windows operating systems).
Implementation
To detect the terminal size:
import shutil
cols, _ = shutil.get_terminal_size()
Note that shutil is part of the standard library and would not require an additional dependency.
It seems to me this could be integrated fairly trivially into qml.circuit_drawer.draw() (below), but perhaps this could be confirmed by a developer more familiar with the code base.
def draw(self):
"""Draw the circuit diagram.
Returns:
str: The circuit diagram
"""
rendered_string = ""
# extract the wire labels as strings and get their maximum length
wire_names = []
padding = 0
for i in range(self.full_representation_grid.num_wires):
wire_name = str(self.active_wires.labels[i])
padding = max(padding, len(wire_name))
wire_names.append(wire_name)
for i in range(self.full_representation_grid.num_wires):
# format wire name nicely
wire = self.full_representation_grid.wire(i)
s = " {:>" + str(padding) + "}: {}"
rendered_string += s.format(wire_names[i], 2 * self.charset.WIRE)
for s in wire:
rendered_string += s
rendered_string += "\n"
for symbol, cache in [
("U", self.representation_resolver.unitary_matrix_cache),
("H", self.representation_resolver.hermitian_matrix_cache),
("M", self.representation_resolver.matrix_cache),
]:
for idx, matrix in enumerate(cache):
rendered_string += "{}{} =\n{}\n".format(symbol, idx, matrix)
return rendered_string
How important would you say this feature is?
1: Not important. Would be nice to have.
Additional information
No response
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (9 by maintainers)

Top Related StackOverflow Question
Iβve updated the test suite but it seems Iβm still having issues running the tests (borking on what appears to be an unrelated problem). Everything should be in order though, going to send a pull request.
@albi3ro Okay sounds good!