From 4e6aa402838de1a1c0e49b05f20fdf95b88bb23d Mon Sep 17 00:00:00 2001 From: Stefan Krastanov Date: Sun, 11 Aug 2024 03:17:21 -0400 Subject: [PATCH] gosh... I really broke dagger --- src/QSymbolicsBase/basic_ops_homogeneous.jl | 23 +++++++++++---------- src/QSymbolicsBase/linalg.jl | 14 +++++++++++-- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/QSymbolicsBase/basic_ops_homogeneous.jl b/src/QSymbolicsBase/basic_ops_homogeneous.jl index 0de8570..ed41ff8 100644 --- a/src/QSymbolicsBase/basic_ops_homogeneous.jl +++ b/src/QSymbolicsBase/basic_ops_homogeneous.jl @@ -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. ## @@ -13,7 +13,7 @@ julia> 2*k 2|k⟩ julia> @op A -A +A julia> 2*A 2A @@ -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) @@ -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 @@ -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) @@ -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 @@ -123,7 +124,7 @@ end ```jldoctest julia> @op A; @op B; -julia> A*B +julia> A*B AB ``` """ @@ -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) @@ -163,7 +164,7 @@ julia> k₁ ⊗ k₂ julia> @op A; @op B; -julia> A ⊗ B +julia> A ⊗ B (A⊗B) ``` """ diff --git a/src/QSymbolicsBase/linalg.jl b/src/QSymbolicsBase/linalg.jl index 77c1762..0fd7362 100644 --- a/src/QSymbolicsBase/linalg.jl +++ b/src/QSymbolicsBase/linalg.jl @@ -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))...)