Skip to content
This repository has been archived by the owner on Dec 18, 2021. It is now read-only.

Commit

Permalink
Fix measure bug (#108)
Browse files Browse the repository at this point in the history
* update measure

* update version number
  • Loading branch information
GiggleLiu authored and Roger-luo committed Dec 18, 2019
1 parent 092fa86 commit 0153cb7
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "YaoBlocks"
uuid = "418bc28f-b43b-5e0b-a6e7-61bbc1a2c1df"
version = "0.9.0"
version = "0.9.1"

[deps]
BitBasis = "50ba71b6-fa0f-514d-ae9a-0916efc90dcf"
Expand Down
1 change: 1 addition & 0 deletions src/primitive/identity_gate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ abstract type TrivialGate{N} <: PrimitiveBlock{N} end

mat(::Type{T}, d::TrivialGate{N}) where {T,N} = IMatrix{1 << N,T}()
Base.adjoint(g::TrivialGate) = g
occupied_locs(g::TrivialGate) = ()

"""
IdentityGate{N} <: TrivialGate{N}
Expand Down
20 changes: 12 additions & 8 deletions src/primitive/measure.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ You can create a `Measure` block on given basis (default is the computational ba
```jldoctest; setup=:(using YaoBlocks)
julia> Measure(4)
Measure(4;postprocess=RemoveMeasured())
Measure(4;postprocess=NoPostProcess())
```
Or you could specify which qubits you are going to measure
```jldoctest; setup=:(using YaoBlocks)
julia> Measure(4; locs=1:3)
Measure(4;locs=(1, 2, 3), postprocess=RemoveMeasured())
Measure(4;locs=(1, 2, 3), postprocess=NoPostProcess())
```
by default this will collapse the current register to measure results.
Expand All @@ -106,7 +106,7 @@ julia> state(r)
0.7071067811865475 + 0.0im
julia> r |> Measure(3)
Measure(3;postprocess=RemoveMeasured())
Measure(3;postprocess=NoPostProcess())
julia> state(r)
1×1 Array{Complex{Float64},2}:
Expand All @@ -130,13 +130,17 @@ function Measure(
resetto = nothing,
remove = false,
) where {OT,LT,RNG}
if resetto !== nothing && remove == true
error("invalid keyword combination, expect resetto or remove, got (resetto=$resetto, remove=true)")
else
if resetto !== nothing
postprocess = ResetTo(BitStr64{locs isa AllLocs ? n : length(locs)}(resetto))
if resetto !== nothing
if remove
error("invalid keyword combination, expect resetto or remove, got (resetto=$resetto, remove=true)")
else
postprocess = ResetTo(BitStr64{locs isa AllLocs ? n : length(locs)}(resetto))
end
else
if remove
postprocess = RemoveMeasured()
else
postprocess = NoPostProcess()
end
end
if locs isa AllLocs
Expand Down
1 change: 1 addition & 0 deletions test/primitive/identity_gate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ using Random, Test
@test ishermitian(block)
@test isreflexive(block)
@test isunitary(block)
@test occupied_locs(block) == ()
@test getiparams(block) == ()
end
3 changes: 3 additions & 0 deletions test/primitive/measure.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ using StatsBase: mean
@test g.results[1] == 0 ? st.state[end] == 0 : st.state[1] == 0
g = Measure(4; locs = (1, 2), resetto = 2)
@test g.postprocess isa ResetTo{BitStr64{2}}

m = Measure(4)
@test m.postprocess isa NoPostProcess
end

@testset "resetto" begin
Expand Down

0 comments on commit 0153cb7

Please sign in to comment.