Skip to content

fix: PI format for rust Circuit display#15917

Merged
eliarbel merged 12 commits intoQiskit:mainfrom
OnyxBrumeSky:fix-15849-pi-formatting
Apr 20, 2026
Merged

fix: PI format for rust Circuit display#15917
eliarbel merged 12 commits intoQiskit:mainfrom
OnyxBrumeSky:fix-15849-pi-formatting

Conversation

@OnyxBrumeSky
Copy link
Copy Markdown
Contributor

@OnyxBrumeSky OnyxBrumeSky commented Mar 30, 2026

fix: added an intermediate function format_float_pi to find closet pi ratio up to a certain limit. also added relative tests named as test_pi_float_format. files modified :crates/circuit/src/circuit_drawer.rs .

Summary

This aims to fix the pi display for rust circuit displays as mentioned in #15849 for fix nb 2. I added a function to convert floats to a ratio of pi if possible.

Details and comments

AI tool used: Claude in web browser with sonnet 4.6.
I used Claude AI to help me understand the file and where to apply my changes.
The function takes a float and try to find the closet ratio of pi possible. The smallest denominator possible is 15 and my difference delta is 1e-9. example : π/15 => "π/15" but π/16 => "0.19634954084936207". The max denominator could be increased and same for the difference delta. I also added test to my function. They are listed at the end of the tests on the page.

  • I have added the tests to cover my changes.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.

…ratio up to a certain limit. also added relative tests named as test_pi_float_format. files modified :crates/circuit/src/circuit_drawer.rs .This concerns the issue Qiskit#15849
@OnyxBrumeSky OnyxBrumeSky requested a review from a team as a code owner March 30, 2026 20:32
@qiskit-bot qiskit-bot added the Community PR PRs from contributors that are not 'members' of the Qiskit repo label Mar 30, 2026
@qiskit-bot
Copy link
Copy Markdown
Collaborator

Thank you for opening a new pull request.

Before your PR can be merged it will first need to pass continuous integration tests and be reviewed. Sometimes the review process can be slow, so please be patient.

While you're waiting, please feel free to review other open PRs. While only a subset of people are authorized to approve pull requests for merging, everyone is encouraged to review open pull requests. Doing reviews helps reduce the burden on the core team and helps make the project's code better for everyone.

One or more of the following people are relevant to this code:

  • @Qiskit/terra-core

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Mar 30, 2026

CLA assistant check
All committers have signed the CLA.

Copy link
Copy Markdown
Member

@eliarbel eliarbel left a comment

Choose a reason for hiding this comment

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

Thanks @OnyxBrumeSky for your interest in contributing to Qiskit!

This is a very nice start, supporting multiples of $\pi$ and fractions with denominators up to 15. I think we should extend the function further to better align with the behavior of

def pi_check(inpt, eps=1e-9, output="text", ndigits=None):

To this end, could you please add support for these cases:

  1. Fractions of the form $\frac{n \pi }{d}$ and $\frac{n}{d \pi}$ where $n \in \pm{1 \ldots 16}$ and $1 \le d \le 16$. Note that the current implementation allows reduced fractions whose numerators are larger than 16. It also limits the denominators up to 15.
  2. Powers of $\pi$, up to degree 4.
  3. $\frac{\pi}{n}$ form, for non-zero integers $n$.

In addition, I've left some minor inline comments.

Comment thread crates/circuit/src/circuit_drawer.rs Outdated
Comment thread crates/circuit/src/circuit_drawer.rs Outdated
Comment thread crates/circuit/src/circuit_drawer.rs Outdated
Comment thread crates/circuit/src/circuit_drawer.rs Outdated
@eliarbel eliarbel added the Rust This PR or issue is related to Rust code in the repository label Apr 4, 2026
@eliarbel eliarbel self-assigned this Apr 4, 2026
@eliarbel eliarbel added the mod: visualization qiskit.visualization label Apr 4, 2026
@eliarbel eliarbel added this to the 2.5.0 milestone Apr 5, 2026
@github-project-automation github-project-automation Bot moved this to Ready in Qiskit 2.5 Apr 5, 2026
@eliarbel eliarbel added the Changelog: None Do not include in the GitHub Release changelog. label Apr 5, 2026
OnyxBrumeSky and others added 3 commits April 10, 2026 14:35
…meters to the function to copy the use of pi_check. Added the possibility of choosing a specific format. Next step is to add the features to support other format.
… for better reading. Added support of formats according to the original py_check.py. Added tests for the rust function. Modified the lib.rs to be able to use the function accross files.
@OnyxBrumeSky
Copy link
Copy Markdown
Contributor Author

Hello, I did the changes you requested and rewrite the function to follow the behavior of :

