Skip to content

Improve the readability of CliffordTableau _full_str_ function#7047

Merged
pavoljuhas merged 7 commits intoquantumlib:mainfrom
babacry:clifford_tab
Feb 12, 2025
Merged

Improve the readability of CliffordTableau _full_str_ function#7047
pavoljuhas merged 7 commits intoquantumlib:mainfrom
babacry:clifford_tab

Conversation

@babacry
Copy link
Copy Markdown
Collaborator

@babacry babacry commented Feb 8, 2025

Came across this and figure improve the readability could potentially be helpful in future developments.

@CirqBot CirqBot added the size: M 50< lines changed <250 label Feb 8, 2025
@codecov
Copy link
Copy Markdown

codecov bot commented Feb 8, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.17%. Comparing base (60f2590) to head (274454c).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7047      +/-   ##
==========================================
- Coverage   98.18%   98.17%   -0.01%     
==========================================
  Files        1089     1089              
  Lines       95202    95201       -1     
==========================================
- Hits        93473    93468       -5     
- Misses       1729     1733       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@babacry babacry marked this pull request as ready for review February 8, 2025 05:00
@babacry babacry requested review from a team and vtomole as code owners February 8, 2025 05:00
@babacry babacry requested a review from maffoo February 8, 2025 05:00
@babacry babacry changed the title Improve the readability of the _full_str_ function Improve the readability of CliffordTableau _full_str_ function Feb 8, 2025
def test_str_full():
t = cirq.CliffordTableau(num_qubits=1)
expected_str = r"""stable | destable
expected_str = r"""stable | destable
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.

Please, do not introduce trailing blanks. The output is more usable without them.

Comment on lines +305 to +303
"""
+ Z0 | + Y0 """
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.

Please keep the newline at the end of the string.
There might be a user code that relies on its presence.

Comment thread cirq-core/cirq/qis/clifford_tableau.py Outdated
Comment on lines +337 to +338
return {
(True, False): f'X{c}',
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.

Please rewrite using match-case statement - https://docs.python.org/3/whatsnew/3.10.html#patterns-with-a-literal-and-variable. Should be more readable and avoid a temporary dictionary for each Pauli symbol resolution.

Comment thread cirq-core/cirq/qis/clifford_tableau.py Outdated
fill_row(
left='{sign} {paulis}'.format( # from row i+n
sign='-' if self.rs[i + self.n] else '+',
paulis=''.join([pauli_from_matrix(i + self.n, j) for j in range(self.n)]),
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.

no need for temporary list -

Suggested change
paulis=''.join([pauli_from_matrix(i + self.n, j) for j in range(self.n)]),
paulis=''.join(pauli_from_matrix(i + self.n, j) for j in range(self.n)),

Comment thread cirq-core/cirq/qis/clifford_tableau.py Outdated
),
right=' {sign} {paulis}'.format( # from row i
sign='-' if self.rs[i] else '+',
paulis=''.join([pauli_from_matrix(i, j) for j in range(self.n)]),
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.

Suggested change
paulis=''.join([pauli_from_matrix(i, j) for j in range(self.n)]),
paulis=''.join(pauli_from_matrix(i, j) for j in range(self.n)),

Copy link
Copy Markdown
Collaborator

@pavoljuhas pavoljuhas left a comment

Choose a reason for hiding this comment

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

Please see inline comments.

@babacry
Copy link
Copy Markdown
Collaborator Author

babacry commented Feb 12, 2025

Thanks for the comments, Pavol! done updating the pr with appended commits.

Avoid possibility of unmatched values, because bool can return
only the `True` or `False` singletons.
We can appease pylint without line exception.
Copy link
Copy Markdown
Collaborator

@pavoljuhas pavoljuhas left a comment

Choose a reason for hiding this comment

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

I pushed a couple of small changes, let me know if it looks OK to you as is.

LGTM on my part, thank you for improving this!

@babacry
Copy link
Copy Markdown
Collaborator Author

babacry commented Feb 12, 2025

LGTM, thanks!

@pavoljuhas pavoljuhas enabled auto-merge February 12, 2025 19:03
@pavoljuhas pavoljuhas added this pull request to the merge queue Feb 12, 2025
Merged via the queue into quantumlib:main with commit bc9fc97 Feb 12, 2025
@babacry babacry deleted the clifford_tab branch February 21, 2025 06:14
BichengYing pushed a commit to BichengYing/Cirq that referenced this pull request Jun 20, 2025
…umlib#7047)

* Improve the readability of Tableau str.

* Apply comments.

* Fix coverage

* Nit - rename internal functions to start with underscore

* Fix matching of np.bool values by converting them to stock bool

Avoid possibility of unmatched values, because bool can return
only the `True` or `False` singletons.

* Rewrite string formatting with f-string

We can appease pylint without line exception.

---------

Co-authored-by: Pavol Juhas <juhas@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size: M 50< lines changed <250

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants