diff --git a/src/QSymbolicsBase/basic_ops_homogeneous.jl b/src/QSymbolicsBase/basic_ops_homogeneous.jl index a2d9652..60fb88a 100644 --- a/src/QSymbolicsBase/basic_ops_homogeneous.jl +++ b/src/QSymbolicsBase/basic_ops_homogeneous.jl @@ -29,8 +29,8 @@ arguments(x::SScaled) = [x.coeff,x.obj] operation(x::SScaled) = * head(x::SScaled) = :* children(x::SScaled) = [:*,x.coeff,x.obj] -Base.:(*)(c, x::Symbolic{T}) where {T<:QObj} = c == 0 ? 0 : SScaled{T}(c,x) -Base.:(*)(x::Symbolic{T}, c) where {T<:QObj} = c == 0 ? 0 : SScaled{T}(c,x) +Base.:(*)(c, x::Symbolic{T}) where {T<:QObj} = SScaled{T}(c,x) +Base.:(*)(x::Symbolic{T}, c) where {T<:QObj} = SScaled{T}(c,x) Base.:(/)(x::Symbolic{T}, c) where {T<:QObj} = SScaled{T}(1/c,x) basis(x::SScaled) = basis(x.obj) @@ -85,17 +85,17 @@ basis(x::SAdd) = basis(first(x.dict).first) const SAddKet = SAdd{AbstractKet} function Base.show(io::IO, x::SAddKet) ordered_terms = sort([repr(i) for i in arguments(x)]) - print(io, "("*join(map(string, ordered_terms),"+")::String*")") # type assert to help inference + print(io, "("*join(ordered_terms,"+")::String*")") # type assert to help inference end const SAddOperator = SAdd{AbstractOperator} function Base.show(io::IO, x::SAddOperator) ordered_terms = sort([repr(i) for i in arguments(x)]) - print(io, "("*join(map(string, ordered_terms),"+")::String*")") # type assert to help inference + print(io, "("*join(ordered_terms,"+")::String*")") # type assert to help inference end const SAddBra = SAdd{AbstractBra} function Base.show(io::IO, x::SAddBra) ordered_terms = sort([repr(i) for i in arguments(x)]) - print(io, "("*join(map(string, ordered_terms),"+")::String*")") # type assert to help inference + print(io, "("*join(ordered_terms,"+")::String*")") # type assert to help inference end """Symbolic application of operator on operator @@ -119,7 +119,7 @@ iscall(::SMulOperator) = true arguments(x::SMulOperator) = x.terms operation(x::SMulOperator) = * head(x::SMulOperator) = :* -children(x::SMulOperator) = pushfirst!(x.terms,:*) +children(x::SMulOperator) = [:*;x.terms] Base.:(*)(xs::Symbolic{AbstractOperator}...) = SMulOperator(collect(xs)) Base.show(io::IO, x::SMulOperator) = print(io, join(map(string, arguments(x)),"")) basis(x::SMulOperator) = basis(x.terms) diff --git a/src/QSymbolicsBase/predefined.jl b/src/QSymbolicsBase/predefined.jl index fa053ec..fc011d6 100644 --- a/src/QSymbolicsBase/predefined.jl +++ b/src/QSymbolicsBase/predefined.jl @@ -235,7 +235,7 @@ end julia> a = SKet(:a, SpinBasis(1//2)); A = SOperator(:A, SpinBasis(1//2)); julia> dagger(2*im*A*a) -0 - 2im⟨a|A† +0 - 2im|a⟩†A† julia> B = SOperator(:B, SpinBasis(1//2)); @@ -263,13 +263,10 @@ children(x::SDagger) = [:dagger, x.obj] dagger(x::Symbolic{AbstractBra}) = SDagger{AbstractKet}(x) dagger(x::Symbolic{AbstractKet}) = SDagger{AbstractBra}(x) dagger(x::Symbolic{AbstractOperator}) = SDagger{AbstractOperator}(x) -dagger(x::SKet) = SBra(x.name, x.basis) dagger(x::SScaledKet) = SScaledBra(conj(x.coeff), dagger(x.obj)) dagger(x::SAddKet) = SAddBra(Dict(dagger(k)=>v for (k,v) in pairs(x.dict))) -dagger(x::SBra) = SKet(x.name, x.basis) dagger(x::SScaledBra) = SScaledKet(conj(x.coeff), dagger(x.obj)) dagger(x::SAddBra) = SAddKet(Dict(dagger(b)=>v for (b,v) in pairs(x.dict))) -dagger(x::SOperator) = SDagger{AbstractOperator}(x) dagger(x::SAddOperator) = SAddOperator(Dict(dagger(o)=>v for (o,v) in pairs(x.dict))) dagger(x::SHermitianOperator) = x dagger(x::SHermitianUnitaryOperator) = x diff --git a/src/QSymbolicsBase/rules.jl b/src/QSymbolicsBase/rules.jl index d5570e2..6a8d6b7 100644 --- a/src/QSymbolicsBase/rules.jl +++ b/src/QSymbolicsBase/rules.jl @@ -22,10 +22,10 @@ function prefactorscalings(xs; scalar=false) # If the scalar keyword is true, th for x in xs if isexpr(x) && operation(x) == * c,t = arguments(x) - if scalar == false + if !scalar coeff *= c push!(terms,t) - elseif scalar == true && c isa Number + elseif scalar && c isa Number coeff *= c push!(terms, t) else @@ -127,7 +127,7 @@ julia> qsimplify(anticommutator(σˣ, σˣ), rewriter=qsimplify_anticommutator) """ function qsimplify(s; rewriter=nothing) if QuantumSymbolics.isexpr(s) - if rewriter == nothing + if isnothing(rewriter) Fixpoint(Chain(RULES_ALL))(s) else Fixpoint(rewriter)(s) diff --git a/test/test_dagger.jl b/test/test_dagger.jl index 8fbadde..ce740b9 100644 --- a/test/test_dagger.jl +++ b/test/test_dagger.jl @@ -15,11 +15,9 @@ U = SUnitaryOperator(:U, SpinBasis(1//2)) ℋ = SHermitianOperator(:ℋ, SpinBasis(1//2)) @testset "symbolic dagger tests" begin - @test isequal(dagger(k₁), SBra(:k₁, SpinBasis(1//2))) - @test isequal(dagger(im*k₁), -im*SBra(:k₁, SpinBasis(1//2))) + @test isequal(dagger(im*k₁), -im*dagger(k₁)) @test isequal(dagger(k₁+k₂), dagger(k₁)+dagger(k₂)) - @test isequal(dagger(b₁), SKet(:b₁, SpinBasis(1//2))) - @test isequal(dagger(im*b₁), -im*SKet(:b₁, SpinBasis(1//2))) + @test isequal(dagger(im*b₁), -im*dagger(b₁)) @test isequal(dagger(b₁+b₂), dagger(b₁)+dagger(b₂)) @test isequal(dagger(A+B), dagger(A) + dagger(B)) @test isequal(dagger(ℋ), ℋ)