Skip to content

Commit

Permalink
examples update
Browse files Browse the repository at this point in the history
  • Loading branch information
adamglos92 committed Jul 10, 2019
1 parent 3e7a695 commit 1aae7aa
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 63 deletions.
25 changes: 15 additions & 10 deletions examples/ex01_path_propagation.ipynb
Expand Up @@ -22,7 +22,8 @@
"source": [
"using QSWalk\n",
"using PyPlot # for plot\n",
"using LightGraphs # for PathGraph"
"using LightGraphs # for PathGraph\n",
"using LinearAlgebra # for diag method"
]
},
{
Expand All @@ -48,10 +49,10 @@
"metadata": {},
"outputs": [],
"source": [
"dim = 251 # odd for unique middle point\n",
"dim = 151 # odd for unique middle point\n",
"midpoint = ceil(Int, dim/2)\n",
"w = 0.5\n",
"timepoint = 100.\n",
"timepoint = 50.\n",
"adjacency = adjacency_matrix(PathGraph(dim));"
]
},
Expand Down Expand Up @@ -109,7 +110,7 @@
"metadata": {},
"outputs": [],
"source": [
"positions = (collect(1:dim) - midpoint)\n",
"positions = (collect(1:dim) .- midpoint)\n",
"plot(positions, real.(diag(rho_local)), \"k\")\n",
"plot(positions, real.(diag(rho_global)), \"b\")\n",
"xlabel(\"position\")\n",
Expand Down Expand Up @@ -140,10 +141,10 @@
"metadata": {},
"outputs": [],
"source": [
"dim = 251 #odd for unique middle point\n",
"dim = 151 #odd for unique middle point\n",
"midpoint = ceil(Int, dim/2)\n",
"w = 0.5\n",
"timepoints = collect(0:5:100)\n",
"timepoints = collect(0:2.5:50)\n",
"adjacency = adjacency_matrix(PathGraph(dim));"
]
},
Expand Down Expand Up @@ -199,7 +200,7 @@
"source": [
"secmoment_global = Float64[]\n",
"secmoment_local = Float64[]\n",
"positions = (collect(1:dim) - midpoint)\n",
"positions = (collect(1:dim) .- midpoint)\n",
"\n",
"for (rho_global, rho_local) = zip(global_states, local_states)\n",
" push!(secmoment_global, sum(positions.^2 .* diag(rho_global)))\n",
Expand Down Expand Up @@ -231,16 +232,20 @@
}
],
"metadata": {
"@webio": {
"lastCommId": null,
"lastKernelId": null
},
"kernelspec": {
"display_name": "Julia 0.6.0",
"display_name": "Julia 1.0.1",
"language": "julia",
"name": "julia-0.6"
"name": "julia-1.0"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "0.6.0"
"version": "1.0.1"
}
},
"nbformat": 4,
Expand Down
15 changes: 8 additions & 7 deletions examples/ex01_path_propagation.jl
@@ -1,4 +1,4 @@

#!/usr/bin/env python
# coding: utf-8

# # Propagation on the path graph
Expand All @@ -8,17 +8,18 @@
using QSWalk
using PyPlot # for plot
using LightGraphs # for PathGraph
using LinearAlgebra # for diag method


# ## Evolution on path graph for fixed time, global vs local interaction

# Basic parameters. Note that *dim* should be an odd number. Otherwise the middle point *midpoint* is not unique. Matrix
# *adjacency* can be alternatively generated as *adjacency = spdiagm((ones(dim-1), ones(dim-1)), (-1, 1))*.

dim = 251 # odd for unique middle point
dim = 151 # odd for unique middle point
midpoint = ceil(Int, dim/2)
w = 0.5
timepoint = 100.
timepoint = 50.
adjacency = adjacency_matrix(PathGraph(dim));


Expand All @@ -37,7 +38,7 @@ rho_local = evolve(op_local, proj(midpoint, dim), timepoint);

# To plot the result of the cannonical measurement, we take a diagonal of the states. Note that both *rhoglobal* and *rholocal* are complex matrices, hence we need to take the real part only. Argument *positions* is generated assuming that *midpoint* (the initial position) is at 0. Note that we have very heavy tails in the global interaction case, which confirms fast propagation in this model.

positions = (collect(1:dim) - midpoint)
positions = (collect(1:dim) .- midpoint)
plot(positions, real.(diag(rho_local)), "k")
plot(positions, real.(diag(rho_global)), "b")
xlabel("position")
Expand All @@ -50,10 +51,10 @@ show()
# Basic parameters. Note that *dim* should be an odd number. Otherwise the middle point *midpoint* is not unique. Matrix
# *adjacency* can be alternatively generated as *adjacency = spdiagm((ones(dim-1), ones(dim-1)), (-1, 1))*.

