Skip to content

Commit

Permalink
Merge branch 'main' into newstructs
Browse files Browse the repository at this point in the history
  • Loading branch information
apkille authored Jun 21, 2024
2 parents 900fa6c + 237d158 commit 076399a
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 33 deletions.
3 changes: 2 additions & 1 deletion src/QSymbolicsBase/QSymbolicsBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ export SymQObj,QObj,
X1,X2,Y1,Y2,Z1,Z2,X₁,X₂,Y₁,Y₂,Z₁,Z₂,L0,L1,Lp,Lm,Lpi,Lmi,L₀,L₁,L₊,L₋,L₊ᵢ,L₋ᵢ,
vac,F₀,F0,F₁,F1,
N,n̂,Create,âꜛ,Destroy,â,SpinBasis,FockBasis,
SBra,SKet,SOperator,
SBra,SKet,SOperator,SHermitianOperator,SUnitaryOperator,SHermitianUnitaryOperator,
@ket,@bra,@op,
SAdd,SAddBra,SAddKet,SAddOperator,
SScaled,SScaledBra,SScaledOperator,SScaledKet,
STensorBra,STensorKet,STensorOperator,
Expand Down
16 changes: 8 additions & 8 deletions src/QSymbolicsBase/basic_ops_homogeneous.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
"""Scaling of a quantum object (ket, operator, or bra) by a number
```jldoctest
julia> k = SKet(:k, SpinBasis(1//2))
julia> @ket k
|k⟩
julia> 2*k
2|k⟩
julia> A = SOperator(:A, SpinBasis(1//2))
julia> @op A
A
julia> 2*A
Expand Down Expand Up @@ -63,7 +63,7 @@ end
"""Addition of quantum objects (kets, operators, or bras)
```jldoctest
julia> k₁ = SKet(:k₁, SpinBasis(1//2)); k₂ = SKet(:k₂, SpinBasis(1//2));
julia> @ket k₁; @ket k₂;
julia> k₁ + k₂
(|k₁⟩+|k₂⟩)
Expand Down Expand Up @@ -112,7 +112,7 @@ end
"""Symbolic application of operator on operator
```jldoctest
julia> A = SOperator(:A, SpinBasis(1//2)); B = SOperator(:B, SpinBasis(1//2));
julia> @op A; @op B;
julia> A*B
AB
Expand Down Expand Up @@ -141,12 +141,12 @@ basis(x::SMulOperator) = basis(x.terms)
"""Tensor product of quantum objects (kets, operators, or bras)
```jldoctest
julia> k₁ = SKet(:k₁, SpinBasis(1//2)); k₂ = SKet(:k₂, SpinBasis(1//2));
julia> @ket k₁; @ket k₂;
julia> k₁ ⊗ k₂
|k₁⟩|k₂⟩
julia> A = SOperator(:A, SpinBasis(1//2)); B = SOperator(:B, SpinBasis(1//2));
julia> @op A; @op B;
julia> A ⊗ B
A⊗B
Expand Down Expand Up @@ -183,7 +183,7 @@ Base.show(io::IO, x::STensorBra) = print(io, join(map(string, arguments(x)),""))
"""Symbolic commutator of two operators
```jldoctest
julia> A = SOperator(:A, SpinBasis(1//2)); B = SOperator(:B, SpinBasis(1//2));
julia> @op A; @op B;
julia> commutator(A, B)
[A,B]
Expand Down Expand Up @@ -217,7 +217,7 @@ expand(x::SCommutator) = x == 0 ? x : x.op1*x.op2 - x.op2*x.op1
"""Symbolic anticommutator of two operators
```jldoctest
julia> A = SOperator(:A, SpinBasis(1//2)); B = SOperator(:B, SpinBasis(1//2));
julia> @op A; @op B;
julia> anticommutator(A, B)
{A,B}
Expand Down
8 changes: 4 additions & 4 deletions src/QSymbolicsBase/basic_ops_inhomogeneous.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"""Symbolic application of an operator on a ket (from the left)
```jldoctest
julia> k = SKet(:k, SpinBasis(1//2)); A = SOperator(:A, SpinBasis(1//2));
julia> @ket k; @op A;
julia> A*k
A|k⟩
Expand Down Expand Up @@ -35,7 +35,7 @@ basis(x::SApplyKet) = basis(x.ket)
"""Symbolic application of an operator on a bra (from the right)
```jldoctest
julia> b = SBra(:b, SpinBasis(1//2)); A = SOperator(:A, SpinBasis(1//2));
julia> @bra b; @op A;
julia> b*A
⟨b|A
Expand Down Expand Up @@ -65,7 +65,7 @@ basis(x::SApplyBra) = basis(x.bra)
"""Symbolic inner product of a bra and a ket
```jldoctest
julia> b = SBra(:b, SpinBasis(1//2)); k = SKet(:k, SpinBasis(1//2));
julia> @bra b; @ket k;
julia> b*k
⟨b||k⟩
Expand Down Expand Up @@ -108,7 +108,7 @@ basis(x::SSuperOpApply) = basis(x.op)

"""Symbolic outer product of a ket and a bra
```jldoctest
julia> b = SBra(:b, SpinBasis(1//2)); k = SKet(:k, SpinBasis(1//2));
julia> @bra b; @ket k;
julia> k*b
|k⟩⟨b|
Expand Down
31 changes: 29 additions & 2 deletions src/QSymbolicsBase/literal_objects.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,68 @@
# This file defines quantum objects (kets, bras, and operators) with various properties
##

struct SKet <: Symbolic{AbstractKet}
struct SBra <: Symbolic{AbstractBra}
name::Symbol
basis::Basis
end
SBra(name) = SBra(name, qubit_basis)
macro bra(name, basis)
:($(esc(name)) = SBra($(Expr(:quote, name)), $(basis)))
end
macro bra(name)
:($(esc(name)) = SBra($(Expr(:quote, name))))
end

struct SBra <: Symbolic{AbstractBra}
struct SKet <: Symbolic{AbstractKet}
name::Symbol
basis::Basis
end
SKet(name) = SKet(name, qubit_basis)
macro ket(name, basis)
:($(esc(name)) = SKet($(Expr(:quote, name)), $(basis)))
end
macro ket(name)
:($(esc(name)) = SKet($(Expr(:quote, name))))
end

struct SOperator <: Symbolic{AbstractOperator}
name::Symbol
basis::Basis
end
SOperator(name) = SOperator(name, qubit_basis)
macro op(name, basis)
:($(esc(name)) = SOperator($(Expr(:quote, name)), $(basis)))
end
macro op(name)
:($(esc(name)) = SOperator($(Expr(:quote, name))))
end
ishermitian(x::SOperator) = false
isunitary(x::SOperator) = false

struct SHermitianOperator <: Symbolic{AbstractOperator}
name::Symbol
basis::Basis
end
SHermitianOperator(name) = SHermitianOperator(name, qubit_basis)

ishermitian(::SHermitianOperator) = true
isunitary(::SHermitianOperator) = false

struct SUnitaryOperator <: Symbolic{AbstractOperator}
name::Symbol
basis::Basis
end
SUnitaryOperator(name) = SUnitaryOperator(name, qubit_basis)

ishermitian(::SUnitaryOperator) = false
isunitary(::SUnitaryOperator) = true

struct SHermitianUnitaryOperator <: Symbolic{AbstractOperator}
name::Symbol
basis::Basis
end
SHermitianUnitaryOperator(name) = SHermitianUnitaryOperator(name, qubit_basis)

ishermitian(::SHermitianUnitaryOperator) = true
isunitary(::SHermitianUnitaryOperator) = true

Expand Down
8 changes: 4 additions & 4 deletions src/QSymbolicsBase/predefined.jl
Original file line number Diff line number Diff line change
Expand Up @@ -233,17 +233,17 @@ end
"""Dagger, i.e., adjoint of quantum objects (kets, bras, operators)
```jldoctest
julia> a = SKet(:a, SpinBasis(1//2)); A = SOperator(:A, SpinBasis(1//2));
julia> @ket a; @op A;
julia> dagger(2*im*A*a)
0 - 2im|a⟩†A†
julia> B = SOperator(:B, SpinBasis(1//2));
julia> @op B;
julia> dagger(A*B)
B†A†
julia> ℋ = SHermitianOperator(:ℋ, SpinBasis(1//2)); U = SUnitaryOperator(:U, SpinBasis(1//2));
julia> ℋ = SHermitianOperator(:ℋ); U = SUnitaryOperator(:U);
julia> dagger(ℋ)
Expand Down Expand Up @@ -291,7 +291,7 @@ symbollabel(x::SDagger) = symbollabel(x.obj)
"""Inverse Operator
```jldoctest
julia> A = SOperator(:A, SpinBasis(1//2));
julia> @op A;
julia> inv(A)
A⁻¹
Expand Down
3 changes: 1 addition & 2 deletions test/test_anticommutator.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using QuantumSymbolics
using Test

A = SOperator(:A, SpinBasis(1//2))
B = SOperator(:B, SpinBasis(1//2))
@op A; @op B;

@testset "symbolic anticommutator tests" begin
@test isequal(anticommutator(2*A, B), anticommutator(A, 2*B)) && isequal(2*anticommutator(A, B), anticommutator(2*A, B)) && isequal(2*anticommutator(A, B), anticommutator(2*A, B))
Expand Down
3 changes: 1 addition & 2 deletions test/test_commutator.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using QuantumSymbolics
using Test

A = SOperator(:A, SpinBasis(1//2))
B = SOperator(:B, SpinBasis(1//2))
@op A; @op B;

@testset "symbolic commutator tests" begin
@test isequal(commutator(2*A, B), commutator(A, 2*B)) && isequal(2*commutator(A, B), commutator(2*A, B)) && isequal(commutator(A, 2*B), 2*commutator(A, B))
Expand Down
14 changes: 4 additions & 10 deletions test/test_dagger.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,11 @@ using QuantumSymbolics
using QuantumInterface: AbstractOperator
using Test

b₁ = SBra(:b₁, SpinBasis(1//2))
b₂ = SBra(:b₂, SpinBasis(1//2))
k₁ = SKet(:k₁, SpinBasis(1//2))
k₂ = SKet(:k₂, SpinBasis(1//2))
@bra b₁; @bra b₂;
@ket k₁; @ket k₂;

A = SOperator(:A, SpinBasis(1//2))
B = SOperator(:B, SpinBasis(1//2))
C = SOperator(:C, SpinBasis(1//2))

U = SUnitaryOperator(:U, SpinBasis(1//2))
= SHermitianOperator(:ℋ, SpinBasis(1//2))
@op A; @op B; @op C;
U = SUnitaryOperator(:U); ℋ = SHermitianOperator(:ℋ);

@testset "symbolic dagger tests" begin
@test isequal(dagger(im*k₁), -im*dagger(k₁))
Expand Down

0 comments on commit 076399a

Please sign in to comment.