From f5d764b5c0c2c40d4343bbd072a3f9a5a5864232 Mon Sep 17 00:00:00 2001 From: Matthew Harrigan Date: Thu, 29 Aug 2024 15:51:57 -0700 Subject: [PATCH] debug decomposition errors --- qualtran/_infra/bloq.py | 11 ++++++++--- qualtran/cirq_interop/_cirq_to_bloq.py | 6 +++++- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/qualtran/_infra/bloq.py b/qualtran/_infra/bloq.py index 6b7a296d9e..b3575cadeb 100644 --- a/qualtran/_infra/bloq.py +++ b/qualtran/_infra/bloq.py @@ -46,9 +46,14 @@ def _decompose_from_build_composite_bloq(bloq: 'Bloq') -> 'CompositeBloq': from qualtran import BloqBuilder - bb, initial_soqs = BloqBuilder.from_signature(bloq.signature, add_registers_allowed=False) - out_soqs = bloq.build_composite_bloq(bb=bb, **initial_soqs) - return bb.finalize(**out_soqs) + try: + bb, initial_soqs = BloqBuilder.from_signature(bloq.signature, add_registers_allowed=False) + out_soqs = bloq.build_composite_bloq(bb=bb, **initial_soqs) + return bb.finalize(**out_soqs) + except (DecomposeTypeError, DecomposeNotImplementedError) as ex: + raise ex + except Exception as ex: + raise RuntimeError(f"Unexpected error when decomposing {bloq}: {ex}") from ex class DecomposeNotImplementedError(NotImplementedError): diff --git a/qualtran/cirq_interop/_cirq_to_bloq.py b/qualtran/cirq_interop/_cirq_to_bloq.py index 01192717ee..6968a76253 100644 --- a/qualtran/cirq_interop/_cirq_to_bloq.py +++ b/qualtran/cirq_interop/_cirq_to_bloq.py @@ -594,8 +594,12 @@ def decompose_from_registers( return cirq_optree_to_cbloq( decomposed_optree, signature=bloq.signature, in_quregs=in_quregs, out_quregs=out_quregs ) + except (DecomposeNotImplementedError, DecomposeTypeError) as exc: + raise exc except ValueError as exc: if "Only gate operations are supported" in str(exc): raise DecomposeNotImplementedError(str(exc)) from exc else: - raise exc + raise RuntimeError(f"Unexpected error when decomposing {bloq}: {exc}") from exc + except Exception as exc: + raise RuntimeError(f"Unexpected error when decomposing {bloq}: {exc}") from exc