dim = 251 #odd for unique middle point
dim = 151 #odd for unique middle point
midpoint = ceil(Int, dim/2)
w = 0.5
timepoints = collect(0:5:100)
timepoints = collect(0:2.5:50)
adjacency = adjacency_matrix(PathGraph(dim));


Expand All @@ -74,7 +75,7 @@ local_states = evolve(op_local, proj(midpoint, dim), timepoints);

secmoment_global = Float64[]
secmoment_local = Float64[]
positions = (collect(1:dim) - midpoint)
positions = (collect(1:dim) .- midpoint)

for (rho_global, rho_local) = zip(global_states, local_states)
push!(secmoment_global, sum(positions.^2 .* diag(rho_global)))
Expand Down
31 changes: 18 additions & 13 deletions examples/ex02_convergence.ipynb
Expand Up @@ -22,7 +22,8 @@
"source": [
"using QSWalk\n",
"using LightGraphs # for graph functions \n",
"using GraphPlot # for ploting graphs "
"using GraphPlot # for ploting graphs \n",
"using LinearAlgebra # for linear algebra utilities"
]
},
{
Expand Down Expand Up @@ -52,8 +53,8 @@
"digraph = erdos_renyi(dim, prob, is_directed=true)\n",
"graph = Graph(digraph)\n",
"\n",
"adj_digraph = full(adjacency_matrix(digraph, dir=:in))\n",
"adj_graph = full(adjacency_matrix(graph))\n",
"adj_digraph = Matrix(adjacency_matrix(digraph, dir=:in))\n",
"adj_graph = Matrix(adjacency_matrix(graph))\n",
"time = 100.\n",
"\n",
"println(\"The graph is strongly connected: $(is_strongly_connected(digraph))\")\n",
Expand Down Expand Up @@ -107,13 +108,13 @@
"metadata": {},
"outputs": [],
"source": [
"eigendecomposition = eigfact(evo_gen)\n",
"zeroindex = find(x -> abs(x)<=1.e-5, eigendecomposition[:values])\n",
"stationary_state = unres(vec(eigendecomposition[:vectors][:, zeroindex]))\n",
"eigendecomposition = eigen(evo_gen)\n",
"zeroindex = findfirst(x -> abs(x)<=1.e-5, eigendecomposition.values)\n",
"stationary_state = unres(vec(eigendecomposition.vectors[:, zeroindex]))\n",
"\n",
"println(\"Trace of stationary state: $(trace(stationary_state))\")\n",
"stationary_state /= trace(stationary_state)\n",
"println(\"Trace of stationary state after the normalization: $(trace(stationary_state))\")"
"println(\"Trace of stationary state: $(sum(diag(stationary_state)))\")\n",
"stationary_state /= sum(diag(stationary_state))\n",
"println(\"Trace of stationary state after the normalization: $(sum(diag(stationary_state)))\")"
]
},
{
Expand All @@ -138,7 +139,7 @@
"source": [
"rhoinit1 = proj(1, dim)\n",
"rhoinit2 = proj(3, dim)\n",
"rhoinit3 = eye(dim)/dim;"
"rhoinit3 = Diagonal(I,dim)/dim"
]
},
{
Expand Down Expand Up @@ -180,16 +181,20 @@
}
],
"metadata": {
"@webio": {
"lastCommId": null,
"lastKernelId": null
},
"kernelspec": {
"display_name": "Julia 0.6.0",
"display_name": "Julia 1.0.1",
"language": "julia",
"name": "julia-0.6"
"name": "julia-1.0"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "0.6.0"
"version": "1.0.1"
}
},
"nbformat": 4,
Expand Down
21 changes: 11 additions & 10 deletions examples/ex02_convergence.jl
@@ -1,4 +1,4 @@

#!/usr/bin/env python
# coding: utf-8

# # Convergence on local interaction model
Expand All @@ -9,6 +9,7 @@
using QSWalk
using LightGraphs # for graph functions
using GraphPlot # for ploting graphs
using LinearAlgebra # for linear algebra utilities


# ## Numerical proof of unique stationary state
Expand All @@ -22,8 +23,8 @@ prob = 0.5
digraph = erdos_renyi(dim, prob, is_directed=true)
graph = Graph(digraph)

