feat: pretty-print (draw) for Statevec and DensityMatrix#542
Conversation
Add pretty-print support via draw() methods with three output formats (Unicode, ASCII, LaTeX). - complex_to_str(): detects zero, integers, fractions (1/2), square roots (1/\u221a2, \u221a3/2), exponentials (e^(i\u03c0/3)), pure real/imaginary, and general a\u00b1bi - statevec_to_str(): formats Statevec amplitudes with ket notation - densitymatrix_to_str(): formats rho elements with Dirac |i\u27e9\u27e8j| notation - Statevec.draw() and DensityMatrix.draw() public methods - 16 new test cases across 5 test classes Closes TeamGraphix#501
|
Thank you for your contribution. As you may have noticed, there is an other pull request #524 that already addresses the same issue. Could you clarify how your contribution differs from, or improves upon, the previous works? Specifically, please explain why you consider your approach to be better. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #542 +/- ##
==========================================
- Coverage 88.85% 88.68% -0.18%
==========================================
Files 49 49
Lines 7135 7317 +182
==========================================
+ Hits 6340 6489 +149
- Misses 795 828 +33 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Thanks for the review and the pointer to #524. I had not seen that PR — we were apparently working on this independently. The core approach is the same (square-then-rationalize for sqrt detection), but there are differences: My approach is lighter: I lean on PR #524 is more thorough: the explicit Other minor differences:
Happy to either: I defer to your judgment on which approach fits the project better. |
|
Thanks for the review @thierry-martinez! I see PR #524 by @Vinny010 also addresses #501. Here is how the two approaches differ: Architecture:
Rendering:
Size:
Testing:
I am happy to adapt my PR in either direction — merge with #524, rebase on top of it, or close mine if #524 is preferred. Let me know what works best for the project. |
|
Thanks for the detailed comparison, @kish-00 ? generous write-up, and I appreciate the offer to coordinate. A couple of notes on the technical points:
Glad to incorporate any pieces from |
Description
Adds
draw()methods toStatevecandDensityMatrixfor human-readable pretty-printing of quantum states and density matrices, supporting three output formats (Unicode, ASCII, LaTeX).New public API
complex_to_str(z, output)— pretty-print a complex number, detecting:1/2,3/4)1/√2,√3/2)e^(iπ/3),e^(iπ/4))a±bistatevec_to_str(state_dict, output)— format a statevector dict as∑ c_i|b_i⟩densitymatrix_to_str(rho, nqubit, output)— format density matrix as∑ w_{ij}|i⟩⟨j|Statevec.draw(encoding, output, ...)— prints the statevectorDensityMatrix.draw(output, ...)— prints the density matrixBreaking changes
None.
Checklist
complex_to_str()with fraction, sqrt, exponential detectionstatevec_to_str()anddensitymatrix_to_str()Statevec.draw()andDensityMatrix.draw()methods__init__.pyCloses #501