This repository has been archived by the owner on Nov 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquantics1d.jl
335 lines (276 loc) · 11.5 KB
/
quantics1d.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# -*- coding: utf-8 -*-
# ---
# jupyter:
# jupytext:
# custom_cell_magics: kql
# formats: ipynb,jl:percent
# text_representation:
# extension: .jl
# format_name: percent
# format_version: '1.3'
# jupytext_version: 1.16.4
# kernelspec:
# display_name: Julia 1.10.5
# language: julia
# name: julia-1.10
# ---
# %% [markdown]
# Click [here](https://tensor4all.org/T4AJuliaTutorials/_sources/ipynbs/quantics1d.ipynb) to download the notebook locally.
#
# %% [markdown]
# # Quantics TCI of univariate function
#
# %%
using Plots
gr() # Use GR backend for plotting
import QuanticsGrids as QG
using QuanticsTCI: quanticscrossinterpolate, integral
# defines mutable struct `SemiLogy` and sets shorthands `semilogy` and `semilogy!`
@userplot SemiLogy
@recipe function f(t::SemiLogy)
x = t.args[begin]
y = t.args[end]
ε = nextfloat(0.0)
yscale := :log10
# Warning: Invalid negative or zero value 0.0 found at series index 16 for log10 based yscale
# prevent log10(0) from being -Inf
(x, ε .+ y)
end
# %% [markdown]
# ## Example 1
#
# The first example is taken from Fig. 1 in [Ritter2024](https://arxiv.org/abs/2303.11819).
#
# We are going to compute the integral $\mathrm{I}[f] = \int_0^{\ln 20} \mathrm{d}x f(x) $ of the function
#
# $$
# f(x) = \cos\left(\frac{x}{B}\right) \cos\left(\frac{x}{4\sqrt{5}B}\right) e^{-x^2} + 2e^{-x},
# $$
#
# where $B = 2^{-30}$.
# The integral evaluates to $\mathrm{I}[f] = 19/10 + O(e^{-1/(4B^2)})$.
#
# We first construct a QTT representation of the function $f(x)$ as follows:
# %%
B = 2^(-30) # global variable
function f(x)
return cos(x / B) * cos(x / (4 * sqrt(5) * B)) * exp(-x^2) + 2 * exp(-x)
end
println(f(0.2))
# %% [markdown]
# Let's examine the behaviour of $f(x)$. This function involves structure on widely different scales: rapid, incommensurate oscillations and a slowly decaying envelope. We'll use [PythonPlot.jl](https://github.com/JuliaPy/PythonPlot.jl) visualisation library which uses Python library [matplotlib](https://matplotlib.org/) behind the scenes.
#
# For small $x$ we have:
#
# %%
xs = LinRange(0, 2.0^(-23), 1000)
plt = plot(title="$(nameof(f))")
plot!(plt, xs, f.(xs), label="$(nameof(f))", legend=true)
plt
# %% [markdown]
# For $x \in (0, 3]$ we will get:
#
# %%
xs2 = LinRange(2.0^(-23), 3, 100000)
plt = plot(title="$(nameof(f))")
plot!(plt, xs2, f.(xs2), label="$(nameof(f))", legend=true)
plt
# %% [markdown]
# ### QTT representation
#
# We construct a QTT representation of this function on the domain $[0, \ln 20]$, discretized on a quantics grid of size $2^\mathcal{R}$ with $\mathcal{R} = 40$ bits:
#
# %%
R = 40 # number of bits
xmin = 0.0
xmax = log(20.0)
N = 2^R # size of the grid
# * Uniform grid (includeendpoint=false, default):
# -xmin, -xmin+dx, ...., -xmin + (2^R-1)*dx
# where dx = (xmax - xmin)/2^R.
# Note that the grid does not include the end point xmin.
#
# * Uniform grid (includeendpoint=true):
# -xmin, -xmin+dx, ...., xmin-dx, xmin,
# where dx = (xmax - xmin)/(2^R-1).
qgrid = QG.DiscretizedGrid{1}(R, xmin, xmax; includeendpoint=true)
ci, ranks, errors = quanticscrossinterpolate(Float64, f, qgrid; maxbonddim=15)
# %%
length(ci.tci)
# %% [markdown]
# Here, we've created the object `ci` of type `QuanticsTensorCI2{Float64}`. This can be evaluated at an linear index $i$ ($1 \le i \le 2^\mathcal{R}$) as follows:
#
# %%
for i in [1, 2, 3, 2^R] # Linear indices
# restore original coordinate `x` from linear index `i`
x = QG.grididx_to_origcoord(qgrid, i)
println("x: $(x), i: $(i), tci: $(ci(i)), ref: $(f(x))")
end
# %% [markdown]
# We see that `ci(i)` approximates the original `f` at `x = QG.grididx_to_origcoord(qgrid, i)`. Let's plot them together.
#
# %%
maxindex = QG.origcoord_to_grididx(qgrid, 2.0^(-23))
testindices = Int.(round.(LinRange(1, maxindex, 1000)))
xs = [QG.grididx_to_origcoord(qgrid, i) for i in testindices]
ys = f.(xs)
yci = ci.(testindices)
plt = plot(title="$(nameof(f)) and TCI", xlabel="x", ylabel="y")
plot!(plt, xs, ys, label="$(nameof(f))", legend=true)
plot!(plt, xs, yci, label="tci", linestyle=:dash, alpha=0.7, legend=true)
plt
# %% [markdown]
# Above, one can see that the original function is interpolated very accurately.
#
# Let's plot of $x$ vs interpolation error $|f(x) - \mathrm{ci}(x)|$ for small $x$
#
# %%
ys = f.(xs)
yci = ci.(testindices)
plt = plot(title="x vs interpolation error: $(nameof(f))", xlabel="x", ylabel="interpolation error")
semilogy!(xs, abs.(ys .- yci), label="log(|f(x) - ci(x)|)", yscale=:log10, legend=:bottomright, ylim=(1e-16, 1e-7), yticks=10.0 .^ collect(-16:1:-7))
plt
# %% [markdown]
# ... and for all $x$:
# %%
plt = plot(title="x vs interpolation error: $(nameof(f))", xlabel="x", ylabel="interpolation error")
testindices = Int.(round.(LinRange(1, 2^R, 1000)))
xs = [QG.grididx_to_origcoord(qgrid, i) for i in testindices]
ys = f.(xs)
yci = ci.(testindices)
semilogy!(xs, abs.(ys .- yci), label="log(|f(x) - ci(x)|)", legend=true, ylim=(1e-16, 1e-6), yticks=10.0 .^ collect(-16:1:-6))
plt
# %% [markdown]
# The function is approximated with an accuracy $\approx 10^{-7}$ over the entire domain.
#
# We are now ready to compute the integral $\mathrm{I}[f] = \int_0^{\ln 20} \mathrm{d}x f(x) \simeq 19/10$ using the QTT representation of $f(x)$.
# %%
integral(ci), 19 / 10
# %% [markdown]
# `integral(ci)` is equivalent to calling `QuanticsTCI.sum(ci)` and multiplying the result by the interval length divided by $2^\mathcal{R}$.
# %%
sum(ci) * (log(20) - 0) / 2^R, 19 / 10
# %% [markdown]
# ### About `ci::QuanticsTensorCI2{Float64}`
#
# Let's dive into the `ci` object:
#
# %%
println(typeof(ci))
# %% [markdown]
# As we've seen before, `ci` is an object of `QuanticsTensorCI2{Float64}` in `QuanticsTCI.jl`, which is a thin wrapper of `TensorCI2{Float64}` in `TensorCrossInterpolation.jl`.
# The undering object of `TensorCI2{Float64}` type can be accessed as `ci.tci`. This will be useful for obtaining more detailed information on the TCI results.
#
# For instance, `ci.tci.maxsamplevalue` is an estimate of the abosolute maximum value of the function, and `ci.tci.pivoterrors` stores the error as function of the bond dimension computed by prrLU.
# In the following figure, we plot the normalized error vs. bond dimension, showing an exponential decay.
#
# %%
# Plot error vs bond dimension obtained by prrLU
plt = plot(title="normalized error vs. bond dimension: $(nameof(f))", xlabel="Bond dimension", ylabel="Normalization error")
semilogy!(1:length(ci.tci.pivoterrors), ci.tci.pivoterrors ./ ci.tci.maxsamplevalue, marker=:x, ylim=(1e-8, 10), yticks=(10.0 .^ (-10:1:0)), legend=false)
plt
# %% [markdown]
# ### Function evaluations
#
# Our TCI algorithm does not call elements of the entire tensor, but constructs the TT (Tensor Train) from some elements chosen adaptively. On which points $x \in [0, 3]$ was the function evaluated to construct a QTT representation of the function $f(x)$? Let's find out. One can retrieve the information on the function evaluations as follows.
#
# %%
import QuanticsTCI
# Dict{Float64,Float64}
# key: `x`
# value: function value at `x`
evaluated = QuanticsTCI.cachedata(ci)
# %% [markdown]
# Let's plot `f` and the evaluated points together.
#
# %%
f̂(x) = ci(QG.origcoord_to_quantics(qgrid, x))
xs = LinRange(0, 2.0^(-23), 1000)
xs_evaluated = collect(keys(evaluated))
fs_evaluated = [evaluated[x] for x in xs_evaluated]
plt = plot(title="$(nameof(f)) and TCI", xlabel="x", ylabel="y", xlim=(0, maximum(xs)))
plot!(plt, xs, f.(xs), label="$(nameof(f))")
scatter!(plt, xs_evaluated, fs_evaluated, marker=:x, label="evaluated points")
plt
# %% [markdown]
# ## Example 2
#
# We now consider the function:
#
# $$
# \newcommand{\sinc}{\mathrm{sinc}}
# \begin{align}
# f(x) &= \sinc(x)+3e^{-0.3(x-4)^2}\sinc(x-4) \nonumber\\
# &\quad - \cos(4x)^2-2\sinc(x+10)e^{-0.6(x+9)} + 4 \cos(2x) e^{-|x+5|}\nonumber \\
# &\quad +\frac{6}{x-11}+ \sqrt{(|x|)}\arctan(x/15).\nonumber
# \end{align}
# $$
#
# One can construct a QTT representation of this function on the domain $[-10, 10)$ using a quantics grid of size $2^\mathcal{R}$ ($\mathcal{R}=20$):
#
# %%
import QuanticsGrids as QG
using QuanticsTCI
R = 20 # number of bits
N = 2^R # size of the grid
qgrid = QG.DiscretizedGrid{1}(R, -10, 10; includeendpoint=false)
# Function of interest
function oscillation_fn(x)
return (
sinc(x) + 3 * exp(-0.3 * (x - 4)^2) * sinc(x - 4) - cos(4 * x)^2 -
2 * sinc(x + 10) * exp(-0.6 * (x + 9)) + 4 * cos(2 * x) * exp(-abs(x + 5)) +
6 * 1 / (x - 11) + sqrt(abs(x)) * atan(x / 15))
end
# Convert to quantics format and sweep
ci, ranks, errors = quanticscrossinterpolate(Float64, oscillation_fn, qgrid; maxbonddim=15)
# %%
for i in [1, 2, 2^R] # Linear indices
x = QG.grididx_to_origcoord(qgrid, i)
println("x: $(x), tci: $(ci(i)), ref: $(oscillation_fn(x))")
end
# %% [markdown]
# Above, one can see that the original function is interpolated very accurately. The function `grididx_to_origcoord` transforms a linear index to a coordinate point $x$ in the original domain ($-10 \le x < 10$).
#
# In the following figure, we plot the normalized error vs. bond dimension, showing an exponential decay.
#
# %%
# Plot error vs bond dimension obtained by prrLU
plt = plot(xlabel="Bond dimension", ylabel="Normalization error", title="normalized error vs. bond dimension")
semilogy!(1:length(ci.tci.pivoterrors), ci.tci.pivoterrors ./ ci.tci.maxsamplevalue, marker=:x, ylim=(1e-8, 10), yticks=(10.0 .^ (-10:1:0)), legend=false)
plt
# %% [markdown]
# ## Example 3
#
# ### Control the error of the TCI by a tolerance
#
# We interpolate the same function as in Example 2, but this time we use a tolerance to control the error of the TCI. The tolerance is a positive number that determines the maximum error of the TCI, which is scaled by an estimate of the abosolute maximum of the function.
# The TCI algorithm will adaptively increase the bond dimension until the error is below the tolerance.
# %%
tol = 1e-8 # Tolerance for the error
# Convert to quantics format and sweep
ci_tol, ranks_tol, errors_tol = quanticscrossinterpolate(
Float64, oscillation_fn, qgrid;
tolerance=tol,
normalizeerror=true, # Normalize the error by the maximum sample value,
verbosity=1, loginterval=1, # Log the error every `loginterval` iterations
)
# %%
println("Max abs sampled value is $(ci_tol.tci.maxsamplevalue)")
# %%
errors_tol ./ ci_tol.tci.maxsamplevalue
# %% [markdown]
# ### Estimate the error of the TCI
# Wait!
# Since we did not sample the function over the entire domain, we do not know the true error of the TCI.
# In theory, we can estimate the error of the TCI by comparing the function values at the sampled points with the TCI values at the same points.
# But, it is not practical to compare the function values with the TCI values at all points in the domain.
# The function `estimatetrueerror` in `TensorCrossInterpolation.jl` provides a good estimate of the error of the TCI.
# The algorithm finds indices (points) where the error is large by a randomized global search algorithm starting with a set of random initial points.
# %%
import TensorCrossInterpolation as TCI
pivoterror_global = TCI.estimatetrueerror(TCI.TensorTrain(ci.tci), ci.quanticsfunction; nsearch=100) # Results are sorted in descending order of the error
# %% [markdown]
# Now, you can see the error estimate of the TCI is below the tolerance of $10^{-8}$ (or close to it).
# %%
println("The largest error found is $(pivoterror_global[1][2]) and the corresponding pivot is $(pivoterror_global[1][1]).")
println("The tolerance used is $(tol * ci_tol.tci.maxsamplevalue).")