Skip to content

Commit

Permalink
fixes and name changes
Browse files Browse the repository at this point in the history
  • Loading branch information
apkille committed Jun 15, 2024
1 parent 3cc3108 commit 57cd644
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 214 deletions.
17 changes: 8 additions & 9 deletions src/QSymbolicsBase/QSymbolicsBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ using TermInterface
import TermInterface: isexpr, head, iscall, children, operation, arguments, metadata

using LinearAlgebra
import LinearAlgebra: eigvecs, ishermitian
import LinearAlgebra: eigvecs, ishermitian, inv

import QuantumInterface:
apply!,
Expand All @@ -17,7 +17,7 @@ import QuantumInterface:
AbstractKet, AbstractOperator, AbstractSuperOperator, AbstractBra

export SymQObj,QObj,
AbstractRepresentation, AbstractUse,
AbstractRepresentation, AbstractUse,
QuantumOpticsRepr, QuantumMCRepr, CliffordRepr,
UseAsState, UseAsObservable, UseAsOperation,
apply!,
Expand All @@ -33,15 +33,14 @@ export SymQObj,QObj,
SAddBra,SAddKet,SAddOperator,
SScaledBra,SScaledOperator,SScaledKet,
STensorBra,STensorKet,STensorOperator,
SProjector,MixedState,IdentityOp,SInverseOperator,SHermitianOperator,SUnitaryOperator,SCommutativeOperator,
SApplyKet,SApplyBra,SApplyOp,SApplyOpSuper,SCommutator,SAnticommutator,SDagger,SBraKet,SOuterKetBra,
SProjector,MixedState,IdentityOp,SInvOperator,SHermitianOperator,SUnitaryOperator,SHermitianUnitaryOperator,
SApplyKet,SApplyBra,SMulOperator,SApplySuperop,SCommutator,SAnticommutator,SDagger,SBraKet,SOuterKetBra,
HGate, XGate, YGate, ZGate, CPHASEGate, CNOTGate,
XBasisState, YBasisState, ZBasisState,
NumberOp, CreateOp, DestroyOp,
XCXGate, XCYGate, XCZGate, YCXGate, YCYGate, YCZGate, ZCXGate, ZCYGate, ZCZGate,
circuit_simplify, commutator_simplify, anticommutator_simplify,
ishermitian, isunitary, iscommutative,
hermitian, unitary, commutative, inverse
XCXGate, XCYGate, XCZGate, YCXGate, YCYGate, YCZGate, ZCXGate, ZCYGate, ZCZGate,
pauli_simplify, commutator_simplify, anticommutator_simplify,
isunitary

