Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QasmModule Circuit Drawer #122

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open

QasmModule Circuit Drawer #122

wants to merge 20 commits into from

Conversation

arulandu
Copy link
Contributor

Summary of changes

Added circuit drawing functionality for QasmModule. Closes #61.

@arulandu arulandu marked this pull request as draft January 11, 2025 03:45
@arulandu arulandu changed the title [Draft] QasmModule Circuit Drawer QasmModule Circuit Drawer Jan 11, 2025
@arulandu arulandu marked this pull request as ready for review January 14, 2025 00:58
pyproject.toml Outdated Show resolved Hide resolved
tests/qasm3/test_printer.py Outdated Show resolved Hide resolved
Copy link
Member

@ryanhill1 ryanhill1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This progress looks good! A couple things:

  1. Gate angle formatting: For parameterized gates, the angle below the gate's name goes all the way out to the edges of the gate box, and for negative numbers, it bleeds over the sides. I think remove the parentheses around the angle and making the font size of the angle slightly smaller than the gate name could make it fit better, and allow the main focus to be on the gate name itself.

e31686c4-ff6b-409d-88f3-7bcb4fb99731

  1. Support for barriers: Add support for barriers. Currently being omitted from the diagram:
from pyqasm import loads

qasm3 = """
OPENQASM 3;
include "stdgates.inc";
qubit[2] q;
h q;
barrier q[0], q[1];
cnot q[0], q[1];
"""

module = loads(qasm3)

module.draw()

3f038bb3-789f-42e2-a7ee-35f5348cfe8b

  1. Measurement gate formatting:
  • Measurement gates are currently being stacked on top of one another which makes it difficult to read. Would be better to space them out.
  • The arrow pointing to the classical bit to measure currently goes over that bottom line.
  • Let's make the arrow for the measure a little bit smaller
  • The current color for the measurement gates is a bit dark. Maybe a slightly lighter grey?

Ours:

from pyqasm import loads

qasm3 = """
OPENQASM 3;
include "stdgates.inc";
qubit[2] q;
bit[2] b;
h q;
cnot q[0], q[1];
b = measure q;
"""

module = loads(qasm3)

module.draw()

ed87f7ca-2532-4a62-b66a-3e59a051c6d0-1

vs. qiskit:

from qbraid import transpile

qiskit_circuit = transpile(qasm3, "qiskit")

qiskit_circuit.draw(output='mpl')

849cf702-7fd0-4246-9880-10235934858b

Note: I'm not saying to copy the way they've done it, but we can both it agree that theirs looks very clean. So we can perhaps take inspiration from the elements which make their diagrams visually appealing.

  1. Optional to exclude idle wires: Can you add an option idle_wires of type bool which defaults to True, but if False, excludes any qubits and clbits that don't have any operation on them? For example:
from pyqasm import loads

qasm3 = """
OPENQASM 3;
include "stdgates.inc";
qubit[2] q;
bit[2] b;
rx(-pi/2) q[0];
"""

module = loads(qasm3)

module.draw()

fa225092-cfeb-4ccc-a85f-bf66cb67b276

module.draw(ide_wires=False)

e31686c4-ff6b-409d-88f3-7bcb4fb99731

  1. Duplicate image from draw output: For some reason, when a call the .draw() method within my VS code notebook, it is "drawing" it twice. See images below. Is the same thing happening for you? See below:

Screenshot 2025-01-15 at 9 15 59 AM

  1. Split lines for long diagrams: For circuit diagrams that are very long, we should at a certain point, split it onto two lines so that it is readable. Currently the font just gets smaller and smaller into oblivion

@arulandu arulandu requested a review from ryanhill1 January 17, 2025 02:03
Copy link
Member

@ryanhill1 ryanhill1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix merge conflicts

src/pyqasm/modules/qasm3.py Outdated Show resolved Hide resolved
@arulandu
Copy link
Contributor Author

TODO: support QuantumPhaseStatement

@ryanhill1
Copy link
Member

ryanhill1 commented Jan 21, 2025

I think qbraid-cli might have a bug with the headers formatting, try setting qbraid-cli==0.9.4 in [testenv:headers] in tox.ini and run tox -e linters again and that should fix the header bugs + the file that looks like it has 2,000+ lines changed. If it doesn't, let me know.

Also, what's causing all of the different CI build workflows to fail?

@TheGupta2012
Copy link
Member

Amazing work @arulandu ! Here are some minor comments -

  1. Add qubit labels for multiple lines : For circuits extending to multiple lines, it would be nice to have qubit markers for the wires. They are currently being omitted whenever the program extends -
import pyqasm
test = """
OPENQASM 3.0;
include "stdgates.inc";
qubit[2] q;
gate custom a, b {
    h a;
    z a;
    z a;
    y a;
    x a;
    rx(0.5) a;
}
for int i in [0:2] {
    custom q;
}
"""
circ = pyqasm.loads(test)
circ.draw()

image

  1. External gates should be drawn as is : Given that we provide support for external gates, we should not unroll them before drawing -
import pyqasm
test = """
OPENQASM 3.0;
include "stdgates.inc";
qubit[2] q;
gate custom a, b {
    h a;
    z a;
    z a;
    y a;
    x a;
    rx(-0.5) a;
}
custom q;

"""
circ = pyqasm.loads(test)
circ.unroll(external_gates = ["custom"])
circ.draw()

image

  1. Support for reset : I believe reset ops are not supported right now. I guess you can open an issue for extending the drawer with resets or add them in this PR, whichever works

@TheGupta2012
Copy link
Member

I think qbraid-cli might have a bug with the headers formatting, try setting qbraid-cli==0.9.4 in [testenv:headers] in tox.ini and run tox -e linters again and that should fix the header bugs + the file that looks like it has 2,000+ lines changed. If it doesn't, let me know.

Also, what's causing all of the different CI build workflows to fail?

@arulandu I think same applies for the ctrl modifier PR where we're seeing a very big change in the src/pyqasm/maps/gates.py

@TheGupta2012
Copy link
Member

TheGupta2012 commented Jan 22, 2025

@arulandu you need to add qbraid in the test extra for pyqasm. This is why the tests are failing in the CI pipeline.

Moreover, since we use pyqasm as a dependency in the qBraid-SDK and transpiler, I'm not entirely sold on using it for randomized testing. Would be better to have a large enough dir with ~50 tests, where we can store pre-transpiled circuits and use them for testing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[FEATURE] Add QasmModule.draw() method
3 participants