Skip to content

Commit 26c22c4

Browse files
Remove Reexport and manually export the functions
1 parent bd61911 commit 26c22c4

File tree

16 files changed

+80
-71
lines changed

16 files changed

+80
-71
lines changed

Project.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ OrdinaryDiffEqCore = "bbf590c4-e513-4bbe-9b18-05decba2e5d8"
1818
OrdinaryDiffEqTsit5 = "b1df2697-797e-41e3-8120-5422d3b24e4a"
1919
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
2020
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
21-
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
2221
SciMLBase = "0bca4576-84f4-4d90-8ffe-ffa030f20462"
2322
SciMLOperators = "c0aeaf25-5076-4817-a8d5-81caf7dfa961"
2423
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
@@ -56,7 +55,6 @@ OrdinaryDiffEqCore = "1"
5655
OrdinaryDiffEqTsit5 = "1"
5756
Pkg = "1"
5857
Random = "1"
59-
Reexport = "1"
6058
SciMLBase = "2"
6159
SciMLOperators = "0.3"
6260
SparseArrays = "1"

docs/src/getting_started/type_stability.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ Which returns a tensor of size `2x2x2x2x2x2`. Let's check the `@code_warntype`:
199199
@code_warntype reshape_operator_data([2, 2, 2])
200200
```
201201

202-
We got a `Any` type, because the compiler doesn't know the size of the `dims` vector. We can fix this by using a `Tuple` (or `SVector`):
202+
We got a `Any` type, because the compiler doesn't know the size of the `dims` vector. We can fix this by using a `Tuple` (or `SVector` from [StaticArrays.jl](https://github.com/JuliaArrays/StaticArrays.jl)):
203203

204204
```@example type-stability
205205
typeof(reshape_operator_data((2, 2, 2)))
@@ -219,7 +219,7 @@ Finally, let's look at the benchmarks
219219
@benchmark reshape_operator_data($((2, 2, 2)))
220220
```
221221

222-
Which is an innocuous but huge difference in terms of performance. Hence, we highly recommend using `Tuple` or `SVector` when defining the dimensions of a user-defined [`QuantumObject`](@ref).
222+
Which is an innocuous but huge difference in terms of performance. Hence, we highly recommend using `Tuple` or `SVector` from [StaticArrays.jl](https://github.com/JuliaArrays/StaticArrays.jl) when defining the dimensions of a user-defined [`QuantumObject`](@ref).
223223

224224
## The use of `Val` in some `QuantumToolbox.jl` functions
225225

docs/src/users_guide/QuantumObject/QuantumObject.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ Qobj(rand(4, 4))
4545
M = rand(ComplexF64, 4, 4)
4646
Qobj(M, dims = [2, 2]) # dims as Vector
4747
Qobj(M, dims = (2, 2)) # dims as Tuple (recommended)
48+
49+
import QuantumObject: SVector # or using StaticArrays
4850
Qobj(M, dims = SVector(2, 2)) # dims as StaticArrays.SVector (recommended)
4951
```
5052

docs/src/users_guide/tensor.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ H = 0.5 * ωa * σz + ωc * a' * a + g * (a' * σm + a * σm')
108108

109109
The partial trace is an operation that reduces the dimension of a Hilbert space by eliminating some degrees of freedom by averaging (tracing). In this sense it is therefore the converse of the tensor product. It is useful when one is interested in only a part of a coupled quantum system. For open quantum systems, this typically involves tracing over the environment leaving only the system of interest. In `QuantumToolbox` the function [`ptrace`](@ref) is used to take partial traces. [`ptrace`](@ref) takes one [`QuantumObject`](@ref) as an input, and also one argument `sel`, which marks the component systems that should be kept, and all the other components are traced out.
110110