def pi_check(inpt, eps=1e-9, output="text", ndigits=None):
.

I added the same comments but adapted to rust. Now the function returns an Option where Some happens when it can format Pi and None is returned if no pi formatting is found.

I also moved the code in the file py_check.rs as there is a py_check.py.

I also added a pub enum for formatting that accepts : Text, Qasm, Latex, Mpl. I don't know if it's really relevant here.

Please let me know if there is any changes required and I will do it with pleasure. Also is there any other features/fix I could be assigned to ?

Copy link
Copy Markdown
Member

@eliarbel eliarbel left a comment

Choose a reason for hiding this comment

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

Thanks for addressing the review comments. I'm afraid there was some misunderstanding of the intent in my earlier comment. The request behind this PR is not about fully porting the Python function pi_check to Rust but only to enable pretty-printing of floating-point angles in the Rust text drawer. So we don't need full feature parity with the Python pi_check function at this point. In addition, since the requested functionality is only needed by the Rust drawer, we should keep the new function inside circuit_drawer.rs for now.

To further clarify, I would spec the required feature as follows:

  1. From output formatting perspective, we should support the cases outlined here.
  2. The function signature should be simple. Something like that:
`fn format_float_pi(f: f64) -> Option<String>`

should work (feel free to rename the function).
3. The function should reside in circuit_drawer.rs.
4. I strongly prefer to have all the new function's tests reside in a single test function (in circuit_drawer.rs), to avoid excessive blow up of mod test in the file.

Thanks for the continuous effort. I'm looking forward for the updated PR version.

@OnyxBrumeSky
Copy link
Copy Markdown
Contributor Author

Oh sorry for the misunderstanding. I will do the changes asap. I should finish it by today.

OnyxBrumeSky added 2 commits April 12, 2026 13:28
… the same file. Removed function parameters as asked and modified output to only have text format
@OnyxBrumeSky
Copy link
Copy Markdown
Contributor Author

I just applied the changes you asked. Please tell me if anything needs to be improved . And again I am very sorry for the misunderstanding.

Copy link
Copy Markdown
Member

@eliarbel eliarbel left a comment

Choose a reason for hiding this comment

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

Thanks again for addressing the comments and no worries, we're getting closer.

Overall, the logic seems fine to me (I assume it's an AI-generated port from pi_check, right?) I've left a couple of correctness-related suggestions (namely returning f itself if no formatting is found) and some code organization comments. I also left recommendations for more idiomatic Rust in several places. I'm fine if those are ignored for now, just please make sure you understand the generated implementation and tests.
Finally, before the next push, please run cargo clippy and cargo fmt and all the Rust tests in of the test module in this file as I would like to trigger CI checks on the PR.

Comment thread crates/circuit/src/circuit_drawer.rs Outdated
Comment thread crates/circuit/src/circuit_drawer.rs Outdated
Comment thread crates/circuit/src/circuit_drawer.rs Outdated
Comment thread crates/circuit/src/circuit_drawer.rs Outdated
Comment thread crates/circuit/src/circuit_drawer.rs Outdated
Comment thread crates/circuit/src/circuit_drawer.rs
Comment thread crates/circuit/src/circuit_drawer.rs Outdated
Comment thread crates/circuit/src/circuit_drawer.rs Outdated
Comment thread crates/circuit/src/circuit_drawer.rs Outdated
Comment thread crates/circuit/src/circuit_drawer.rs Outdated
@OnyxBrumeSky
Copy link
Copy Markdown
Contributor Author

Hello, I just applied the changes. I also run cargo clippy and fmt plus the tests in the file. Let me know if any modifications are still needed.

@eliarbel
Copy link
Copy Markdown
Member

Hello, I just applied the changes. I also run cargo clippy and fmt plus the tests in the file. Let me know if any modifications are still needed.

Thanks! This looks almost ready to go. I just pushed a commit with final touch-ups mostly around comments and the tests, before having this checked by CI.

Copy link
Copy Markdown
Member

@eliarbel eliarbel 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 ready to merge now. Thanks @OnyxBrumeSky for your contribution!

If you'd like to contribute more to the Rust circuit drawer, feel free to pick up any remaining tasks from here: #15849. Obviously you're welcome to pick up issues in other areas.

@eliarbel eliarbel added this pull request to the merge queue Apr 20, 2026
Merged via the queue into Qiskit:main with commit 142954b Apr 20, 2026
26 checks passed
@github-project-automation github-project-automation Bot moved this from Ready to Done in Qiskit 2.5 Apr 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Changelog: None Do not include in the GitHub Release changelog. Community PR PRs from contributors that are not 'members' of the Qiskit repo mod: visualization qiskit.visualization Rust This PR or issue is related to Rust code in the repository

Projects

Status: Done
Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants