Skip to content

Commit bc9fc97

Browse files
babacrypavoljuhas
andauthored
Improve the readability of CliffordTableau _full_str_ function (#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>
1 parent 60f2590 commit bc9fc97

File tree

2 files changed

+37
-26
lines changed

2 files changed

+37
-26
lines changed

cirq-core/cirq/qis/clifford_tableau.py

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -326,32 +326,42 @@ def __str__(self) -> str:
326326
return string
327327

328328
def _str_full_(self) -> str:
329-
string = ''
330-
331-
string += 'stable' + ' ' * max(self.n * 2 - 3, 1)
332-
string += '| destable\n'
333-
string += '-' * max(7, self.n * 2 + 3) + '+' + '-' * max(10, self.n * 2 + 4) + '\n'
334-
335-
for j in range(self.n):
336-
for i in [j + self.n, j]:
337-
string += '- ' if self.rs[i] else '+ '
338-
339-
for k in range(self.n):
340-
if self.xs[i, k] & (not self.zs[i, k]):
341-
string += f'X{k}'
342-
elif (not self.xs[i, k]) & self.zs[i, k]:
343-
string += f'Z{k}'
344-
elif self.xs[i, k] & self.zs[i, k]:
345-
string += f'Y{k}'
346-
else:
347-
string += ' '
348-
349-
if i == j + self.n:
350-
string += ' ' * max(0, 4 - self.n * 2) + ' | '
351-
352-
string += '\n'
329+
left_col_width = max(7, self.n * 2 + 3)
330+
right_col_width = max(10, self.n * 2 + 4)
331+
332+
def _fill_row(left: str, right: str, mid='|', fill=' ') -> str:
333+
"""Builds a left-aligned fixed-width row with 2 columns."""
334+
return f"{left:{fill}<{left_col_width}}{mid}{right:{fill}<{right_col_width}}".rstrip()
335+
336+
def _pauli_from_matrix(r: int, c: int) -> str:
337+
match (bool(self.xs[r, c]), bool(self.zs[r, c])):
338+
case (True, False):
339+
return f'X{c}'
340+
case (False, True):
341+
return f'Z{c}'
342+
case (True, True):
343+
return f'Y{c}'
344+
case _:
345+
# (False, False) is the only leftover option
346+
return ' '
347+
348+
title_row = _fill_row('stable', ' destable')
349+
divider = _fill_row('', '', mid='+', fill='-')
350+
contents = [
351+
_fill_row(
352+
left=(
353+
f"{'-' if self.rs[i + self.n] else '+'} "
354+
f"{''.join(_pauli_from_matrix(i + self.n, j) for j in range(self.n))}"
355+
),
356+
right=(
357+
f" {'-' if self.rs[i] else '+'} "
358+
f"{''.join(_pauli_from_matrix(i, j) for j in range(self.n))}"
359+
),
360+
)
361+
for i in range(self.n)
362+
]
353363

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

356366
def then(self, second: 'CliffordTableau') -> 'CliffordTableau':
357367
"""Returns a composed CliffordTableau of this tableau and the second tableau.

cirq-core/cirq/qis/clifford_tableau_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
"""Tests for clifford tableau."""
16+
1617
import numpy as np
1718
import pytest
1819

@@ -308,7 +309,7 @@ def test_str_full():
308309
t = cirq.CliffordTableau(num_qubits=2)
309310
expected_str = r"""stable | destable
310311
-------+----------
311-
+ Z0 | + X0
312+
+ Z0 | + X0
312313
+ Z1 | + X1
313314
"""
314315
assert t._str_full_() == expected_str

0 commit comments

Comments
 (0)