Skip to content

Commit 7cd8e0c

Browse files
committed
change Settings to mutable struct
1 parent 0a1be54 commit 7cd8e0c

File tree

4 files changed

+20
-20
lines changed

4 files changed

+20
-20
lines changed

docs/src/users_guide/settings.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ QuantumToolbox.settings
2929
Next, one can overwrite the default settings by
3030

3131
```@example settings
32-
QuantumToolbox.settings.tidyup_tol[] = 1e-10
33-
QuantumToolbox.settings.auto_tidyup[] = false
32+
QuantumToolbox.settings.tidyup_tol = 1e-10
33+
QuantumToolbox.settings.auto_tidyup = false
3434
3535
QuantumToolbox.settings
3636
```

src/qobj/arithmetic_and_attributes.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -629,28 +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=settings.tidyup_tol[])
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 = settings.tidyup_tol[]) where {T<:Real} =
636+
tidyup(A::QuantumObject, tol::T = settings.tidyup_tol) where {T<:Real} =
637637
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)
638+
tidyup(A::AbstractArray, tol::T2 = settings.tidyup_tol) where {T2<:Real} = tidyup!(copy(A), tol)
639639

640640
@doc raw"""
641-
tidyup!(A::QuantumObject, tol::Real=settings.tidyup_tol[])
641+
tidyup!(A::QuantumObject, tol::Real=settings.tidyup_tol)
642642
643643
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`.
644644
645645
Note that this function is an in-place version of [`tidyup`](@ref).
646646
"""
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}
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}
649649
tidyup!(nonzeros(A), tol) # tidyup A.nzval in-place (also support for CUDA sparse arrays)
650650
return dropzeros!(A)
651651
end
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} =
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} =
654654
@. A = T(abs(real(A)) > tol) * real(A) + 1im * T(abs(imag(A)) > tol) * imag(A)
655655

656656
@doc raw"""

src/qobj/eigsolve.jl

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

254254
return EigsolveResult(vals, vecs, type, dimensions, iter, numops, (iter < maxiter))
255255
end
@@ -351,7 +351,7 @@ function eigsolve(
351351
end
352352

353353
vecs = res.vectors
354-
settings.auto_tidyup[] && tidyup!(vecs)
354+
settings.auto_tidyup && tidyup!(vecs)
355355

356356
return EigsolveResult(vals, vecs, res.type, res.dimensions, res.iter, res.numops, res.converged)
357357
end
@@ -434,7 +434,7 @@ function eigsolve_al(
434434
@. vecs[:, i] = vec * exp(-1im * angle(vec[1]))
435435
end
436436

437-
settings.auto_tidyup[] && tidyup!(vecs)
437+
settings.auto_tidyup && tidyup!(vecs)
438438

439439
return EigsolveResult(vals, vecs, res.type, res.dimensions, res.iter, res.numops, res.converged)
440440
end
@@ -479,7 +479,7 @@ function LinearAlgebra.eigen(A::QuantumObject{OpType}; kwargs...) where {OpType<
479479
# This fixes a type inference issue. But doesn't work for GPU arrays
480480
E::mat2vec(to_dense(MT)) = F.values
481481
U::to_dense(MT) = F.vectors
482-
settings.auto_tidyup[] && tidyup!(U)
482+
settings.auto_tidyup && tidyup!(U)
483483

484484
return EigsolveResult(E, U, A.type, A.dimensions, 0, 0, true)
485485
end

src/settings.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
Base.@kwdef struct Settings
2-
tidyup_tol::RefValue{Float64} = 1e-14
3-
auto_tidyup::RefValue{Bool} = true
1+
Base.@kwdef mutable struct Settings
2+
tidyup_tol::Float64 = 1e-14
3+
auto_tidyup::Bool = true
44
end
55

66
function Base.show(io::IO, s::Settings)
77
println(io, "QuantumToolbox.jl Settings")
88
println(io, "--------------------------")
9-
map(x -> println(io, "$x[] = ", getfield(s, x)[]), fieldnames(Settings))
9+
map(x -> println(io, "$x = ", getfield(s, x)), fieldnames(Settings))
1010
return nothing
1111
end
1212

@@ -29,8 +29,8 @@ One can overwrite the default global settings by
2929
```julia
3030
using QuantumToolbox
3131
32-
QuantumToolbox.settings.tidyup_tol[] = 1e-10
33-
QuantumToolbox.settings.auto_tidyup[] = false
32+
QuantumToolbox.settings.tidyup_tol = 1e-10
33+
QuantumToolbox.settings.auto_tidyup = false
3434
```
3535
"""
3636
const settings = Settings()

0 commit comments

Comments
 (0)