Skip to content

Commit e9df9fb

Browse files
committed
modify some comments
1 parent 4c848c1 commit e9df9fb

File tree

5 files changed

+32
-37
lines changed

5 files changed

+32
-37
lines changed

src/negativity.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ function _partial_transpose(ρ::QuantumObject{<:AbstractArray,OperatorQuantumObj
8787
# 2 - the subsystem need be transposed
8888

8989
nsys = length(mask2)
90-
dimslist = dims_to_list.to) # need `ρ.to` here if ρ is CompoundDimensions
90+
dimslist = dims_to_list.to)
9191
pt_dims = reshape(Vector(1:(2*nsys)), (nsys, 2))
9292
pt_idx = [
9393
[pt_dims[n, mask2[n]] for n in 1:nsys] # origin value in mask2
@@ -107,7 +107,7 @@ function _partial_transpose(ρ::QuantumObject{<:AbstractSparseArray,OperatorQuan
107107
throw(ArgumentError("Invalid partial transpose for dims = $(ρ.dims)"))
108108

109109
M, N = size(ρ)
110-
dimsTuple = Tuple(dims_to_list.dims.to)) # need `dims.to` here if QO is CompoundDimensions
110+
dimsTuple = Tuple(dims_to_list.to))
111111
colptr = ρ.data.colptr
112112
rowval = ρ.data.rowval
113113
nzval = ρ.data.nzval

src/qobj/arithmetic_and_attributes.jl

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ function ptrace(QO::QuantumObject{<:AbstractArray,OperatorQuantumObject}, sel::U
624624
(n_d == 1) && return QO
625625
end
626626

627-
dimslist = dims_to_list(QO.dims.to) # need `QO.dims.to` here if QO is CompoundDimensions
627+
dimslist = dims_to_list(QO.to)
628628
_sort_sel = sort(SVector{length(sel),Int}(sel))
629629
ρtr, dkeep = _ptrace_oper(QO.data, dimslist, _sort_sel)
630630
return QuantumObject(ρtr, type = Operator, dims = Dimensions(dkeep))
@@ -793,10 +793,6 @@ function permute(
793793
A::QuantumObject{<:AbstractArray{T},ObjType},
794794
order::Union{AbstractVector{Int},Tuple},
795795
) where {T,ObjType<:Union{KetQuantumObject,BraQuantumObject,OperatorQuantumObject}}
796-
isa(A.dims, CompoundDimensions) &&
797-
(A.to != A.from) &&
798-
throw(ArgumentError("Invalid permutation for dims = $(A.dims)"))
799-
800796
(length(order) != length(A.dims)) &&
801797
throw(ArgumentError("The order list must have the same length as the number of subsystems (A.dims)"))
802798

@@ -807,7 +803,7 @@ function permute(
807803
order_svector = SVector{length(order),Int}(order) # convert it to SVector for performance
808804

809805
# obtain the arguments: dims for reshape; perm for PermutedDimsArray
810-
dimslist = dims_to_list(A.dims.to) # need `A.dims.to` here if A is CompoundDimensions
806+
dimslist = dims_to_list(A.dims)
811807
dims, perm = _dims_and_perm(A.type, dimslist, order_svector, length(order_svector))
812808

813809
return QuantumObject(
@@ -817,15 +813,17 @@ function permute(
817813
)
818814
end
819815

820-
function _dims_and_perm(
816+
_dims_and_perm(
821817
::ObjType,
822-
dims::SVector{N,Int},
818+
dimslist::SVector{N,Int},
823819
order::AbstractVector{Int},
824820
L::Int,
825-
) where {ObjType<:Union{KetQuantumObject,BraQuantumObject},N}
826-
return reverse(dims), reverse((L + 1) .- order)
827-
end
821+
) where {ObjType<:Union{KetQuantumObject,BraQuantumObject},N} = reverse(dimslist), reverse((L + 1) .- order)
828822

829-
function _dims_and_perm(::OperatorQuantumObject, dims::SVector{N,Int}, order::AbstractVector{Int}, L::Int) where {N}
830-
return reverse(vcat(dims, dims)), reverse((2 * L + 1) .- vcat(order, order .+ L))
831-
end
823+
# if dims originates from Dimensions
824+
_dims_and_perm(::OperatorQuantumObject, dimslist::SVector{N,Int}, order::AbstractVector{Int}, L::Int) where {N} =
825+
reverse(vcat(dimslist, dimslist)), reverse((2 * L + 1) .- vcat(order, order .+ L))
826+
827+
# if dims originates from CompoundDimensions
828+
_dims_and_perm(::OperatorQuantumObject, dimslist::SVector{2, SVector{N, Int}}, order::AbstractVector{Int}, L::Int) where {N} =
829+
reverse(vcat(dimslist[1], dimslist[2])), reverse((2 * L + 1) .- vcat(order, order .+ L))

src/qobj/dimensions.jl

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,3 @@ Base.prod(dims::Dimensions) = prod(dims.to)
7272

7373
LinearAlgebra.transpose(dims::Dimensions) = dims
7474
LinearAlgebra.transpose(dims::CompoundDimensions) = CompoundDimensions(dims.from, dims.to) # switch `to` and `from`
75-
76-
LinearAlgebra.kron(Adims::Dimensions{NA}, Bdims::Dimensions{NB}) where {NA,NB} =
77-
Dimensions{NA + NB}(vcat(Adims.to, Bdims.to))
78-
LinearAlgebra.kron(Adims::CompoundDimensions{NA}, Bdims::CompoundDimensions{NB}) where {NA,NB} =
79-
CompoundDimensions{NA + NB}(vcat(Adims.to, Bdims.to), vcat(Adims.from, Bdims.from))

src/qobj/operators.jl

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Returns a random unitary [`QuantumObject`](@ref).
1919
2020
The `dimensions` can be either the following types:
2121
- `dimensions::Int`: Number of basis states in the Hilbert space.
22-
- `dimensions::Union{AbstractVector{Int},Tuple}`: list of dimensions representing the each number of basis in the subsystems.
22+
- `dimensions::Union{Dimensions,AbstractVector{Int},Tuple}`: list of dimensions representing the each number of basis in the subsystems.
2323
2424
The `distribution` specifies which of the method used to obtain the unitary matrix:
2525
- `:haar`: Haar random unitary matrix using the algorithm from reference 1
@@ -33,9 +33,11 @@ The `distribution` specifies which of the method used to obtain the unitary matr
3333
"""
3434
rand_unitary(dimensions::Int, distribution::Union{Symbol,Val} = Val(:haar)) =
3535
rand_unitary(SVector(dimensions), makeVal(distribution))
36-
rand_unitary(dimensions::Union{AbstractVector{Int},Tuple}, distribution::Union{Symbol,Val} = Val(:haar)) =
37-
rand_unitary(dimensions, makeVal(distribution))
38-
function rand_unitary(dimensions::Union{AbstractVector{Int},Tuple}, ::Val{:haar})
36+
rand_unitary(
37+
dimensions::Union{Dimensions,AbstractVector{Int},Tuple},
38+
distribution::Union{Symbol,Val} = Val(:haar),
39+
) = rand_unitary(dimensions, makeVal(distribution))
40+
function rand_unitary(dimensions::Union{Dimensions,AbstractVector{Int},Tuple}, ::Val{:haar})
3941
N = prod(dimensions)
4042

4143
# generate N x N matrix Z of complex standard normal random variates
@@ -50,7 +52,7 @@ function rand_unitary(dimensions::Union{AbstractVector{Int},Tuple}, ::Val{:haar}
5052
Λ ./= abs.(Λ) # rescaling the elements
5153
return QuantumObject(sparse_to_dense(Q * Diagonal(Λ)); type = Operator, dims = dimensions)
5254
end
53-
function rand_unitary(dimensions::Union{AbstractVector{Int},Tuple}, ::Val{:exp})
55+
function rand_unitary(dimensions::Union{Dimensions,AbstractVector{Int},Tuple}, ::Val{:exp})
5456
N = prod(dimensions)
5557

5658
# generate N x N matrix Z of complex standard normal random variates
@@ -61,7 +63,7 @@ function rand_unitary(dimensions::Union{AbstractVector{Int},Tuple}, ::Val{:exp})
6163

6264
return sparse_to_dense(exp(-1.0im * H))
6365
end
64-
rand_unitary(dimensions::Union{AbstractVector{Int},Tuple}, ::Val{T}) where {T} =
66+
rand_unitary(dimensions::Union{Dimensions,AbstractVector{Int},Tuple}, ::Val{T}) where {T} =
6567
throw(ArgumentError("Invalid distribution: $(T)"))
6668

6769
@doc raw"""
@@ -534,7 +536,7 @@ Generates a discrete Fourier transform matrix ``\hat{F}_N`` for [Quantum Fourier
534536
535537
The `dimensions` can be either the following types:
536538
- `dimensions::Int`: Number of basis states in the Hilbert space.
537-
- `dimensions::Union{AbstractVector{Int},Tuple}`: list of dimensions representing the each number of basis in the subsystems.
539+
- `dimensions::Union{Dimensions,AbstractVector{Int},Tuple}`: list of dimensions representing the each number of basis in the subsystems.
538540
539541
``N`` represents the total dimension, and therefore the matrix is defined as
540542
@@ -555,7 +557,7 @@ where ``\omega = \exp(\frac{2 \pi i}{N})``.
555557
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.
556558
"""
557559
qft(dimensions::Int) = QuantumObject(_qft_op(dimensions), Operator, dimensions)
558-
qft(dimensions::Union{AbstractVector{T},Tuple}) where {T} =
560+
qft(dimensions::Union{Dimensions,AbstractVector{Int},Tuple}) =
559561
QuantumObject(_qft_op(prod(dimensions)), Operator, dimensions)
560562
function _qft_op(N::Int)
561563
ω = exp(2.0im * π / N)

src/qobj/states.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ Returns a zero [`Ket`](@ref) vector with given argument `dimensions`.
1414
1515
The `dimensions` can be either the following types:
1616
- `dimensions::Int`: Number of basis states in the Hilbert space.
17-
- `dimensions::Union{AbstractVector{Int}, Tuple}`: list of dimensions representing the each number of basis in the subsystems.
17+
- `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!"
2020
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.
2121
"""
2222
zero_ket(dimensions::Int) = QuantumObject(zeros(ComplexF64, dimensions), Ket, dimensions)
23-
zero_ket(dimensions::Union{AbstractVector{Int},Tuple}) =
23+
zero_ket(dimensions::Union{Dimensions,AbstractVector{Int},Tuple}) =
2424
QuantumObject(zeros(ComplexF64, prod(dimensions)), Ket, dimensions)
2525

2626
@doc raw"""
@@ -71,13 +71,13 @@ Generate a random normalized [`Ket`](@ref) vector with given argument `dimension
7171
7272
The `dimensions` can be either the following types:
7373
- `dimensions::Int`: Number of basis states in the Hilbert space.
74-
- `dimensions::Union{AbstractVector{Int},Tuple}`: list of dimensions representing the each number of basis in the subsystems.
74+
- `dimensions::Union{Dimensions,AbstractVector{Int},Tuple}`: list of dimensions representing the each number of basis in the subsystems.
7575
7676
!!! warning "Beware of type-stability!"
7777
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.
7878
"""
7979
rand_ket(dimensions::Int) = rand_ket(SVector(dimensions))
80-
function rand_ket(dimensions::Union{AbstractVector{Int},Tuple})
80+
function rand_ket(dimensions::Union{Dimensions,AbstractVector{Int},Tuple})
8181
N = prod(dimensions)
8282
ψ = rand(ComplexF64, N) .- (0.5 + 0.5im)
8383
return QuantumObject(normalize!(ψ); type = Ket, dims = dimensions)
@@ -144,13 +144,13 @@ Returns the maximally mixed density matrix with given argument `dimensions`.
144144
145145
The `dimensions` can be either the following types:
146146
- `dimensions::Int`: Number of basis states in the Hilbert space.
147-
- `dimensions::Union{AbstractVector{Int},Tuple}`: list of dimensions representing the each number of basis in the subsystems.
147+
- `dimensions::Union{Dimensions,AbstractVector{Int},Tuple}`: list of dimensions representing the each number of basis in the subsystems.
148148
149149
!!! warning "Beware of type-stability!"
150150
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.
151151
"""
152152
maximally_mixed_dm(dimensions::Int) = QuantumObject(I(dimensions) / complex(dimensions), Operator, SVector(dimensions))
153-
function maximally_mixed_dm(dimensions::Union{AbstractVector{Int},Tuple})
153+
function maximally_mixed_dm(dimensions::Union{Dimensions,AbstractVector{Int},Tuple})
154154
N = prod(dimensions)
155155
return QuantumObject(I(N) / complex(N), Operator, dimensions)
156156
end
@@ -162,7 +162,7 @@ Generate a random density matrix from Ginibre ensemble with given argument `dime
162162
163163
The `dimensions` can be either the following types:
164164
- `dimensions::Int`: Number of basis states in the Hilbert space.
165-
- `dimensions::Union{AbstractVector{Int},Tuple}`: list of dimensions representing the each number of basis in the subsystems.
165+
- `dimensions::Union{Dimensions,AbstractVector{Int},Tuple}`: list of dimensions representing the each number of basis in the subsystems.
166166
167167
The default keyword argument `rank = prod(dimensions)` (full rank).
168168
@@ -174,7 +174,7 @@ The default keyword argument `rank = prod(dimensions)` (full rank).
174174
- [K. Życzkowski, et al., Generating random density matrices, Journal of Mathematical Physics 52, 062201 (2011)](http://dx.doi.org/10.1063/1.3595693)
175175
"""
176176
rand_dm(dimensions::Int; rank::Int = prod(dimensions)) = rand_dm(SVector(dimensions), rank = rank)
177-
function rand_dm(dimensions::Union{AbstractVector{Int},Tuple}; rank::Int = prod(dimensions))
177+
function rand_dm(dimensions::Union{Dimensions,AbstractVector{Int},Tuple}; rank::Int = prod(dimensions))
178178
N = prod(dimensions)
179179
(rank < 1) && throw(DomainError(rank, "The argument rank must be larger than 1."))
180180
(rank > N) && throw(DomainError(rank, "The argument rank cannot exceed dimensions."))

0 commit comments

Comments
 (0)