Skip to content

Commit 30fa6f5

Browse files
authored
Fix bug in cirq-ft due to which T-complexity fails for gates with an empty decomposition / no-op (#6252)
1 parent 95c672a commit 30fa6f5

File tree

4 files changed

+22
-3
lines changed

4 files changed

+22
-3
lines changed

cirq-ft/cirq_ft/algos/select_swap_qrom.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def __init__(
124124
assert len(target_bitsizes) == len(data)
125125
assert all(t >= max(d).bit_length() for t, d in zip(target_bitsizes, data))
126126
self._num_sequences = len(data)
127-
self._target_bitsizes = target_bitsizes
127+
self._target_bitsizes = tuple(target_bitsizes)
128128
self._iteration_length = len(data[0])
129129
if block_size is None:
130130
# Figure out optimal value of block_size

cirq-ft/cirq_ft/algos/select_swap_qrom_test.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,9 @@ def test_qroam_diagram():
101101
def test_qroam_raises():
102102
with pytest.raises(ValueError, match="must be of equal length"):
103103
_ = cirq_ft.SelectSwapQROM([1, 2], [1, 2, 3])
104+
105+
106+
def test_qroam_hashable():
107+
qrom = cirq_ft.SelectSwapQROM([1, 2, 5, 6, 7, 8])
108+
assert hash(qrom) is not None
109+
assert cirq_ft.t_complexity(qrom) == cirq_ft.TComplexity(32, 160, 0)

cirq-ft/cirq_ft/infra/decompose_protocol.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,4 @@ def _decompose_once_considering_known_decomposition(val: Any) -> DecomposeResult
9898
else:
9999
decomposed = cirq.decompose_once(val, context=context, flatten=False, default=None)
100100

101-
return [*cirq.flatten_to_ops(decomposed)] if decomposed else None
101+
return [*cirq.flatten_to_ops(decomposed)] if decomposed is not None else None

cirq-ft/cirq_ft/infra/decompose_protocol_test.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515
import cirq
1616
import numpy as np
1717
import pytest
18-
from cirq_ft.infra.decompose_protocol import _fredkin, _try_decompose_from_known_decompositions
18+
from cirq_ft.infra.decompose_protocol import (
19+
_fredkin,
20+
_try_decompose_from_known_decompositions,
21+
_decompose_once_considering_known_decomposition,
22+
)
1923

2024

2125
def test_fredkin_unitary():
@@ -43,3 +47,12 @@ def test_decompose_fredkin(gate):
4347
for o in cirq.flatten_op_tree(_fredkin((c, t1, t2), context))
4448
)
4549
assert want == _try_decompose_from_known_decompositions(op, context)
50+
51+
52+
def test_known_decomposition_empty_unitary():
53+
class DecomposeEmptyList(cirq.testing.SingleQubitGate):
54+
def _decompose_(self, _):
55+
return []
56+
57+
gate = DecomposeEmptyList()
58+
assert _decompose_once_considering_known_decomposition(gate) == []

0 commit comments

Comments
 (0)