Skip to content

Commit e6921ad

Browse files
committed
Address review comments.
1 parent d7a0b0d commit e6921ad

2 files changed

Lines changed: 38 additions & 32 deletions

File tree

qualtran/rotation_synthesis/matrix/_clifford_t_repr.py

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -80,53 +80,59 @@ def _xz_sequence(
8080
return None
8181

8282

83-
def _matsumoto_amano_sequence(matrix: _su2_ct.SU2CliffordT) -> tuple[str, ...]:
84-
r"""Represents the Clifford+T operator in the Matsumoto-Amano normal form.
83+
def _matsumoto_amano_syllable(matrix: _su2_ct.SU2CliffordT) -> list[str]:
84+
"""Computes the next syllable in the Matsumoto-Amano decomposition for matrix.
8585
8686
Returns:
87-
a list of gates matching the regular expression $(T|\eps)(HT|SHT)^*C$,
88-
where C is a Clifford operator, itself represented as a list of H and S gates.
89-
The gates are returned in the order they need to be applied to generate the
90-
input matrix.
87+
the next syllable as a list of gates, representing either T, HT, or SHT.
9188
9289
Raises:
9390
ValueError if the parity matrix doesn't match any of the forms in
94-
Lemma 4.10, https://arxiv.org/abs/1312.6584, or if during the decomposition
95-
an invalid SU2CliffordT matrix is created.
91+
Lemma 4.10, https://arxiv.org/abs/1312.6584.
9692
"""
97-
if matrix.det() == 2:
98-
return clifford(matrix)
9993
parity = matrix.bloch_form_parity()
10094
# Parity matrix must have a 0 column, see Lemma 4.10, https://arxiv.org/abs/1312.6584.
10195
# We move it to be last.
10296
for i in range(2):
10397
if np.all(parity[:, i] == 0):
10498
parity[:, [i, 2]] = parity[:, [2, i]]
10599
break
106-
gates: tuple[str, ...] = ()
107-
new: Union[_su2_ct.SU2CliffordT, None]
108100
if np.array_equal(parity, np.array([[1, 1, 0], [1, 1, 0], [0, 0, 0]])):
109-
# Leftmost syllabe is T
110-
new = _su2_ct.Tz.adjoint() @ matrix
111-
gates = ('T',)
101+
# Leftmost syllable is T
102+
return ['T']
112103
elif np.array_equal(parity, np.array([[0, 0, 0], [1, 1, 0], [1, 1, 0]])):
113-
# Leftmost syllabe is HT
114-
new = _su2_ct.HSqrt2.adjoint() @ matrix
115-
new = _su2_ct.Tz.adjoint() @ new
116-
gates = ('T', 'H')
104+
# Leftmost syllable is HT
105+
return ['H', 'T']
117106
elif np.array_equal(parity, np.array([[1, 1, 0], [0, 0, 0], [1, 1, 0]])):
118-
# Leftmost syllabe is SHT
119-
new = _su2_ct.SSqrt2.adjoint() @ matrix
120-
new = _su2_ct.HSqrt2.adjoint() @ new
121-
new = _su2_ct.Tz.adjoint() @ new
122-
gates = ('T', 'H', 'S')
107+
# Leftmost syllable is SHT
108+
return ['S', 'H', 'T']
123109
else:
124110
raise ValueError(f'Unexpected parity matrix:\n{parity}')
125-
new = new.scale_down()
126-
if new is None or not new.is_valid():
127-
raise ValueError('Invalid SU2CliffordT matrix')
128-
seq = _matsumoto_amano_sequence(new)
129-
return seq + gates
111+
112+
113+
def _matsumoto_amano_sequence(matrix: _su2_ct.SU2CliffordT) -> tuple[str, ...]:
114+
r"""Represents the Clifford+T operator in the Matsumoto-Amano normal form.
115+
116+
Returns:
117+
a list of gates matching the regular expression $(T|\eps)(HT|SHT)^*C$,
118+
where C is a Clifford operator, itself represented as a list of H and S gates.
119+
The gates are returned in the order they need to be applied to generate the
120+
input matrix.
121+
122+
Raises:
123+
ValueError if during the decomposition an invalid SU2CliffordT matrix is created.
124+
"""
125+
gates = []
126+
while matrix.det() != _TWO:
127+
syllable = _matsumoto_amano_syllable(matrix)
128+
gates += syllable
129+
for gate in syllable:
130+
matrix = _su2_ct.GATE_MAP[gate].adjoint() @ matrix
131+
new = matrix.scale_down()
132+
if new is None or not new.is_valid():
133+
raise ValueError('Invalid SU2CliffordT matrix')
134+
matrix = new
135+
return clifford(matrix) + tuple(gates[::-1])
130136

131137

132138
def to_sequence(matrix: _su2_ct.SU2CliffordT, fmt: str = 'xyz') -> tuple[str, ...]:
@@ -135,11 +141,11 @@ def to_sequence(matrix: _su2_ct.SU2CliffordT, fmt: str = 'xyz') -> tuple[str, ..
135141
Allowable format strings are
136142
- 'xyz' uses Tx, Ty, Tz gates.
137143
- 'xz' uses $Tx, Tz, Tx^\dagger, Tz^\dagger$
138-
- 't' uses T gates, and returns the Matsumoto-Amano normal form.
144+
- 't' uses only Tz gates, and returns the Matsumoto-Amano normal form.
139145
140146
Args:
141147
matrix: The matrix to represent.
142-
fmt: A string from the set {'xz', 'xyz', t'} representing the allowed T gates described above.
148+
fmt: A string from the set {'xz', 'xyz', 't'} representing the allowed T gates described above.
143149
144150
Returns:
145151
A tuple of strings representing the gates.

qualtran/rotation_synthesis/matrix/clifford_t_repr_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def test_to_xz_seq(g: _su2_ct.SU2CliffordT):
6060

6161

6262
@pytest.mark.parametrize("g", _make_random_su(50, 5, random_cliffords=True, seed=0))
63-
@pytest.mark.parametrize("fmt", ('xyz', 't'))
63+
@pytest.mark.parametrize("fmt", ('xyz', 'xz', 't'))
6464
def test_to_cirq(g: _su2_ct.SU2CliffordT, fmt: str):
6565
g = g.rescale()
6666
circuit = cirq.Circuit(ctr.to_cirq(g, fmt))

0 commit comments

Comments
 (0)