question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Preventing printing overflow for long circuits in terminal with `qml.draw_circuit()`

See original GitHub issue

Feature 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:closed
  • Created 2 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
greenstickcommented, Nov 12, 2021

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.

0reactions
greenstickcommented, Nov 11, 2021

@albi3ro Okay sounds good!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can I create a quantum circuit from a list of operations in ...
Say, I have a list of operations which are pennylane gates, can I create a circuit from it? I tried: import pennylane as...
Read more >
How to stop my terminal from printing my board array output ...
However when I am trying to check my solutions visually my terminal is printing each nested array onto two separate lines, which makes...
Read more >
Console API β€” Rich 12.6.0 documentation
Overflow is what happens when text you print is larger than the available space. Overflow may occur if you print long 'words' such...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found