Skip to content

Commit

Permalink
Improve the readability of CliffordTableau _full_str_ function (quant…
Browse files Browse the repository at this point in the history
…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 <[email protected]>
  • Loading branch information
babacry and pavoljuhas authored Feb 12, 2025
1 parent 60f2590 commit bc9fc97
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 26 deletions.
60 changes: 35 additions & 25 deletions cirq-core/cirq/qis/clifford_tableau.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,32 +326,42 @@ def __str__(self) -> str:
return string

def _str_full_(self) -> str:
string = ''

string += 'stable' + ' ' * max(self.n * 2 - 3, 1)
string += '| destable\n'
string += '-' * max(7, self.n * 2 + 3) + '+' + '-' * max(10, self.n * 2 + 4) + '\n'

for j in range(self.n):
for i in [j + self.n, j]:
string += '- ' if self.rs[i] else '+ '

for k in range(self.n):
if self.xs[i, k] & (not self.zs[i, k]):
string += f'X{k}'
elif (not self.xs[i, k]) & self.zs[i, k]:
string += f'Z{k}'
elif self.xs[i, k] & self.zs[i, k]:
string += f'Y{k}'
else:
string += ' '

if i == j + self.n:
string += ' ' * max(0, 4 - self.n * 2) + ' | '

string += '\n'
left_col_width = max(7, self.n * 2 + 3)
right_col_width = max(10, self.n * 2 + 4)

def _fill_row(left: str, right: str, mid='|', fill=' ') -> str:
"""Builds a left-aligned fixed-width row with 2 columns."""
return f"{left:{fill}<{left_col_width}}{mid}{right:{fill}<{right_col_width}}".rstrip()

def _pauli_from_matrix(r: int, c: int) -> str:
match (bool(self.xs[r, c]), bool(self.zs[r, c])):
case (True, False):
return f'X{c}'
case (False, True):
return f'Z{c}'
case (True, True):
return f'Y{c}'
case _:
# (False, False) is the only leftover option
return ' '

title_row = _fill_row('stable', ' destable')
divider = _fill_row('', '', mid='+', fill='-')
contents = [
_fill_row(
left=(
f"{'-' if self.rs[i + self.n] else '+'} "
f"{''.join(_pauli_from_matrix(i + self.n, j) for j in range(self.n))}"
),
right=(
f" {'-' if self.rs[i] else '+'} "
f"{''.join(_pauli_from_matrix(i, j) for j in range(self.n))}"
),
)
for i in range(self.n)
]

return string
return '\n'.join([title_row, divider, *contents]) + '\n'

def then(self, second: 'CliffordTableau') -> 'CliffordTableau':
"""Returns a composed CliffordTableau of this tableau and the second tableau.
Expand Down
3 changes: 2 additions & 1 deletion cirq-core/cirq/qis/clifford_tableau_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

"""Tests for clifford tableau."""

import numpy as np
import pytest

Expand Down Expand Up @@ -308,7 +309,7 @@ def test_str_full():
t = cirq.CliffordTableau(num_qubits=2)
expected_str = r"""stable | destable
-------+----------
+ Z0 | + X0
+ Z0 | + X0
+ Z1 | + X1
"""
assert t._str_full_() == expected_str
Expand Down

0 comments on commit bc9fc97

Please sign in to comment.