Skip to content

Commit e2b34f1

Browse files
authored
Introduce QuantumToolbox.settings and auto_tidyup (#460)
1 parent 243bff7 commit e2b34f1

File tree

8 files changed

+99
-13
lines changed

8 files changed

+99
-13
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased](https://github.com/qutip/QuantumToolbox.jl/tree/main)
99

10+
- Introduce `QuantumToolbox.settings` and `auto_tidyup`. ([#460])
11+
1012
## [v0.31.0]
1113
Release date: 2025-05-03
1214

@@ -221,3 +223,4 @@ Release date: 2024-11-13
221223
[#453]: https://github.com/qutip/QuantumToolbox.jl/issues/453
222224
[#455]: https://github.com/qutip/QuantumToolbox.jl/issues/455
223225
[#456]: https://github.com/qutip/QuantumToolbox.jl/issues/456
226+
[#460]: https://github.com/qutip/QuantumToolbox.jl/issues/460

docs/make.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ const PAGES = [
6464
"Hierarchical Equations of Motion" => "users_guide/HEOM.md",
6565
"Solving for Steady-State Solutions" => "users_guide/steadystate.md",
6666
"Two-time correlation functions" => "users_guide/two_time_corr_func.md",
67+
"QuantumToolbox Settings" => "users_guide/settings.md",
6768
"Extensions" => [
6869
"users_guide/extensions/cuda.md",
6970
"users_guide/extensions/cairomakie.md",

docs/src/resources/api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ AbstractLinearMap
299299
## [Utility functions](@id doc-API:Utility-functions)
300300

301301
```@docs
302+
QuantumToolbox.settings
302303
QuantumToolbox.versioninfo
303304
QuantumToolbox.about
304305
gaussian

docs/src/users_guide/settings.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# [QuantumToolbox Settings](@id doc:QuantumToolbox-Settings)
2+
3+
In this section, we introduce the default global settings used throughout the package and show how to modify them.
4+
5+
All settings are stored in [`QuantumToolbox.settings`](@ref).
6+
7+
!!! warning "Differences from QuTiP"
8+
Due to the differences in programming languages, solving algorithms, and many other reasons, these global settings (including their default values and usage) may be very different from those in `Python QuTiP`.
9+
10+
## List of settings
11+
12+
Here, we list out each setting along with the specific functions that will use it.
13+
14+
- `tidyup_tol::Float64 = 1e-14` : tolerance for [`tidyup`](@ref) and [`tidyup!`](@ref).
15+
- `auto_tidyup::Bool = true` : Automatically tidyup during the following situations:
16+
* Solving for eigenstates, including [`eigenstates`](@ref), [`eigsolve`](@ref), and [`eigsolve_al`](@ref).
17+
- (to be announced)
18+
19+
## Change default settings
20+
21+
First, we can check the current [`QuantumToolbox.settings`](@ref):
22+
23+
```@example settings
24+
using QuantumToolbox
25+
26+
QuantumToolbox.settings
27+
```
28+
29+
Next, one can overwrite the default settings by
30+
31+
```@example settings
32+
QuantumToolbox.settings.tidyup_tol = 1e-10
33+
QuantumToolbox.settings.auto_tidyup = false
34+
35+
QuantumToolbox.settings
36+
```

src/QuantumToolbox.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ export permute
7575
export cache_operator, iscached, isconstant
7676

7777
# Utility
78+
include("settings.jl")
7879
include("utilities.jl")
7980
include("versioninfo.jl")
8081
include("progress_bar.jl")

src/qobj/arithmetic_and_attributes.jl

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -629,27 +629,28 @@ purity(ρ::QuantumObject{ObjType}) where {ObjType<:Union{Ket,Bra}} = sum(abs2,
629629
purity::QuantumObject{Operator}) = real(tr.data^2))
630630

631631
@doc raw"""
632-
tidyup(A::QuantumObject, tol::Real=1e-14)
632+
tidyup(A::QuantumObject, tol::Real=settings.tidyup_tol)
633633
634634
Given a [`QuantumObject`](@ref) `A`, check the real and imaginary parts of each element separately. Remove the real or imaginary value if its absolute value is less than `tol`.
635635
"""
636-
tidyup(A::QuantumObject, tol::T = 1e-14) where {T<:Real} = QuantumObject(tidyup(A.data, tol), A.type, A.dimensions)
637-
tidyup(A::AbstractArray, tol::T2 = 1e-14) where {T2<:Real} = tidyup!(copy(A), tol)
636+
tidyup(A::QuantumObject, tol::T = settings.tidyup_tol) where {T<:Real} =
637+
QuantumObject(tidyup(A.data, tol), A.type, A.dimensions)
638+
tidyup(A::AbstractArray, tol::T2 = settings.tidyup_tol) where {T2<:Real} = tidyup!(copy(A), tol)
638639

639640
@doc raw"""
640-
tidyup!(A::QuantumObject, tol::Real=1e-14)
641+
tidyup!(A::QuantumObject, tol::Real=settings.tidyup_tol)
641642
642643
Given a [`QuantumObject`](@ref) `A`, check the real and imaginary parts of each element separately. Remove the real or imaginary value if its absolute value is less than `tol`.
643644
644645
Note that this function is an in-place version of [`tidyup`](@ref).
645646
"""
646-
tidyup!(A::QuantumObject, tol::T = 1e-14) where {T<:Real} = (tidyup!(A.data, tol); A)
647-
function tidyup!(A::AbstractSparseArray, tol::T2 = 1e-14) where {T2<:Real}
647+
tidyup!(A::QuantumObject, tol::T = settings.tidyup_tol) where {T<:Real} = (tidyup!(A.data, tol); A)
648+
function tidyup!(A::AbstractSparseArray, tol::T2 = settings.tidyup_tol) where {T2<:Real}
648649
tidyup!(nonzeros(A), tol) # tidyup A.nzval in-place (also support for CUDA sparse arrays)
649650
return dropzeros!(A)
650651
end
651-
tidyup!(A::AbstractArray{T}, tol::T2 = 1e-14) where {T<:Real,T2<:Real} = @. A = T(abs(A) > tol) * A
652-
tidyup!(A::AbstractArray{T}, tol::T2 = 1e-14) where {T,T2<:Real} =
652+
tidyup!(A::AbstractArray{T}, tol::T2 = settings.tidyup_tol) where {T<:Real,T2<:Real} = @. A = T(abs(A) > tol) * A
653+
tidyup!(A::AbstractArray{T}, tol::T2 = settings.tidyup_tol) where {T,T2<:Real} =
653654
@. A = T(abs(real(A)) > tol) * real(A) + 1im * T(abs(imag(A)) > tol) * imag(A)
654655

655656
@doc raw"""

src/qobj/eigsolve.jl

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ function _eigsolve(
249249
end
250250
mul!(cache1, Vₘ, M(Uₘ * VR))
251251
vecs = cache1[:, 1:k]
252+
settings.auto_tidyup && tidyup!(vecs)
252253

253254
return EigsolveResult(vals, vecs, type, dimensions, iter, numops, (iter < maxiter))
254255
end
@@ -349,7 +350,10 @@ function eigsolve(
349350
vals = @. (1 + sigma * res.values) / res.values
350351
end
351352

352-
return EigsolveResult(vals, res.vectors, res.type, res.dimensions, res.iter, res.numops, res.converged)
353+
vecs = res.vectors
354+
settings.auto_tidyup && tidyup!(vecs)
355+
356+
return EigsolveResult(vals, vecs, res.type, res.dimensions, res.iter, res.numops, res.converged)
353357
end
354358

355359
@doc raw"""
@@ -430,6 +434,8 @@ function eigsolve_al(
430434
@. vecs[:, i] = vec * exp(-1im * angle(vec[1]))
431435
end
432436

437+
settings.auto_tidyup && tidyup!(vecs)
438+
433439
return EigsolveResult(vals, vecs, res.type, res.dimensions, res.iter, res.numops, res.converged)
434440
end
435441

@@ -457,11 +463,11 @@ values:
457463
2.8569700138728056 + 0.0im
458464
vectors:
459465
5×5 Matrix{ComplexF64}:
460-
0.106101+0.0im -0.471249-0.0im … 0.471249-0.0im 0.106101-0.0im
461-
-0.303127-0.0im 0.638838+0.0im 0.638838+0.0im 0.303127-0.0im
462-
0.537348+0.0im -0.279149-0.0im 0.279149-0.0im 0.537348-0.0im
466+
0.106101+0.0im -0.471249-0.0im … 0.471249+0.0im 0.106101+0.0im
467+
-0.303127-0.0im 0.638838+0.0im 0.638838+0.0im 0.303127+0.0im
468+
0.537348+0.0im -0.279149-0.0im 0.279149+0.0im 0.537348+0.0im
463469
-0.638838-0.0im -0.303127-0.0im -0.303127-0.0im 0.638838+0.0im
464-
0.447214+0.0im 0.447214+0.0im -0.447214-0.0im 0.447214-0.0im
470+
0.447214+0.0im 0.447214+0.0im -0.447214-0.0im 0.447214+0.0im
465471
466472
julia> expect(H, ψ[1]) ≈ E[1]
467473
true
@@ -473,6 +479,7 @@ function LinearAlgebra.eigen(A::QuantumObject{OpType}; kwargs...) where {OpType<
473479
# This fixes a type inference issue. But doesn't work for GPU arrays
474480
E::mat2vec(to_dense(MT)) = F.values
475481
U::to_dense(MT) = F.vectors
482+
settings.auto_tidyup && tidyup!(U)
476483

477484
return EigsolveResult(E, U, A.type, A.dimensions, 0, 0, true)
478485
end

src/settings.jl

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
Base.@kwdef mutable struct Settings
2+
tidyup_tol::Float64 = 1e-14
3+
auto_tidyup::Bool = true
4+
end
5+
6+
function Base.show(io::IO, s::Settings)
7+
println(io, "QuantumToolbox.jl Settings")
8+
println(io, "--------------------------")
9+
map(x -> println(io, "$x = ", getfield(s, x)), fieldnames(Settings))
10+
return nothing
11+
end
12+
13+
@doc raw"""
14+
QuantumToolbox.settings
15+
16+
Contains all the default global settings of QuantumToolbox.jl.
17+
18+
# List of settings
19+
20+
- `tidyup_tol::Float64 = 1e-14` : tolerance for [`tidyup`](@ref) and [`tidyup!`](@ref).
21+
- `auto_tidyup::Bool = true` : Automatically tidyup.
22+
23+
For detailed explanation of each settings, see our documentation [here](https://qutip.org/QuantumToolbox.jl/stable/users_guide/settings).
24+
25+
# Change default settings
26+
27+
One can overwrite the default global settings by
28+
29+
```julia
30+
using QuantumToolbox
31+
32+
QuantumToolbox.settings.tidyup_tol = 1e-10
33+
QuantumToolbox.settings.auto_tidyup = false
34+
```
35+
"""
36+
const settings = Settings()

0 commit comments

Comments
 (0)