Skip to content

Commit

Permalink
Legacy _t_complexity_ still supported via getattr
Browse files Browse the repository at this point in the history
  • Loading branch information
mpharrigan committed Oct 11, 2024
1 parent 7cd87f0 commit c334b60
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
1 change: 1 addition & 0 deletions qualtran/cirq_interop/t_complexity_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

from qualtran import Bloq
from qualtran.symbolics import ceil, log2, SymbolicFloat, SymbolicInt

from .decompose_protocol import _decompose_once_considering_known_decomposition

_T_GATESET = cirq.Gateset(cirq.T, cirq.T**-1, unroll_circuit_op=False)
Expand Down
9 changes: 5 additions & 4 deletions qualtran/cirq_interop/t_complexity_protocol_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import TYPE_CHECKING

import cirq
import pytest
Expand All @@ -30,9 +29,6 @@
from qualtran.cirq_interop.testing import GateHelper
from qualtran.testing import execute_notebook

if TYPE_CHECKING:
pass


class DoesNotSupportTComplexity: ...

Expand Down Expand Up @@ -66,6 +62,11 @@ def signature(self) -> 'Signature':
return Signature.build(q=1)


def test_legacy_t_complexity_annotation():
# Test the deprecated `getattr(.., '_t_complexity_')
assert SupportsTComplexityGateWithRegisters().t_complexity() == TComplexity(t=1, clifford=2)


def test_t_complexity_for_bloq_does_not_support():
with pytest.raises(DecomposeNotImplementedError):
_ = t_complexity(DoesNotSupportTComplexityBloq())
Expand Down
16 changes: 16 additions & 0 deletions qualtran/resource_counting/_bloq_counts.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
import warnings
from collections import defaultdict
from typing import Callable, cast, Dict, Sequence, Tuple, TYPE_CHECKING

Expand Down Expand Up @@ -295,6 +296,21 @@ def compute(self, bloq: 'Bloq', get_callee_cost: Callable[['Bloq'], GateCounts])
from qualtran.bloqs.bookkeeping._bookkeeping_bloq import _BookkeepingBloq
from qualtran.bloqs.mcmt import And, MultiTargetCNOT

if self.legacy_shims:
if hasattr(bloq, '_t_complexity_'):
legacy_val = bloq._t_complexity_()
else:
legacy_val = NotImplemented
if legacy_val is not NotImplemented:
warnings.warn(
"Please migrate explicit cost annotations to the general "
"`Bloq.my_static_costs` method override.",
DeprecationWarning,
)
return GateCounts(
t=legacy_val.t, clifford=legacy_val.clifford, rotation=legacy_val.rotations
)

# T gates
if bloq_is_t_like(bloq):
return GateCounts(t=1)
Expand Down

0 comments on commit c334b60

Please sign in to comment.