Skip to content

Commit

Permalink
move 'isolderthan' and 'findfreeslot' back to queries.jl'
Browse files Browse the repository at this point in the history
  • Loading branch information
ba2tripleO committed Aug 31, 2024
1 parent e7274c6 commit 1986f6b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 45 deletions.
45 changes: 0 additions & 45 deletions src/ProtocolZoo/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,51 +22,6 @@ function findswapablequbits(net, node, pred_low, pred_high, choose_low, choose_h
return (low_nodes[il], high_nodes[ih])
end

"""Find an empty unlocked slot in a given [`Register`](@ref).
```jldoctest
julia> reg = Register(3); initialize!(reg[1], X); lock(reg[2]);
julia> findfreeslot(reg) == reg[3]
true
julia> lock(findfreeslot(reg));
julia> findfreeslot(reg) |> isnothing
true
```
"""
function findfreeslot(reg::Register; randomize=false, margin=0)
n_slots = length(reg.staterefs)
freeslots = sum((!isassigned(reg[i]) for i in 1:n_slots))
if freeslots >= margin
perm = randomize ? randperm : (x->1:x)
for i in perm(n_slots)
slot = reg[i]
islocked(slot) || isassigned(slot) || return slot
end
end
end


struct NotAssignedError <: Exception # TODO use this in all places where we are throwing something on isassigned (maybe rename to IsAssignedError and check whether we need to keep `f` as part of it (might already be provided by the stacktrace) and check it does not allocate even when the error is not triggered)
msg
f
end

function Base.showerror(io::IO, err::NotAssignedError)
print(io, "NotAssignedError: ")
println(io, err.msg)
println("In function: $(err.f)")
end

"""Check whether a qubit has existed for a time more than its cutoff/coherence time"""
function isolderthan(slot::RegRef, time_left::Float64)
if !isassigned(slot) throw(NotAssignedError("Slot must be assigned with a quantum state before checking coherence.", isolderthan)) end
id = query(slot, QuantumSavory.ProtocolZoo.EntanglementCounterpart, ❓, ❓).id
slot_time = slot.reg.tag_info[id][3]
return (now(get_time_tracker(slot))) - slot_time > time_left
end

"""
$TYPEDEF
Expand Down
45 changes: 45 additions & 0 deletions src/queries.jl
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,51 @@ function query(mb::MessageBuffer, query::Tag)
return nothing
end

"""Find an empty unlocked slot in a given [`Register`](@ref).
```jldoctest
julia> reg = Register(3); initialize!(reg[1], X); lock(reg[2]);
julia> findfreeslot(reg) == reg[3]
true
julia> lock(findfreeslot(reg));
julia> findfreeslot(reg) |> isnothing
true
```
"""
function findfreeslot(reg::Register; randomize=false, margin=0)
n_slots = length(reg.staterefs)
freeslots = sum((!isassigned(reg[i]) for i in 1:n_slots))
if freeslots >= margin
perm = randomize ? randperm : (x->1:x)
for i in perm(n_slots)
slot = reg[i]
islocked(slot) || isassigned(slot) || return slot
end
end
end


struct NotAssignedError <: Exception # TODO use this in all places where we are throwing something on isassigned (maybe rename to IsAssignedError and check whether we need to keep `f` as part of it (might already be provided by the stacktrace) and check it does not allocate even when the error is not triggered)
msg
f
end

function Base.showerror(io::IO, err::NotAssignedError)
print(io, "NotAssignedError: ")
println(io, err.msg)
println("In function: $(err.f)")
end

"""Check whether a qubit has existed for a time more than its cutoff/coherence time"""
function isolderthan(slot::RegRef, time_left::Float64)
if !isassigned(slot) throw(NotAssignedError("Slot must be assigned with a quantum state before checking coherence.", isolderthan)) end
id = query(slot, QuantumSavory.ProtocolZoo.EntanglementCounterpart, ❓, ❓).id
slot_time = slot.reg.tag_info[id][3]
return (now(get_time_tracker(slot))) - slot_time > time_left
end

function Base.isassigned(r::Register,i::Int) # TODO erase
r.stateindices[i] != 0 # TODO this also usually means r.staterefs[i] !== nothing - choose one and make things consistent
Expand Down

0 comments on commit 1986f6b

Please sign in to comment.