Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated operators to print with hat #79

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# News

## v0.4.6 - dev

- Operators now print with a hat

## v0.4.5 - 2024-11-14

- Updated compat lower bounds for Symbolics to v6 (and for SymbolicUtils and TermInterface)
Expand Down
30 changes: 15 additions & 15 deletions docs/src/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ julia> @ket k # object of type SKet
|k⟩

julia> @op A # object of type SOperator
A

julia> @superop S # object of type SSuperOperator
S
Expand All @@ -41,13 +41,13 @@ By default, each of the above macros defines a symbolic quantum object in the sp

```jldoctest
julia> @op B FockBasis(Inf, 0.0)
B

julia> basis(B)
Fock(cutoff=Inf)

julia> @op C SpinBasis(1//2)⊗SpinBasis(5//2)
C

julia> basis(C)
[Spin(1/2) ⊗ Spin(5/2)]
Expand All @@ -69,7 +69,7 @@ julia> 2*k
2|k⟩

julia> A*k
A|k⟩
|k⟩
```

Similar scaling procedures can be performed on bras and operators. Addition between symbolic objects is also available, for instance:
Expand All @@ -78,7 +78,7 @@ Similar scaling procedures can be performed on bras and operators. Addition betw
julia> @op A₁; @op A₂;

julia> A₁+A₂
(A₁+A₂)
(Â₁+Â₂)

julia> @bra b;

Expand Down Expand Up @@ -117,10 +117,10 @@ More involved combinations of operations can be explored. Here are few other str
julia> @bra b; @ket k; @op A; @op B;

julia> 3*A*B*k
3AB|k⟩
3ÂB̂|k⟩

julia> A⊗(k*b + B)
(A⊗(B+|k⟩⟨b|))
(Â⊗(B̂+|k⟩⟨b|))

julia> A-A
𝟎
Expand All @@ -135,10 +135,10 @@ QuantumSymbolics supports a wide variety of linear algebra on symbolic bras, ket
julia> @op A; @op B;

julia> commutator(A, B)
[A,B]
[Â,B̂]

julia> anticommutator(A, B)
{A,B}
{Â,B̂}

julia> commutator(A, A)
𝟎
Expand All @@ -149,13 +149,13 @@ Or, one can take the dagger of a quantum object with the [`dagger`](@ref) functi
julia> @ket k; @op A; @op B;

julia> dagger(A)
A

julia> dagger(A*k)
|k⟩†A
|k⟩†

julia> dagger(A*B)
B†A
B̂†Â
```
Below, we state all of the supported linear algebra operations on quantum objects:

Expand Down Expand Up @@ -259,18 +259,18 @@ Symbolic expressions containing quantum objects can be expanded with the [`qexpa
julia> @op A; @op B; @op C;

julia> qexpand(A⊗(B+C))
((A⊗B)+(A⊗C))
((Â⊗B̂)+(Â⊗Ĉ))

julia> qexpand((B+C)*A)
(BA+CA)
(B̂Â+ĈÂ)

julia> @ket k₁; @ket k₂; @ket k₃;

julia> qexpand(k₁⊗(k₂+k₃))
(|k₁⟩|k₂⟩+|k₁⟩|k₃⟩)

julia> qexpand((A*B)*(k₁+k₂))
(AB|k₁⟩+AB|k₂⟩)
(ÂB̂|k₁⟩+ÂB̂|k₂⟩)
```

## Numerical Translation of Symbolic Objects
Expand Down
8 changes: 4 additions & 4 deletions src/QSymbolicsBase/basic_ops_homogeneous.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ julia> 2*k
2|k⟩

julia> @op A
A

julia> 2*A
2A
2Â
```
"""
@withmetadata struct SScaled{T<:QObj} <: Symbolic{T}
Expand Down Expand Up @@ -125,7 +125,7 @@ end
julia> @op A; @op B;

julia> A*B
AB
ÂB̂
```
"""
@withmetadata struct SMulOperator <: Symbolic{AbstractOperator}
Expand Down Expand Up @@ -165,7 +165,7 @@ julia> k₁ ⊗ k₂
julia> @op A; @op B;

