qdk icon indicating copy to clipboard operation
qdk copied to clipboard

Python: Long gate names are rendered poorly in ASCII art circuits

Open minestarks opened this issue 1 year ago • 10 comments

When rendering circuits (see https://github.com/microsoft/qsharp/blob/main/samples/notebooks/circuits.ipynb) , if the gate label is longer than our fixed column width (e.g. rx(3.1416)) then the resulting ASCII art circuit looks terrible.

We should make column widths vary with the gate label, so that the gates are displayed properly on the qubit wire.

See the below samples in Python.

import qsharp

print("gates that fit the column width")
print("circuit looks okay")

qsharp.eval("""

use q0 = Qubit();
use q1 = Qubit();

H(q0);
H(q1);
X(q1);     
CNOT(q0, q1);
M(q0)

""")

print(qsharp.dump_circuit())


print("q_2 has gates that are too wide for the column width")
print("circuit wires disappear, and the vertical columns don't align anymore")

qsharp.eval("""

use q2 = Qubit();

Rx(1.0, q2);
Rx(1.0, q2);

""")

print(qsharp.dump_circuit())


print("q_3 has gates with both short and long gate labels")
print("here, the column widths should be variable so that the H doesn't take up too much width, but rx(1.000) still fits within a column")

qsharp.eval("""

use q3 = Qubit();

H(q3);
Rx(1.0, q3);
H(q3);
Rx(1.0, q3);
H(q3);
Rx(1.0, q3);


""")

print(qsharp.dump_circuit())

Output:

gates that fit the column width
circuit looks okay
q_0    ── H ─────────── ● ──── M ──
                        │      ╘═══
q_1    ── H ──── X ──── X ─────────

q_2 has gates that are too wide for the column width
circuit wires disappear, and the vertical columns don't align anymore
q_0    ── H ─────────── ● ──── M ──
                        │      ╘═══
q_1    ── H ──── X ──── X ─────────
q_2     rx(1.0000)  rx(1.0000) ──────────────

q_3 has gates with both short and long gate labels
here, the column widths should be variable so that the H doesn't take up too much width, but rx(1.000) still fits within a column
q_0    ── H ─────────── ● ──── M ────────────────
                        │      ╘═════════════════
q_1    ── H ──── X ──── X ───────────────────────
q_2     rx(1.0000)  rx(1.0000) ────────────────────────────
q_3    ── H ── rx(1.0000) ── H ── rx(1.0000) ── H ── rx(1.0000)

minestarks avatar May 03 '24 23:05 minestarks

@minestarks @tcNickolas , I would like to work on this issue. Can please tell me how critical is the issue of long gate names affecting the readability of ASCII art circuits in the Q# framework?

Pulkit1822 avatar May 10 '24 21:05 Pulkit1822

@Pulkit1822 apologies for missing your post. It's been a while so hopefully you're still interested -- I'd say this is a fairly low-priority bug, and with no-one that I know currently working on it, it's probably a good first issue to experiment with.

minestarks avatar Jun 19 '24 14:06 minestarks

@minestarks Could you describe a bit more about typical development loop here? As far as I understand, if ...\pipsrc*.rs files are modified:

  1. Rust sources compiled using Cargo from pip directory but full build is "python ./build --pip"
  2. Rust tests are in ...\pip\src\displayable_output\test.rs (and part of Cargo build)
  3. Python tests are in ...\pip\tests\ and ...\pip\test-integration (and part of full build) Is there any shortcut for python only build?
  4. Local python package installed as "pip install <path to ...\pip folder>"

Any other hints?

ggridin avatar Jul 30 '24 21:07 ggridin