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

Arbitrary diagonal unitary

sami-b95 opened this issue · comments

I would suggest to include the possibility of creating and applying arbitrary diagonal unitary gate in Yao.jl, similar to what other quantum simulation frameworks like Qiskit and Cirq offer.

Currently, the only way to do that seems to be by passing a Diagonal matrix to matblock, but based on the performance of this approach, I conjecture Yao.jl doesn't use the diagonal structure of the matrix to make it efficient.

using Yao, BenchmarkTools, LinearAlgebra

julia> g = matblock(Diagonal([1, 0.0im]))

julia> g2 = matblock(rand_unitary(2))

julia> @benchmark apply!($(rand_state(16)), $(put(2=>g2)))
BenchmarkTools.Trial: 
  memory estimate:  512 bytes
  allocs estimate:  12
  --------------
  minimum time:     138.404 μs (0.00% GC)
  median time:      142.537 μs (0.00% GC)
  mean time:        145.342 μs (0.00% GC)
  maximum time:     278.408 μs (0.00% GC)
  --------------
  samples:          10000
  evals/sample:     1

julia> g = matblock(Diagonal([1, 1.0im]))
matblock(...)

julia> @benchmark apply!($(rand_state(16)), $(put(2=>g)))
BenchmarkTools.Trial: 
  memory estimate:  512 bytes
  allocs estimate:  12
  --------------
  minimum time:     74.102 μs (0.00% GC)
  median time:      78.880 μs (0.00% GC)
  mean time:        81.950 μs (0.00% GC)
  maximum time:     205.396 μs (0.00% GC)
  --------------
  samples:          10000
  evals/sample:     1

It is supported in Yao. Please make sure you are doing the benchmark correctly. In Yao, the method for applying a gate are specialized on

  • Array
  • SparseMatrixCSC
  • PermMatrix (generalized permutation)
  • Diagonal
  • IMatrix (identity, the static version)

FYI Julia has generic types with a JIT but Python doesn't so you don't need to define a primitive to enable specialization here.

I think we do much more specialization than other Python packages due to this so if you suspect any performance issue please always benchmark and report that specific case.