Skip to content

Commit

Permalink
Bump Observers to v0.2 and Optimisers to v0.2 (#303)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtfishman authored Aug 2, 2023
1 parent b95df90 commit 3989d7c
Show file tree
Hide file tree
Showing 20 changed files with 136 additions and 141 deletions.
6 changes: 4 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ version = "0.0.25"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
HDF5 = "f67ccb44-e63f-5c2f-98bd-6dc0ccc4ba2f"
ITensors = "9136182c-28ba-11e9-034c-db9fb085ebd5"
JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
Expand All @@ -18,11 +19,12 @@ StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"

[compat]
ChainRulesCore = "1.10"
DataFrames = "1.6"
HDF5 = "0.13.1, 0.14, 0.15, 0.16"
ITensors = "0.3.20"
JLD2 = "0.4.14"
Observers = "0.0"
Optimisers = "0.1.0"
Observers = "0.2"
Optimisers = "0.2"
Requires = "1.0"
StatsBase = "0.33"
julia = "1.6"
10 changes: 5 additions & 5 deletions examples/04_circuitobserver.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ end
σz::MPS) = [measure_pauli(ψ, j, "Z") for j in 1:length(ψ)]

# define the Circuit observer
obs = Observer([
obs = observer([
"χs" => linkdims, # bond dimension at each bond
"χmax" => maxlinkdim, # maximum bond dimension
"σˣ(2)" => σx2, # pauli X on site 2
Expand All @@ -40,17 +40,17 @@ obs = Observer([

# collect the measurements
println("Bond dimensions at each layer:")
display(results(obs, "χs"))
display(obs[!, "χs"])
println()

println("Maximum bond dimension at each layer:")
display(results(obs, "χmax")')
display(obs[!, "χmax"]')
println()

println("⟨ψ|σˣ(2)|ψ⟩ at each layer:")
display(results(obs, "σˣ(2)"))
display(obs[!, "σˣ(2)"])
println()

println("⟨ψ|σᶻ(n)|ψ⟩ at each layer:")
display(results(obs, "σᶻ"))
display(obs[!, "σᶻ"])
println()
4 changes: 2 additions & 2 deletions examples/07_qst_circuit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ opt = Optimisers.Descent(0.01)

# Initialize the observer for the fidelity
F::MPS; kwargs...) = fidelity(ψ, Ψ)
obs = Observer(["F" => F])
obs = observer(["F" => F])

# Run quantum state tomography, where a variational MPS `|ψ(θ)⟩`
# is optimized to mimimize the cross entropy between the data and
Expand Down Expand Up @@ -84,7 +84,7 @@ opt = Optimisers.ADAM()
# Initialize the observer
F::LPDO; kwargs...) = fidelity(ρ, ϱ)

obs = Observer(["F" => F])
obs = observer(["F" => F])

# Run quantum state tomography, where a variational LPDO `ρ(θ)`
# is optimized to mimimize the cross entropy between the data and
Expand Down
4 changes: 2 additions & 2 deletions examples/08_qpt_circuit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ U0 = randomprocess(Û; χ=χ)
opt = Optimisers.Descent(0.01)

F(U::MPO; kwargs...) = fidelity(U, Û; process=true)
obs = Observer(["F" => F])
obs = observer(["F" => F])

# Initialize stochastic gradient descent optimizer
@show maxlinkdim(U0)
Expand Down Expand Up @@ -86,7 +86,7 @@ N = length(Φ)
opt = Optimisers.ADAM()

F::LPDO; kwargs...) = fidelity(Λ, Φ; process=true)
obs = Observer(["F" => F])
obs = observer(["F" => F])

# Run process tomography
println("Run process tomography to learn noisy process Λ")
Expand Down
4 changes: 2 additions & 2 deletions examples/09_qst_ising.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ F(ψ::MPS) = fidelity(ψ, Ψ)
ZZ::MPS) = correlation_matrix(Ψ, "Z", "Z")

# Initialize observer
obs = Observer(["fidelity" => F, "energy" => Energy, "correlations" => ZZ])
#obs = Observer(["energy" => Energy])
obs = observer(["fidelity" => F, "energy" => Energy, "correlations" => ZZ])
#obs = observer(["energy" => Energy])

@printf("⟨Ψ|Ĥ|Ψ⟩ = %.5f ", E)
# Run tomography
Expand Down
24 changes: 11 additions & 13 deletions examples/Quantum gates via tunable couplers.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -39221,7 +39221,7 @@
"# set initial state |ψ⟩ = |0,1⟩\n",
"ψ₀ = productstate(hilbert, [0,0,1,0,1])\n",
"\n",
"obs = Observer(observables)\n",
"obs = observer(observables)\n",
"\n",
"# perform TEBD simulation and generate output `MPS`\n",
"ψ = runcircuit(ψ₀, circuit; \n",
Expand All @@ -39230,11 +39230,10 @@
" move_sites_back_before_measurements = true, \n",
" outputlevel = 0)\n",
"\n",
"res = DataFrame(results(obs))\n",
"plot(title = \"Population Dynamics\", xlabel = \"Time (ns)\", ylabel = \"Energy (GHz)\"; plot_args...)\n",
"plot!(ts, res[!,\"n(q₁)\"] , label = \"n(q₁)\"; plot_args...)\n",
"plot!(ts, res[!,\"n(q₂)\"] , label = \"n(q₂)\"; plot_args...)\n",
"plot!(ts, res[!,\"n(q₃)\"] , label = \"n(q₃)\"; plot_args...)"
"plot!(ts, obs[!,\"n(q₁)\"] , label = \"n(q₁)\"; plot_args...)\n",
"plot!(ts, obs[!,\"n(q₂)\"] , label = \"n(q₂)\"; plot_args...)\n",
"plot!(ts, obs[!,\"n(q₃)\"] , label = \"n(q₃)\"; plot_args...)"
]
},
{
Expand Down Expand Up @@ -40491,9 +40490,9 @@
],
"source": [
"plot(title = \"Population Dynamics\", xlabel = \"Time (ns)\", ylabel = \"Energy (GHz)\"; plot_args...)\n",
"plot!(ts, res[!,\"n(q₁)\"] , label = \"n(q₁)\"; plot_args...)\n",
"plot!(ts, res[!,\"n(q₂)\"] , label = \"n(q₂)\"; plot_args...)\n",
"plot!(ts, res[!,\"n(q₃)\"] , label = \"n(q₃)\"; plot_args...)"
"plot!(ts, obs[!,\"n(q₁)\"] , label = \"n(q₁)\"; plot_args...)\n",
"plot!(ts, obs[!,\"n(q₂)\"] , label = \"n(q₂)\"; plot_args...)\n",
"plot!(ts, obs[!,\"n(q₃)\"] , label = \"n(q₃)\"; plot_args...)"
]
},
{
Expand Down Expand Up @@ -41782,7 +41781,7 @@
"# set initial state |ψ⟩ = |0,1⟩\n",
"ψ₀ = productstate(hilbert, [0,0,1,0,1])\n",
"\n",
"obs = Observer(observables)\n",
"obs = observer(observables)\n",
"\n",
"# perform TEBD simulation and generate output `MPS`\n",
"ψ = runcircuit(ψ₀, circuit; \n",
Expand All @@ -41791,12 +41790,11 @@
" move_sites_back_before_measurements = true, \n",
" outputlevel = 0)\n",
"\n",
"res = DataFrame(results(obs))\n",
"\n",
"plot(title = \"Population Dynamics\", xlabel = \"Time (ns)\", ylabel = \"Energy (GHz)\"; plot_args...)\n",
"plot!(ts, res[!,\"n(q₁)\"] , label = \"n(q₁)\"; plot_args...)\n",
"plot!(ts, res[!,\"n(q₂)\"] , label = \"n(q₂)\"; plot_args...)\n",
"plot!(ts, res[!,\"n(q₃)\"] , label = \"n(q₃)\"; plot_args...)"
"plot!(ts, obs[!,\"n(q₁)\"] , label = \"n(q₁)\"; plot_args...)\n",
"plot!(ts, obs[!,\"n(q₂)\"] , label = \"n(q₂)\"; plot_args...)\n",
"plot!(ts, obs[!,\"n(q₃)\"] , label = \"n(q₃)\"; plot_args...)"
]
}
],
Expand Down
21 changes: 9 additions & 12 deletions examples/optimal-coherent-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@
"#define a vector of observables and create the `Observer`.\n",
"observables = [\"n($α)\" => x -> population(x, k) # actually x -> expect(x, \"a† * a\"; sites = k)\n",
" for (k,α) in enumerate(modes)]\n",
"obs = Observer(observables)"
"obs = observer(observables)"
],
"metadata": {
"name": "A slide ",
Expand Down Expand Up @@ -918,10 +918,9 @@
],
"cell_type": "code",
"source": [
"res = DataFrame(results(obs));\n",
"p = plot(xlabel = \"time (ns)\", ylabel = \"n̂(t)\", legend = (0.40,0.9); plot_args...)\n",
"p = plot!(p, ts, res[!,\"n(q₁)\"], label = \"n(q₁)\"; plot_args...)\n",
"p = plot!(p, ts, res[!,\"n(q₂)\"], label = \"n(q₂)\"; plot_args...)\n",
"p = plot!(p, ts, obs[!,\"n(q₁)\"], label = \"n(q₁)\"; plot_args...)\n",
"p = plot!(p, ts, obs[!,\"n(q₂)\"], label = \"n(q₂)\"; plot_args...)\n",
"p"
],
"metadata": {
Expand Down Expand Up @@ -1628,7 +1627,7 @@
"\n",
"H = hamiltonian(ω⃗, g)\n",
"\n",
"obs = Observer(observables)\n",
"obs = observer(observables)\n",
"\n",
"circuit = trottercircuit(H; ts = ts, layered = true)\n",
"\n",
Expand All @@ -1637,10 +1636,9 @@
"ψ = runcircuit(ψ₀, circuit; (observer!) = obs,\n",
" move_sites_back_before_measurements = true, outputlevel = 0)\n",
"\n",
"res = DataFrame(results(obs));\n",
"p = plot(xlabel = \"time (ns)\", ylabel = \"n̂(t)\", legend = (0.50,0.9); plot_args...)\n",
"p = plot!(p, ts, res[!,\"n(q₁)\"], label = \"n(q₁)\"; plot_args...)\n",
"p = plot!(p, ts, res[!,\"n(q₂)\"], label = \"n(q₂)\"; plot_args...)\n",
"p = plot!(p, ts, obs[!,\"n(q₁)\"], label = \"n(q₁)\"; plot_args...)\n",
"p = plot!(p, ts, obs[!,\"n(q₂)\"], label = \"n(q₂)\"; plot_args...)\n",
"p"
],
"metadata": {
Expand Down Expand Up @@ -3323,13 +3321,12 @@
"ψ₀ = productstate(hilbert, [1,0])\n",
"observables = [\"n($α)\" => x -> population(x, k)\n",
" for (k,α) in enumerate(modes)]\n",
"obs = Observer(observables)\n",
"obs = observer(observables)\n",
"ψ = runcircuit(ψ₀, circuit; (observer!) = obs,\n",
" move_sites_back_before_measurements = true, outputlevel = 0)\n",
"res = DataFrame(results(obs));\n",
"p = plot(xlabel = \"time (ns)\", ylabel = \"n̂(t)\", legend = (0.50,0.9); plot_args...)\n",
"p = plot!(p, ts, res[!,\"n(q₁)\"], label = \"n(q₁)\"; plot_args...)\n",
"p = plot!(p, ts, res[!,\"n(q₂)\"], label = \"n(q₂)\"; plot_args...)\n",
"p = plot!(p, ts, obs[!,\"n(q₁)\"], label = \"n(q₁)\"; plot_args...)\n",
"p = plot!(p, ts, obs[!,\"n(q₂)\"], label = \"n(q₂)\"; plot_args...)\n",
"p"
],
"metadata": {},
Expand Down
21 changes: 9 additions & 12 deletions examples/optimal-coherent-control.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ end;
#define a vector of observables and create the `Observer`.
observables = ["n()" => x -> population(x, k) # actually x -> expect(x, "a† * a"; sites = k)
for (k, α) in enumerate(modes)]
obs = Observer(observables)
obs = observer(observables)

