Skip to content

Commit

Permalink
Defining literal quantum objects with macros (#51)
Browse files Browse the repository at this point in the history
* literal object macros

* stylistic changes

* changed JET threshold to 7

* version updates
  • Loading branch information
apkille authored Jun 21, 2024
1 parent 6847efb commit 237d158
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 36 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# News

## v0.3.1 - dev

- Macros for defining symbolic quantum objects.

## v0.3.0 - 2024-06-12

- Bump compat for symbolics-related foundational packages.
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "QuantumSymbolics"
uuid = "efa7fd63-0460-4890-beb7-be1bbdfbaeae"
authors = ["QuantumSymbolics.jl contributors"]
version = "0.3.0"
version = "0.3.1-dev"

[deps]
Latexify = "23fbe1c1-3f47-55db-b15f-69d7ec21a316"
Expand Down
5 changes: 3 additions & 2 deletions src/QSymbolicsBase/QSymbolicsBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ 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,
SProjector,MixedState,IdentityOp,SInvOperator,SHermitianOperator,SUnitaryOperator,SHermitianUnitaryOperator,
SProjector,MixedState,IdentityOp,SInvOperator,
SApplyKet,SApplyBra,SMulOperator,SSuperOpApply,SCommutator,SAnticommutator,SDagger,SBraKet,SOuterKetBra,
HGate,XGate,YGate,ZGate,CPHASEGate,CNOTGate,
XBasisState,YBasisState,ZBasisState,
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 @@ -5,13 +5,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 @@ -62,7 +62,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 @@ -101,7 +101,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 All @@ -127,12 +127,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 @@ -166,7 +166,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 @@ -197,7 +197,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 All @@ -32,7 +32,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 All @@ -58,7 +58,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 @@ -95,7 +95,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 @@ -232,17 +232,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
2 changes: 1 addition & 1 deletion test/test_jet.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ rep = report_package("QuantumSymbolics";
)
@show rep
@test_broken length(JET.get_reports(rep)) == 0
@test length(JET.get_reports(rep)) <= 6
@test length(JET.get_reports(rep)) <= 7

0 comments on commit 237d158

Please sign in to comment.