-
Notifications
You must be signed in to change notification settings - Fork 52
Extend plot support for entanglementplot and transferplot to Makie.jl
#428
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
borisdevos
wants to merge
19
commits into
main
Choose a base branch
from
bd/plotting
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 18 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
cf5effa
first attempt at extension packages
borisdevos 0f295ba
plots weakdep
borisdevos 15a9022
only recipesbase weakdep
borisdevos 49bd34d
tests
borisdevos b0395fc
Merge branch 'main' of https://github.com/QuantumKitHub/MPSKit.jl int…
borisdevos f583b6f
get makie working
borisdevos babffbf
get plots working
borisdevos d66b9ce
minor
borisdevos 97729ac
update docstrings
borisdevos 28b8b17
Merge branch 'main' of https://github.com/QuantumKitHub/MPSKit.jl int…
borisdevos 59779ca
remove todo
borisdevos ed6054a
get makie plot kwargs working
borisdevos a6bf25f
potential project.toml test fix
borisdevos b0ff466
remove latexstrings dep in plots.jl, but keep in makie for now
borisdevos 5be4fc4
docstrings
borisdevos 7d90389
fix makie tests
borisdevos 1595a47
i have no clue how to fix aqua tests
borisdevos a74a333
Merge branch 'main' of https://github.com/QuantumKitHub/MPSKit.jl int…
borisdevos 14c37e2
remove extras and targets
borisdevos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,192 @@ | ||
| module MPSKitMakieExt | ||
|
|
||
| using Makie, LaTeXStrings | ||
| using MPSKit, TensorKit | ||
|
|
||
| #TODO?: add Colors.jl to access this, allows Plots extension to also use these colors | ||
| const JLCOLORS = Makie.Colors.JULIA_LOGO_COLORS | ||
|
|
||
| @recipe(EntanglementPlot, mps) do scene | ||
| Attributes( | ||
| site = 0, | ||
| expand_symmetry = false, | ||
| sortby = maximum, | ||
| sector_margin = 1 // 10, | ||
| sector_formatter = string, | ||
| ) | ||
| end | ||
|
|
||
| function Makie.plot!(ep::EntanglementPlot) | ||
| #TODO: still want this style where sectors are separated? | ||
| mps = ep.mps[] | ||
| site = ep.site[] | ||
| margin = ep.sector_margin[] | ||
|
|
||
| (isa(mps, FiniteMPS) && (site == 0 || site > length(mps))) && | ||
| throw(ArgumentError("Invalid site $site for the given mps.")) | ||
|
|
||
| spectra = entanglement_spectrum(mps, site) | ||
|
|
||
| sectors = sectortype(mps)[] | ||
| spectrum = Vector{Vector{Float64}}() | ||
|
|
||
| for (c, b) in pairs(spectra) | ||
| if ep.expand_symmetry[] | ||
| b′ = repeat(b, dim(c)) | ||
| sort!(b′; rev = true) | ||
| push!(spectrum, b′) | ||
| else | ||
| push!(spectrum, b) | ||
| end | ||
| push!(sectors, c) | ||
| end | ||
|
|
||
| # Sort sectors according to provided method | ||
| if length(spectrum) > 1 | ||
| order = sortperm(spectrum; by = ep.sortby[], rev = true) | ||
| spectrum = spectrum[order] | ||
| sectors = sectors[order] | ||
| end | ||
|
|
||
| ax = Makie.current_axis() | ||
|
|
||
| # Axis styling | ||
| ax.title = L"\text{Entanglement Spectrum}" | ||
| ax.titlesize = 24 | ||
|
|
||
| ax.xlabel = latexstring("\$\\chi\$ = $(round(Int, dim(left_virtualspace(mps, site))))") # still want this? | ||
| ax.xlabelsize = 24 | ||
| ax.xticks = (1:length(sectors), ep.sector_formatter[].(sectors)) | ||
| ax.xticklabelsize = 16 | ||
| ax.xticklabelrotation = 45 | ||
| ax.xticklabelalign = (:right, :top) | ||
| xlims!(ax, 1, length(sectors) + 1) | ||
|
|
||
| ax.ylabel = L"\log(\lambda)" | ||
| ax.ylabelsize = 24 | ||
| bottom = floor(Int, log10(minimum(spectra))) | ||
| ax.yticks = (bottom:2:0, latexstring.(collect(bottom:2:0))) | ||
| ax.yticklabelsize = 16 | ||
| ylims!(ax, bottom, 0 + 1.0e-1) | ||
|
|
||
| # Plot data | ||
| for (i, (partial_spectrum, sector)) in enumerate(zip(spectrum, sectors)) | ||
| n_spectrum = length(partial_spectrum) | ||
| if n_spectrum == 1 | ||
| x = [i + 0.5] | ||
| else | ||
| x = collect(range(i + float(margin), i + 1 - float(margin); length = n_spectrum)) | ||
| end | ||
| scatter!(ep, x, log10.(partial_spectrum), color = JLCOLORS[mod1(i, length(JLCOLORS))]) | ||
| end | ||
|
|
||
| return ep | ||
| end | ||
|
|
||
| function MPSKit.entanglementplot(args...; plotkwargs = (;), kwargs...) | ||
| p = entanglementplot(args...; kwargs...) | ||
| ax = p.axis | ||
|
|
||
| # overwrite user-provided axis attributes | ||
| for (k, v) in pairs(plotkwargs) | ||
| setproperty!(ax, k, v) | ||
| end | ||
| return p | ||
| end | ||
|
|
||
| #------------------------------------------------------------ | ||
|
|
||
| @recipe(TransferPlot, mps) do scene | ||
| Attributes( | ||
| below = nothing, | ||
| sectors = nothing, | ||
| transferkwargs = NamedTuple(), | ||
| thetaorigin = 0.0, | ||
| sector_formatter = string, | ||
| ) | ||
| end | ||
|
|
||
| function Makie.plot!(tp::TransferPlot) | ||
| #TODO: consider radial plot | ||
| mps = tp.mps[] | ||
| below = tp.below[] === nothing ? mps : tp.below[] | ||
| sectors = tp.sectors[] === nothing ? [leftunit(mps)] : tp.sectors[] | ||
| transferkwargs = NamedTuple( # weird convert thing | ||
| k => (v isa Observable ? v[] : v) for (k, v) in pairs(tp.transferkwargs[]) | ||
| ) | ||
| thetaorigin = tp.thetaorigin[] | ||
| sector_formatter = tp.sector_formatter[] | ||
|
|
||
| ax = Makie.current_axis() | ||
| ax.title = L"\text{Transfer Spectrum}" | ||
| ax.titlesize = 24 | ||
| ax.xlabel = L"\theta" | ||
| ax.xlabelsize = 24 | ||
| ax.xticklabelsize = 16 | ||
| ax.ylabel = L"r" | ||
| ax.ylabelsize = 24 | ||
| ax.yticklabelsize = 16 | ||
|
|
||
| ax.xticks = pitick(0, 2pi, 4; mode = :latex) | ||
| ax.yticks = (range(0, 1.0; length = 6), latexstring.(range(0, 1.0; length = 6))) | ||
| ax.xgridvisible = true | ||
| ax.ygridvisible = true | ||
|
|
||
| ax.leftspinevisible = true | ||
| ax.rightspinevisible = false | ||
| ax.bottomspinevisible = true | ||
| ax.topspinevisible = false | ||
|
|
||
| for (i, sector) in enumerate(sectors) | ||
| spectrum = transfer_spectrum(mps; below = below, sector = sector, transferkwargs...) | ||
| θ = mod2pi.(angle.(spectrum) .+ thetaorigin) .- thetaorigin | ||
| r = abs.(spectrum) | ||
| scatter!(tp, θ, r; label = sector_formatter(sector), color = JLCOLORS[mod1(i, length(JLCOLORS))]) | ||
| end | ||
|
|
||
| xlims!(ax, thetaorigin - 0.1, thetaorigin + 2π + 0.1) | ||
| ylims!(ax, nothing, 1.05) | ||
| Legend(Makie.current_figure()[1, 1], tp.plots, [sector_formatter(s) for s in sectors]; tellwidth = false, halign = :center, valign = :top) | ||
| return tp | ||
| end | ||
|
|
||
| function MPSKit.transferplot(args...; plotkwargs = (;), kwargs...) | ||
| p = transferplot(args...; kwargs...) | ||
| ax = p.axis | ||
|
|
||
| # overwrite user-provided axis attributes | ||
| for (k, v) in pairs(plotkwargs) | ||
| setproperty!(ax, k, v) | ||
| end | ||
| return p | ||
| end | ||
|
|
||
| # utility for plotting | ||
|
|
||
| function pitick(start, stop, denom; mode = :latex) | ||
| a = Int(cld(start, π / denom)) | ||
| b = Int(fld(stop, π / denom)) | ||
| tick = range(a * π / denom, b * π / denom; step = π / denom) | ||
| ticklabel = piticklabel.((a:b) .// denom, Val(mode)) | ||
| return tick, ticklabel | ||
| end | ||
|
|
||
| function piticklabel(x::Rational, ::Val{:text}) | ||
| iszero(x) && return "0" | ||
| S = x < 0 ? "-" : "" | ||
| n, d = abs(numerator(x)), denominator(x) | ||
| N = n == 1 ? "" : repr(n) | ||
| d == 1 && return S * N * "π" | ||
| return S * N * "π/" * repr(d) | ||
| end | ||
|
|
||
| function piticklabel(x::Rational, ::Val{:latex}) | ||
| iszero(x) && return L"0" | ||
| S = x < 0 ? "-" : "" | ||
| n, d = abs(numerator(x)), denominator(x) | ||
| N = n == 1 ? "" : repr(n) | ||
| d == 1 && return L"%$S%$N\pi" | ||
| return L"%$S\frac{%$N\pi}{%$d}" | ||
| end | ||
|
|
||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| module MPSKitPlotsExt | ||
|
|
||
| using RecipesBase | ||
| using MPSKit, TensorKit | ||
|
|
||
| @userplot EntanglementPlot | ||
|
|
||
| @recipe function f( | ||
| h::EntanglementPlot; site = 0, expand_symmetry = false, sortby = maximum, | ||
| sector_margin = 1 // 10, sector_formatter = string | ||
| ) | ||
| mps = h.args[1] | ||
| (isa(mps, FiniteMPS) && (site == 0 || site > length(mps))) && | ||
| throw(ArgumentError("Invalid site $site for the given mps.")) | ||
|
|
||
| spectra = entanglement_spectrum(mps, site) | ||
| sectors = sectortype(mps)[] | ||
| spectrum = Vector{Vector{Float64}}() | ||
| for (c, b) in pairs(spectra) | ||
| if expand_symmetry # Duplicate entries according to the quantum dimension. | ||
| b′ = repeat(b, dim(c)) | ||
| sort!(b′; rev = true) | ||
| push!(spectrum, b′) | ||
| else | ||
| push!(spectrum, b) | ||
| end | ||
| push!(sectors, c) | ||
| end | ||
|
|
||
| if length(spectrum) > 1 | ||
| order = sortperm(spectrum; by = sortby, rev = true) | ||
| spectrum = spectrum[order] | ||
| sectors = sectors[order] | ||
| end | ||
|
|
||
| for (i, (partial_spectrum, sector)) in enumerate(zip(spectrum, sectors)) | ||
| @series begin | ||
| title --> "Entanglement Spectrum" | ||
| legend --> false | ||
| grid --> :xy | ||
| widen --> true | ||
| bottom_margin -->(10, :mm) | ||
|
|
||
| xguide --> "χ = $(round(Int, dim(left_virtualspace(mps, site))))" | ||
| xticks --> (1:length(sectors), sector_formatter.(sectors)) | ||
| xtickfonthalign --> :center | ||
| xtick_direction --> :out | ||
| xrotation --> 45 | ||
| xlims --> (1, length(sectors) + 1) | ||
|
|
||
| ylims --> (-Inf, 1 + 1.0e-1) | ||
| yscale --> :log10 | ||
| seriestype := :scatter | ||
| label := sector_formatter(sector) | ||
| n_spectrum = length(partial_spectrum) | ||
|
|
||
| # Put single dot in the middle, or a linear range with padding. | ||
| if n_spectrum == 1 | ||
| x = [i + 1 // 2] | ||
| else | ||
| x = range(i + sector_margin, i + 1 - sector_margin; length = n_spectrum) | ||
| end | ||
| return x, partial_spectrum | ||
| end | ||
| end | ||
|
|
||
| return nothing | ||
| end | ||
|
|
||
| MPSKit.entanglementplot(args...; kwargs...) = entanglementplot(args...; kwargs...) | ||
|
|
||
| #----------------------------------------------------------------------------- | ||
|
|
||
| @userplot TransferPlot | ||
|
|
||
| @recipe function f( | ||
| h::TransferPlot; sectors = nothing, transferkwargs = (;), thetaorigin = 0, | ||
| sector_formatter = string | ||
| ) | ||
| if sectors === nothing | ||
| sectors = [leftunit(h.args[1])] | ||
| end | ||
|
|
||
| for sector in sectors | ||
| below = length(h.args) == 1 ? h.args[1] : h.args[2] | ||
| spectrum = transfer_spectrum( | ||
| h.args[1]; below = below, sector = sector, | ||
| transferkwargs... | ||
| ) | ||
|
|
||
| @series begin | ||
| yguide --> "r" | ||
| ylims --> (-Inf, 1.05) | ||
|
|
||
| xguide --> "θ" | ||
| xlims --> (thetaorigin, thetaorigin + 2pi) | ||
| xticks --> range(0, 2pi; length = 7) | ||
| xformatter --> x -> "$(rationalize(x / π, tol = 0.05))π" | ||
| xwiden --> true | ||
| seriestype := :scatter | ||
| markershape --> :auto | ||
| label := sector_formatter(sector) | ||
| return mod2pi.(angle.(spectrum) .+ thetaorigin) .- thetaorigin, abs.(spectrum) | ||
| end | ||
| end | ||
|
|
||
| title --> "Transfer Spectrum" | ||
| legend --> false | ||
| grid --> :xy | ||
| framestyle --> :zerolines | ||
|
|
||
| return nothing | ||
| end | ||
|
|
||
| MPSKit.transferplot(args...; kwargs...) = transferplot(args...; kwargs...) | ||
|
|
||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.