julia> A ⊗ B
(A⊗B)
(Â⊗B̂)
```
"""
@withmetadata struct STensor{T<:QObj} <: Symbolic{T}
Expand Down
4 changes: 2 additions & 2 deletions src/QSymbolicsBase/basic_ops_inhomogeneous.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
julia> @ket k; @op A;

julia> A*k
A|k⟩
|k⟩
```
"""
@withmetadata struct SApplyKet <: Symbolic{AbstractKet}
Expand Down Expand Up @@ -41,7 +41,7 @@ basis(x::SApplyKet) = basis(x.ket)
julia> @bra b; @op A;

julia> b*A
⟨b|A
⟨b|
```
"""
@withmetadata struct SApplyBra <: Symbolic{AbstractBra}
Expand Down
4 changes: 2 additions & 2 deletions src/QSymbolicsBase/basic_superops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ julia> K = kraus(A₁, A₂, A₃)
julia> @op ρ;

julia> K*ρ
(A₁ρA₁†+A₂ρA₂†+A₃ρA₃†)
(Â₁ρ̂Â₁†+Â₂ρ̂Â₂†+Â₃ρ̂Â₃†)
```
"""
@withmetadata struct KrausRepr <: Symbolic{AbstractSuperOperator}
Expand Down Expand Up @@ -41,7 +41,7 @@ Base.show(io::IO, x::KrausRepr) = print(io, "𝒦("*join([symbollabel(i) for i i
julia> @op A; @superop S;

julia> S*A
S[A]
S[]
```
"""
@withmetadata struct SSuperOpApply <: Symbolic{AbstractOperator}
Expand Down
44 changes: 22 additions & 22 deletions src/QSymbolicsBase/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function anticommutator end
julia> @op A; @op B;

julia> commutator(A, B)
[A,B]
[Â,B̂]

julia> commutator(A, A)
𝟎
Expand Down Expand Up @@ -51,7 +51,7 @@ basis(x::SCommutator) = basis(x.op1)
julia> @op A; @op B;

julia> anticommutator(A, B)
{A,B}
{Â,B̂}
```
"""
@withmetadata struct SAnticommutator <: Symbolic{AbstractOperator}
Expand Down Expand Up @@ -85,7 +85,7 @@ basis(x::SAnticommutator) = basis(x.op1)
julia> @op A; @ket k;

julia> conj(A)
Âˣ

julia> conj(k)
|k⟩ˣ
Expand Down Expand Up @@ -161,10 +161,10 @@ end
julia> @op A; @op B; @ket k;

julia> transpose(A)
Aᵀ
Âᵀ

julia> transpose(A+B)
(Aᵀ+Bᵀ)
(Âᵀ+B̂ᵀ)

julia> transpose(k)
|k⟩ᵀ
Expand Down Expand Up @@ -206,20 +206,20 @@ end
julia> @ket a; @op A;

julia> dagger(2*im*A*a)
(0 - 2im)|a⟩†A
(0 - 2im)|a⟩†

julia> @op B;

julia> dagger(A*B)
B†A
B̂†Â

julia> ℋ = SHermitianOperator(:ℋ); U = SUnitaryOperator(:U);

julia> dagger(ℋ)
ℋ̂

