Skip to content

Commit c41ced9

Browse files
author
alexander papageorge
committed
more tests for composite bases
1 parent d61d107 commit c41ced9

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

src/operators.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import LinearAlgebra: tr, ishermitian
99
import SparseArrays: sparse
1010
import ..bases: basis, tensor, ptrace, permutesystems,
1111
samebases, check_samebases, multiplicable
12-
# import ..operators_sparse: SparseOperator
1312
import ..states: dagger, normalize, normalize!
1413

1514
using ..sortedindices, ..bases, ..states
@@ -92,7 +91,10 @@ tensor(operators::AbstractOperator...) = reduce(tensor, operators)
9291

9392

9493
"""
95-
docstring
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.
9698
"""
9799
function check_indices(indices::Array)
98100
status = true
@@ -124,6 +126,7 @@ function embed(basis_l::CompositeBasis, basis_r::CompositeBasis,
124126
@assert length(basis_r.bases) == N
125127
@assert length(indices) == length(operators)
126128

129+
# Embed all single-subspace operators.
127130
idxop_sb = [x for x in zip(indices, operators) if typeof(x[1]) <: Int64]
128131
indices_sb = [x[1] for x in idxop_sb]
129132
ops_sb = [x[2] for x in idxop_sb]
@@ -135,6 +138,7 @@ function embed(basis_l::CompositeBasis, basis_r::CompositeBasis,
135138

136139
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]...)
137140

141+
# Embed all joint-subspace operators.
138142
idxop_comp = [x for x in zip(indices, operators) if typeof(x[1]) <: Array]
139143

140144
for (idxs, op) in idxop_comp

test/test_operators.jl

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,45 @@ b_comp = b⊗b
7070
@test_throws bases.IncompatibleBases embed(b_comp, [[1,2],3], [op,op2])
7171
@test_throws bases.IncompatibleBases embed(b_comp, [[1,3],4], [op,op2])
7272

73+
function basis_vec(n, N)
74+
x = zeros(Complex{Float64}, N)
75+
x[n+1] = 1
76+
return x
77+
end
78+
function basis_maker(dims...)
79+
function bm(ns...)
80+
bases = [basis_vec(n, dim) for (n, dim) in zip(ns, dims)][end:-1:1]
81+
return reduce(kron, bases)
82+
end
83+
end
84+
85+
embed_op = embed(b_comp, [1,4], op)
86+
bv = basis_maker(3,2,3,2)
87+
all_idxs = [(idx, jdx) for (idx, jdx) in [Iterators.product(0:1, 0:2)...]]
88+
89+
m11 = reshape([Bra(b_comp, bv(0,idx,jdx,0)) * embed_op * Ket(b_comp, bv(0,kdx,ldx,0))
90+
for ((idx, jdx), (kdx, ldx)) in Iterators.product(all_idxs, all_idxs)], (6,6))
91+
@test isapprox(m11 / op.data[1, 1], diagm(0=>ones(Complex{Float64}, 6)))
92+
93+
m21 = reshape([Bra(b_comp, bv(1,idx,jdx,0)) * embed_op * Ket(b_comp, bv(0,kdx,ldx,0))
94+
for ((idx, jdx), (kdx, ldx)) in Iterators.product(all_idxs, all_idxs)], (6,6))
95+
@test isapprox(m21 / op.data[2,1], diagm(0=>ones(Complex{Float64}, 6)))
96+
97+
m12 = reshape([Bra(b_comp, bv(0,idx,jdx,0)) * embed_op * Ket(b_comp, bv(1,kdx,ldx,0))
98+
for ((idx, jdx), (kdx, ldx)) in Iterators.product(all_idxs, all_idxs)], (6,6))
99+
@test isapprox(m12 / op.data[1,2], diagm(0=>ones(Complex{Float64}, 6)))
100+
101+
73102
b_comp = b_compb_comp
74103
OP_test1 = dense(tensor([op1,one(b2),op,one(b1),one(b2),op1,one(b2)]...))
75104
OP_test2 = embed(b_comp, [1,[3,4],7], [op1,op,op1])
76-
OP_dif_data = (OP_test1 - OP_test2).data
77-
@test real(sum(abs.(OP_dif_data))) < 1e-10
105+
@test isapprox(OP_test1.data, OP_test2.data)
78106

79107
b8 = b2b2b2
80108
cnot = [1 0 0 0; 0 1 0 0; 0 0 0 1; 0 0 1 0]
81109
op_cnot = DenseOperator(b2b2, cnot)
82110
OP_cnot = embed(b8, [1,3], op_cnot)
83-
@assert ptrace(OP_cnot, [2])/2. == op_cnot
111+
@test ptrace(OP_cnot, [2])/2. == op_cnot
84112

85113
@test_throws ErrorException QuantumOptics.operators.gemm!()
86114
@test_throws ErrorException QuantumOptics.operators.gemv!()

0 commit comments

Comments
 (0)