Skip to content

Commit

Permalink
Remove _t_complexity_
Browse files Browse the repository at this point in the history
  • Loading branch information
mpharrigan committed Sep 3, 2024
1 parent abd7ffb commit 05b759e
Show file tree
Hide file tree
Showing 27 changed files with 25 additions and 289 deletions.
18 changes: 0 additions & 18 deletions qualtran/_infra/adjoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,21 +180,3 @@ def wire_symbol(
return self.subbloq.wire_symbol(reg=None).adjoint()

return self.subbloq.wire_symbol(reg=reg.adjoint(), idx=idx).adjoint()

def _t_complexity_(self):
"""The cirq-style `_t_complexity_` delegates to the subbloq's method with a special shim.
The cirq-style t complexity protocol does not leverage the heirarchical decomposition
of high-level bloqs, so we need to shim in an extra `adjoint` boolean flag.
"""
# TODO: https://github.com/quantumlib/Qualtran/issues/735
if not hasattr(self.subbloq, '_t_complexity_'):
return NotImplemented

try:
return self.subbloq._t_complexity_(adjoint=True) # type: ignore[call-arg]
except TypeError as e:
if 'adjoint' in str(e):
return self.subbloq._t_complexity_()
else:
raise e
3 changes: 0 additions & 3 deletions qualtran/_infra/bloq.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,9 +436,6 @@ def t_complexity(self) -> 'TComplexity':

return t_complexity(self)

def _t_complexity_(self) -> 'TComplexity':
return NotImplemented

def as_cirq_op(
self, qubit_manager: 'cirq.QubitManager', **cirq_quregs: 'CirqQuregT'
) -> Tuple[Union['cirq.Operation', None], Dict[str, 'CirqQuregT']]:
Expand Down
5 changes: 0 additions & 5 deletions qualtran/bloqs/arithmetic/conversions/contiguous_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

from qualtran import Bloq, bloq_example, BloqDocSpec, QUInt, Register, Signature
from qualtran.bloqs.basic_gates import Toffoli
from qualtran.cirq_interop.t_complexity_protocol import TComplexity
from qualtran.drawing import WireSymbol
from qualtran.drawing.musical_score import Text, TextBox

Expand Down Expand Up @@ -70,10 +69,6 @@ def on_classical_vals(
) -> Dict[str, 'ClassicalValT']:
return {'mu': mu, 'nu': nu, 's': nu * (nu + 1) // 2 + mu}

def _t_complexity_(self) -> 'TComplexity':
num_toffoli = self.bitsize**2 + self.bitsize - 1
return TComplexity(t=4 * num_toffoli)

def wire_symbol(self, reg: Optional[Register], idx: Tuple[int, ...] = tuple()) -> WireSymbol:
if reg is None:
return Text('')
Expand Down
4 changes: 0 additions & 4 deletions qualtran/bloqs/basic_gates/cnot.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
Signature,
SoquetT,
)
from qualtran.cirq_interop.t_complexity_protocol import TComplexity
from qualtran.drawing import Circle, ModPlus, Text, WireSymbol

if TYPE_CHECKING:
Expand Down Expand Up @@ -132,9 +131,6 @@ def wire_symbol(self, reg: Optional[Register], idx: Tuple[int, ...] = tuple()) -
return ModPlus()
raise ValueError(f'Unknown wire symbol register name: {reg.name}')

def _t_complexity_(self) -> 'TComplexity':
return TComplexity(clifford=1)


@bloq_example
def _cnot() -> CNOT:
Expand Down
4 changes: 0 additions & 4 deletions qualtran/bloqs/basic_gates/global_phase.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
)
from qualtran.bloqs.basic_gates.rotation import ZPowGate
from qualtran.cirq_interop import CirqGateAsBloqBase
from qualtran.cirq_interop.t_complexity_protocol import TComplexity
from qualtran.symbolics import pi, sarg, sexp, SymbolicComplex, SymbolicFloat

if TYPE_CHECKING:
Expand Down Expand Up @@ -112,9 +111,6 @@ def pretty_name(self) -> str:
def __str__(self) -> str:
return f'GPhase({self.coefficient})'

def _t_complexity_(self) -> 'TComplexity':
return TComplexity()


