Skip to content

Commit a514ee7

Browse files
author
alexander papageorge
committed
code review and a few more tests
1 parent c41ced9 commit a514ee7

File tree

4 files changed

+38
-29
lines changed

4 files changed

+38
-29
lines changed

src/operators.jl

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -89,29 +89,6 @@ tensor(op::AbstractOperator) = op
8989
tensor(operators::AbstractOperator...) = reduce(tensor, operators)
9090

9191

92-
93-
"""
94-
check_indices(indices::Array)
95-
96-
Determine whether a collection of indices, written as a list of (integers or lists of integers) is unique.
97-
This assures that the embedded operators are in non-overlapping subspaces.
98-
"""
99-
function check_indices(indices::Array)
100-
status = true
101-
# Check that no sub-list contains duplicates.
102-
for i in filter(x -> typeof(x) <: Array, indices)
103-
status &= (length(Set(i)) == length(i))
104-
end
105-
# Check that there are no duplicates across `indices`
106-
for (idx, i) in enumerate(indices[1:end-1])
107-
for j in indices[idx+1:end]
108-
status &= (length(intersect(i, j)) == 0)
109-
end
110-
end
111-
return status
112-
end
113-
114-
11592
"""
11693
embed(basis1[, basis2], indices::Vector, operators::Vector)
11794
@@ -120,7 +97,7 @@ Tensor product of operators where missing indices are filled up with identity op
12097
function embed(basis_l::CompositeBasis, basis_r::CompositeBasis,
12198
indices::Vector, operators::Vector{T}) where T<:AbstractOperator
12299

123-
@assert check_indices(indices)
100+
@assert sortedindices.check_embed_indices(indices)
124101

125102
N = length(basis_l.bases)
126103
@assert length(basis_r.bases) == N
@@ -132,15 +109,14 @@ function embed(basis_l::CompositeBasis, basis_r::CompositeBasis,
132109
ops_sb = [x[2] for x in idxop_sb]
133110

134111
for (idxsb, opsb) in zip(indices_sb, ops_sb)
135-
@assert (opsb.basis_l == basis_l.bases[idxsb]) || throw(bases.IncompatibleBases())
136-
@assert (opsb.basis_r == basis_r.bases[idxsb]) || throw(bases.IncompatibleBases())
112+
(opsb.basis_l == basis_l.bases[idxsb]) || throw(bases.IncompatibleBases())
113+
(opsb.basis_r == basis_r.bases[idxsb]) || throw(bases.IncompatibleBases())
137114
end
138115

139116
embed_op = tensor([i indices_sb ? ops_sb[indexin(i, indices_sb)[1]] : identityoperator(T, basis_l.bases[i], basis_r.bases[i]) for i=1:N]...)
140117

141118
# Embed all joint-subspace operators.
142119
idxop_comp = [x for x in zip(indices, operators) if typeof(x[1]) <: Array]
143-
144120
for (idxs, op) in idxop_comp
145121
embed_op *= embed(basis_l, basis_r, idxs, op)
146122
end
@@ -159,8 +135,8 @@ function embed(basis_l::CompositeBasis, basis_r::CompositeBasis,
159135
N = length(basis_l.bases)
160136
@assert length(basis_r.bases) == N
161137

162-
@assert reduce(tensor, basis_l.bases[indices]) == op.basis_l || throw(bases.IncompatibleBases())
163-
@assert reduce(tensor, basis_r.bases[indices]) == op.basis_r || throw(bases.IncompatibleBases())
138+
reduce(tensor, basis_l.bases[indices]) == op.basis_l || throw(bases.IncompatibleBases())
139+
reduce(tensor, basis_r.bases[indices]) == op.basis_r || throw(bases.IncompatibleBases())
164140

165141
index_order = [idx for idx in 1:length(basis_l.bases) if idx indices]
166142
all_operators = AbstractOperator[identityoperator(T, basis_l.bases[i], basis_r.bases[i]) for i in index_order]

src/sortedindices.jl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,31 @@ function check_sortedindices(imax::Int, indices::Vector{Int})
176176
end
177177
end
178178

179+
"""
180+
check_embed_indices(indices::Array)
181+
182+
Determine whether a collection of indices, written as a list of (integers or lists of integers) is unique.
183+
This assures that the embedded operators are in non-overlapping subspaces.
184+
"""
185+
function check_embed_indices(indices::Array)
186+
# Check whether `indices` is empty.
187+
(length(indices) == 0) ? (return true) : nothing
188+
189+
err_str = "Variable `indices` comes in an unexpected form. Expecting `Array{Union{Int64, Array{Int64, 1}}, 1}`"
190+
@assert mapreduce(x -> typeof(x)<:Array || typeof(x)<:Int64, &, indices) err_str
191+
192+
isunique = true
193+
# Check that no sub-list contains duplicates.
194+
for i in filter(x -> typeof(x) <: Array, indices)
195+
isunique &= (length(Set(i)) == length(i))
196+
end
197+
# Check that there are no duplicates across `indices`
198+
for (idx, i) in enumerate(indices[1:end-1])
199+
for j in indices[idx+1:end]
200+
isunique &= (length(Base.intersect(i, j)) == 0)
201+
end
202+
end
203+
return isunique
204+
end
205+
179206
end # module

test/test_operators.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ cnot = [1 0 0 0; 0 1 0 0; 0 0 0 1; 0 0 1 0]
109109
op_cnot = DenseOperator(b2b2, cnot)
110110
OP_cnot = embed(b8, [1,3], op_cnot)
111111
@test ptrace(OP_cnot, [2])/2. == op_cnot
112+
@test_throws AssertionError embed(b2b2, [1,1], op_cnot)
112113

113114
@test_throws ErrorException QuantumOptics.operators.gemm!()
114115
@test_throws ErrorException QuantumOptics.operators.gemv!()

test/test_sortedindices.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,9 @@ s.reducedindices!(x, [2, 3, 5, 6])
5252
@test s.check_sortedindices(5, Int[]) == nothing
5353
@test s.check_sortedindices(5, [1, 3]) == nothing
5454

55+
@test s.check_embed_indices([1,[3,5],10,2,[70,11]]) == true
56+
@test s.check_embed_indices([1,3,1]) == false
57+
@test s.check_embed_indices([1,[10,11],7,[3,1]]) == false
58+
@test s.check_embed_indices([[10,3],5,6,[3,7]]) == false
59+
@test s.check_embed_indices([]) == true
5560
end # testset

0 commit comments

Comments
 (0)