QuantumBFS / Yao.jl

Extensible, Efficient Quantum Algorithm Design for Humans.
https://yaoquantum.org
Other
910 stars 118 forks source link

`dispatch!(circuit, angles)` fails when gates are created with `Irrational` angles #474

Closed jlbosse closed 1 year ago

jlbosse commented 1 year ago

Here is an MWE illustrating the problem:

using Yao

circuit = chain(1)
push!(circuit, put(1, 1=>shift(π)))
push!(circuit, put(1, 1=>Rx(1))

parameters(circuit)

# out
2-element Vector{Float64}:
 3.141592653589793
 1.0

dispatch!(circuit, zeros(2))

# out
ERROR: MethodError: no method matching Irrational{:π}(::Float64)

Closest candidates are:
  (::Type{T})(::T) where T<:Number
   @ Core boot.jl:792
  (::Type{Irrational{sym}} where sym)()
   @ Base irrationals.jl:29
  (::Type{T})(::AbstractChar) where T<:Union{AbstractChar, Number}
   @ Base char.jl:50
  ...

Clearly, this can be fixed by wrapping the π in a Float64, but feels a bit cumbersome. But I am also not sure what a good fix is, since having generic types is quite nice.

Roger-luo commented 1 year ago

I understand the use case, but I don't see a solution to this because Yao doesn't really handle symbolic scalar expressions...this should be the right behavior of YaoBlocks, and the error message is also quite clear?

jlbosse commented 1 year ago

Fair enough, I guess it is more down to the fact that π behaves funny in some contexts and julia users are (hopefully) used to that. So maybe give this issue a tag "wont-fix" and then close it for future reference?

Roger-luo commented 1 year ago

so the real issue is still a mixture of value and symbolic expression (aka the program), pi is technically a symbol but unfortunately Yao doesn't understand it. This problem will automatically disappear if there is an explicit symbolic expression containing scalar symbols then use an explicit compile stage to get the value. So it automatically gets fixed with YaoExpr (the Yao 2.0), but I currently don't have time to work on the new engine, might need to wait until I have more spare time to sketch that out.