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.
Cause:
decompose_bloqcalls_decompose_from_build_composite_bloq, which first initializes the registers before callingbuild_composite_bloq.This raises a
ValueErrorwhen a symbolic register shape is encounteredCauses most of the "Cannot get real-valued shape." issues in #1367