Skip to content

Commit

Permalink
Update for latest Observers.jl syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
mtfishman committed Aug 2, 2023
1 parent 8e4e797 commit ea0596d
Show file tree
Hide file tree
Showing 13 changed files with 34 additions and 31 deletions.
2 changes: 2 additions & 0 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,6 +19,7 @@ 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"
Expand Down
2 changes: 1 addition & 1 deletion 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 Down
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
4 changes: 2 additions & 2 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 Down Expand Up @@ -41782,7 +41782,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 Down
6 changes: 3 additions & 3 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 @@ -1628,7 +1628,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 Down Expand Up @@ -3323,7 +3323,7 @@
"ψ₀ = 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",
Expand Down
6 changes: 3 additions & 3 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 Down Expand Up @@ -75,7 +75,7 @@ p

H = hamiltonian(ω⃗, g)

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

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

Expand Down Expand Up @@ -167,7 +167,7 @@ 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
)
Expand Down
6 changes: 3 additions & 3 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 @@ -152,7 +152,7 @@ p

H = hamiltonian(ω⃗, g)

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

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

Expand Down Expand Up @@ -280,7 +280,7 @@ 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
)
Expand Down
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
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/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ 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])
Expand Down
2 changes: 1 addition & 1 deletion src/tomography/quantumtomography.jl
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ function tomography(train_data::AbstractMatrix, L::LPDO; (observer!)=nothing, kw
localnorm = isqpt ? 2.0 : 1.0

# observer is not passed but earlystop is called
observer! = (isnothing(observer!) || earlystop) ? Observer() : observer!
observer! = (isnothing(observer!) || earlystop) ? observer() : observer!

# observer is defined
if !isnothing(observer!)
Expand Down
12 changes: 6 additions & 6 deletions test/test_io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ end
ψ = runcircuit(sites, circuit)
Ftest = fidelity(ψ, ϕ)
f::MPS) = fidelity(ψ, ϕ)#; kwargs...) = fidelity(ψ, ϕ)
obs = Observer(["f" => f])
obs = observer(["f" => f])
ψ = runcircuit(
sites,
circuit;
Expand Down Expand Up @@ -150,7 +150,7 @@ end
ρ = runcircuit(sites, circuit; noise=("DEP", (p=0.001,)))
Ftest = fidelity(ϱ, ρ)
g::MPO; kwargs...) = fidelity(ρ, ϱ)#; kwargs...) = fidelity(ψ, ϕ)
obs = Observer(["g" => g])
obs = observer(["g" => g])
outputpath = "simulation"
ρ₀ = projector(productstate(sites))
ρ = runcircuit(
Expand Down Expand Up @@ -194,7 +194,7 @@ end
opt = Optimisers.Descent(0.01)

F::MPS; kwargs...) = fidelity(ψ, Ψ)
obs = Observer(["F" => F])
obs = observer(["F" => F])
epochs = 18

batchsize = 10
Expand Down Expand Up @@ -243,7 +243,7 @@ end
opt = Optimisers.Descent(0.01)

F::LPDO; kwargs...) = fidelity(ρ, ϱ)
obs = Observer(["F" => F])
obs = observer(["F" => F])
epochs = 9

batchsize = 10
Expand Down Expand Up @@ -292,7 +292,7 @@ end
opt = Optimisers.Descent(0.01)

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

batchsize = 10
Expand Down Expand Up @@ -342,7 +342,7 @@ end
opt = Optimisers.Descent(0.01)

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

batchsize = 10
Expand Down

0 comments on commit ea0596d

Please sign in to comment.