tg = 30 # final time (in ns)
trottersteps = 100 # number of Trotter steps
Expand All @@ -63,10 +63,9 @@ circuit = trottercircuit(H; ts=ts, layered=true)
ψ₀, circuit; (observer!)=obs, move_sites_back_before_measurements=true, outputlevel=0
)

res = DataFrame(results(obs));
p = plot(; xlabel="time (ns)", ylabel="n̂(t)", legend=(0.40, 0.9), plot_args...)
p = plot!(p, ts, res[!, "n(q₁)"]; label="n(q₁)", plot_args...)
p = plot!(p, ts, res[!, "n(q₂)"]; label="n(q₂)", plot_args...)
p = plot!(p, ts, obs[!, "n(q₁)"]; label="n(q₁)", plot_args...)
p = plot!(p, ts, obs[!, "n(q₂)"]; label="n(q₂)", plot_args...)
p

ω₁ = 5.0 * GHz
Expand All @@ -75,7 +74,7 @@ p

H = hamiltonian(ω⃗, g)

obs = Observer(observables)
obs = observer(observables)

circuit = trottercircuit(H; ts=ts, layered=true)

Expand All @@ -85,10 +84,9 @@ circuit = trottercircuit(H; ts=ts, layered=true)
ψ₀, circuit; (observer!)=obs, move_sites_back_before_measurements=true, outputlevel=0
)