function countmap(samples) # A simpler version of StatsBase.countmap, because StatsBase is slow to import
counts = Dict{Any,Any}()
Expand Down Expand Up @@ -164,7 +163,7 @@ propsequal(x,y) = all(n->isequal(getproperty(x,n),getproperty(y,n)), propertynam
# Most symbolic objects defined here
##

include("objects.jl")
include("literal_objects.jl")
include("basic_ops_homogeneous.jl")
include("basic_ops_inhomogeneous.jl")
include("predefined.jl")
Expand Down
44 changes: 13 additions & 31 deletions src/QSymbolicsBase/basic_ops_homogeneous.jl
Original file line number Diff line number Diff line change
Expand Up @@ -123,22 +123,22 @@ julia> A*B
AB
```
"""
@withmetadata struct SApplyOp <: Symbolic{AbstractOperator}
@withmetadata struct SMulOperator <: Symbolic{AbstractOperator}
terms
function SApplyOp(terms)
function SMulOperator(terms)
coeff, cleanterms = prefactorscalings(terms)
coeff*new(cleanterms)
end
end
isexpr(::SApplyOp) = true
iscall(::SApplyOp) = true
arguments(x::SApplyOp) = x.terms
operation(x::SApplyOp) = *
head(x::SApplyOp) = :*
children(x::SApplyOp) = pushfirst!(x.terms,:*)
Base.:(*)(xs::Symbolic{AbstractOperator}...) = SApplyOp(collect(xs))
Base.show(io::IO, x::SApplyOp) = print(io, join(map(string, arguments(x)),""))
basis(x::SApplyOp) = basis(x.terms)
isexpr(::SMulOperator) = true
iscall(::SMulOperator) = true
arguments(x::SMulOperator) = x.terms
operation(x::SMulOperator) = *
head(x::SMulOperator) = :*
children(x::SMulOperator) = pushfirst!(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)

"""Tensor product of quantum objects (kets, operators, or bras)
Expand Down Expand Up @@ -187,14 +187,8 @@ julia> A = SOperator(:A, SpinBasis(1//2)); B = SOperator(:B, SpinBasis(1//2));
julia> commutator(A, B)
[A,B]
julia> expand(commutator(A, B))
(AB+-1BA)
julia> commutator(A, A)
0
julia> commutator(commutative(A), B)
0
```
"""
@withmetadata struct SCommutator <: Symbolic{AbstractOperator}
Expand All @@ -212,12 +206,9 @@ operation(x::SCommutator) = commutator
head(x::SCommutator) = :commutator
children(x::SCommutator) = [:commutator, x.op1, x.op2]
commutator(o1::Symbolic{AbstractOperator}, o2::Symbolic{AbstractOperator}) = SCommutator(o1, o2)
commutator(o1::SCommutativeOperator, o2::Symbolic{AbstractOperator}) = 0
commutator(o1::Symbolic{AbstractOperator}, o2::SCommutativeOperator) = 0
commutator(o1::SCommutativeOperator, o2::SCommutativeOperator) = 0
Base.show(io::IO, x::SCommutator) = print(io, "[$(x.op1),$(x.op2)]")
basis(x::SCommutator) = basis(x.op1)
expand(x::SCommutator) = x == 0 ? x : SApplyOp([x.op1, x.op2]) - SApplyOp([x.op2, x.op1])
expand(x::SCommutator) = x == 0 ? x : SMulOperator([x.op1, x.op2]) - SMulOperator([x.op2, x.op1])

"""Symbolic anticommutator of two operators
Expand All @@ -226,12 +217,6 @@ julia> A = SOperator(:A, SpinBasis(1//2)); B = SOperator(:B, SpinBasis(1//2));
julia> anticommutator(A, B)
{A,B}
julia> expand(anticommutator(A, B))
(AB+BA)
julia> anticommutator(commutative(A), B)
2AB
```
"""
@withmetadata struct SAnticommutator <: Symbolic{AbstractOperator}
Expand All @@ -249,9 +234,6 @@ operation(x::SAnticommutator) = anticommutator
head(x::SAnticommutator) = :anticommutator
children(x::SAnticommutator) = [:anticommutator, x.op1, x.op2]
anticommutator(o1::Symbolic{AbstractOperator}, o2::Symbolic{AbstractOperator}) = SAnticommutator(o1, o2)
anticommutator(o1::SCommutativeOperator, o2::Symbolic{AbstractOperator}) = 2*o1*o2
anticommutator(o1::Symbolic{AbstractOperator}, o2::SCommutativeOperator) = 2*o1*o2
anticommutator(o1::SCommutativeOperator, o2::SCommutativeOperator) = 2*o1*o2
Base.show(io::IO, x::SAnticommutator) = print(io, "{$(x.op1),$(x.op2)}")
basis(x::SAnticommutator) = basis(x.op1)
expand(x::SAnticommutator) = x == 0 ? x : SApplyOp([x.op1, x.op2]) + SApplyOp([x.op2, x.op1])
expand(x::SAnticommutator) = x == 0 ? x : SMulOperator([x.op1, x.op2]) + SMulOperator([x.op2, x.op1])
26 changes: 11 additions & 15 deletions src/QSymbolicsBase/basic_ops_inhomogeneous.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,6 @@ julia> b*k
@withmetadata struct SBraKet <: Symbolic{Complex}
bra
ket
function SBraKet(b, k)
coeff, cleanterms = prefactorscalings([b k])
coeff*new(cleanterms...)
end
end
isexpr(::SBraKet) = true
iscall(::SBraKet) = true
Expand All @@ -80,20 +76,20 @@ Base.:(*)(b::Symbolic{AbstractBra}, k::Symbolic{AbstractKet}) = SBraKet(b,k)
Base.show(io::IO, x::SBraKet) = begin print(io,x.bra); print(io,x.ket) end

"""Symbolic application of a superoperator on an operator"""
@withmetadata struct SApplyOpSuper <: Symbolic{AbstractOperator}
@withmetadata struct SApplySuperop <: Symbolic{AbstractOperator}
sop
op
end
isexpr(::SApplyOpSuper) = true
iscall(::SApplyOpSuper) = true
arguments(x::SApplyOpSuper) = [x.sop,x.op]
operation(x::SApplyOpSuper) = *
head(x::SApplyOpSuper) = :*
children(x::SApplyOpSuper) = [:*,x.sop,x.op]
Base.:(*)(sop::Symbolic{AbstractSuperOperator}, op::Symbolic{AbstractOperator}) = SApplyOpSuper(sop,op)
Base.:(*)(sop::Symbolic{AbstractSuperOperator}, k::Symbolic{AbstractKet}) = SApplyOpSuper(sop,SProjector(k))
Base.show(io::IO, x::SApplyOpSuper) = begin print(io, x.sop); print(io, x.op) end
basis(x::SApplyOpSuper) = basis(x.op)
isexpr(::SApplySuperop) = true
iscall(::SApplySuperop) = true
arguments(x::SApplySuperop) = [x.sop,x.op]
operation(x::SApplySuperop) = *
head(x::SApplySuperop) = :*
children(x::SApplySuperop) = [:*,x.sop,x.op]
Base.:(*)(sop::Symbolic{AbstractSuperOperator}, op::Symbolic{AbstractOperator}) = SApplySuperop(sop,op)
Base.:(*)(sop::Symbolic{AbstractSuperOperator}, k::Symbolic{AbstractKet}) = SApplySuperop(sop,SProjector(k))
Base.show(io::IO, x::SApplySuperop) = begin print(io, x.sop); print(io, x.op) end
basis(x::SApplySuperop) = basis(x.op)

"""Symbolic outer product of a ket and a bra
```jldoctest
Expand Down
52 changes: 52 additions & 0 deletions src/QSymbolicsBase/literal_objects.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
##
# This file defines quantum objects (kets, bras, and operators) with various properties
##

struct SKet <: Symbolic{AbstractKet}
name::Symbol
basis::Basis
end

struct SBra <: Symbolic{AbstractBra}
name::Symbol
basis::Basis
end

struct SOperator <: Symbolic{AbstractOperator}
name::Symbol
basis::Basis
end
ishermitian(x::SOperator) = false
isunitary(x::SOperator) = false

struct SHermitianOperator <: Symbolic{AbstractOperator}
name::Symbol
basis::Basis
end
ishermitian(::SHermitianOperator) = true
isunitary(::SHermitianOperator) = false

struct SUnitaryOperator <: Symbolic{AbstractOperator}
name::Symbol
basis::Basis
end
ishermitian(::SUnitaryOperator) = false
isunitary(::SUnitaryOperator) = true

struct SHermitianUnitaryOperator <: Symbolic{AbstractOperator}
name::Symbol
basis::Basis
end
ishermitian(::SHermitianUnitaryOperator) = true
isunitary(::SHermitianUnitaryOperator) = true

const SymQ = Union{SKet, SBra, SOperator, SHermitianOperator, SUnitaryOperator, SHermitianUnitaryOperator}
isexpr(::SymQ) = false
metadata(::SymQ) = nothing
symbollabel(x::SymQ) = x.name
basis(x::SymQ) = x.basis

Base.show(io::IO, x::SKet) = print(io, "|$(symbollabel(x))")
Base.show(io::IO, x::SBra) = print(io, "$(symbollabel(x))|")
Base.show(io::IO, x::Union{SOperator, SHermitianOperator, SUnitaryOperator, SHermitianUnitaryOperator}) = print(io, "$(symbollabel(x))")
Base.show(io::IO, x::SymQObj) = print(io, symbollabel(x)) # fallback that probably is not great
71 changes: 0 additions & 71 deletions src/QSymbolicsBase/objects.jl

This file was deleted.

Loading

0 comments on commit 57cd644

Please sign in to comment.