adj_digraph = full(adjacency_matrix(digraph, dir=:in))
adj_graph = full(adjacency_matrix(graph))
adj_digraph = Matrix(adjacency_matrix(digraph, dir=:in))
adj_graph = Matrix(adjacency_matrix(graph))
time = 100.

println("The graph is strongly connected: $(is_strongly_connected(digraph))")
Expand All @@ -44,13 +45,13 @@ println("Dimensionality of null-space of the evolution operator: $null_dim")

# This allows efficient stationary state generation. Note that the trace may differ from one, as the eigenstate is normalized according to different norm.

eigendecomposition = eigfact(evo_gen)
zeroindex = find(x -> abs(x)<=1.e-5, eigendecomposition[:values])
stationary_state = unres(vec(eigendecomposition[:vectors][:, zeroindex]))
eigendecomposition = eigen(evo_gen)
zeroindex = findfirst(x -> abs(x)<=1.e-5, eigendecomposition.values)
stationary_state = unres(vec(eigendecomposition.vectors[:, zeroindex]))

println("Trace of stationary state: $(trace(stationary_state))")
stationary_state /= trace(stationary_state)
println("Trace of stationary state after the normalization: $(trace(stationary_state))")
println("Trace of stationary state: $(sum(diag(stationary_state)))")
stationary_state /= sum(diag(stationary_state))
println("Trace of stationary state after the normalization: $(sum(diag(stationary_state)))")


# ## Convergence
Expand All @@ -59,7 +60,7 @@ println("Trace of stationary state after the normalization: $(trace(stationary_s

rhoinit1 = proj(1, dim)
rhoinit2 = proj(3, dim)
rhoinit3 = eye(dim)/dim;
rhoinit3 = Diagonal(I,dim)/dim


# Since we apply the same evolution for all of the initial states, it is more efficient to calulate exponent once.
Expand Down
19 changes: 12 additions & 7 deletions examples/ex03_moralization_simple.ipynb
Expand Up @@ -21,8 +21,9 @@
"outputs": [],
"source": [
"using QSWalk\n",
"using TikzGraphs # For plotting graph\n",
"using LightGraphs "
"using GraphPlot # For plotting graph\n",
"using LightGraphs \n",
"using LinearAlgebra"
]
},
{
Expand All @@ -49,7 +50,7 @@
"add_edge!(digraph, 1, 3)\n",
"add_edge!(digraph, 2, 3)\n",
"\n",
"TikzGraphs.plot(digraph)"
"gplot(digraph)"
]
},
{
Expand All @@ -67,7 +68,7 @@
"metadata": {},
"outputs": [],
"source": [
"adjacency = full(transpose(adjacency_matrix(digraph)))\n",
"adjacency = Matrix(transpose(adjacency_matrix(digraph)))\n",
"timepoint = 100.\n",
"\n",
"opmoral = evolve_generator(zero(adjacency), [adjacency])\n",
Expand Down Expand Up @@ -172,16 +173,20 @@
}
],
"metadata": {
"@webio": {
"lastCommId": null,
"lastKernelId": null
},
"kernelspec": {
"display_name": "Julia 0.6.0",
"display_name": "Julia 1.0.1",
"language": "julia",
"name": "julia-0.6"
"name": "julia-1.0"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "0.6.0"
"version": "1.0.1"
}
},
"nbformat": 4,
Expand Down
9 changes: 5 additions & 4 deletions examples/ex03_moralization_simple.jl
@@ -1,13 +1,14 @@

#!/usr/bin/env python
# coding: utf-8

# # Spontaneous moralization on simple graph

# ## Loading modules

using QSWalk
using TikzGraphs # For plotting graph
using GraphPlot # For plotting graph
using LightGraphs
using LinearAlgebra


# ## Moralizing evolution
Expand All @@ -18,14 +19,14 @@ digraph = DiGraph(3)
add_edge!(digraph, 1, 3)
add_edge!(digraph, 2, 3)

TikzGraphs.plot(digraph)
gplot(digraph)


# Here we generate some basic operators. Note in the case of directed graphs we need to transpose adjacency matrix, as *QSWalk.jl* multiplies the state on the right side of evolution operator. Note we choose zero matrix as Hamiltonian of the system.
#
# As we deal with the graph of very small size, we choose full-matrix evolution algorithm. In order to do such, we need to provide in opmoral at least one full matrix.

adjacency = full(transpose(adjacency_matrix(digraph)))
adjacency = Matrix(transpose(adjacency_matrix(digraph)))
timepoint = 100.

opmoral = evolve_generator(zero(adjacency), [adjacency])
Expand Down

0 comments on commit 1aae7aa

Please sign in to comment.