QuantumBFS / YaoBlocks.jl

Standard basic quantum circuit simulator building blocks. (archived, for it is moved to Yao.jl)
Apache License 2.0
26 stars 11 forks source link

fix size check #156

Closed GiggleLiu closed 3 years ago

GiggleLiu commented 3 years ago

fix https://github.com/QuantumBFS/Yao.jl/issues/276

codecov[bot] commented 3 years ago

Codecov Report

Merging #156 (4f57057) into master (7a07598) will increase coverage by 3.43%. The diff coverage is 94.44%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #156      +/-   ##
==========================================
+ Coverage   75.96%   79.39%   +3.43%     
==========================================
  Files          46       46              
  Lines        1822     2038     +216     
==========================================
+ Hits         1384     1618     +234     
+ Misses        438      420      -18     
Impacted Files Coverage Δ
src/composite/repeated.jl 75.00% <66.66%> (+5.55%) :arrow_up:
src/composite/tag/cache.jl 60.52% <75.00%> (+5.14%) :arrow_up:
src/abstract_block.jl 74.10% <100.00%> (+2.38%) :arrow_up:
src/composite/chain.jl 97.05% <100.00%> (+0.23%) :arrow_up:
src/composite/control.jl 75.00% <100.00%> (+1.66%) :arrow_up:
src/composite/kron.jl 93.24% <100.00%> (+7.30%) :arrow_up:
src/composite/put_block.jl 97.67% <100.00%> (+0.30%) :arrow_up:
src/composite/reduce.jl 95.12% <100.00%> (+0.67%) :arrow_up:
src/composite/subroutine.jl 63.33% <100.00%> (+5.64%) :arrow_up:
src/composite/tag/scale.jl 90.90% <100.00%> (+0.43%) :arrow_up:
... and 39 more

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 7a07598...4f57057. Read the comment docs.

Roger-luo commented 3 years ago

the doc tests fail because you changed the printings to 1.6, should change the that back. the doc test only runs on 1.5 at the moment.

Also I don't quite understand why this is causing that issue, I also can't reproduce the segment fault

GiggleLiu commented 3 years ago

the doc tests fail because you changed the printings to 1.6, should change the that back. the doc test only runs on 1.5 at the moment.

Yeah, but do we need to fix them for 1.5? We need to change it sooner or later. We can still tag a version without doctests pass.

Also I don't quite understand why this is causing that issue, I also can't reproduce the segment fault

It does not produce error, this is an issue too.

Roger-luo commented 3 years ago

I don't understand what this fixes, it seems you are just forwarding _apply to _apply_fallback, this doesn't look like fixing anything to me. can you reproduce QuantumBFS/Yao.jl#276 ? and how to reproduce that?

GiggleLiu commented 3 years ago

I don't understand what this fixes, it seems you are just forwarding _apply to _apply_fallback, this doesn't look like fixing anything to me. can you reproduce QuantumBFS/Yao.jl#276 ? and how to reproduce that?

There are tests added for that. Before,

julia> using Yao

julia> apply!(rand_state(3), repeat(5,X, (2,3)))
ArrayReg{1, ComplexF64, Array...}
    active qubits: 3/3

After

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

julia> apply!(rand_state(3), repeat(5,X, (2,3)))
ERROR: QubitMismatchError("register size 3 mismatch with block size 5")
Stacktrace:
 [1] _check_size(r::ArrayReg{1, ComplexF64, Matrix{ComplexF64}}, pb::RepeatedBlock{5, 2, XGate})
   @ YaoBlocks ~/.julia/dev/YaoBlocks/src/abstract_block.jl:332
 [2] apply!(r::ArrayReg{1, ComplexF64, Matrix{ComplexF64}}, b::AbstractBlock)
   @ YaoBlocks ~/.julia/dev/YaoBlocks/src/abstract_block.jl:9
 [3] top-level scope
   @ REPL[2]:1
Roger-luo commented 3 years ago

OK, why this fixes QuantumBFS/Yao.jl#276?

GiggleLiu commented 3 years ago

Now Yao will error you correctly