res = DataFrame(results(obs));
p = plot(; xlabel="time (ns)", ylabel="n̂(t)", legend=(0.50, 0.9), plot_args...)
p = plot!(p, ts, res[!, "n(q₁)"]; label="n(q₁)", plot_args...)
p = plot!(p, ts, res[!, "n(q₂)"]; label="n(q₂)", plot_args...)
p = plot!(p, ts, obs[!, "n(q₁)"]; label="n(q₁)", plot_args...)
p = plot!(p, ts, obs[!, "n(q₂)"]; label="n(q₂)", plot_args...)
p

using Zygote
Expand Down Expand Up @@ -167,14 +165,13 @@ Ht = [hamiltonian(θ⃗, t) for t in ts]
circuit = trottercircuit(Ht; ts=ts, layered=true)
ψ₀ = productstate(hilbert, [1, 0])
observables = ["n()" => x -> population(x, k) for (k, α) in enumerate(modes)]
obs = Observer(observables)
obs = observer(observables)
ψ = runcircuit(
ψ₀, circuit; (observer!)=obs, move_sites_back_before_measurements=true, outputlevel=0
)
res = DataFrame(results(obs));
p = plot(; xlabel="time (ns)", ylabel="n̂(t)", legend=(0.50, 0.9), plot_args...)
p = plot!(p, ts, res[!, "n(q₁)"]; label="n(q₁)", plot_args...)
p = plot!(p, ts, res[!, "n(q₂)"]; label="n(q₂)", plot_args...)
p = plot!(p, ts, obs[!, "n(q₁)"]; label="n(q₁)", plot_args...)
p = plot!(p, ts, obs[!, "n(q₂)"]; label="n(q₂)", plot_args...)
p

