Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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