julia> using Yao, YaoExtensions
[ Info: Precompiling YaoExtensions [7a06699c-c960-11e9-3c98-9f78548b5f0f]

julia> circ = dispatch!(variational_circuit(4, 2), :random);

julia> h = heisenberg(2);

julia> expect(h, zero_state(4)=>circ)
ERROR: QubitMismatchError("register size 4 mismatch with block size 2")
Stacktrace:
  [1] _check_size(r::ArrayReg{1, ComplexF64, Matrix{ComplexF64}}, pb::RepeatedBlock{2, 2, XGate})
    @ YaoBlocks ~/.julia/dev/YaoBlocks/src/abstract_block.jl:332
  [2] apply!
    @ ~/.julia/dev/YaoBlocks/src/abstract_block.jl:9 [inlined]
  [3] expect(op::RepeatedBlock{2, 2, XGate}, reg::ArrayReg{1, ComplexF64, Matrix{ComplexF64}})
    @ YaoBlocks ~/.julia/dev/YaoBlocks/src/blocktools.jl:101
  [4] #126
    @ ~/.julia/dev/YaoBlocks/src/blocktools.jl:128 [inlined]
  [5] (::Base.MappingRF{YaoBlocks.var"#126#127"{ArrayReg{1, ComplexF64, Matrix{ComplexF64}}}, Base.BottomRF{typeof(Base.add_sum)}})(acc::Base._InitialValue, x::RepeatedBlock{2, 2, XGate})
    @ Base ./reduce.jl:93
  [6] _foldl_impl(op::Base.MappingRF{YaoBlocks.var"#126#127"{ArrayReg{1, ComplexF64, Matrix{ComplexF64}}}, Base.BottomRF{typeof(Base.add_sum)}}, init::Base._InitialValue, itr::Add{2})
    @ Base ./reduce.jl:58
  [7] foldl_impl(op::Base.MappingRF{YaoBlocks.var"#126#127"{ArrayReg{1, ComplexF64, Matrix{ComplexF64}}}, Base.BottomRF{typeof(Base.add_sum)}}, nt::Base._InitialValue, itr::Add{2})
    @ Base ./reduce.jl:48
  [8] mapfoldl_impl(f::YaoBlocks.var"#126#127"{ArrayReg{1, ComplexF64, Matrix{ComplexF64}}}, op::typeof(Base.add_sum), nt::Base._InitialValue, itr::Add{2})
    @ Base ./reduce.jl:44
  [9] mapfoldl(f::Function, op::Function, itr::Add{2}; init::Base._InitialValue)
    @ Base ./reduce.jl:160
 [10] mapfoldl
    @ ./reduce.jl:160 [inlined]
 [11] #mapreduce#218
    @ ./reduce.jl:287 [inlined]
 [12] mapreduce
    @ ./reduce.jl:287 [inlined]
 [13] #sum#221
    @ ./reduce.jl:501 [inlined]
 [14] sum(f::Function, a::Add{2})
    @ Base ./reduce.jl:501
 [15] expect
    @ ~/.julia/dev/YaoBlocks/src/blocktools.jl:128 [inlined]
 [16] #126
    @ ~/.julia/dev/YaoBlocks/src/blocktools.jl:128 [inlined]
 [17] (::Base.MappingRF{YaoBlocks.var"#126#127"{ArrayReg{1, ComplexF64, Matrix{ComplexF64}}}, Base.BottomRF{typeof(Base.add_sum)}})(acc::Base._InitialValue, x::Add{2})
    @ Base ./reduce.jl:93
 [18] _foldl_impl(op::Base.MappingRF{YaoBlocks.var"#126#127"{ArrayReg{1, ComplexF64, Matrix{ComplexF64}}}, Base.BottomRF{typeof(Base.add_sum)}}, init::Base._InitialValue, itr::Add{2})
    @ Base ./reduce.jl:58
 [19] foldl_impl(op::Base.MappingRF{YaoBlocks.var"#126#127"{ArrayReg{1, ComplexF64, Matrix{ComplexF64}}}, Base.BottomRF{typeof(Base.add_sum)}}, nt::Base._InitialValue, itr::Add{2})
    @ Base ./reduce.jl:48
 [20] mapfoldl_impl(f::YaoBlocks.var"#126#127"{ArrayReg{1, ComplexF64, Matrix{ComplexF64}}}, op::typeof(Base.add_sum), nt::Base._InitialValue, itr::Add{2})
    @ Base ./reduce.jl:44
 [21] mapfoldl(f::Function, op::Function, itr::Add{2}; init::Base._InitialValue)
    @ Base ./reduce.jl:160
 [22] mapfoldl
    @ ./reduce.jl:160 [inlined]
 [23] #mapreduce#218
    @ ./reduce.jl:287 [inlined]
 [24] mapreduce
    @ ./reduce.jl:287 [inlined]
 [25] #sum#221
    @ ./reduce.jl:501 [inlined]
 [26] sum(f::Function, a::Add{2})
    @ Base ./reduce.jl:501
 [27] expect
    @ ~/.julia/dev/YaoBlocks/src/blocktools.jl:128 [inlined]
 [28] expect(op::Add{2}, plan::Pair{ArrayReg{1, ComplexF64, Matrix{ComplexF64}}, ChainBlock{4}})
    @ YaoBlocks ~/.julia/dev/YaoBlocks/src/blocktools.jl:136
 [29] top-level scope
    @ REPL[4]:1

That issue is caused by missing _check_size in both Add and Repeat. This is why I feel it is not guaranteed to check size correctly with the previous implementation, even though we have been careful to add size check when implementing a block.

Roger-luo commented 3 years ago

I see, it gives me a result somehow. LGTM

GiggleLiu commented 3 years ago

I see, it gives me a result somehow. LGTM

I think the problem is, it shouldn't give you a result...