QuantumBFS / Yao.jl

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

fix issue 432 #435

Closed GiggleLiu closed 4 months ago

GiggleLiu commented 1 year ago

fix #432

Change List

Fixes the type stability issue

julia> using Yao
[ Info: Precompiling Yao [5872b779-8223-5990-8dd0-5abbb0748c8c]

julia> nq = 2
2

julia> O1 = put(nq, 1 => X)
nqubits: 2
put on (1)
└─ X

julia> reg = rand_state(nq)
ArrayReg{2, ComplexF64, Array...}
    active qubits: 2/2
    nlevel: 2

julia> @code_warntype expect(O1, reg)
MethodInstance for YaoAPI.expect(::PutBlock{2, 1, XGate}, ::ArrayReg{2, ComplexF64, Matrix{ComplexF64}})
  from expect(op::AbstractBlock, reg::AbstractRegister) in YaoBlocks at /home/leo/.julia/dev/Yao/lib/YaoBlocks/src/blocktools.jl:95
Arguments
  #self#::Core.Const(YaoAPI.expect)
  op::PutBlock{2, 1, XGate}
  reg::ArrayReg{2, ComplexF64, Matrix{ComplexF64}}
Body::Float64
1 ─ %1 = YaoBlocks.sandwich(reg, op, reg)::ComplexF64
│   %2 = Base.broadcasted(YaoBlocks.safe_real, %1)::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{0}, Nothing, typeof(YaoBlocks.safe_real), Tuple{ComplexF64}}
│   %3 = Base.materialize(%2)::Float64
└──      return %3

Examples

julia> expect(O1, reg)
0.05754647503710675

julia> sandwich(reg, O1, reg)
0.05754647503710675 + 0.0im

Note

For expect(O1 + O2, reg), the code is still type unstable, this is due to the Add block contains a vector of abstract type.

codecov[bot] commented 1 year ago

Codecov Report

Attention: Patch coverage is 96.87500% with 1 lines in your changes are missing coverage. Please review.

Project coverage is 88.31%. Comparing base (e17a32b) to head (430123e). Report is 6 commits behind head on master.

:exclamation: Current head 430123e differs from pull request most recent head f70f095. Consider uploading reports for the commit f70f095 to get more accurate results

Files Patch % Lines
lib/YaoBlocks/src/utils.jl 80.00% 1 Missing :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #435 +/- ## ========================================== - Coverage 89.03% 88.31% -0.73% ========================================== Files 83 76 -7 Lines 4835 4765 -70 ========================================== - Hits 4305 4208 -97 - Misses 530 557 +27 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

GiggleLiu commented 4 months ago

I think this PR should be merged. Automatic differentiating complex numbers does not make sense.