Skip to content

Commit

Permalink
Raise DecomposeTypeError for symbolic register shapes (#1375)
Browse files Browse the repository at this point in the history
* raise `DecomposeTypeError` for symbolic register shapes

* add test

---------

Co-authored-by: Matthew Harrigan <[email protected]>
  • Loading branch information
anurudhp and mpharrigan authored Sep 3, 2024
1 parent abd7ffb commit 8be25c6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
7 changes: 7 additions & 0 deletions qualtran/_infra/composite_bloq.py
Original file line number Diff line number Diff line change
Expand Up @@ -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. "
Expand All @@ -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)
Expand Down
19 changes: 19 additions & 0 deletions qualtran/_infra/composite_bloq_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -30,6 +31,7 @@
BloqInstance,
CompositeBloq,
Connection,
DecomposeTypeError,
LeftDangle,
Register,
RightDangle,
Expand All @@ -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():
Expand Down Expand Up @@ -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')
2 changes: 1 addition & 1 deletion qualtran/cirq_interop/_cirq_to_bloq.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8be25c6

Please sign in to comment.