Skip to content

Commit

Permalink
Minor updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ba2tro committed Sep 22, 2024
1 parent 1986f6b commit f99806b
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 41 deletions.
24 changes: 18 additions & 6 deletions src/ProtocolZoo/ProtocolZoo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ using Random

export
# protocols
EntanglerProt, SwapperProt, EntanglementTracker, EntanglementConsumer, CutoffProt, RequestTracker,
EntanglerProt, SwapperProt, EntanglementTracker, EntanglementConsumer, CutoffProt, RequestTracker, RequestGenerator,
# tags
EntanglementCounterpart, EntanglementHistory, EntanglementUpdateX, EntanglementUpdateZ, EntanglementRequest, SwapRequest,
EntanglementCounterpart, EntanglementHistory, EntanglementUpdateX, EntanglementUpdateZ, EntanglementRequest, SwapRequest, DistributionRequest, RequestCompletion,
# from Switches
SimpleSwitchDiscreteProt, SwitchRequest,
# controllers
NetController
NetController, Controller,
# utils
PhysicalGraph, path_selection

abstract type AbstractProtocol end

get_time_tracker(prot::AbstractProtocol) = prot.sim
Expand Down Expand Up @@ -563,6 +566,8 @@ end
end

Check warning on line 566 in src/ProtocolZoo/ProtocolZoo.jl

View check run for this annotation

Codecov / codecov/patch

src/ProtocolZoo/ProtocolZoo.jl#L566

Added line #L566 was not covered by tests
end

include("utils.jl")

"""
$TYPEDEF
Expand All @@ -589,17 +594,25 @@ $TYPEDFIELDS
λ::Int = 4
end

function RequestGenerator(sim, net, src, dst, controller, phys_graph; kwargs...)
return RequestGenerator(;sim, net, src, dst, controller, phys_graph, kwargs...)

Check warning on line 598 in src/ProtocolZoo/ProtocolZoo.jl

View check run for this annotation

Codecov / codecov/patch

src/ProtocolZoo/ProtocolZoo.jl#L597-L598

Added lines #L597 - L598 were not covered by tests
end

@resumable function (prot::RequestGenerator)()
d = Exponential(inv(λ)) # Parametrized with the scale which is inverse of the rate
d = Exponential(inv(prot.λ)) # Parametrized with the scale which is inverse of the rate
mb = messagebuffer(prot.net, prot.src)
while true
path_index = path_selection(prot.phys_graph)
if isnothing(path_index)
prot.phys_graph.failures[] += 1
continue

Check warning on line 608 in src/ProtocolZoo/ProtocolZoo.jl

View check run for this annotation

Codecov / codecov/patch

src/ProtocolZoo/ProtocolZoo.jl#L601-L608

Added lines #L601 - L608 were not covered by tests
end
msg = Tag(DistributionRequest, prot.src, prot.dst, path_index)
put!(channel(prot.net, prot.src=>prot.controller; permit_forward=true), msg)
@yield timeout(prot.sim, rand(d))

Check warning on line 612 in src/ProtocolZoo/ProtocolZoo.jl

View check run for this annotation

Codecov / codecov/patch

src/ProtocolZoo/ProtocolZoo.jl#L610-L612

Added lines #L610 - L612 were not covered by tests

# incoming message from the controller after a request has been served
in_msg = querydelete!(RequestCompletion, ❓)
in_msg = querydelete!(mb, RequestCompletion, ❓)
if !isnothing(in_msg)
(src, (_, path_id)) = in_msg
prot.phys_graph.workloads[path_id] -= 1

Check warning on line 618 in src/ProtocolZoo/ProtocolZoo.jl

View check run for this annotation

Codecov / codecov/patch

src/ProtocolZoo/ProtocolZoo.jl#L615-L618

Added lines #L615 - L618 were not covered by tests
Expand All @@ -611,7 +624,6 @@ end
include("cutoff.jl")
include("swapping.jl")
include("controllers.jl")
include("utils.jl")
include("switches.jl")
using .Switches

Expand Down
4 changes: 2 additions & 2 deletions src/ProtocolZoo/controllers.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""
"""
$TYPEDEF
A network control protocol that is connection oriented, non-distributed and centralized. The generation of
Expand Down Expand Up @@ -86,7 +86,7 @@ end
msg = querydelete!(mb, DistributionRequest, ❓, ❓, ❓)
if !isnothing(msg)
(msg_src, (_, src, dst, path_ind)) = msg
path = phys_graph.paths[path_ind]
path = prot.phys_graph.paths[path_ind]
@debug "Running Entanglement Distribution on path $(path) @ $(now(prot.sim))"
for i in 1:length(path)-1
msg = Tag(EntanglementRequest, path[i], path[i+1], 1)
Expand Down
4 changes: 2 additions & 2 deletions src/ProtocolZoo/swapping.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ end
xmeas, zmeas = swapcircuit(q1, q2)
# send from here to new entanglement counterpart:
# tag with EntanglementUpdateX past_local_node, past_local_slot_idx past_remote_slot_idx new_remote_node, new_remote_slot, correction
msg1 = Tag(EntanglementUpdateX, prot.node, q1.idx, tag1[3], tag2[2], tag2[3], xmeas)
msg1 = Tag(EntanglementUpdateX, prot.node, q1.idx, tag1[3], tag2[2], tag2[3], Int(xmeas))
put!(channel(prot.net, prot.node=>tag1[2]; permit_forward=true), msg1)
@debug "SwapperProt @$(prot.node)|round $(round): Send message to $(tag1[2]) | message=`$msg1` | time = $(now(prot.sim))"
# send from here to new entanglement counterpart:
# tag with EntanglementUpdateZ past_local_node, past_local_slot_idx past_remote_slot_idx new_remote_node, new_remote_slot, correction
msg2 = Tag(EntanglementUpdateZ, prot.node, q2.idx, tag2[3], tag1[2], tag1[3], zmeas)
msg2 = Tag(EntanglementUpdateZ, prot.node, q2.idx, tag2[3], tag1[2], tag1[3], Int(zmeas))
put!(channel(prot.net, prot.node=>tag2[2]; permit_forward=true), msg2)
@debug "SwapperProt @$(prot.node)|round $(round): Send message to $(tag2[2]) | message=`$msg2` | time = $(now(prot.sim))"
@yield timeout(prot.sim, prot.local_busy_time)

