Skip to content

Commit

Permalink
gosh... I really broke dagger
Browse files Browse the repository at this point in the history
  • Loading branch information
Krastanov committed Aug 11, 2024
1 parent 48e2972 commit 4e6aa40
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
23 changes: 12 additions & 11 deletions src/QSymbolicsBase/basic_ops_homogeneous.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
##
# This file defines the symbolic operations for quantum objects (kets, operators, and bras)
# This file defines the symbolic operations for quantum objects (kets, operators, and bras)
# that are homogeneous in their arguments.
##

Expand All @@ -13,7 +13,7 @@ julia> 2*k
2|k⟩
julia> @op A
A
A
julia> 2*A
2A
Expand All @@ -29,18 +29,19 @@ arguments(x::SScaled) = [x.coeff,x.obj]
operation(x::SScaled) = *
head(x::SScaled) = :*
children(x::SScaled) = [:*,x.coeff,x.obj]
function Base.:(*)(c, x::Symbolic{T}) where {T<:QObj}
function Base.:(*)(c, x::Symbolic{T}) where {T<:QObj}
if (isa(c, Number) && iszero(c)) || iszero(x)
SZero{T}()
elseif _isone(c)
x
elseif isa(x, SScaled)
SScaled{T}(c*x.coeff, x.obj)
else
SScaled{T}(c, x)
else
SScaled{T}(c, x)
end
end
Base.:(*)(x::Symbolic{T}, c) where {T<:QObj} = c*x
Base.:(*)(x::Symbolic{T}, y::Symbolic{S}) where {T<:QObj,S<:QObj} = throw(ArgumentError("multiplication between $(typeof(x)) and $(typeof(y)) is not defined; maybe you are looking for a tensor product `tensor`"))
Base.:(/)(x::Symbolic{T}, c) where {T<:QObj} = iszero(c) ? throw(DomainError(c,"cannot divide QSymbolics expressions by zero")) : (1/c)*x
basis(x::SScaled) = basis(x.obj)

Expand Down Expand Up @@ -83,7 +84,7 @@ julia> k₁ + k₂
_set_precomputed
_arguments_precomputed
end
function SAdd{S}(d) where S
function SAdd{S}(d) where S
terms = [c*obj for (obj,c) in d]
length(d)==1 ? first(terms) : SAdd{S}(d,Set(terms),terms)
end
Expand All @@ -93,7 +94,7 @@ arguments(x::SAdd) = x._arguments_precomputed
operation(x::SAdd) = +
head(x::SAdd) = :+
children(x::SAdd) = [:+; x._arguments_precomputed]
function Base.:(+)(xs::Vararg{Symbolic{T},N}) where {T<:QObj,N}
function Base.:(+)(xs::Vararg{Symbolic{T},N}) where {T<:QObj,N}
xs = collect(xs)
f = first(xs)
nonzero_terms = filter!(x->!iszero(x),xs)
Expand All @@ -113,7 +114,7 @@ function Base.show(io::IO, x::SAddKet)
print(io, "("*join(ordered_terms,"+")::String*")") # type assert to help inference
end
const SAddOperator = SAdd{AbstractOperator}
function Base.show(io::IO, x::SAddOperator)
function Base.show(io::IO, x::SAddOperator)
ordered_terms = sort([repr(i) for i in arguments(x)])
print(io, "("*join(ordered_terms,"+")::String*")") # type assert to help inference
end
Expand All @@ -123,7 +124,7 @@ end
```jldoctest
julia> @op A; @op B;
julia> A*B
julia> A*B
AB
```
"""
Expand All @@ -136,7 +137,7 @@ arguments(x::SMulOperator) = x.terms
operation(x::SMulOperator) = *
head(x::SMulOperator) = :*
children(x::SMulOperator) = [:*;x.terms]
function Base.:(*)(xs::Symbolic{AbstractOperator}...)
function Base.:(*)(xs::Symbolic{AbstractOperator}...)
zero_ind = findfirst(x->iszero(x), xs)
if isnothing(zero_ind)
if any(x->!(samebases(basis(x),basis(first(xs)))),xs)
Expand All @@ -163,7 +164,7 @@ julia> k₁ ⊗ k₂
julia> @op A; @op B;
julia> A ⊗ B
julia> A ⊗ B
(A⊗B)
```
"""
Expand Down
14 changes: 12 additions & 2 deletions src/QSymbolicsBase/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,22 @@ head(x::SDagger) = :dagger
children(x::SDagger) = [:dagger, x.obj]
"""
dagger(x::Symbolic{AbstractKet})
Symbolic transpose operation. See also [`SDagger`](@ref).
"""
dagger(x::Symbolic{AbstractKet})= SDagger{AbstractBra}(x)
"""
dagger(x::Symbolic{AbstractBra})
Symbolic transpose operation. See also [`SDagger`](@ref).
"""
dagger(x::Symbolic{AbstractBra})= SDagger{AbstractKet}(x)
"""
dagger(x::Symbolic{AbstractOperator})
Symbolic transpose operation. See also [`STranspose`](@ref).
Symbolic transpose operation. See also [`SDagger`](@ref).
"""
dagger(x::Symbolic{T}) where {T<:Union{AbstractKet,AbstractBra,AbstractOperator}} = SDagger{T}(x)
dagger(x::Symbolic{AbstractOperator}) = SDagger{AbstractOperator}(x)
dagger(x::SScaled) = conj(x.coeff)*dagger(x.obj)
dagger(x::SAdd) = (+)((dagger(i) for i in arguments(x))...)
dagger(x::SMulOperator) = (*)((dagger(i) for i in reverse(x.terms))...)
Expand Down

0 comments on commit 4e6aa40

Please sign in to comment.