Multi-level Bloq decomposition for symbolic Bloqs

Open
#1,644 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
25/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
python

Research direction

Start with the symbolic-width guards in Bloqs such as LessThanEqual, QROM, and CSwap, and trace how decompose_bloq() and _decompose_from_build_composite_bloq() handle decomposition. The issue gives a minimal reproduction using StatePreparationAliasSampling.from_n_coeff; run it to confirm the failure and inspect the resulting call graph. Done means symbolic subtrees expand into QREF routines with port definitions and connection wiring so GraphViz preserves their topology.

Written by the indexing model from the issue text.

Description

qref_interop

Description:

When you instantiate certain Bloq subclasses with Sympy symbols for register widths (e.g.)

StatePreparationAliasSampling.from_n_coeff(L, lam, precision=eps)

the call to decompose_bloq() immediately raises

DecomposeTypeError("bitsize must be a concrete value")

because each of these Bloq classes contains a guard like:

if not isinstance(self.bitsize, int):
    raise DecomposeTypeError("bitsize must be a concrete value")

This affects many Bloqs (e.g. LessThanEqual, QROM, CSwap, etc.) and prevents any symbolic-parameter subtree from ever being expanded when you convert to QREF + GraphViz. All you ever see is a monolithic leaf.

There is a private _decompose_from_build_composite_bloq() that builds a CompositeBloq via the call-graph, but it still runs the same per-class guards, so it also refuses on symbolic bitsizes.

Why the “obvious” fixes don’t work
  • Centralizing dual-fallback in the QREF importer
    We could catch DecomposeTypeError and call _decompose_from_build_composite_bloq() instead—but that builder itself enforces the same isinstance(bitsize, int) checks, so symbolic widths are still rejected.

  • Using from_callgraph=True
    There is already a bloq_to_qref(..., from_callgraph=True) path, but it only reconstructs a flat call-graph of Bloqs. It does not produce real port definitions or connection wiring in the resulting RoutineV1, so to_graphviz on that schema loses all topology.

Minimal repro

import sympy
from qualtran.bloqs.state_preparation import StatePreparationAliasSampling

L, lam, eps = sympy.symbols("L lam eps")
alias = StatePreparationAliasSampling.from_n_coeff(L, lam, precision=eps)

cb = alias.decompose_bloq()       # CompositeBloq with 5 leaves
for inst in cb.bloq_instances:
    inst.bloq.decompose_bloq()    # always raises DecomposeTypeError

Related

MR #1643 adds a dual-fallback for numeric Bloqs, but it still fails for symbolic cases because of the per-class guards.

Tagging @mstechly

Dominant language
Python
Stars
397
Forks
127
Avg merge
2d 22h
Merged PRs (30d)
1

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from quantumlib/Qualtran

All issues in quantumlib/Qualtran

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.