111-
Remember that the index of `Julia` starts from `1`, and all the elements in `sel` should be positive `Integer`. Therefore, the type of `sel` can be either `Integer`, `Tuple`, `SVector`, or `Vector`.
111+
Remember that the index of `Julia` starts from `1`, and all the elements in `sel` should be positive `Integer`. Therefore, the type of `sel` can be either `Integer`, `Tuple`, `SVector` ([StaticArrays.jl](https://github.com/JuliaArrays/StaticArrays.jl)), or `Vector`.
112112

113113
!!! warning "Beware of type-stability!"
114114
Although it supports also `Vector` type, it is recommended to use `Tuple` or `SVector` from [`StaticArrays.jl`](https://github.com/JuliaArrays/StaticArrays.jl) to improve performance. For a brief explanation on the impact of the type of `sel`, see the section [The Importance of Type-Stability](@ref doc:Type-Stability).

src/QuantumToolbox.jl

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
module QuantumToolbox
22

3-
# Re-export:
4-
# 1. StaticArraysCore.SVector for the type of dims
5-
# 2. basic functions in LinearAlgebra and SparseArrays
6-
# 3. some functions in SciMLOperators
7-
import Reexport: @reexport
8-
import StaticArraysCore: SVector
3+
"The argument sel should be a Tuple or a StaticVector for better performance. Try to use `sel = (1, 2)` instead of `sel = [1, 2]`. Alternatively, you can do `import QuantumToolbox: SVector` and use `dims = SVector(1, 2)`."
4+
5+
"The argument sel should be a Tuple or a StaticVector for better performance. Try to use `sel = (1, 2)` instead of `sel = [1, 2]`. Alternatively, you can do `import QuantumToolbox: SVector` and use `sel = SVector(1, 2)`."
6+
97
using LinearAlgebra
108
using SparseArrays
9+
import StaticArraysCore: SVector
1110
import SciMLOperators: cache_operator, iscached, isconstant
1211

1312
# other functions in LinearAlgebra
@@ -69,9 +68,30 @@ import Random: AbstractRNG, default_rng, seed!
6968
import SpecialFunctions: loggamma
7069
import StaticArraysCore: MVector
7170

72-
# Setting the number of threads to 1 allows
73-
# to achieve better performances for more massive parallelizations
74-
BLAS.set_num_threads(1)
71+
# Export functions from the other modules
72+
73+
# LinearAlgebra
74+
export ishermitian,
75+
issymmetric,
76+
isposdef,
77+
adjoint,
78+
transpose,
79+
dot,
80+
tr,
81+
svdvals,
82+
norm,
83+
normalize,
84+
normalize!,
85+
diag,
86+
kron,
87+
Hermitian,
88+
Symmetric
89+
90+
# SparseArrays
91+
export permute
92+
93+
# SciMLOperators
94+
export cache_operator, iscached, isconstant
7595

7696
# Utility
7797
include("utilities.jl")

src/qobj/arithmetic_and_attributes.jl

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,11 @@ for ADimType in (:Dimensions, :GeneralDimensions)
9191
end
9292
end
9393

94-
function Base.:(*)(
95-
A::AbstractQuantumObject{OperatorQuantumObject},
96-
B::QuantumObject{KetQuantumObject,<:Dimensions},
97-
)
94+
function Base.:(*)(A::AbstractQuantumObject{OperatorQuantumObject}, B::QuantumObject{KetQuantumObject,<:Dimensions})
9895
check_mul_dimensions(get_dimensions_from(A), get_dimensions_to(B))
9996
return QuantumObject(A.data * B.data, Ket, Dimensions(get_dimensions_to(A)))
10097
end
101-
function Base.:(*)(
102-
A::QuantumObject{BraQuantumObject,<:Dimensions},
103-
B::AbstractQuantumObject{OperatorQuantumObject},
104-
)
98+
function Base.:(*)(A::QuantumObject{BraQuantumObject,<:Dimensions}, B::AbstractQuantumObject{OperatorQuantumObject})
10599
check_mul_dimensions(get_dimensions_from(A), get_dimensions_to(B))
106100
return QuantumObject(A.data * B.data, Bra, Dimensions(get_dimensions_from(B)))
107101
end
@@ -113,35 +107,25 @@ function Base.:(*)(A::QuantumObject{BraQuantumObject}, B::QuantumObject{KetQuant
113107
check_dimensions(A, B)
114108
return A.data * B.data
115109
end
116-
function Base.:(*)(
117-
A::AbstractQuantumObject{SuperOperatorQuantumObject},
118-
B::QuantumObject{OperatorQuantumObject},
119-
)
110+
function Base.:(*)(A::AbstractQuantumObject{SuperOperatorQuantumObject}, B::QuantumObject{OperatorQuantumObject})
120111
check_dimensions(A, B)
121112
return QuantumObject(vec2mat(A.data * mat2vec(B.data)), Operator, A.dimensions)
122113
end
123114
function Base.:(*)(A::QuantumObject{OperatorBraQuantumObject}, B::QuantumObject{OperatorKetQuantumObject})
124115
check_dimensions(A, B)
125116
return A.data * B.data
126117
end
127-
function Base.:(*)(
128-
A::AbstractQuantumObject{SuperOperatorQuantumObject},
129-
B::QuantumObject{OperatorKetQuantumObject},
130-
)
118+
function Base.:(*)(A::AbstractQuantumObject{SuperOperatorQuantumObject}, B::QuantumObject{OperatorKetQuantumObject})
131119
check_dimensions(A, B)
132120
return QuantumObject(A.data * B.data, OperatorKet, A.dimensions)
133121
end
134-
function Base.:(*)(
135-
A::QuantumObject{OperatorBraQuantumObject},
136-
B::AbstractQuantumObject{SuperOperatorQuantumObject},
137-
)
122+
function Base.:(*)(A::QuantumObject{OperatorBraQuantumObject}, B::AbstractQuantumObject{SuperOperatorQuantumObject})
138123
check_dimensions(A, B)
139124
return QuantumObject(A.data * B.data, OperatorBra, A.dimensions)
140125
end
141126

142127
Base.:(^)(A::QuantumObject, n::T) where {T<:Number} = QuantumObject(^(A.data, n), A.type, A.dimensions)
143-
Base.:(/)(A::AbstractQuantumObject, n::T) where {T<:Number} =
144-
get_typename_wrapper(A)(A.data / n, A.type, A.dimensions)
128+
Base.:(/)(A::AbstractQuantumObject, n::T) where {T<:Number} = get_typename_wrapper(A)(A.data / n, A.type, A.dimensions)
145129

146130
@doc raw"""
147131
A ⋅ B
@@ -236,9 +220,7 @@ Lazy adjoint (conjugate transposition) of the [`AbstractQuantumObject`](@ref)
236220
!!! note
237221
`A'` and `dag(A)` are synonyms of `adjoint(A)`.
238222
"""
239-
Base.adjoint(
240-
A::AbstractQuantumObject{OpType},
241-
) where {OpType<:Union{OperatorQuantumObject,SuperOperatorQuantumObject}} =
223+
Base.adjoint(A::AbstractQuantumObject{OpType}) where {OpType<:Union{OperatorQuantumObject,SuperOperatorQuantumObject}} =
242224
get_typename_wrapper(A)(adjoint(A.data), A.type, adjoint(A.dimensions))
243225
Base.adjoint(A::QuantumObject{KetQuantumObject}) = QuantumObject(adjoint(A.data), Bra, adjoint(A.dimensions))
244226
Base.adjoint(A::QuantumObject{BraQuantumObject}) = QuantumObject(adjoint(A.data), Ket, adjoint(A.dimensions))
@@ -768,7 +750,7 @@ true
768750
```
769751
770752
!!! warning "Beware of type-stability!"
771-
It is highly recommended to use `permute(A, order)` with `order` as `Tuple` or `SVector` to keep type stability. See the [related Section](@ref doc:Type-Stability) about type stability for more details.
753+
It is highly recommended to use `permute(A, order)` with `order` as `Tuple` or `SVector` from [StaticArrays.jl](https://github.com/JuliaArrays/StaticArrays.jl) to keep type stability. See the [related Section](@ref doc:Type-Stability) about type stability for more details.
772754
"""
773755
function SparseArrays.permute(
774756
A::QuantumObject{ObjType},

src/qobj/operators.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ The `distribution` specifies which of the method used to obtain the unitary matr
2929
1. [F. Mezzadri, How to generate random matrices from the classical compact groups, arXiv:math-ph/0609050 (2007)](https://arxiv.org/abs/math-ph/0609050)
3030
3131
!!! warning "Beware of type-stability!"
32-
If you want to keep type stability, it is recommended to use `rand_unitary(dimensions, Val(distribution))` instead of `rand_unitary(dimensions, distribution)`. Also, put `dimensions` as `Tuple` or `SVector`. See [this link](https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-value-type) and the [related Section](@ref doc:Type-Stability) about type stability for more details.
32+
If you want to keep type stability, it is recommended to use `rand_unitary(dimensions, Val(distribution))` instead of `rand_unitary(dimensions, distribution)`. Also, put `dimensions` as `Tuple` or `SVector` from [StaticArrays.jl](https://github.com/JuliaArrays/StaticArrays.jl). See [this link](https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-value-type) and the [related Section](@ref doc:Type-Stability) about type stability for more details.
3333
"""
3434
rand_unitary(dimensions::Int, distribution::Union{Symbol,Val} = Val(:haar)) =
3535
rand_unitary(SVector(dimensions), makeVal(distribution))
@@ -553,7 +553,7 @@ The `dimensions` can be either the following types:
553553
where ``\omega = \exp(\frac{2 \pi i}{N})``.
554554
555555
!!! warning "Beware of type-stability!"
556-
It is highly recommended to use `qft(dimensions)` with `dimensions` as `Tuple` or `SVector` to keep type stability. See the [related Section](@ref doc:Type-Stability) about type stability for more details.
556+
It is highly recommended to use `qft(dimensions)` with `dimensions` as `Tuple` or `SVector` from [StaticArrays.jl](https://github.com/JuliaArrays/StaticArrays.jl) to keep type stability. See the [related Section](@ref doc:Type-Stability) about type stability for more details.
557557
"""
558558
qft(dimensions::Int) = QuantumObject(_qft_op(dimensions), Operator, dimensions)
559559
qft(dimensions::Union{Dimensions,AbstractVector{Int},Tuple}) =

src/qobj/quantum_object.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ julia> a isa QuantumObject
3737
true
3838
3939
julia> a.dims
40-
1-element SVector{1, Int64} with indices SOneTo(1):
40+
1-element StaticArraysCore.SVector{1, Int64} with indices SOneTo(1):
4141
20
4242
4343
julia> a.dimensions

src/qobj/states.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ The `dimensions` can be either the following types:
1717
- `dimensions::Union{Dimensions,AbstractVector{Int}, Tuple}`: list of dimensions representing the each number of basis in the subsystems.
1818
1919
!!! warning "Beware of type-stability!"
20-
It is highly recommended to use `zero_ket(dimensions)` with `dimensions` as `Tuple` or `SVector` to keep type stability. See the [related Section](@ref doc:Type-Stability) about type stability for more details.
20+
It is highly recommended to use `zero_ket(dimensions)` with `dimensions` as `Tuple` or `SVector` from [StaticArrays.jl](https://github.com/JuliaArrays/StaticArrays.jl) to keep type stability. See the [related Section](@ref doc:Type-Stability) about type stability for more details.
2121
"""
2222
zero_ket(dimensions::Int) = QuantumObject(zeros(ComplexF64, dimensions), Ket, dimensions)
2323
zero_ket(dimensions::Union{Dimensions,AbstractVector{Int},Tuple}) =
@@ -31,7 +31,7 @@ Generates a fock state ``\ket{\psi}`` of dimension `N`.
3131
It is also possible to specify the list of dimensions `dims` if different subsystems are present.
3232
3333
!!! warning "Beware of type-stability!"
34-
If you want to keep type stability, it is recommended to use `fock(N, j, dims=dims, sparse=Val(sparse))` instead of `fock(N, j, dims=dims, sparse=sparse)`. Consider also to use `dims` as a `Tuple` or `SVector` instead of `Vector`. See [this link](https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-value-type) and the [related Section](@ref doc:Type-Stability) about type stability for more details.
34+
If you want to keep type stability, it is recommended to use `fock(N, j, dims=dims, sparse=Val(sparse))` instead of `fock(N, j, dims=dims, sparse=sparse)`. Consider also to use `dims` as a `Tuple` or `SVector` from [StaticArrays.jl](https://github.com/JuliaArrays/StaticArrays.jl) instead of `Vector`. See [this link](https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-value-type) and the [related Section](@ref doc:Type-Stability) about type stability for more details.
3535
"""
3636
function fock(N::Int, j::Int = 0; dims::Union{Int,AbstractVector{Int},Tuple} = N, sparse::Union{Bool,Val} = Val(false))
3737
if getVal(sparse)
@@ -50,7 +50,7 @@ Generates a fock state like [`fock`](@ref).
5050
It is also possible to specify the list of dimensions `dims` if different subsystems are present.
5151
5252
!!! warning "Beware of type-stability!"
53-
If you want to keep type stability, it is recommended to use `basis(N, j, dims=dims)` with `dims` as a `Tuple` or `SVector` instead of `Vector`. See [this link](https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-value-type) and the [related Section](@ref doc:Type-Stability) about type stability for more details.
53+
If you want to keep type stability, it is recommended to use `basis(N, j, dims=dims)` with `dims` as a `Tuple` or `SVector` from [StaticArrays.jl](https://github.com/JuliaArrays/StaticArrays.jl) instead of `Vector`. See [this link](https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-value-type) and the [related Section](@ref doc:Type-Stability) about type stability for more details.
5454
"""
5555
basis(N::Int, j::Int = 0; dims::Union{Int,AbstractVector{Int},Tuple} = N) = fock(N, j, dims = dims)
5656

@@ -73,7 +73,7 @@ The `dimensions` can be either the following types:
7373
- `dimensions::Union{Dimensions,AbstractVector{Int},Tuple}`: list of dimensions representing the each number of basis in the subsystems.
7474
7575
!!! warning "Beware of type-stability!"
76-
If you want to keep type stability, it is recommended to use `rand_ket(dimensions)` with `dimensions` as `Tuple` or `SVector` to keep type stability. See the [related Section](@ref doc:Type-Stability) about type stability for more details.
76+
If you want to keep type stability, it is recommended to use `rand_ket(dimensions)` with `dimensions` as `Tuple` or `SVector` from [StaticArrays.jl](https://github.com/JuliaArrays/StaticArrays.jl) to keep type stability. See the [related Section](@ref doc:Type-Stability) about type stability for more details.
7777
"""
7878
rand_ket(dimensions::Int) = rand_ket(SVector(dimensions))
7979
function rand_ket(dimensions::Union{Dimensions,AbstractVector{Int},Tuple})
@@ -90,7 +90,7 @@ Density matrix representation of a Fock state.
9090
Constructed via outer product of [`fock`](@ref).
9191
9292
!!! warning "Beware of type-stability!"
93-
If you want to keep type stability, it is recommended to use `fock_dm(N, j, dims=dims, sparse=Val(sparse))` instead of `fock_dm(N, j, dims=dims, sparse=sparse)`. Consider also to use `dims` as a `Tuple` or `SVector` instead of `Vector`. See [this link](https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-value-type) and the [related Section](@ref doc:Type-Stability) about type stability for more details.
93+
If you want to keep type stability, it is recommended to use `fock_dm(N, j, dims=dims, sparse=Val(sparse))` instead of `fock_dm(N, j, dims=dims, sparse=sparse)`. Consider also to use `dims` as a `Tuple` or `SVector` from [StaticArrays.jl](https://github.com/JuliaArrays/StaticArrays.jl) instead of `Vector`. See [this link](https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-value-type) and the [related Section](@ref doc:Type-Stability) about type stability for more details.
9494
"""
9595
function fock_dm(
9696
N::Int,
@@ -146,7 +146,7 @@ The `dimensions` can be either the following types:
146146
- `dimensions::Union{Dimensions,AbstractVector{Int},Tuple}`: list of dimensions representing the each number of basis in the subsystems.
147147
148148
!!! warning "Beware of type-stability!"
149-
If you want to keep type stability, it is recommended to use `maximally_mixed_dm(dimensions)` with `dimensions` as `Tuple` or `SVector` to keep type stability. See the [related Section](@ref doc:Type-Stability) about type stability for more details.
149+
If you want to keep type stability, it is recommended to use `maximally_mixed_dm(dimensions)` with `dimensions` as `Tuple` or `SVector` from [StaticArrays.jl](https://github.com/JuliaArrays/StaticArrays.jl) to keep type stability. See the [related Section](@ref doc:Type-Stability) about type stability for more details.
150150
"""
151151
maximally_mixed_dm(dimensions::Int) = QuantumObject(I(dimensions) / complex(dimensions), Operator, SVector(dimensions))
152152
function maximally_mixed_dm(dimensions::Union{Dimensions,AbstractVector{Int},Tuple})
@@ -166,7 +166,7 @@ The `dimensions` can be either the following types:
166166
The default keyword argument `rank = prod(dimensions)` (full rank).
167167
168168
!!! warning "Beware of type-stability!"
169-
If you want to keep type stability, it is recommended to use `rand_dm(dimensions; rank=rank)` with `dimensions` as `Tuple` or `SVector` instead of `Vector`. See [this link](https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-value-type) and the [related Section](@ref doc:Type-Stability) about type stability for more details.
169+
If you want to keep type stability, it is recommended to use `rand_dm(dimensions; rank=rank)` with `dimensions` as `Tuple` or `SVector` from [StaticArrays.jl](https://github.com/JuliaArrays/StaticArrays.jl) instead of `Vector`. See [this link](https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-value-type) and the [related Section](@ref doc:Type-Stability) about type stability for more details.
170170
171171
# References
172172
- [J. Ginibre, Statistical ensembles of complex, quaternion, and real matrices, Journal of Mathematical Physics 6.3 (1965): 440-449](https://doi.org/10.1063/1.1704292)

src/spin_lattice.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ A Julia function for generating a multi-site operator ``\\hat{O} = \\hat{O}_i \\
3131
julia> op = multisite_operator(Val(8), 5=>sigmax(), 7=>sigmaz());
3232
3333
julia> op.dims
34-
8-element SVector{8, Int64} with indices SOneTo(8):
34+
8-element StaticArraysCore.SVector{8, Int64} with indices SOneTo(8):
3535
2
3636
2
3737
2

0 commit comments

Comments
 (0)