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

YaoBlocks.cunmat has problems during tracing #174

Open
mofeing opened this issue Oct 7, 2024 · 6 comments
Open

YaoBlocks.cunmat has problems during tracing #174

mofeing opened this issue Oct 7, 2024 · 6 comments
Labels
good first issue Good for newcomers

Comments

@mofeing
Copy link
Collaborator

mofeing commented Oct 7, 2024

YaoBlocks.cunmat generates the matrix representation of a controlled unitary gate. For that, it calls for the Matrix representation of the controlled gate and it embeds it around a... "larger identity matrix". For example, it starts from $4 \times 4$ identity matrix and it sets the matrix representation of the bottom-right $2 \times 2$ submatrix to the matrix representation of the controlled gate.

The problema arises when trying to trace over cunmat, since the matrix representation of the controlled gate, called U0, will be a TracedRArray{ComplexF64,2} which dispatches to this code:

https://github.com/QuantumBFS/Yao.jl/blob/3c6a767623c34b376f131900110e71a8135e2ba3/lib/YaoBlocks/src/routines.jl#L212-L220

... and it will call Matrix(::TracedRArray{ComplexF64,2}) which is not defined. Furthermore, if we defined this method as a no-op and just return the argument, it would just recurse infinitely. We may need a package extension for YaoBlocks to fix this.

CC @jofrevalles

@mofeing mofeing added the good first issue Good for newcomers label Oct 7, 2024
@jofrevalles
Copy link

... and it will call Matrix(::TracedRArray{ComplexF64,2}) which is not defined. Furthermore, if we defined this method as a no-op and just return the argument, it would just recurse infinitely. We may need a package extension for YaoBlocks to fix this.

Why would it recurse infinitely? What would you add as extension for YaoBlocks that you can not code inside Reactant?

@mofeing
Copy link
Collaborator Author

mofeing commented Oct 9, 2024

The problem is that Matrix(::TracedRArray{ComplexF64,2}) is impossible to implement, because TracedRArray is only used during tracing and it has no information about the contents of the array. The only thing you could do is bypass these semantics and return the TracedRArray. Yao will then call cunmat on TracedRArray which will dispatch... on the AbstractMatrix method again. And thus, infinite recursion.

What we need to do is a cunmat method for TracedRArray.

@wsmoses
Copy link
Member

wsmoses commented Oct 9, 2024

I mean honestly an intermediate thing we should do is define a convert(Array, TracedRArray) which throws an error saying that this is an illegal operation.

@mofeing mofeing changed the title YaoBlocks.cunmat has problems during tracings YaoBlocks.cunmat has problems during tracing Oct 9, 2024
@mofeing
Copy link
Collaborator Author

mofeing commented Oct 9, 2024

I mean honestly an intermediate thing we should do is define a convert(Array, TracedRArray) which throws an error saying that this is an illegal operation.

Yeah, but we would like to trace over this function.

@wsmoses
Copy link
Member

wsmoses commented Oct 9, 2024

Yeah but throwing an error will help anyone else with such a conversion in their code (and nicely show where/why/what's up).

In this particular case though, I think the fix is better in YaoBlocks.jl to define their Array specialization to actually take a DenseArray and define TracedRArray as a densearray [meaning everything works properly without much extra handling]

@mofeing
Copy link
Collaborator Author

mofeing commented Oct 9, 2024

Yeah but throwing an error will help anyone else with such a conversion in their code (and nicely show where/why/what's up).

I'm ok with this: throwing an error when you call Array(::TracedRArray).

In this particular case though, I think the fix is better in YaoBlocks.jl to define their Array specialization to actually take a DenseArray and define TracedRArray as a densearray [meaning everything works properly without much extra handling]

Probably won't work either because it then tries to construct a sparse matrix and fill the bottom-right 2x2 sub matrix with the TracedRArray elements. So the

I agree that this should be a fix or a package extension in YaoBlocks.jl, but since Reactant.jl is moving fast and breaking, I guess is better if we do a package extension right now in here and later move it to YaoBlocks.jl when Reactant.jl achieves some stability.

Also, we can ask @Roger-luo @GiggleLiu

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

3 participants