@bloq_example
def _global_phase() -> GlobalPhase:
Expand Down
12 changes: 0 additions & 12 deletions qualtran/bloqs/basic_gates/hadamard.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
Signature,
SoquetT,
)
from qualtran.cirq_interop.t_complexity_protocol import TComplexity
from qualtran.drawing import Circle, Text, TextBox, WireSymbol

if TYPE_CHECKING:
Expand Down Expand Up @@ -104,9 +103,6 @@ def as_cirq_op(
(q,) = q
return cirq.H(q), {'q': np.array([q])}

def _t_complexity_(self):
return TComplexity(clifford=1)

def wire_symbol(self, reg: Optional[Register], idx: Tuple[int, ...] = tuple()) -> 'WireSymbol':
if reg is None:
return Text('')
Expand Down Expand Up @@ -173,14 +169,6 @@ def as_cirq_op(
'target': np.array([target]),
}

def _t_complexity_(self) -> 'TComplexity':
# This is based on the decomposition provided by `cirq.decompose_multi_controlled_rotation`
# which uses three cirq.MatrixGate's to do a controlled version of any single-qubit gate.
# The first MatrixGate happens to be a clifford, Hadamard operation in this case.
# The other two are considered 'rotations'.
# https://github.com/quantumlib/Qualtran/issues/237
return TComplexity(rotations=2, clifford=4)

def my_static_costs(self, cost_key: 'CostKey'):
from qualtran.resource_counting import GateCounts, QECGatesCost

Expand Down
4 changes: 0 additions & 4 deletions qualtran/bloqs/basic_gates/identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
Signature,
SoquetT,
)
from qualtran.cirq_interop.t_complexity_protocol import TComplexity
from qualtran.drawing import Text, TextBox, WireSymbol
from qualtran.symbolics import is_symbolic, SymbolicInt

Expand Down Expand Up @@ -88,9 +87,6 @@ def as_cirq_op(

return cirq.IdentityGate(self.bitsize).on(*q), {'q': q}

def _t_complexity_(self):
return TComplexity()

def wire_symbol(self, reg: Optional[Register], idx: Tuple[int, ...] = tuple()) -> 'WireSymbol':
if reg is None:
return Text('')
Expand Down
6 changes: 0 additions & 6 deletions qualtran/bloqs/basic_gates/rotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

from qualtran import bloq_example, BloqDocSpec, CompositeBloq, DecomposeTypeError, Register
from qualtran.cirq_interop import CirqGateAsBloqBase
from qualtran.cirq_interop.t_complexity_protocol import TComplexity
from qualtran.drawing import Text, TextBox, WireSymbol
from qualtran.symbolics import SymbolicFloat

Expand Down Expand Up @@ -117,11 +116,6 @@ def decompose_bloq(self) -> 'CompositeBloq':
def cirq_gate(self) -> cirq.Gate:
return cirq.CZPowGate(exponent=self.exponent, global_shift=self.global_shift)

def _t_complexity_(self) -> 'TComplexity':
if cirq.has_stabilizer_effect(self.cirq_gate):
return TComplexity(clifford=1)
return TComplexity(rotations=1)

def __pow__(self, power):
g = self.cirq_gate**power
return CZPowGate(g.exponent, g.global_shift, self.eps)
Expand Down
4 changes: 0 additions & 4 deletions qualtran/bloqs/basic_gates/s_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
from attrs import frozen

from qualtran import Bloq, bloq_example, BloqDocSpec, ConnectionT, Register, Signature
from qualtran.cirq_interop.t_complexity_protocol import TComplexity
from qualtran.drawing import Text, TextBox, WireSymbol

if TYPE_CHECKING:
Expand Down Expand Up @@ -56,9 +55,6 @@ class SGate(Bloq):
def signature(self) -> 'Signature':
return Signature.build(q=1)

def _t_complexity_(self) -> 'TComplexity':
return TComplexity(clifford=1)

def my_tensors(
self, incoming: Dict[str, 'ConnectionT'], outgoing: Dict[str, 'ConnectionT']
) -> List['qtn.Tensor']:
Expand Down
7 changes: 0 additions & 7 deletions qualtran/bloqs/basic_gates/swap.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
SoquetT,
)
from qualtran.cirq_interop import CirqQuregT, decompose_from_cirq_style_method
from qualtran.cirq_interop.t_complexity_protocol import TComplexity
from qualtran.drawing import Circle, Text, TextBox, WireSymbol
from qualtran.resource_counting.generalizers import ignore_split_join

