Skip to content

Commit

Permalink
Add plot example for DiagonalReduction and RankSimplification transfo…
Browse files Browse the repository at this point in the history
…rmations
  • Loading branch information
jofrevalles committed Sep 12, 2023
1 parent 42c4e58 commit 2739264
Showing 1 changed file with 152 additions and 5 deletions.
157 changes: 152 additions & 5 deletions docs/src/transformations.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,43 @@ transform!
# Example
Here we show how can we reduce the complexity of the tensor network by applying a tranformation to it:
```@setup plot
using Makie
Makie.inline!(true)
set_theme!(resolution=(800,200))
using CairoMakie
CairoMakie.activate!(type = "svg")
using Tenet
using NetworkLayout
using Pkg
Pkg.add("QuacIO")
function smooth_annotation!(f; color=Makie.RGBAf(110 // 256, 170 // 256, 250 // 256, 60 // 256), xlims=[-2, 2], ylims=[-2, 2], offset_x=0.0, offset_y=0.0, radius_x=1.0, radius_y=1.0, num_waves=5, fluctuation_amplitude=0.1, phase_shift=0.0)
ax = Axis(f)
hidedecorations!(ax)
hidespines!(ax)
# Define limits of the plot
xlims!(ax, xlims...)
ylims!(ax, ylims...)
# Create a perturbed filled shape
theta = LinRange(0, 2π, 100)
fluctuations = fluctuation_amplitude .* sin.(num_waves .* theta .+ phase_shift)
# Apply the fluctuations and radius scaling
perturbed_radius_x = radius_x .+ fluctuations
perturbed_radius_y = radius_y .+ fluctuations
circle_points = [Point2f((perturbed_radius_x[i]) * cos(theta[i]) + offset_x,
(perturbed_radius_y[i]) * sin(theta[i]) + offset_y) for i in 1:length(theta)]
poly!(ax, circle_points, color=color, closed=true)
end
```
```@example transformation
```@example plot
using QuacIO
using CairoMakie
using Tenet
sites = [5, 6, 14, 15, 16, 17, 24, 25, 26, 27, 28, 32, 33, 34, 35, 36, 37, 38, 39, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 61, 62, 63, 64, 65, 66, 67, 72, 73, 74, 75, 76, 83, 84, 85, 94]
circuit = QuacIO.parse(joinpath(@__DIR__, "sycamore_53_10_0.qasm"), format=QuacIO.Qflex(), sites=sites)
Expand All @@ -44,10 +71,130 @@ fig # hide

# Transformations

## Rank simplification
!!! warn ""
This transformation is always used by default when visualizing a `TensorNetwork` with `plot`.
```@docs
Tenet.HyperindConverter
```

## Diagonal reduction
```@example plot
fig = Figure() #hide
data = zeros(Float64, 2, 2, 2, 2) #hide
for i in 1:2 #hide
for j in 1:2 #hide
for k in 1:2 #hide
data[i, i, j, k] = k #hide
end #hide
end #hide
end #hide
A = Tensor(data, (:i, :j, :k, :l)) #hide
B = Tensor(rand(2, 2), (:i, :m)) #hide
C = Tensor(rand(2, 2), (:j, :n)) #hide
bg_blue = Makie.RGBAf(110 // 256, 170 // 256, 250 // 256, 50 // 256) #hide
orange = Makie.RGBf(240 // 256, 180 // 256, 100 // 256) #hide
red = Makie.RGBf(240 // 256, 90 // 256, 70 // 256) #hide
tn = TensorNetwork([A, B, C]) #hide
reduced = transform(tn, Tenet.DiagonalReduction) #hide
smooth_annotation!( #hide
fig[1, 1]; #hide
color = bg_blue, #hide
xlims = [-2, 2], #hide
ylims = [-2, 2], #hide
offset_x = -0.21, #hide
offset_y = -0.42, #hide
radius_x = 0.38, #hide
radius_y = 0.8, #hide
num_waves = 6, #hide
fluctuation_amplitude = 0.02, #hide
phase_shift = 0.0) #hide
plot!(fig[1, 1], tn, layout=Spring(iterations=1000, C=0.5, seed=100); node_color=[red, orange, orange, :black, :black,:black, :black]) #hide
smooth_annotation!( #hide
fig[1, 2]; #hide
color = bg_blue, #hide
xlims = [-2, 2], #hide
ylims = [-2, 2], #hide
offset_x = 0.1, #hide
offset_y = -0.35, #hide
radius_x = 0.38, #hide
radius_y = 1.1, #hide
num_waves = 5, #hide
fluctuation_amplitude = 0.02, #hide
phase_shift = 1.9) #hide
plot!(fig[1, 2], reduced, layout=Spring(iterations=1000, C=0.5, seed=100), node_color=[orange, orange, red, :black, :black, :black, :black, :black]) #hide
Label(fig[1, 1, Bottom()], "Original Tensor Network") #hide
Label(fig[1, 2, Bottom()], "Transformed Tensor Network") #hide
fig #hide
```

```@docs
Tenet.DiagonalReduction
Tenet.RankSimplification
```

## Rank reduction
```@docs
Tenet.RankReduction
```

```@example plot
fig = Figure() #hide
A = Tensor(rand(2, 2, 2, 2), (:i, :j, :k, :l)) #hide
B = Tensor(rand(2, 2), (:i, :m)) #hide
C = Tensor(rand(2, 2, 2), (:m, :n, :o)) #hide
E = Tensor(rand(2, 2, 2, 2), (:o, :p, :q, :j)) #hide
bg_blue = Makie.RGBAf(110 // 256, 170 // 256, 250 // 256, 50 // 256) #hide
orange = Makie.RGBf(240 // 256, 180 // 256, 100 // 256) #hide
red = Makie.RGBf(240 // 256, 90 // 256, 70 // 256) #hide
tn = TensorNetwork([A, B, C, E]) #hide
reduced = transform(tn, Tenet.RankSimplification) #hide
smooth_annotation!( #hide
fig[1, 1]; #hide
color = bg_blue, #hide
xlims = [-2, 2], #hide
ylims = [-2, 2], #hide
offset_x = -0.32, #hide
offset_y = -0.5, #hide
radius_x = 0.25, #hide
radius_y = 0.94, #hide
num_waves = 6, #hide
fluctuation_amplitude = 0.01, #hide
phase_shift = 0.0) #hide
plot!(fig[1, 1], tn, layout=Spring(iterations=1000, C=0.5, seed=20); node_color=[orange, red, orange, orange, :black, :black, :black, :black, :black]) #hide
smooth_annotation!( #hide
fig[1, 2]; #hide
color = bg_blue, #hide
xlims = [-2, 2], #hide
ylims = [-2, 2], #hide
offset_x = 0.12, #hide
offset_y = -0.62, #hide
radius_x = 0.18, #hide
radius_y = 0.46, #hide
num_waves = 5, #hide
fluctuation_amplitude = 0.01, #hide
phase_shift = 0) #hide
plot!(fig[1, 2], reduced, layout=Spring(iterations=1000, C=0.5, seed=1); node_color=[red, orange, orange, :black, :black, :black, :black, :black]) #hide
Label(fig[1, 1, Bottom()], "Original Tensor Network") #hide
Label(fig[1, 2, Bottom()], "Transformed Tensor Network") #hide
fig #hide
```

```@docs
Tenet.AntiDiagonalGauging
Tenet.ColumnReduction
Tenet.SplitSimplification
Expand Down

0 comments on commit 2739264

Please sign in to comment.