Check warning on line 78 in src/ProtocolZoo/swapping.jl

View check run for this annotation

Codecov / codecov/patch

src/ProtocolZoo/swapping.jl#L78

Added line #L78 was not covered by tests
Expand Down
64 changes: 33 additions & 31 deletions src/ProtocolZoo/utils.jl
Original file line number Diff line number Diff line change
@@ -1,28 +1,3 @@
function random_index(arr)
return rand(keys(arr))
end

"""
Find a qubit pair in a register that is suitable for performing a swap by [`SwapperProt`](@ref) according to the given predicate and choosing functions, satisfying the agelimit(if any) of the qubits
"""
function findswapablequbits(net, node, pred_low, pred_high, choose_low, choose_high; agelimit=nothing)
reg = net[node]
low_nodes = [
n for n in queryall(reg, EntanglementCounterpart, pred_low, ❓; locked=false, assigned=true)
if isnothing(agelimit) || !isolderthan(n.slot, agelimit)
]
high_nodes = [
n for n in queryall(reg, EntanglementCounterpart, pred_high, ❓; locked=false, assigned=true)
if isnothing(agelimit) || !isolderthan(n.slot, agelimit)
]

(isempty(low_nodes) || isempty(high_nodes)) && return nothing
il = choose_low((n.tag[2] for n in low_nodes)) # TODO make [2] into a nice named property
ih = choose_high((n.tag[2] for n in high_nodes))
return (low_nodes[il], high_nodes[ih])
end