Expand Down Expand Up @@ -94,9 +93,6 @@ def as_cirq_op(
(y,) = y
return cirq.SWAP.on(x, y), {'x': np.asarray([x]), 'y': np.asarray([y])}

def _t_complexity_(self) -> 'TComplexity':
return TComplexity(clifford=1)

def my_tensors(
self, incoming: Dict[str, 'ConnectionT'], outgoing: Dict[str, 'ConnectionT']
) -> List['qtn.Tensor']:
Expand Down Expand Up @@ -200,9 +196,6 @@ def on_classical_vals(
return {'ctrl': 1, 'x': y, 'y': x}
raise ValueError("Bad control value for TwoBitCSwap classical simulation.")

def _t_complexity_(self) -> 'TComplexity':
return TComplexity(t=7, clifford=10)

def build_call_graph(self, ssa: 'SympySymbolAllocator') -> Set['BloqCountT']:
return {(TGate(), 7), (CNOT(), 8), (Hadamard(), 2)}

Expand Down
4 changes: 0 additions & 4 deletions qualtran/bloqs/basic_gates/toffoli.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

from qualtran import Bloq, bloq_example, BloqDocSpec, Connection, QBit, Register, Signature
from qualtran.bloqs.basic_gates import TGate
from qualtran.cirq_interop.t_complexity_protocol import TComplexity
from qualtran.resource_counting import SympySymbolAllocator

if TYPE_CHECKING:
Expand Down Expand Up @@ -59,9 +58,6 @@ def adjoint(self) -> 'Bloq':
def build_call_graph(self, ssa: 'SympySymbolAllocator') -> Set['BloqCountT']:
return {(TGate(), 4)}

def _t_complexity_(self):
return TComplexity(t=4)

def my_tensors(
self,
incoming: Dict[str, NDArray[Connection]], # type: ignore[type-var]
Expand Down
4 changes: 0 additions & 4 deletions qualtran/bloqs/basic_gates/x_basis.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
Signature,
SoquetT,
)
from qualtran.cirq_interop.t_complexity_protocol import TComplexity
from qualtran.drawing import directional_text_box, Text, WireSymbol

if TYPE_CHECKING:
Expand Down Expand Up @@ -257,9 +256,6 @@ def as_cirq_op(
(q,) = q
return cirq.X(q), {'q': np.asarray([q])}

def _t_complexity_(self):
return TComplexity(clifford=1)

def wire_symbol(self, reg: Register, idx: Tuple[int, ...] = tuple()) -> 'WireSymbol':
from qualtran.drawing import ModPlus

Expand Down
4 changes: 0 additions & 4 deletions qualtran/bloqs/basic_gates/y_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
Signature,
SoquetT,
)
from qualtran.cirq_interop.t_complexity_protocol import TComplexity
from qualtran.drawing import Circle, Text, TextBox, WireSymbol

if TYPE_CHECKING:
Expand Down Expand Up @@ -98,9 +97,6 @@ def as_cirq_op(
(q,) = q
return cirq.Y(q), {'q': np.asarray([q])}

def _t_complexity_(self) -> 'TComplexity':
return TComplexity(clifford=1)

def wire_symbol(
self, reg: Optional['Register'], idx: Tuple[int, ...] = tuple()
) -> 'WireSymbol':
Expand Down
4 changes: 0 additions & 4 deletions qualtran/bloqs/basic_gates/z_basis.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
SoquetT,
)
from qualtran.bloqs.bookkeeping import ArbitraryClifford
from qualtran.cirq_interop.t_complexity_protocol import TComplexity
from qualtran.drawing import Circle, directional_text_box, Text, TextBox, WireSymbol

if TYPE_CHECKING:
Expand Down Expand Up @@ -277,9 +276,6 @@ def as_cirq_op(
(q,) = q
return cirq.Z(q), {'q': np.asarray([q])}

def _t_complexity_(self) -> 'TComplexity':
return TComplexity(clifford=1)

def wire_symbol(
self, reg: Optional['Register'], idx: Tuple[int, ...] = tuple()
) -> 'WireSymbol':
Expand Down
4 changes: 0 additions & 4 deletions qualtran/bloqs/bookkeeping/_bookkeeping_bloq.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from typing import Dict, Iterable, Optional, Sequence, Tuple, TYPE_CHECKING

from qualtran import Bloq, BloqBuilder, SoquetT
from qualtran.cirq_interop.t_complexity_protocol import TComplexity

if TYPE_CHECKING:

Expand All @@ -42,6 +41,3 @@ def add_controlled(
return ctrl_soqs, out_soqs

return self, add_controlled

def _t_complexity_(self) -> 'TComplexity':
return TComplexity()
4 changes: 0 additions & 4 deletions qualtran/bloqs/bookkeeping/arbitrary_clifford.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from sympy import Expr

from qualtran import Bloq, QAny, Register, Signature
from qualtran.cirq_interop.t_complexity_protocol import TComplexity


@frozen
Expand All @@ -40,8 +39,5 @@ class ArbitraryClifford(Bloq):
def signature(self) -> Signature:
return Signature([Register('x', QAny(bitsize=self.n))])

def _t_complexity_(self) -> 'TComplexity':
return TComplexity(clifford=1)

def __str__(self):
return f'ArbitraryClifford(n={self.n})'
10 changes: 4 additions & 6 deletions qualtran/bloqs/for_testing/atom.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
GateWithRegisters,
Signature,
)
from qualtran.cirq_interop.t_complexity_protocol import TComplexity
from qualtran.resource_counting import CostKey, GateCounts, QECGatesCost

if TYPE_CHECKING:
Expand Down Expand Up @@ -72,9 +71,6 @@ def my_static_costs(self, cost_key: 'CostKey'):
return GateCounts(t=100)
return NotImplemented

def _t_complexity_(self) -> 'TComplexity':
return TComplexity(100)

def __repr__(self):
if self.tag:
return f'TestAtom({self.tag!r})'
Expand Down Expand Up @@ -161,8 +157,10 @@ def _unitary_(self):
def adjoint(self) -> 'TestGWRAtom':
return attrs.evolve(self, is_adjoint=not self.is_adjoint)

def _t_complexity_(self) -> 'TComplexity':
return TComplexity(100)
def my_static_costs(self, cost_key: 'CostKey'):
if isinstance(cost_key, QECGatesCost):
return GateCounts(t=100)
return NotImplemented

def __repr__(self):
tag = f'{self.tag!r}' if self.tag else ''
Expand Down
14 changes: 0 additions & 14 deletions qualtran/bloqs/mcmt/and_bloq.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
from qualtran.bloqs.basic_gates import TGate, XGate
from qualtran.bloqs.bookkeeping import ArbitraryClifford
from qualtran.cirq_interop import decompose_from_cirq_style_method
from qualtran.cirq_interop.t_complexity_protocol import TComplexity
from qualtran.drawing import Circle, directional_text_box, Text, WireSymbol
from qualtran.resource_counting import big_O, BloqCountT, SympySymbolAllocator
from qualtran.resource_counting.generalizers import (
Expand Down Expand Up @@ -121,19 +120,6 @@ def build_call_graph(self, ssa: 'SympySymbolAllocator') -> Set['BloqCountT']:

return {(ArbitraryClifford(n=2), 9 + 2 * pre_post_cliffords), (TGate(), 4)}

def _t_complexity_(self) -> 'TComplexity':
if not FLAG_AND_AS_LEAF:
return NotImplemented

if isinstance(self.cv1, sympy.Expr) or isinstance(self.cv2, sympy.Expr):
pre_post_cliffords: Union[sympy.Order, int] = 0
else:
pre_post_cliffords = 2 - self.cv1 - self.cv2
if self.uncompute:
return TComplexity(clifford=4 + 2 * pre_post_cliffords)

return TComplexity(t=4, clifford=9 + 2 * pre_post_cliffords)

def on_classical_vals(
self, *, ctrl: NDArray[np.uint8], target: Optional[int] = None
) -> Dict[str, ClassicalValT]:
Expand Down
4 changes: 0 additions & 4 deletions qualtran/cirq_interop/_bloq_to_cirq.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,6 @@ def bloq_on(
)
return BloqAsCirqGate(bloq=bloq).on_registers(**all_quregs), out_quregs

def _t_complexity_(self):
"""Delegate to the bloq's t complexity."""
return self._bloq.t_complexity()

def _num_qubits_(self) -> int:
return total_bits(self.signature)

Expand Down
Loading

0 comments on commit 05b759e

Please sign in to comment.