Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove _t_complexity_ #1377

Merged
merged 3 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 0 additions & 18 deletions qualtran/_infra/adjoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,21 +184,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 @@ -432,9 +432,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 @@ -109,9 +108,6 @@ def _add_ctrled(
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 @@ -106,9 +105,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 @@ -175,14 +171,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 @@ -89,9 +88,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 @@ -120,11 +119,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 @@ -57,9 +56,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
4 changes: 0 additions & 4 deletions qualtran/bloqs/basic_gates/swap.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
SoquetT,
)
from qualtran.cirq_interop import CirqQuregT
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 @@ -87,9 +86,6 @@ def as_cirq_op(

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
4 changes: 0 additions & 4 deletions qualtran/bloqs/basic_gates/toffoli.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
Register,
Signature,
)
from qualtran.cirq_interop.t_complexity_protocol import TComplexity

if TYPE_CHECKING:
import cirq
Expand Down Expand Up @@ -66,9 +65,6 @@ def adjoint(self) -> 'Bloq':
def decompose_bloq(self) -> 'CompositeBloq':
raise DecomposeTypeError(f"{self} is atomic")

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 @@ -154,8 +150,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,
Expand Down Expand Up @@ -126,19 +125,6 @@ def build_call_graph(self, ssa: 'SympySymbolAllocator') -> 'BloqCountDictT':

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
3 changes: 0 additions & 3 deletions qualtran/cirq_interop/_bloq_to_cirq_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
from qualtran.bloqs.mcmt.and_bloq import And, MultiAnd
from qualtran.bloqs.state_preparation import PrepareUniformSuperposition
from qualtran.cirq_interop._bloq_to_cirq import BloqAsCirqGate, CirqQuregT
from qualtran.cirq_interop.t_complexity_protocol import t_complexity_compat
from qualtran.testing import execute_notebook

if TYPE_CHECKING:
Expand Down Expand Up @@ -235,7 +234,6 @@ def test_bloq_as_cirq_gate_for_mod_exp():
# newly allocated RIGHT registers in the decomposition to the one's specified by the user
# when constructing the original operation (in this case, register `x`).
circuit = cirq.Circuit(op, cirq.decompose_once(op))
assert t_complexity_compat(circuit) == 2 * mod_exp.t_complexity()
cirq.testing.assert_has_diagram(
circuit,
'''
Expand All @@ -260,7 +258,6 @@ def test_bloq_as_cirq_gate_for_mod_exp():
# Whereas when directly applying a cirq gate on qubits to get an operations, we need to
# specify both input and output registers.
circuit = cirq.Circuit(gate.on_registers(**out_regs), decomposed_circuit)
assert t_complexity_compat(circuit) == 2 * mod_exp.t_complexity()
# Notice the newly allocated qubits _C(0) and _C(1) for output register x.
cirq.testing.assert_has_diagram(
circuit,
Expand Down
Loading
Loading