Qualtran icon indicating copy to clipboard operation
Qualtran copied to clipboard

Unclear how to use symbolics

Open mstechly opened this issue 1 year ago • 4 comments

I tried to create some bloqs with symbolic costs and it's unclear how exactly to do that.

Here's the numeric version:

from qualtran.bloqs.arithmetic.comparison import LessThanEqual
bloq = LessThanEqual(10, 15)
bloq.t_complexity()

This works well and returns: TComplexity(t=96, clifford=533, rotations=0)

However if I try the following:

from qualtran.bloqs.arithmetic.comparison import LessThanEqual
import sympy
a, b = sympy.symbols("a b")
bloq = LessThanEqual(a, b)
bloq.t_complexity()

I'm getting: TypeError: cannot determine truth value of Relational

Perhaps things are working properly and I'm just using them in a wrong way, but I also wanted to give you some feedback on how it looks like from a user's perspective :) From reading the docs and tests I wasn't able to figure out how to make it work – I didn't see tests for symbolics for this bloq.

mstechly avatar May 13 '24 22:05 mstechly

Yes, only some bloqs support symbolic parameters but there's no way of (reliably) knowing which. Some fixes that we need

  • [ ] write a general docs page about symbolics support and how it's up to the bloq to make it work or not
  • [ ] update type annotations and arg docstrings to make it clear which parameters can be symbolic
  • [ ] make more things support symbolic parameters

mpharrigan avatar May 13 '24 23:05 mpharrigan

Can you tell me which bloqs do support it, just so I have some examples to work with for now :) ?

mstechly avatar May 14 '24 01:05 mstechly

qrom supports it after https://github.com/quantumlib/Qualtran/pull/945

tanujkhattar avatar May 14 '24 01:05 tanujkhattar

Thanks @tanujkhattar, now this works properly!

import sympy
N, M, b1, b2, c = sympy.symbols('N M b1 b2 c')
qrom_symb = QROM.build_from_bitsize((N, M), (b1, b2), num_controls=c)
qrom_symb.t_complexity()

TComplexity(t=4*M*N + 4*c - 8, clifford=M*N*b1*b2 + 13*M*N + 13*c - 26, rotations=0) :)

However, while I can decompose a numeric bloq, I can't decompose a symbolic one:

import sympy
N, M, b1, b2, c = sympy.symbols('N M b1 b2 c')
qrom_symb = QROM.build_from_bitsize((N, M), (b1, b2), num_controls=c)
qrom_symb.decompose_bloq()
*** qualtran._infra.bloq.DecomposeTypeError: Cannot decompose parameterized QROM(data_or_shape=Shaped(shape=(2, N, M)), selection_bitsizes=(ceiling(log2(N - 1)), ceiling(log2(M - 1))), target_bitsizes=(b1, b2), num_controls=c).

mstechly avatar May 16 '24 04:05 mstechly

This is because a QROM decomposes into a unary iteration circuit; if the number of selection registers is symbolic (depends upon (N, M)) or the size of the target registers is symbolic (depends upon (b1, b2)) then the number of ports would be symbolic -- which is not supported.

Similar to qref, the size of each port can be symbolic but the number of ports in the decomposition should not be. Whenever a symbolic bloq would have variable number of ports in its decomposition, you would get an error.

tanujkhattar avatar May 28 '24 12:05 tanujkhattar

Discussed offline: but state prep alias sampling should be able to do one level of decomposition since the symbolic bitsizes of all the wires are preserved.

Decomposing a qrom circuit with symbolic size is not possible since we need to split symbolically-sized registers into a concrete number of qubit wires

One can use the call graph protocol to get callees without knowing exactly how they're wired up

mpharrigan avatar Jun 18 '24 20:06 mpharrigan