Skip to content

Add pretty printers for Statevec and DensityMatrix#503

Closed
donglrd wants to merge 1 commit into
TeamGraphix:masterfrom
donglrd:pretty-print-statevec-density
Closed

Add pretty printers for Statevec and DensityMatrix#503
donglrd wants to merge 1 commit into
TeamGraphix:masterfrom
donglrd:pretty-print-statevec-density

Conversation

@donglrd

@donglrd donglrd commented May 12, 2026

Copy link
Copy Markdown

Summary\n- add complex-number pretty formatting for fractions, square-root constants, and phase exponentials\n- add statevector ket expansion rendering via Statevec.draw()\n- add density-matrix outer-product rendering via DensityMatrix.draw()\n- cover the new formatting helpers and draw methods with tests\n\nCloses #501\n\n## Testing\n- pytest tests/test_pretty_print.py -q\n- python3 -m ruff check graphix/pretty_print.py graphix/sim/statevec.py graphix/sim/density_matrix.py tests/test_pretty_print.py

@thierry-martinez

Copy link
Copy Markdown
Collaborator

Thank you for your contribution! As noted in the preamble of #501, this issue is part of unitaryHACK 2026; therefore, contributions will be evaluated during the hackathon period, from June 3 to June 17, 2026.

@codecov

codecov Bot commented Jun 4, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 75.43860% with 28 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.74%. Comparing base (4715099) to head (607fb23).
⚠️ Report is 16 commits behind head on master.

Files with missing lines Patch % Lines
graphix/pretty_print.py 74.07% 28 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #503      +/-   ##
==========================================
- Coverage   89.97%   89.74%   -0.24%     
==========================================
  Files          48       48              
  Lines        7015     7129     +114     
==========================================
+ Hits         6312     6398      +86     
- Misses        703      731      +28     

☔ View full report in Codecov by Harness.
📢 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.

@thierry-martinez thierry-martinez left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank you very much for your contribution!

Could you please fix the issue that mypy has detected?

In addition, Codecov reports that some parts of your code are not covered by tests.

As you may have noticed, PR #523 and PR #524 address the same issue (#501). We invite you to cross‑review each other's work: PR reviews are an inherent part of software development, just as important as writing code. Moreover, this review process will help you position your PR relative to the others, allowing us to determine which contribution best resolves the issue.

Comment thread graphix/pretty_print.py
def _bra(label: str, output: OutputFormat) -> str:
if output == OutputFormat.LaTeX:
return rf"\langle {label}\rvert"
if output == OutputFormat.Unicode:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Using \bra{} would be more standard.

@Vinny010

Vinny010 commented Jun 5, 2026

Copy link
Copy Markdown

Hi @donglrd — taking a look at this per @thierry-martinez's nudge. A few thoughts:

The compact, readable structure is nice, and rendering density matrices as the bra-ket outer-product sum Σ aᵢⱼ|i⟩⟨j| is a different (and mathematically clean) alternative to the grid form — worth noting as a design choice for the maintainers.

A couple of observations on the recognizer:

  • _real_to_str only enumerates √2/2, √3/2 and a Fraction.limit_denominator(16) rational fallback. This leaves common values like 1/3 (denominator > 16), √5/2, or 1/(2√2) in the decimal fallback. Generalising — for example with a "square then rationalize" trick that recognises as a rational and reads off the squarefree part — would cover more cases for free.
  • The atol, max_denominator and the encoding (encoding: str) are not configurable / typed at the API. Worth adopting Literal["LSB", "MSB"] for encoding to match Statevec.to_dict.

@thierry-martinez

Copy link
Copy Markdown
Collaborator

I'm closing this PR because it has little chance of being merged: #524 is on a good track to pass the review process, and I would recommend not spending more time on this PR. Feel free to let me know if you have any questions or need further clarification.

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.

3 participants