julia> dagger(U)
U⁻¹
⁻¹
```
"""
@withmetadata struct SDagger{T<:QObj} <: Symbolic{T}
Expand Down Expand Up @@ -275,7 +275,7 @@ end
julia> @op A; @op B;

julia> tr(A)
tr(A)
tr()

julia> tr(commutator(A, B))
0
Expand Down Expand Up @@ -318,35 +318,35 @@ Base.isequal(x::STrace, y::STrace) = isequal(x.op, y.op)
julia> @op 𝒪 SpinBasis(1//2)⊗SpinBasis(1//2);

julia> op = ptrace(𝒪, 1)
tr1(𝒪)
tr1(𝒪̂)

julia> QuantumSymbolics.basis(op)
Spin(1/2)

julia> @op A; @op B;

julia> ptrace(A⊗B, 1)
(tr(A))B
(tr(Â))B̂

julia> @ket k; @bra b;

julia> factorizable = A ⊗ (k*b)
(A⊗|k⟩⟨b|)
(⊗|k⟩⟨b|)

julia> ptrace(factorizable, 1)
(tr(A))|k⟩⟨b|
(tr())|k⟩⟨b|

julia> ptrace(factorizable, 2)
(⟨b||k⟩)A
(⟨b||k⟩)

julia> mixed_state = (A⊗(k*b)) + ((k*b)⊗B)
((A⊗|k⟩⟨b|)+(|k⟩⟨b|⊗B))
((⊗|k⟩⟨b|)+(|k⟩⟨b|⊗))

julia> ptrace(mixed_state, 1)
((0 + ⟨b||k⟩)B+(tr(A))|k⟩⟨b|)
((0 + ⟨b||k⟩)+(tr())|k⟩⟨b|)

julia> ptrace(mixed_state, 2)
((0 + ⟨b||k⟩)A+(tr(B))|k⟩⟨b|)
((0 + ⟨b||k⟩)+(tr())|k⟩⟨b|)
```
"""
@withmetadata struct SPartialTrace <: Symbolic{AbstractOperator}
Expand Down Expand Up @@ -441,7 +441,7 @@ end
julia> @op A;

julia> inv(A)
A⁻¹
⁻¹

julia> inv(A)*A
𝕀
Expand Down Expand Up @@ -474,7 +474,7 @@ inv(x::Symbolic{AbstractOperator}) = SInvOperator(x)
julia> @op A; @op B;

julia> exp(A)
exp(A)
exp()
```
"""
@withmetadata struct SExpOperator <: Symbolic{AbstractOperator}
Expand Down Expand Up @@ -502,10 +502,10 @@ exp(x::Symbolic{AbstractOperator}) = SExpOperator(x)
julia> @op A; @op B;

julia> vec(A)
|A⟩⟩
|⟩⟩

julia> vec(A+B)
(|A⟩⟩+|B⟩⟩)
(|⟩⟩+|⟩⟩)
```
"""
@withmetadata struct SVec <: Symbolic{AbstractKet}
Expand Down
9 changes: 6 additions & 3 deletions src/QSymbolicsBase/literal_objects.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ Define a symbolic ket of type `SOperator`. By default, the defined basis is the

```jldoctest
julia> @op A
A

julia> @op B FockBasis(2)
B
```
"""
macro op(name, basis)
Expand Down Expand Up @@ -138,7 +138,10 @@ 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,SSuperOperator}) = print(io, "$(symbollabel(x))")
Base.show(io::IO, x::Union{SOperator,SHermitianOperator,SUnitaryOperator,SHermitianUnitaryOperator}) = begin
symbol_name = String(symbollabel(x))
print(io, symbol_name[1:1] * "\u0302" * symbol_name[2:end]) # add hat to the first character
end
Base.show(io::IO, x::SymQObj) = print(io, symbollabel(x)) # fallback that probably is not great

struct SZero{T<:QObj} <: Symbolic{T} end
Expand Down
6 changes: 3 additions & 3 deletions src/QSymbolicsBase/rules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,15 @@ Manually expand a symbolic expression of quantum objects.
julia> @op A; @op B; @op C;

julia> qexpand(commutator(A, B))
(-1BA+AB)
(-1B̂Â+ÂB̂)

julia> qexpand(A⊗(B+C))
((A⊗B)+(A⊗C))
((Â⊗B̂)+(Â⊗Ĉ))

julia> @ket k₁; @ket k₂;

julia> qexpand(A*(k₁+k₂))
(A|k₁⟩+A|k₂⟩)
(|k₁⟩+|k₂⟩)
```
"""
function qexpand(s)
Expand Down
Loading