Skip to content

Commit 403926d

Browse files
authored
Remove excessive parenthesis in addition and tensor products (#106)
* rm excessive parenthesis in homogeneous ops * add similar printing methods for inhomogeneous ops * make slight tweak to SApplyKet printing
1 parent 26c0176 commit 403926d

File tree

6 files changed

+45
-29
lines changed

6 files changed

+45
-29
lines changed

docs/src/introduction.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ Similar scaling procedures can be performed on bras and operators. Addition betw
7878
julia> @op A₁; @op A₂;
7979
8080
julia> A₁+A₂
81-
(A₁+A₂)
81+
A₁+A₂
8282
8383
julia> @bra b;
8484
@@ -120,7 +120,7 @@ julia> 3*A*B*k
120120
3AB|k⟩
121121
122122
julia> A⊗(k*b + B)
123-
(A⊗(B+|k⟩⟨b|))
123+
A⊗(B+|k⟩⟨b|)
124124
125125
julia> A-A
126126
𝟎
@@ -215,10 +215,10 @@ julia> using Symbolics
215215
julia> @op A; @ket k;
216216
217217
julia> ex = 2*A + projector(k)
218-
(2A+𝐏[|k⟩])
218+
2A+𝐏[|k⟩]
219219
220220
julia> substitute(ex, Dict([A => X, k => X1]))
221-
(2X+𝐏[|X₁⟩])
221+
2X+𝐏[|X₁⟩]
222222
```
223223

224224
## Simplifying Expressions
@@ -259,18 +259,18 @@ Symbolic expressions containing quantum objects can be expanded with the [`qexpa
259259
julia> @op A; @op B; @op C;
260260
261261
julia> qexpand(A⊗(B+C))
262-
((A⊗B)+(A⊗C))
262+
(A⊗B)+(A⊗C)
263263
264264
julia> qexpand((B+C)*A)
265-
(BA+CA)
265+
BA+CA
266266
267267
julia> @ket k₁; @ket k₂; @ket k₃;
268268
269269
julia> qexpand(k₁⊗(k₂+k₃))
270-
(|k₁⟩|k₂⟩+|k₁⟩|k₃⟩)
270+
|k₁⟩|k₂⟩+|k₁⟩|k₃⟩
271271
272272
julia> qexpand((A*B)*(k₁+k₂))
273-
(AB|k₁⟩+AB|k₂⟩)
273+
AB|k₁⟩+AB|k₂⟩
274274
```
275275

276276
## Numerical Translation of Symbolic Objects

src/QSymbolicsBase/basic_ops_homogeneous.jl

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ end
7676
julia> @ket k₁; @ket k₂;
7777
7878
julia> k₁ + k₂
79-
(|k₁⟩+|k₂⟩)
79+
|k₁⟩+|k₂⟩
8080
```
8181
"""
8282
@withmetadata struct SAdd{T<:QObj} <: Symbolic{T}
@@ -107,17 +107,18 @@ basis(x::SAdd) = basis(first(x.dict).first)
107107
const SAddBra = SAdd{AbstractBra}
108108
function Base.show(io::IO, x::SAddBra)
109109
ordered_terms = sort([repr(i) for i in arguments(x)])
110-
print(io, "("*join(ordered_terms,"+")::String*")") # type assert to help inference
110+
print(io, join(ordered_terms,"+")::String) # type assert to help inference
111111
end
112112
const SAddKet = SAdd{AbstractKet}
113113
function Base.show(io::IO, x::SAddKet)
114114
ordered_terms = sort([repr(i) for i in arguments(x)])
115-
print(io, "("*join(ordered_terms,"+")::String*")") # type assert to help inference
115+
print(io, join(ordered_terms,"+")::String) # type assert to help inference
116116
end
117117
const SAddOperator = SAdd{AbstractOperator}
118118
function Base.show(io::IO, x::SAddOperator)
119-
ordered_terms = sort([repr(i) for i in arguments(x)])
120-
print(io, "("*join(ordered_terms,"+")::String*")") # type assert to help inference
119+
repr_func = x -> x isa STensor ? "("*repr(x)*")" : repr(x)
120+
ordered_terms = sort([repr_func(i) for i in arguments(x)])
121+
print(io, join(ordered_terms,"+")::String) # type assert to help inference
121122
end
122123

123124
"""Symbolic application of operator on operator.
@@ -153,7 +154,10 @@ function Base.:(*)(x::Symbolic{AbstractOperator}, xs::Vararg{Symbolic{AbstractOp
153154
SZeroOperator()
154155
end
155156
end
156-
Base.show(io::IO, x::SMulOperator) = print(io, join(map(string, arguments(x)),""))
157+
function Base.show(io::IO, x::SMulOperator)
158+
str_func = x -> x isa SAdd || x isa STensor ? "("*string(x)*")" : string(x)
159+
print(io, join(map(str_func, arguments(x)),""))
160+
end
157161
basis(x::SMulOperator) = basis(first(x.terms))
158162

159163
"""Tensor product of quantum objects (kets, operators, or bras).
@@ -167,7 +171,7 @@ julia> k₁ ⊗ k₂
167171
julia> @op A; @op B;
168172
169173
julia> A ⊗ B
170-
(A⊗B)
174+
A⊗B
171175
```
172176
"""
173177
@withmetadata struct STensor{T<:QObj} <: Symbolic{T}
@@ -196,6 +200,12 @@ Base.show(io::IO, x::STensorBra) = print(io, join(map(string, arguments(x)),""))
196200
const STensorKet = STensor{AbstractKet}
197201
Base.show(io::IO, x::STensorKet) = print(io, join(map(string, arguments(x)),""))
198202
const STensorOperator = STensor{AbstractOperator}
199-
Base.show(io::IO, x::STensorOperator) = print(io, "("*join(map(string, arguments(x)),"")*")")
203+
function Base.show(io::IO, x::STensorOperator)
204+
str_func = x -> x isa SAdd ? "("*string(x)*")" : string(x)
205+
print(io, join(map(str_func, arguments(x)),""))
206+
end
200207
const STensorSuperOperator = STensor{AbstractSuperOperator}
201-
Base.show(io::IO, x::STensorSuperOperator) = print(io, "("*join(map(string, arguments(x)),"")*")")
208+
function Base.show(io::IO, x::STensorSuperOperator)
209+
str_func = x -> x isa SAdd ? "("*string(x)*")" : string(x)
210+
print(io, join(map(str_func, arguments(x)),""))
211+
end

src/QSymbolicsBase/basic_ops_inhomogeneous.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ end
3232
Base.:(*)(op::SZeroOperator, k::Symbolic{AbstractKet}) = SZeroKet()
3333
Base.:(*)(op::Symbolic{AbstractOperator}, k::SZeroKet) = SZeroKet()
3434
Base.:(*)(op::SZeroOperator, k::SZeroKet) = SZeroKet()
35-
Base.show(io::IO, x::SApplyKet) = begin print(io, x.op); print(io, x.ket) end
35+
function Base.show(io::IO, x::SApplyKet)
36+
str_func = x -> x isa SAdd || x isa STensorOperator ? "("*string(x)*")" : string(x)
37+
print(io, join(map(str_func, arguments(x)),""))
38+
end
3639
basis(x::SApplyKet) = basis(x.ket)
3740

3841
"""Symbolic application of an operator on a bra (from the right).
@@ -65,7 +68,10 @@ end
6568
Base.:(*)(b::SZeroBra, op::Symbolic{AbstractOperator}) = SZeroBra()
6669
Base.:(*)(b::Symbolic{AbstractBra}, op::SZeroOperator) = SZeroBra()
6770
Base.:(*)(b::SZeroBra, op::SZeroOperator) = SZeroBra()
68-
Base.show(io::IO, x::SApplyBra) = begin print(io, x.bra); print(io, x.op) end
71+
function Base.show(io::IO, x::SApplyBra)
72+
str_func = x -> x isa SAdd || x isa STensor ? "("*string(x)*")" : string(x)
73+
print(io, join(map(str_func, arguments(x)),""))
74+
end
6975
basis(x::SApplyBra) = basis(x.bra)
7076

7177
"""Symbolic inner product of a bra and a ket.

src/QSymbolicsBase/basic_superops.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ julia> K = kraus(A₁, A₂, A₃)
1313
julia> @op ρ;
1414
1515
julia> K*ρ
16-
(A₁ρA₁†+A₂ρA₂†+A₃ρA₃†)
16+
A₁ρA₁†+A₂ρA₂†+A₃ρA₃†
1717
```
1818
"""
1919
@withmetadata struct KrausRepr <: Symbolic{AbstractSuperOperator}

src/QSymbolicsBase/linalg.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ julia> transpose(A)
164164
Aᵀ
165165
166166
julia> transpose(A+B)
167-
(Aᵀ+Bᵀ)
167+
Aᵀ+Bᵀ
168168
169169
julia> transpose(k)
170170
|k⟩ᵀ
@@ -331,7 +331,7 @@ julia> ptrace(A⊗B, 1)
331331
julia> @ket k; @bra b;
332332
333333
julia> factorizable = A ⊗ (k*b)
334-
(A⊗|k⟩⟨b|)
334+
A⊗|k⟩⟨b|
335335
336336
julia> ptrace(factorizable, 1)
337337
(tr(A))|k⟩⟨b|
@@ -340,13 +340,13 @@ julia> ptrace(factorizable, 2)
340340
(⟨b||k⟩)A
341341
342342
julia> mixed_state = (A⊗(k*b)) + ((k*b)⊗B)
343-
((A⊗|k⟩⟨b|)+(|k⟩⟨b|⊗B))
343+
(A⊗|k⟩⟨b|)+(|k⟩⟨b|⊗B)
344344
345345
julia> ptrace(mixed_state, 1)
346-
((0 + ⟨b||k⟩)B+(tr(A))|k⟩⟨b|)
346+
(0 + ⟨b||k⟩)B+(tr(A))|k⟩⟨b|
347347
348348
julia> ptrace(mixed_state, 2)
349-
((0 + ⟨b||k⟩)A+(tr(B))|k⟩⟨b|)
349+
(0 + ⟨b||k⟩)A+(tr(B))|k⟩⟨b|
350350
```
351351
"""
352352
@withmetadata struct SPartialTrace <: Symbolic{AbstractOperator}
@@ -505,7 +505,7 @@ julia> vec(A)
505505
|A⟩⟩
506506
507507
julia> vec(A+B)
508-
(|A⟩⟩+|B⟩⟩)
508+
|A⟩⟩+|B⟩⟩
509509
```
510510
"""
511511
@withmetadata struct SVec <: Symbolic{AbstractKet}

src/QSymbolicsBase/rules.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,15 +183,15 @@ Manually expand a symbolic expression of quantum objects.
183183
julia> @op A; @op B; @op C;
184184
185185
julia> qexpand(commutator(A, B))
186-
(-1BA+AB)
186+
-1BA+AB
187187
188188
julia> qexpand(A⊗(B+C))
189-
((A⊗B)+(A⊗C))
189+
(A⊗B)+(A⊗C)
190190
191191
julia> @ket k₁; @ket k₂;
192192
193193
julia> qexpand(A*(k₁+k₂))
194-
(A|k₁⟩+A|k₂⟩)
194+
A|k₁⟩+A|k₂⟩
195195
```
196196
"""
197197
function qexpand(s)

0 commit comments

Comments
 (0)