diff --git a/qualtran/_infra/composite_bloq.py b/qualtran/_infra/composite_bloq.py index ee9448600..456be5598 100644 --- a/qualtran/_infra/composite_bloq.py +++ b/qualtran/_infra/composite_bloq.py @@ -846,6 +846,8 @@ def add_register_from_dtype( initial, left-dangling soquets for the register. Otherwise, this is a RIGHT register and will be used for error checking in `finalize()` and nothing is returned. """ + from qualtran.symbolics import is_symbolic + if not self.add_register_allowed: raise ValueError( "This BloqBuilder was constructed from pre-specified registers. " @@ -864,6 +866,11 @@ def add_register_from_dtype( ) reg = Register(name=reg, dtype=dtype) + if is_symbolic(*reg.shape_symbolic): + raise DecomposeTypeError( + f"cannot add register with symbolic shape {reg.shape_symbolic}" + ) + self._regs.append(reg) if reg.side & Side.LEFT: return _reg_to_soq(LeftDangle, reg, available=self._available) diff --git a/qualtran/_infra/composite_bloq_test.py b/qualtran/_infra/composite_bloq_test.py index 5685641ab..34d42e991 100644 --- a/qualtran/_infra/composite_bloq_test.py +++ b/qualtran/_infra/composite_bloq_test.py @@ -20,6 +20,7 @@ import networkx as nx import numpy as np import pytest +import sympy from numpy.typing import NDArray import qualtran.testing as qlt_testing @@ -30,6 +31,7 @@ BloqInstance, CompositeBloq, Connection, + DecomposeTypeError, LeftDangle, Register, RightDangle, @@ -45,6 +47,7 @@ from qualtran.bloqs.for_testing.atom import TestAtom, TestTwoBitOp from qualtran.bloqs.for_testing.many_registers import TestMultiTypedRegister, TestQFxp from qualtran.bloqs.for_testing.with_decomposition import TestParallelCombo, TestSerialCombo +from qualtran.symbolics import SymbolicInt def _manually_make_test_cbloq_cxns(): @@ -597,6 +600,22 @@ def test_add_and_partition(): assert len(cbloq.bloq_instances) == 1 +@attrs.frozen +class TestSymbolicRegisterShape(Bloq): + n: 'SymbolicInt' + + @property + def signature(self) -> 'Signature': + return Signature([Register('q', QBit(), shape=(self.n,))]) + + +def test_decompose_symbolic_register_shape_raises(): + n = sympy.Symbol("n") + bloq = TestSymbolicRegisterShape(n) + with pytest.raises(DecomposeTypeError): + bloq.decompose_bloq() + + @pytest.mark.notebook def test_notebook(): qlt_testing.execute_notebook('composite_bloq') diff --git a/qualtran/cirq_interop/_cirq_to_bloq.py b/qualtran/cirq_interop/_cirq_to_bloq.py index 078051bb1..3911b3e24 100644 --- a/qualtran/cirq_interop/_cirq_to_bloq.py +++ b/qualtran/cirq_interop/_cirq_to_bloq.py @@ -585,7 +585,7 @@ def decompose_from_registers( yields the cirq-style decomposition. """ if any( - cirq.is_parameterized(reg.bitsize) or cirq.is_parameterized(reg.side) + cirq.is_parameterized(reg.bitsize) or cirq.is_parameterized(reg.side) or reg.is_symbolic() for reg in bloq.signature ): # pylint: disable=raise-missing-from