Skip to content

Commit

Permalink
remove _get_all_rotation_types
Browse files Browse the repository at this point in the history
  • Loading branch information
anurudhp committed Aug 20, 2024
1 parent 044ff6e commit 576560d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 48 deletions.
34 changes: 4 additions & 30 deletions qualtran/resource_counting/t_counts_from_sigma.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
# 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.
import inspect
import sys
from typing import cast, Mapping, Optional, Protocol, runtime_checkable, Tuple, Type, TYPE_CHECKING
from typing import Mapping, TYPE_CHECKING

import cirq

Expand All @@ -23,38 +21,14 @@
from qualtran import Bloq


@runtime_checkable
class _HasEps(Protocol):
"""Protocol for typing `RotationBloq` base class mixin that has accuracy specified as eps."""

eps: float


def _get_all_rotation_types() -> Tuple[Type['_HasEps'], ...]:
"""Returns all classes defined in bloqs.basic_gates which have an attribute `eps`."""
from qualtran.bloqs.basic_gates import GlobalPhase

bloqs_to_exclude = [GlobalPhase]

return tuple(
cast(Type['_HasEps'], v) # Can't use `issubclass` with protocols with attributes.
for (_, v) in inspect.getmembers(sys.modules['qualtran.bloqs.basic_gates'], inspect.isclass)
if isinstance(v, _HasEps) and v not in bloqs_to_exclude
)


def t_counts_from_sigma(
sigma: Mapping['Bloq', SymbolicInt],
rotation_types: Optional[Tuple[Type['_HasEps'], ...]] = None,
) -> SymbolicInt:
def t_counts_from_sigma(sigma: Mapping['Bloq', SymbolicInt]) -> SymbolicInt:
"""Aggregates T-counts from a sigma dictionary by summing T-costs for all rotation bloqs."""
from qualtran.bloqs.basic_gates import TGate
from qualtran.cirq_interop.t_complexity_protocol import TComplexity
from qualtran.resource_counting.classify_bloqs import bloq_is_rotation

if rotation_types is None:
rotation_types = _get_all_rotation_types()
ret = sigma.get(TGate(), 0) + sigma.get(TGate().adjoint(), 0)
for bloq, counts in sigma.items():
if isinstance(bloq, rotation_types) and not cirq.has_stabilizer_effect(bloq):
if bloq_is_rotation(bloq) and not cirq.has_stabilizer_effect(bloq):
ret += ceil(TComplexity.rotation_cost(bloq.eps)) * counts
return ret
19 changes: 1 addition & 18 deletions qualtran/resource_counting/t_counts_from_sigma_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,14 @@
Rx,
Ry,
Rz,
SU2RotationGate,
TGate,
Toffoli,
XPowGate,
YPowGate,
ZPowGate,
)
from qualtran.cirq_interop.t_complexity_protocol import TComplexity
from qualtran.resource_counting.t_counts_from_sigma import (
_get_all_rotation_types,
t_counts_from_sigma,
)


def test_all_rotation_types():
assert set(_get_all_rotation_types()) == {
CZPowGate,
Rx,
Ry,
Rz,
XPowGate,
YPowGate,
ZPowGate,
SU2RotationGate,
}
from qualtran.resource_counting.t_counts_from_sigma import t_counts_from_sigma


def test_t_counts_from_sigma():
Expand Down

0 comments on commit 576560d

Please sign in to comment.