microsoft / qsharp

Azure Quantum Development Kit, including the Q# programming language, resource estimator, and Quantum Katas
https://microsoft.github.io/qsharp/
MIT License
369 stars 73 forks source link

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

Open minestarks opened 2 months ago

minestarks commented 2 months ago

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)
Pulkit1822 commented 1 month ago

@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?

minestarks commented 2 weeks ago

@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.