Skip to content

feat: add circuit drawer module with Matplotlib (#1083) #1094

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

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

Conversation

Qubit1718
Copy link
Contributor

Issue #, if available: #1083

Description of changes:

  1. Added a new file in src/braket/circuits/text_diagram_builders - matplotlib_circuit_diagram.py
  2. Added the new function(MatplotlibCircuitDiagram) into init.py
  3. Added matplotlib as requirement in setup.py
  4. Added a new file for testing

Testing done: Yes

Merge Checklist

Put an x in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your pull request.

General

Tests

  • [ x] I have added tests that prove my fix is effective or that my feature works (if appropriate)
  • [x ] I have checked that my tests are not configured for a specific region or account (if appropriate)

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

draws the circuit using matplotlib
added matplotlib requirement for the library
@Qubit1718 Qubit1718 requested a review from a team as a code owner June 12, 2025 02:53
Copy link

codecov bot commented Jun 12, 2025

Codecov Report

Attention: Patch coverage is 87.01299% with 20 lines in your changes missing coverage. Please review.

Project coverage is 99.78%. Comparing base (47aad1f) to head (ce5b01c).

Files with missing lines Patch % Lines
...ext_diagram_builders/matplotlib_circuit_diagram.py 86.92% 11 Missing and 9 partials ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##              main    #1094      +/-   ##
===========================================
- Coverage   100.00%   99.78%   -0.22%     
===========================================
  Files          137      138       +1     
  Lines         9246     9400     +154     
  Branches      1077     1110      +33     
===========================================
+ Hits          9246     9380     +134     
- Misses           0       11      +11     
- Partials         0        9       +9     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Contributor

@rmshaffer rmshaffer left a comment

Choose a reason for hiding this comment

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

This is a nice start! I left a few specific comments. Overall I think there are still some critical features missing that would be great to add before we merge this PR.

To compare with our existing text-based visualization, I tested with this code snippet:

from braket.circuits import Circuit, MatplotlibCircuitDiagram, ResultType

verbatim_circuit = Circuit().h(0).cnot(0,1)
circuit = Circuit().add_verbatim_box(verbatim_circuit).cnot(0,2).ccnot(0,2,3).ms(1,3,1.1,1.2).add_result_type(ResultType.StateVector())

print(circuit)
plot = MatplotlibCircuitDiagram.build_diagram(circuit)

which produces this output:
image

We can see a few issues here off the bat:

  • the controlled and multiqubit gates (CNOT, CCNOT, MS) don't visualize which specific qubits they act on, and don't indicate which qubit is the control and which is the target
  • the StartVerbatim and EndVerbatim markers are not printed
  • the gate angle parameters (to MS in this case) are not displayed
  • the qubit labels could be simplified to just q0 or q[0] instead of q[Qubit(0)]
  • the result type StateVector is shown only on the first qubit, when it actually applies to all qubits (or, we could just omit that - since our text-based visualizer does not print out the result types)

I haven't tested thoroughly - in general, though, the guidance is that the information we show in the matplotlib image should be the same as what we show in the text-based version.


# Adjust layout and return
plt.tight_layout()
return fig
Copy link
Contributor

Choose a reason for hiding this comment

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

In a Jupyter notebook, when calling build_diagram(), it's always displaying the image automatically - even if I capture the return value in a variable. If I capture the return value of build_diagram() in a variable, or if this is not the last line in the cell, then the image should not be displayed automatically.

Comment on lines +120 to +121
if abs(circuit.global_phase - 0.5) < 1e-9:
phase_str = "0.50π"
Copy link
Contributor

Choose a reason for hiding this comment

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

This is cool. Two comments
(1) would be cool to do this for other multiples of pi, not just 0.5
(2) we should share the logic for displaying global phase and for angle parameters to other gates

return str(operator.ascii_symbols[0])
# Measurement
if operator.__class__.__name__ == "Measure":
return "Measure"
Copy link
Contributor

Choose a reason for hiding this comment

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

for text diagrams we just use M here - could do the same for consistency

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.

2 participants