Skip to content

Attempting to decompose bloq with symbolic shaped registers raises an incorrect ValueError #1374

@anurudhp

Description

@anurudhp

Cause: decompose_bloq calls _decompose_from_build_composite_bloq, which first initializes the registers before calling build_composite_bloq.
This raises a ValueError when a symbolic register shape is encountered

Causes most of the "Cannot get real-valued shape." issues in #1367

from attrs import frozen
import sympy
from qualtran import Bloq, Signature, Register, QBit, DecomposeTypeError
from qualtran.bloqs.basic_gates import XGate
from qualtran.symbolics import SymbolicInt, is_symbolic


@frozen
class XGateOnEach(Bloq):
    n: SymbolicInt

    @property
    def signature(self):
        return Signature([Register('q', QBit(), shape=(self.n,))])

    def build_composite_bloq(self, bb, q):
        if is_symbolic(self.n):
            raise DecomposeTypeError(f"cannot decompose symbolic {self}")

        for i in range(self.n):
            q[i] = bb.add(XGate(), q=q[i])

        return {'q': q}


def test_decomposition():
    n = sympy.Symbol("n")
    _ = XGateOnEach(n).decompose_bloq()


test_decomposition()
  File "qualtran/_infra/registers.py", line 85, in shape
    raise ValueError(f"{self} is symbolic. Cannot get real-valued shape.")
ValueError: Register(name='q', dtype=QBit(), _shape=(n,), side=<Side.THRU: 3>) is symbolic. Cannot get real-valued shape.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions