-
Notifications
You must be signed in to change notification settings - Fork 139
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
base: main
Are you sure you want to change the base?
Conversation
draws the circuit using matplotlib
added matplotlib requirement for the library
Codecov ReportAttention: Patch coverage is
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. 🚀 New features to boost your workflow:
|
There was a problem hiding this 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)
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
orq[0]
instead ofq[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 |
There was a problem hiding this comment.
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.
if abs(circuit.global_phase - 0.5) < 1e-9: | ||
phase_str = "0.50π" |
There was a problem hiding this comment.
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" |
There was a problem hiding this comment.
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
Issue #, if available: #1083
Description of changes:
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
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.