# This file was generated using Literate.jl, https://github.com/fredrikekre/Literate.jl
21 changes: 9 additions & 12 deletions examples/src/optimal-coherent-control.jl
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ end;
#define a vector of observables and create the `Observer`.
observables = ["n()" => x -> population(x, k) # actually x -> expect(x, "a† * a"; sites = k)
for (k, α) in enumerate(modes)]
obs = Observer(observables)
obs = observer(observables)

#nb # %% A slide [markdown] {"slideshow": {"slide_type": "subslide"}}
# We are not ready to simulate the system dynamics using a Trotter expansion.
Expand Down Expand Up @@ -128,10 +128,9 @@ circuit = trottercircuit(H; ts=ts, layered=true)
# We plot here the average occupation of the two modes as a function of time:

#nb %% A slide [code] {"slideshow": {"slide_type": "subslide"}}
res = DataFrame(results(obs));
p = plot(; xlabel="time (ns)", ylabel="n̂(t)", legend=(0.40, 0.9), plot_args...)
p = plot!(p, ts, res[!, "n(q₁)"]; label="n(q₁)", plot_args...)
p = plot!(p, ts, res[!, "n(q₂)"]; label="n(q₂)", plot_args...)
p = plot!(p, ts, obs[!, "n(q₁)"]; label="n(q₁)", plot_args...)
p = plot!(p, ts, obs[!, "n(q₂)"]; label="n(q₂)", plot_args...)
p

