quantumlib / Qualtran

Qᴜᴀʟᴛʀᴀɴ is a Python library for expressing and analyzing Fault Tolerant Quantum algorithms.
https://qualtran.readthedocs.io/en/latest/
Apache License 2.0
135 stars 35 forks source link

Unclear how to use symbolics #951

Open mstechly opened 2 months ago

mstechly commented 2 months ago

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.

mpharrigan commented 2 months ago

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

mstechly commented 2 months ago

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

tanujkhattar commented 2 months ago

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

mstechly commented 2 months ago

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).
tanujkhattar commented 1 month ago

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.

mpharrigan commented 1 month ago

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