QuantumBFS / Yao.jl

Extensible, Efficient Quantum Algorithm Design for Humans.

Home Page:https://yaoquantum.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

customed (random) unitary operation too hard to define

jzzhuang opened this issue · comments

I want to sample a random unitary circuit. The desired code is like

for _ in 1:10
   @const_gate U = rand(4, 4)
   circuit = chain(2, put((1, 2)=>U)
   psi = zero_state(2)
   apply!(psi, circuit)
end

The problems of redefining the gate U are summarized here:

  1. When I repeat this for multiple times, gives too many warnings when redefining U.
  2. Such code cannot be put into a function: "World Too New Error"
  3. Redefining costs too much time on compilation

There is a better way to define a generic block from a matrix. @const is just not designed for it because it creates a new type every time you call it. Please check matblock for generic gates.

julia> for _ in 1:10
          U = matblock(rand(ComplexF64, 4, 4); tag="U")
          circuit = chain(2, put((1, 2)=>U))
          psi = zero_state(2)
          apply!(psi, circuit)
       end

It works well! Thank you so much!