"""
$TYPEDEF
Expand All @@ -35,27 +10,54 @@ $TYPEDFIELDS
"""The vector of paths between the user pair"""
paths::Vector{Vector{Int}}
"""The vector containing the workload information of a path"""
workloads::Vec{Float64}
workloads::Vector{Int}
"""The number of slots available at each node. Scalar if all are same, vector otherwise."""
capacity::Union{Vec{Int}, Int}
capacity::Union{Vector{Int}, Int}
"""Number of failed requests due to high request traffic"""
failures::Ref{Int}
end

function PhysicalGraph(graph::SimpleGraph{Int64}, src::Int, dst::Int, caps::Union{Vector{Int}, Int})
function PhysicalGraph(graph::SimpleGraph{Int64}, src::Int, dst::Int, caps::Union{Vector{Int}, Int}; failures=Ref{Int}(0))
paths = sort(collect(all_simple_paths(graph, src, dst)); by = x->length(x))
workloads = zeros(length(paths))
PhysicalGraph(paths, workloads, caps)
PhysicalGraph(paths, workloads, caps, failures)

Check warning on line 23 in src/ProtocolZoo/utils.jl

View check run for this annotation

Codecov / codecov/patch

src/ProtocolZoo/utils.jl#L20-L23

Added lines #L20 - L23 were not covered by tests
end


"""
A simple path selection algorithm for connection oriented networks.
"""
function path_selection(phys_graph::PhysicalGraph)
function path_selection(phys_graph::PhysicalGraph)
for i in 1:length(phys_graph.paths)
capacity = isa(phys_graph.capacity, Number) ? phys_graph.capacity : phys_graph.capacity[i]
if phys_graph.workloads[i]<capacity
phys_graph.workloads[i] += 1 # This is workload for the whole path not considering the individual nodes yet
phys_graph.workloads[i] += 1
return i

Check warning on line 35 in src/ProtocolZoo/utils.jl

View check run for this annotation

Codecov / codecov/patch

src/ProtocolZoo/utils.jl#L30-L35

Added lines #L30 - L35 were not covered by tests
end
end

Check warning on line 37 in src/ProtocolZoo/utils.jl

View check run for this annotation

Codecov / codecov/patch

src/ProtocolZoo/utils.jl#L37

Added line #L37 was not covered by tests
end


function random_index(arr)
return rand(keys(arr))
end

"""
Find a qubit pair in a register that is suitable for performing a swap by [`SwapperProt`](@ref) according to the given predicate and choosing functions, satisfying the agelimit(if any) of the qubits
"""
function findswapablequbits(net, node, pred_low, pred_high, choose_low, choose_high; agelimit=nothing)
reg = net[node]
low_nodes = [
n for n in queryall(reg, EntanglementCounterpart, pred_low, ❓; locked=false, assigned=true)
if isnothing(agelimit) || !isolderthan(n.slot, agelimit)
]
high_nodes = [
n for n in queryall(reg, EntanglementCounterpart, pred_high, ❓; locked=false, assigned=true)
if isnothing(agelimit) || !isolderthan(n.slot, agelimit)
]

(isempty(low_nodes) || isempty(high_nodes)) && return nothing
il = choose_low((n.tag[2] for n in low_nodes)) # TODO make [2] into a nice named property
ih = choose_high((n.tag[2] for n in high_nodes))
return (low_nodes[il], high_nodes[ih])
end
1 change: 1 addition & 0 deletions src/states_registers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Register(traits) = Register(traits,default_repr.(traits))
Register(nqubits::Int) = Register([Qubit() for _ in 1:nqubits])
Register(nqubits::Int,repr::AbstractRepresentation) = Register(fill(Qubit(),nqubits),fill(repr,nqubits))
Register(nqubits::Int,bg::AbstractBackground) = Register(fill(Qubit(),nqubits),fill(bg,nqubits))
Register(nqubits::Int,repr::AbstractRepresentation,bg::AbstractBackground) = Register(fill(Qubit(),nqubits),fill(repr,nqubits),fill(bg, nqubits))

Check warning on line 41 in src/states_registers.jl

View check run for this annotation

Codecov / codecov/patch

src/states_registers.jl#L41

Added line #L41 was not covered by tests

"""
A reference to a [`Register`](@ref) slot, convenient for use with functions like [`apply!`](@ref), etc.
Expand Down

0 comments on commit f99806b

Please sign in to comment.