From 0852ab66d98f454837502e6a97325ef5c4b9969d Mon Sep 17 00:00:00 2001 From: Matthew Harrigan Date: Tue, 27 Aug 2024 15:44:25 -0700 Subject: [PATCH] Decompose Type Error --- qualtran/bloqs/arithmetic/comparison.py | 2 ++ qualtran/bloqs/arithmetic/controlled_addition.py | 4 ++++ qualtran/bloqs/data_loading/select_swap_qrom.py | 12 ++++++++++-- qualtran/bloqs/mcmt/and_bloq.py | 2 ++ qualtran/bloqs/mod_arithmetic/mod_addition.py | 6 +++++- 5 files changed, 23 insertions(+), 3 deletions(-) diff --git a/qualtran/bloqs/arithmetic/comparison.py b/qualtran/bloqs/arithmetic/comparison.py index ff4986262..290649b32 100644 --- a/qualtran/bloqs/arithmetic/comparison.py +++ b/qualtran/bloqs/arithmetic/comparison.py @@ -1080,6 +1080,8 @@ def build_composite_bloq( a = bb.add(SignExtend(self.dtype, QInt(self.dtype.bitsize + 1)), x=a) b = bb.add(SignExtend(self.dtype, QInt(self.dtype.bitsize + 1)), x=b) else: + if self.dtype.is_symbolic(): + raise DecomposeTypeError(f"Cannot decompose symbolic {self}") a = bb.join(np.concatenate([[bb.allocate(1)], bb.split(a)])) b = bb.join(np.concatenate([[bb.allocate(1)], bb.split(b)])) diff --git a/qualtran/bloqs/arithmetic/controlled_addition.py b/qualtran/bloqs/arithmetic/controlled_addition.py index cfe4bcc5d..da75a1e6e 100644 --- a/qualtran/bloqs/arithmetic/controlled_addition.py +++ b/qualtran/bloqs/arithmetic/controlled_addition.py @@ -23,6 +23,7 @@ bloq_example, BloqBuilder, BloqDocSpec, + DecomposeTypeError, QBit, QInt, QUInt, @@ -134,6 +135,9 @@ def wire_symbol(self, soq: 'Soquet') -> 'WireSymbol': def build_composite_bloq( self, bb: 'BloqBuilder', ctrl: 'Soquet', a: 'Soquet', b: 'Soquet' ) -> Dict[str, 'SoquetT']: + if self.a_dtype.is_symbolic() or self.b_dtype.is_symbolic(): + raise DecomposeTypeError(f"Cannot support symbolic {self}") + a_arr = bb.split(a) ctrl_q = bb.split(ctrl)[0] ancilla_arr = [] diff --git a/qualtran/bloqs/data_loading/select_swap_qrom.py b/qualtran/bloqs/data_loading/select_swap_qrom.py index 52952719e..c98e45c25 100644 --- a/qualtran/bloqs/data_loading/select_swap_qrom.py +++ b/qualtran/bloqs/data_loading/select_swap_qrom.py @@ -22,7 +22,15 @@ import sympy from numpy.typing import ArrayLike -from qualtran import bloq_example, BloqDocSpec, BQUInt, GateWithRegisters, Register, Signature +from qualtran import ( + bloq_example, + BloqDocSpec, + BQUInt, + DecomposeTypeError, + GateWithRegisters, + Register, + Signature, +) from qualtran.bloqs.arithmetic.bitwise import Xor from qualtran.bloqs.bookkeeping import Partition from qualtran.bloqs.data_loading.qrom import QROM @@ -394,7 +402,7 @@ def build_composite_bloq(self, bb: 'BloqBuilder', **soqs: 'SoquetT') -> Dict[str target = [soqs.pop(reg.name) for reg in self.target_registers] # Allocate intermediate clean/dirty ancilla for the underlying QROM call. if is_symbolic(*self.block_sizes): - raise ValueError( + raise DecomposeTypeError( f"Cannot decompose SelectSwapQROM bloq with symbolic block sizes. Found {self.block_sizes=}" ) block_sizes = cast(Tuple[int, ...], self.block_sizes) diff --git a/qualtran/bloqs/mcmt/and_bloq.py b/qualtran/bloqs/mcmt/and_bloq.py index 29f8d4188..0d8cc24cc 100644 --- a/qualtran/bloqs/mcmt/and_bloq.py +++ b/qualtran/bloqs/mcmt/and_bloq.py @@ -386,6 +386,8 @@ def decompose_from_registers( yield self._decompose_via_tree(control, self.concrete_cvs, ancilla, *target) def decompose_bloq(self) -> 'CompositeBloq': + if is_symbolic(self.cvs): + raise DecomposeTypeError(f"Cannot decompose symbolic {self}.") return decompose_from_cirq_style_method(self) def wire_symbol(self, reg: Optional[Register], idx: Tuple[int, ...] = tuple()) -> 'WireSymbol': diff --git a/qualtran/bloqs/mod_arithmetic/mod_addition.py b/qualtran/bloqs/mod_arithmetic/mod_addition.py index 31f66a847..4d4d31638 100644 --- a/qualtran/bloqs/mod_arithmetic/mod_addition.py +++ b/qualtran/bloqs/mod_arithmetic/mod_addition.py @@ -23,6 +23,7 @@ Bloq, bloq_example, BloqDocSpec, + DecomposeTypeError, GateWithRegisters, QBit, QMontgomeryUInt, @@ -89,7 +90,7 @@ def on_classical_vals( def build_composite_bloq(self, bb: 'BloqBuilder', x: Soquet, y: Soquet) -> Dict[str, 'SoquetT']: if is_symbolic(self.bitsize): - raise NotImplementedError(f'symbolic decomposition is not supported for {self}') + raise DecomposeTypeError(f'Symbolic decomposition is not supported for {self}') # Allocate ancilla bits for use in addition. junk_bit = bb.allocate(n=1) sign = bb.allocate(n=1) @@ -390,6 +391,9 @@ def on_classical_vals( def build_composite_bloq( self, bb: 'BloqBuilder', ctrl, x: Soquet, y: Soquet ) -> Dict[str, 'SoquetT']: + if self.dtype.is_symbolic(): + raise DecomposeTypeError(f"Cannot decompose symbolic {self}") + y_arr = bb.split(y) ancilla = bb.allocate(1) x = bb.add(Cast(self.dtype, QUInt(self.dtype.bitsize)), reg=x)