Skip to content

Commit 8aef7dd

Browse files
authored
Add test_bases from QuantumOpticsBase and reorganize a bit (#37)
1 parent 46223c3 commit 8aef7dd

File tree

6 files changed

+76
-20
lines changed

6 files changed

+76
-20
lines changed

src/embed_permute.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ function embed(basis_l::CompositeBasis, basis_r::CompositeBasis,
6767
ops_sb = [x[2] for x in idxop_sb]
6868

6969
for (idxsb, opsb) in zip(indices_sb, ops_sb)
70-
(opsb.basis_l == basis_l.bases[idxsb]) || throw(IncompatibleBases())
71-
(opsb.basis_r == basis_r.bases[idxsb]) || throw(IncompatibleBases())
70+
(opsb.basis_l == basis_l.bases[idxsb]) || throw(IncompatibleBases()) # FIXME issue #12
71+
(opsb.basis_r == basis_r.bases[idxsb]) || throw(IncompatibleBases()) # FIXME issue #12
7272
end
7373

7474
S = length(operators) > 0 ? mapreduce(eltype, promote_type, operators) : Any

src/julia_base.jl

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@ addnumbererror() = throw(ArgumentError("Can't add or subtract a number and an op
88
# States
99
##
1010

11-
-(a::T) where {T<:StateVector} = T(a.basis, -a.data)
11+
-(a::T) where {T<:StateVector} = T(a.basis, -a.data) # FIXME issue #12
1212
*(a::StateVector, b::Number) = b*a
13-
copy(a::T) where {T<:StateVector} = T(a.basis, copy(a.data))
14-
length(a::StateVector) = length(a.basis)::Int
15-
basis(a::StateVector) = a.basis
13+
copy(a::T) where {T<:StateVector} = T(a.basis, copy(a.data)) # FIXME issue #12
14+
length(a::StateVector) = length(a.basis)::Int # FIXME issue #12
15+
basis(a::StateVector) = a.basis # FIXME issue #12
1616
directsum(x::StateVector...) = reduce(directsum, x)
1717

1818
# Array-like functions
19-
Base.size(x::StateVector) = size(x.data)
20-
@inline Base.axes(x::StateVector) = axes(x.data)
19+
Base.size(x::StateVector) = size(x.data) # FIXME issue #12
20+
@inline Base.axes(x::StateVector) = axes(x.data) # FIXME issue #12
2121
Base.ndims(x::StateVector) = 1
2222
Base.ndims(::Type{<:StateVector}) = 1
23-
Base.eltype(x::StateVector) = eltype(x.data)
23+
Base.eltype(x::StateVector) = eltype(x.data) # FIXME issue #12
2424

2525
# Broadcasting
2626
Base.broadcastable(x::StateVector) = x
@@ -32,9 +32,9 @@ Base.adjoint(a::StateVector) = dagger(a)
3232
# Operators
3333
##
3434

35-
length(a::AbstractOperator) = length(a.basis_l)::Int*length(a.basis_r)::Int
36-
basis(a::AbstractOperator) = (check_samebases(a); a.basis_l)
37-
basis(a::AbstractSuperOperator) = (check_samebases(a); a.basis_l[1])
35+
length(a::AbstractOperator) = length(a.basis_l)::Int*length(a.basis_r)::Int # FIXME issue #12
36+
basis(a::AbstractOperator) = (check_samebases(a); a.basis_l) # FIXME issue #12
37+
basis(a::AbstractSuperOperator) = (check_samebases(a); a.basis_l[1]) # FIXME issue #12
3838

3939
# Ensure scalar broadcasting
4040
Base.broadcastable(x::AbstractOperator) = Ref(x)
@@ -60,11 +60,11 @@ Operator exponential.
6060
"""
6161
exp(op::AbstractOperator) = throw(ArgumentError("exp() is not defined for this type of operator: $(typeof(op)).\nTry to convert to dense operator first with dense()."))
6262

63-
Base.size(op::AbstractOperator) = (length(op.basis_l),length(op.basis_r))
63+
Base.size(op::AbstractOperator) = (length(op.basis_l),length(op.basis_r)) # FIXME issue #12
6464
function Base.size(op::AbstractOperator, i::Int)
6565
i < 1 && throw(ErrorException("dimension index is < 1"))
6666
i > 2 && return 1
67-
i==1 ? length(op.basis_l) : length(op.basis_r)
67+
i==1 ? length(op.basis_l) : length(op.basis_r) # FIXME issue #12
6868
end
6969

7070
Base.adjoint(a::AbstractOperator) = dagger(a)

src/julia_linalg.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ tr(x::AbstractOperator) = arithmetic_unary_error("Trace", x)
1717
1818
Norm of the given bra or ket state.
1919
"""
20-
norm(x::StateVector) = norm(x.data)
20+
norm(x::StateVector) = norm(x.data) # FIXME issue #12
2121

2222
"""
2323
normalize(x::StateVector)
@@ -31,7 +31,7 @@ normalize(x::StateVector) = x/norm(x)
3131
3232
In-place normalization of the given bra or ket so that `norm(x)` is one.
3333
"""
34-
normalize!(x::StateVector) = (normalize!(x.data); x)
34+
normalize!(x::StateVector) = (normalize!(x.data); x) # FIXME issue #12
3535

3636
"""
3737
normalize(op)

src/linalg.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
samebases(a::AbstractOperator) = samebases(a.basis_l, a.basis_r)::Bool
2-
samebases(a::AbstractOperator, b::AbstractOperator) = samebases(a.basis_l, b.basis_l)::Bool && samebases(a.basis_r, b.basis_r)::Bool
3-
check_samebases(a::Union{AbstractOperator, AbstractSuperOperator}) = check_samebases(a.basis_l, a.basis_r)
4-
multiplicable(a::AbstractOperator, b::AbstractOperator) = multiplicable(a.basis_r, b.basis_l)
1+
samebases(a::AbstractOperator) = samebases(a.basis_l, a.basis_r)::Bool # FIXME issue #12
2+
samebases(a::AbstractOperator, b::AbstractOperator) = samebases(a.basis_l, b.basis_l)::Bool && samebases(a.basis_r, b.basis_r)::Bool # FIXME issue #12
3+
check_samebases(a::Union{AbstractOperator, AbstractSuperOperator}) = check_samebases(a.basis_l, a.basis_r) # FIXME issue #12
4+
multiplicable(a::AbstractOperator, b::AbstractOperator) = multiplicable(a.basis_r, b.basis_l) # FIXME issue #12
55
dagger(a::AbstractOperator) = arithmetic_unary_error("Hermitian conjugate", a)
66
transpose(a::AbstractOperator) = arithmetic_unary_error("Transpose", a)
77
directsum(a::AbstractOperator...) = reduce(directsum, a)

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ end
2626
println("Starting tests with $(Threads.nthreads()) threads out of `Sys.CPU_THREADS = $(Sys.CPU_THREADS)`...")
2727

2828
@doset "sortedindices"
29+
@doset "bases"
2930
#VERSION >= v"1.9" && @doset "doctests"
3031
get(ENV,"JET_TEST","")=="true" && @doset "jet"
3132
VERSION >= v"1.9" && @doset "aqua"

test/test_bases.jl

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using Test
2+
using QuantumInterface: tensor, , ptrace, reduced, permutesystems, equal_bases, multiplicable
3+
using QuantumInterface: GenericBasis, CompositeBasis, NLevelBasis, FockBasis
4+
5+
@testset "basis" begin
6+
7+
shape1 = [5]
8+
shape2 = [2, 3]
9+
shape3 = [6]
10+
11+
b1 = GenericBasis(shape1)
12+
b2 = GenericBasis(shape2)
13+
b3 = GenericBasis(shape3)
14+
15+
@test b1.shape == shape1
16+
@test b2.shape == shape2
17+
@test b1 != b2
18+
@test b1 != FockBasis(2)
19+
@test b1 == b1
20+
21+
@test tensor(b1) == b1
22+
comp_b1 = tensor(b1, b2)
23+
comp_uni = b1 b2
24+
comp_b2 = tensor(b1, b1, b2)
25+
@test comp_b1.shape == [prod(shape1), prod(shape2)]
26+
@test comp_uni.shape == [prod(shape1), prod(shape2)]
27+
@test comp_b2.shape == [prod(shape1), prod(shape1), prod(shape2)]
28+
29+
@test b1^3 == CompositeBasis(b1, b1, b1)
30+
@test (b1b2)^2 == CompositeBasis(b1, b2, b1, b2)
31+
@test_throws ArgumentError b1^(0)
32+
33+
comp_b1_b2 = tensor(comp_b1, comp_b2)
34+
@test comp_b1_b2.shape == [prod(shape1), prod(shape2), prod(shape1), prod(shape1), prod(shape2)]
35+
@test comp_b1_b2 == CompositeBasis(b1, b2, b1, b1, b2)
36+
37+
@test_throws ArgumentError tensor()
38+
@test comp_b2.shape == tensor(b1, comp_b1).shape
39+
@test comp_b2 == tensor(b1, comp_b1)
40+
41+
@test_throws ArgumentError ptrace(comp_b1, [1, 2])
42+
@test ptrace(comp_b2, [1]) == ptrace(comp_b2, [2]) == comp_b1 == ptrace(comp_b2, 1)
43+
@test ptrace(comp_b2, [1, 2]) == ptrace(comp_b1, [1])
44+
@test ptrace(comp_b2, [2, 3]) == ptrace(comp_b1, [2])
45+
@test ptrace(comp_b2, [2, 3]) == reduced(comp_b2, [1])
46+
@test_throws ArgumentError reduced(comp_b1, [])
47+
48+
comp1 = tensor(b1, b2, b3)
49+
comp2 = tensor(b2, b1, b3)
50+
@test permutesystems(comp1, [2,1,3]) == comp2
51+
52+
@test !equal_bases([b1, b2], [b1, b3])
53+
@test !multiplicable(comp1, b1 b2 NLevelBasis(prod(b3.shape)))
54+
55+
end # testset

0 commit comments

Comments
 (0)