Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unclear how to use symbolics #951

Closed
mstechly opened this issue May 13, 2024 · 6 comments · Fixed by #1084
Closed

Unclear how to use symbolics #951

mstechly opened this issue May 13, 2024 · 6 comments · Fixed by #1084
Labels
docs Improvements or additions to documentation symbolic
Milestone

Comments

@mstechly
Copy link
Contributor

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 mpharrigan added the docs Improvements or additions to documentation label May 13, 2024
@mpharrigan
Copy link
Collaborator

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

@mstechly
Copy link
Contributor Author

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

@tanujkhattar
Copy link
Collaborator

qrom supports it after #945

@mstechly
Copy link
Contributor Author

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
Copy link
Collaborator

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
Copy link
Collaborator

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 mpharrigan added this to the After v1.0 milestone Aug 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Improvements or additions to documentation symbolic
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants