Skip to content

Commit

Permalink
Remove sparse_identity and dense_identity functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
bastikr committed Jan 31, 2017
1 parent 6c8009e commit 6d737b6
Show file tree
Hide file tree
Showing 16 changed files with 52 additions and 36 deletions.
4 changes: 1 addition & 3 deletions docs/source/api.rst
Expand Up @@ -96,7 +96,7 @@ Operators
.. jl:autofunction:: operators.jl normalize!
.. jl:autofunction:: operators_sparse.jl sparse_identityoperator
.. jl:autofunction:: operators.jl identityoperator
.. jl:autofunction:: operators.jl expect
Expand All @@ -122,8 +122,6 @@ DenseOperators
.. jl:autofunction:: operators.jl full
.. jl:autofunction:: operators.jl dense_identityoperator
.. jl:autofunction:: operators.jl permutesystems
Expand Down
2 changes: 1 addition & 1 deletion examples/particle2d.jl
Expand Up @@ -27,7 +27,7 @@ psamplepoints = quantumoptics.particle.samplepoints(basis_momentum)
@time p = SparseOperator(quantumoptics.particle.momentumoperator(basis_position))
@time x = SparseOperator(quantumoptics.particle.positionoperator(basis_position))
@time V = SparseOperator(basis_position, diagm(complex(omega)^2*xsamplepoints.^2))
@time I = sparse_identityoperator(basis_position)
@time I = identityoperator(basis_position)

@time p2 = p^2/2
@time Hkin2d = (tensor(p2, I) + tensor(I, p2))
Expand Down
4 changes: 2 additions & 2 deletions src/QuantumOptics.jl
Expand Up @@ -7,8 +7,8 @@ export bases, Basis, GenericBasis, CompositeBasis,
states, StateVector, Bra, Ket, basis_bra, basis_ket,
dagger, normalize, normalize!,
operators, Operator, expect, identityoperator, ptrace, embed,
operators_dense, DenseOperator, projector, dense_identityoperator,
operators_sparse, SparseOperator, sparse_identityoperator, diagonaloperator,
operators_dense, DenseOperator, projector,
operators_sparse, SparseOperator, diagonaloperator,
operators_lazy, LazyOperator, LazyTensor, LazySum, LazyProduct,
super, DenseSuperOperator, SparseSuperOperator,
spre, spost, liouvillian,
Expand Down
1 change: 1 addition & 0 deletions src/operators.jl
Expand Up @@ -56,6 +56,7 @@ dagger(a::Operator) = arithmetic_unary_error("Hermitian conjugate", a)
Identity operator.
"""
identityoperator{T<:Operator}(::Type{T}, b1::Basis, b2::Basis) = throw(ArgumentError("Identity operator not defined for operator type $T."))
identityoperator{T<:Operator}(::Type{T}, b::Basis) = identityoperator(T, b, b)
identityoperator{T<:Operator}(op::T) = identityoperator(T, op.basis_l, op.basis_r)

"""
Expand Down
9 changes: 1 addition & 8 deletions src/operators_dense.jl
Expand Up @@ -9,7 +9,7 @@ using Base.LinAlg
using Base.Cartesian
using ..bases, ..states, ..operators

export DenseOperator, projector, dense_identityoperator
export DenseOperator, projector

"""
Dense array implementation of Operator.
Expand All @@ -33,7 +33,6 @@ Converting an arbitrary Operator into a DenseOperator.
"""
Base.copy(x::DenseOperator) = deepcopy(x)
Base.full(x::DenseOperator) = deepcopy(x)
Base.full(op::Operator) = op*dense_identityoperator(op.basis_r)

==(x::DenseOperator, y::DenseOperator) = (x.basis_l == y.basis_l) && (x.basis_r == y.basis_r) && (x.data == y.data)

Expand Down Expand Up @@ -81,9 +80,6 @@ end

dagger(x::DenseOperator) = DenseOperator(x.basis_r, x.basis_l, x.data')

dense_identityoperator(b::Basis) = DenseOperator(b, b, eye(Complex, length(b)))
dense_identityoperator(b1::Basis, b2::Basis) = DenseOperator(b1, b2, eye(Complex, length(b1), length(b2)))
dense_identityoperator(op::Operator) = dense_identityoperator(op.basis_l, op.basis_r)
identityoperator(::Type{DenseOperator}, b1::Basis, b2::Basis) = DenseOperator(b1, b2, eye(Complex128, length(b1), length(b2)))

trace(op::DenseOperator) = trace(op.data)
Expand Down Expand Up @@ -145,9 +141,6 @@ p-norm of given operator.
Base.norm(op::DenseOperator, p) = norm(op.data, p)


Base.prod{B<:Basis, T<:AbstractArray}(basis::B, operators::T) = (length(operators)==0 ? dense_identityoperator(basis) : prod(operators))


# Partial trace for dense operators.
function _strides(shape::Vector{Int})
N = length(shape)
Expand Down
4 changes: 2 additions & 2 deletions src/operators_lazy.jl
Expand Up @@ -111,7 +111,7 @@ function operators.full(x::LazyTensor)
if i in keys(x.operators)
push!(op_list, full(x.operators[i]))
else
push!(op_list, dense_identityoperator(x.basis_l.bases[i], x.basis_r.bases[i]))
push!(op_list, identityoperator(DenseOperator, x.basis_l.bases[i], x.basis_r.bases[i]))
end
end
return x.factor*tensor(op_list...)
Expand Down Expand Up @@ -139,7 +139,7 @@ function operators_sparse.sparse(x::LazyTensor)
if i in keys(x.operators)
push!(op_list, sparse(x.operators[i]))
else
push!(op_list, sparse_identityoperator(x.basis_l.bases[i], x.basis_r.bases[i]))
push!(op_list, identityoperator(SparseOperator, x.basis_l.bases[i], x.basis_r.bases[i]))
end
end
return x.factor*tensor(op_list...)
Expand Down
5 changes: 1 addition & 4 deletions src/operators_sparse.jl
Expand Up @@ -6,7 +6,7 @@ import ..operators: dagger, identityoperator,

using ..bases, ..states, ..operators, ..operators_dense, ..sparsematrix

export SparseOperator, diagonaloperator, sparse_identityoperator
export SparseOperator, diagonaloperator


"""
Expand Down Expand Up @@ -58,9 +58,6 @@ Base.sparse(a::DenseOperator) = SparseOperator(a.basis_l, a.basis_r, sparse(a.da

dagger(x::SparseOperator) = SparseOperator(x.basis_r, x.basis_l, x.data')

sparse_identityoperator(b::Basis) = SparseOperator(b, b, speye(Complex128, length(b)))
sparse_identityoperator(b1::Basis, b2::Basis) = SparseOperator(b1, b2, speye(Complex128, length(b1), length(b2)))

identityoperator(::Type{SparseOperator}, b1::Basis, b2::Basis) = SparseOperator(b1, b2, speye(Complex128, length(b1), length(b2)))
identityoperator(b1::Basis, b2::Basis) = identityoperator(SparseOperator, b1, b2)
identityoperator(b::Basis) = identityoperator(b, b)
Expand Down
1 change: 1 addition & 0 deletions src/particle.jl
Expand Up @@ -344,6 +344,7 @@ function FFTOperator(basis_l::PositionBasis, basis_r::MomentumBasis)
FFTOperator(basis_l, basis_r, plan_fft!(x), plan_bfft!(x), mul_before, mul_after)
end

operators.full(op::FFTOperator) = op*identityoperator(DenseOperator, op.basis_r)

operators.dagger(op::FFTOperator) = FFTOperator(op.basis_r, op.basis_l)

Expand Down
2 changes: 1 addition & 1 deletion test/test_cumulantexpansion.jl
Expand Up @@ -5,7 +5,7 @@ T = [0.:0.1:0.5;]

# Define Spin 1/2 operators
spinbasis = SpinBasis(1//2)
I = dense_identityoperator(spinbasis)
I = full(identityoperator(spinbasis))
sigmax = full(spin.sigmax(spinbasis))
sigmay = full(spin.sigmay(spinbasis))
sigmaz = full(spin.sigmaz(spinbasis))
Expand Down
6 changes: 3 additions & 3 deletions test/test_embed.jl
Expand Up @@ -10,9 +10,9 @@ b1 = NLevelBasis(3)
b2 = SpinBasis(1//2)
b3 = FockBasis(2)

I1 = dense_identityoperator(b1)
I2 = dense_identityoperator(b2)
I3 = dense_identityoperator(b3)
I1 = full(identityoperator(b1))
I2 = full(identityoperator(b2))
I3 = full(identityoperator(b3))

b = b1 b2 b3

Expand Down
32 changes: 32 additions & 0 deletions test/test_identityoperator.jl
@@ -0,0 +1,32 @@
using Base.Test
using QuantumOptics

b1 = NLevelBasis(2)
b2 = SpinBasis(3//2)
b3 = FockBasis(2)
b = b1b2b3

function test_op_equal(op1, op2)
@test_approx_eq_eps 0. tracedistance_general(full(op1), full(op2)) 1e-11
end

Idense = identityoperator(DenseOperator, b)
Isparse = identityoperator(SparseOperator, b)

@test typeof(Idense) == DenseOperator
@test typeof(Isparse) == SparseOperator

@test full(Isparse) == Idense
@test Isparse == identityoperator(b)

@test Isparse == identityoperator(b1) identityoperator(b2) identityoperator(b3)
@test Idense == identityoperator(DenseOperator, b1) identityoperator(DenseOperator, b2) identityoperator(DenseOperator, b3)


op = DenseOperator(b, rand(Complex128, length(b), length(b)))

test_op_equal(op, identityoperator(op)*op)
test_op_equal(op, op*identityoperator(op))

test_op_equal(sparse(op), identityoperator(op)*op)
test_op_equal(sparse(op), sparse(op)*identityoperator(sparse(op)))
4 changes: 2 additions & 2 deletions test/test_lazyoperators.jl
Expand Up @@ -16,8 +16,8 @@ sx = sigmax(spinbasis)
sy = sigmay(spinbasis)
sz = sigmaz(spinbasis)

Ispin = sparse_identityoperator(spinbasis)
Ifock = sparse_identityoperator(fockbasis)
Ispin = identityoperator(spinbasis)
Ifock = identityoperator(fockbasis)

# LazyTensor
basis = tensor(spinbasis, fockbasis, spinbasis)
Expand Down
5 changes: 1 addition & 4 deletions test/test_operators.jl
Expand Up @@ -24,7 +24,7 @@ ybra = dagger(yket)

op1 = DenseOperator(spinbasis, GenericBasis([3]), [1 1 1; 1 1 1])
op2 = DenseOperator(GenericBasis([3]), spinbasis, [1 1; 1 1; 1 1])
I = dense_identityoperator(fockbasis)
I = full(identityoperator(fockbasis))


# Test creation
Expand Down Expand Up @@ -61,9 +61,6 @@ op_diag = P*op*dagger(P)
op_diag_exp = DenseOperator(v, diagm(exp(diag(op_diag.data))))
@test_approx_eq_eps 0. tracedistance(expm(op), dagger(P)*op_diag_exp*P) 1e-13

# Test identity function
@test full(I) == dense_identityoperator(a)

# Test gemv implementation
result_ket = deepcopy(xket)
operators.gemv!(complex(1.0), at, xket, complex(0.), result_ket)
Expand Down
2 changes: 1 addition & 1 deletion test/test_particle.jl
Expand Up @@ -135,7 +135,7 @@ operators.gemv!(Complex(1.), LazyProduct(Txp, Tpx), psix0, Complex(0.), psi_)
@test_approx_eq_eps 0. norm(Txp*(Tpx*psix0) - psix0) 1e-12

psi_ = deepcopy(psix0)
I = dense_identityoperator(basis_momentum)
I = full(identityoperator(basis_momentum))
operators.gemv!(Complex(1.), LazyProduct(Txp, I, Tpx), psix0, Complex(0.), psi_)
@test_approx_eq_eps 0. norm(psi_ - psix0) 1e-12
@test_approx_eq_eps 0. norm(Txp*I*(Tpx*psix0) - psix0) 1e-12
Expand Down
5 changes: 1 addition & 4 deletions test/test_sparseoperators.jl
Expand Up @@ -19,10 +19,7 @@ sz_dense = full(sz)
@test sparse(sx_dense) == sx

b = FockBasis(3)
I = sparse_identityoperator(b)
I_dense = dense_identityoperator(b)

@test I == identityoperator(destroy(b))
I = identityoperator(b)

type A <: Operator
end
Expand Down
2 changes: 1 addition & 1 deletion test/test_spin.jl
Expand Up @@ -70,7 +70,7 @@ end
# Test special relations for spin 1/2

spinbasis = SpinBasis(1//2)
I = sparse_identityoperator(spinbasis)
I = identityoperator(spinbasis)
Zero = SparseOperator(spinbasis)
sx = sigmax(spinbasis)
sy = sigmay(spinbasis)
Expand Down

0 comments on commit 6d737b6

Please sign in to comment.