amazon-braket-sdk-python icon indicating copy to clipboard operation
amazon-braket-sdk-python copied to clipboard

feat: add circuit drawer module with Matplotlib and LaTeX for Braket (#1083)

Open bryanhzaz opened this issue 7 months ago • 7 comments

This pull request introduces a new Python module named braket_circuit_drawer.py, designed to visualize Braket Circuit objects. It provides functionality similar to Qiskit's circuit visualization utilities and supports two primary output formats:

Matplotlib visualization:

Enables graphical rendering of quantum circuits directly within Python scripts or Jupyter Notebooks.

Supported features include:

Single-qubit gates: H, X, Y, Z, S, T, etc.

Parameterized gates: RX(theta), RY(theta), RZ(theta), PHASE(theta), U3(theta1, theta2, theta3)

Controlled gates: CNOT (CX), Toffoli (CCNOT)

Generic multi-qubit gates (e.g., Unitary operations)

Multi-qubit measurement symbols for quantum readout

LaTeX quantikz export:

Generates LaTeX-compatible code using the quantikz package, allowing users to embed high-quality circuit diagrams into academic documents.

The output includes:

\gate{} for single and parameterized gates

\ctrl{} and \targ{} for CNOT/Toffoli gates

\meter{} for quantum measurements

\gate[wires=n]{} for generic multi-qubit gates

Optional labeling of initial qubit states using the initial_states parameter

Files included in this Pull Request:

src/braket_circuit_drawer.py: Main implementation module.

test/test_braket_circuit_drawer.py: Unit tests for key functionality.

braket_drawer_README.md: Standalone documentation with usage instructions and examples.

examples/generate_visuals.py: Script to generate PNG diagrams and LaTeX from sample circuits for visual verification.

Sample Output:

Example 1: Basic gates and measurement

LaTeX and output:

\begin{quantikz} |0> & \gate{H} & \ctrl{1} & \qw & \meter{}
|0> & \qw & \targ{} & \gate{RZ(0.500)} & \qw
\end{quantikz}

circuit_basic

Example 2: Toffoli and parameterized gate

\begin{quantikz} |0> & \ctrl{2} & \qw & \qw
|0> & \ctrl{1} & \qw & \qw
|0> & \targ{} & \qw & \qw
|1> & \qw & \gate{RZ(0.750)} & \meter{}
\end{quantikz}

circuit_toffoli

Example 3: Generic multi-qubit gate

\begin{quantikz} |+> & \gate[wires=2]{UNITARY}
|-> &
\end{quantikz} circuit_unitary

This contribution addresses Issue #1083, providing enhanced visualization capabilities for Braket circuits, both for programmatic use and LaTeX-based rendering.

Feedback, suggestions, and further improvements are welcome.

Merge Checklist

Put an 'x' in the boxes that apply. You can also fill these out after creating the PR.

General

  • [x] I have read the CONTRIBUTING doc

  • [x] I used the PR title format described in CONTRIBUTING

  • [x] I have updated any necessary documentation, including READMEs and API docs (if appropriate)

  • [x] 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.

bryanhzaz avatar May 31 '25 19:05 bryanhzaz

To fix the CI code formatting errors, you can run ruff check src --fix locally

rmshaffer avatar Jun 03 '25 13:06 rmshaffer

I also ran the unit tests in CI, please check on those failures as well. Running tox -e unit-tests locally should succeed. You can check linters similarly by running tox -e linters

rmshaffer avatar Jun 03 '25 14:06 rmshaffer

Codecov Report

:x: Patch coverage is 70.83333% with 49 lines in your changes missing coverage. Please review. :white_check_mark: Project coverage is 99.46%. Comparing base (6a1682f) to head (4ca7d77). :warning: Report is 93 commits behind head on main.

Files with missing lines Patch % Lines
...circuits/diagram_builders/braket_circuit_drawer.py 70.48% 37 Missing and 12 partials :warning:
Additional details and impacted files
@@             Coverage Diff             @@
##              main    #1086      +/-   ##
===========================================
- Coverage   100.00%   99.46%   -0.54%     
===========================================
  Files          135      137       +2     
  Lines         9063     9231     +168     
  Branches      1043     1073      +30     
===========================================
+ Hits          9063     9182     +119     
- Misses           0       37      +37     
- Partials         0       12      +12     

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

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

codecov[bot] avatar Jun 04 '25 19:06 codecov[bot]

Hello @bryanhzaz and @rmshaffer

I think it will be great to have a standardized object from where the circuit is to be drawn. Either the circuit object or an IR like QASM. Also to follow some good coding practices, I would prefer to the logic for the ploting and actual plotter be kept in seperate classes or files (although this is more of a personal choice😅)

skushagra avatar Jun 06 '25 22:06 skushagra

Hi @bryanhzaz and @skushagra - please let me know if either/both of you want to continue working on this PR! Happy to continue reviewing or provide suggestions as I can.

rmshaffer avatar Jun 10 '25 20:06 rmshaffer

Hi @rmshaffer would have really enjoyed working on this but I cannot do that at the moment due to factors beyond my control. Although I am following this repo for new issues and reviews. Apologies for the inconvenience.

skushagra avatar Jun 10 '25 20:06 skushagra

Hi @rmshaffer would have really enjoyed working on this but I cannot do that at the moment due to factors beyond my control. Although I am following this repo for new issues and reviews. Apologies for the inconvenience.

No problem at all! Thank you for your interest.

rmshaffer avatar Jun 10 '25 21:06 rmshaffer

Closing due to inactivity. Please re-open this one (or make a new PR) if you'd like to continue working on this!

rmshaffer avatar Jul 18 '25 15:07 rmshaffer