#nb # %% A slide [markdown] {"slideshow": {"slide_type": "subslide"}}
Expand All @@ -152,7 +151,7 @@ p

H = hamiltonian(ω⃗, g)

obs = Observer(observables)
obs = observer(observables)

circuit = trottercircuit(H; ts=ts, layered=true)

Expand All @@ -162,10 +161,9 @@ circuit = trottercircuit(H; ts=ts, layered=true)
ψ₀, circuit; (observer!)=obs, move_sites_back_before_measurements=true, outputlevel=0
)

res = DataFrame(results(obs));
p = plot(; xlabel="time (ns)", ylabel="n̂(t)", legend=(0.50, 0.9), plot_args...)
p = plot!(p, ts, res[!, "n(q₁)"]; label="n(q₁)", plot_args...)
p = plot!(p, ts, res[!, "n(q₂)"]; label="n(q₂)", plot_args...)
p = plot!(p, ts, obs[!, "n(q₁)"]; label="n(q₁)", plot_args...)
p = plot!(p, ts, obs[!, "n(q₂)"]; label="n(q₂)", plot_args...)
p

#nb # %% A slide [markdown] {"slideshow": {"slide_type": "subslide"}}
Expand Down Expand Up @@ -280,12 +278,11 @@ Ht = [hamiltonian(θ⃗, t) for t in ts]
circuit = trottercircuit(Ht; ts=ts, layered=true)
ψ₀ = productstate(hilbert, [1, 0])
observables = ["n()" => x -> population(x, k) for (k, α) in enumerate(modes)]
obs = Observer(observables)
obs = observer(observables)
ψ = runcircuit(
ψ₀, circuit; (observer!)=obs, move_sites_back_before_measurements=true, outputlevel=0
)
res = DataFrame(results(obs));
p = plot(; xlabel="time (ns)", ylabel="n̂(t)", legend=(0.50, 0.9), plot_args...)
p = plot!(p, ts, res[!, "n(q₁)"]; label="n(q₁)", plot_args...)
p = plot!(p, ts, res[!, "n(q₂)"]; label="n(q₂)", plot_args...)
p = plot!(p, ts, obs[!, "n(q₁)"]; label="n(q₁)", plot_args...)
p = plot!(p, ts, obs[!, "n(q₂)"]; label="n(q₂)", plot_args...)
p
11 changes: 6 additions & 5 deletions src/PastaQ.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
module PastaQ

using ITensors
using Random
using LinearAlgebra
using DataFrames: DataFrame
using HDF5
using ITensors
using JLD2
using Printf
using LinearAlgebra
using Observers
using StatsBase: StatsBase, Weights
using Optimisers: Optimisers
using Printf
using Random
using StatsBase: StatsBase, Weights

include("imports.jl")
include("exports.jl")
Expand Down
2 changes: 1 addition & 1 deletion src/circuits/runcircuit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ function runcircuit(
end
if !isnothing(outputpath)
observerpath = outputpath * "_observer.jld2"
save(observerpath, observer!)
save(observerpath; observer!)
if savestate
statepath = outputpath * "_state.h5"
h5rewrite(statepath) do fout
Expand Down
6 changes: 3 additions & 3 deletions src/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,13 @@ function printmetric(name::String, metric::Complex)
end
end

function printobserver(observer::Observer, print_metrics::Union{String,AbstractArray})
function printobserver(observer::DataFrame, print_metrics::Union{String,AbstractArray})
if !isempty(print_metrics)
if print_metrics isa String
printmetric(print_metrics, results(observer, print_metrics)[end])
printmetric(print_metrics, observer[end, print_metrics])
else
for metric in print_metrics
printmetric(metric, results(observer, metric)[end])
printmetric(metric, observer[end, metric])
end
end
end
Expand Down
Loading

0 comments on commit 3989d7c

Please sign in to comment.