Skip to content

Commit f03b07c

Browse files
authored
added function Base.zero(x::Symbolic{T}) (#109)
1 parent ddb19ed commit f03b07c

File tree

10 files changed

+46
-9
lines changed

10 files changed

+46
-9
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# News
22

3-
## v0.4.7 - 2025-02-25
3+
## v0.4.9 - 2025-04-23
4+
5+
- `Base.zero` implemented for quantum symbolic objects.
6+
7+
## v0.4.8 - 2025-02-25
48

59
- Proper expression of outer products in QuantumOptics.
610

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "QuantumSymbolics"
22
uuid = "efa7fd63-0460-4890-beb7-be1bbdfbaeae"
33
authors = ["QuantumSymbolics.jl contributors"]
4-
version = "0.4.8"
4+
version = "0.4.9"
55

66
[deps]
77
Latexify = "23fbe1c1-3f47-55db-b15f-69d7ec21a316"

docs/src/express.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ DocTestSetup = quote
66
end
77
```
88

9-
A principle feature of `QuantumSymbolics` is to numerically represent symbolic quantum expressions in various formalisms using [`express`](@ref). In particular, one can translate symbolic logic to back-end toolboxes such as [`QuantumOptics.jl`](https://github.com/qojulia/QuantumOptics.jl) or [`QuantumClifford.jl`](https://github.com/QuantumSavory/QuantumClifford.jl) for simulating quantum systems with great flexibiity.
9+
A principle feature of `QuantumSymbolics` is to numerically represent symbolic quantum expressions in various formalisms using [`express`](@ref). In particular, one can translate symbolic logic to back-end toolboxes such as [`QuantumOptics.jl`](https://github.com/qojulia/QuantumOptics.jl) or [`QuantumClifford.jl`](https://github.com/QuantumSavory/QuantumClifford.jl) for simulating quantum systems with great flexibility.
1010

1111
As a straightforward example, consider the spin-up state $|\uparrow\rangle = |0\rangle$, the eigenstate of the Pauli operator $Z$, which can be expressed in `QuantumSymbolics` as follows:
1212

docs/src/introduction.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,9 @@ julia> using QuantumOptics
284284
285285
julia> express(exp(X))
286286
Operator(dim=2x2)
287-
basis: Spin(1/2)sparse([1, 2, 1, 2], [1, 1, 2, 2], ComplexF64[1.5430806327160496 + 0.0im, 1.1752011684303352 + 0.0im, 1.1752011684303352 + 0.0im, 1.5430806327160496 + 0.0im], 2, 2)
287+
basis: Spin(1/2)
288+
1.5430806327160496 + 0.0im 1.1752011684303352 + 0.0im
289+
1.1752011684303352 + 0.0im 1.5430806327160496 + 0.0im
288290
```
289291

290292
To convert to the Clifford representation, an instance of `CliffordRepr` must be passed to [`express`](@ref). For instance, we can represent the projection of the basis state [`X1`](@ref) of the Pauli operator [`X`](@ref) as follows:

src/QSymbolicsBase/basic_ops_homogeneous.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ function Base.:(*)(c::U, x::Symbolic{T}) where {U<:Union{Number, Symbolic{<:Numb
4040
SScaled{T}(c, x)
4141
end
4242
end
43+
4344
Base.:(*)(x::Symbolic{T}, c::Number) where {T<:QObj} = c*x
4445
Base.:(*)(x::Symbolic{T}, y::Symbolic{S}) where {T<:QObj,S<:QObj} = throw(ArgumentError("multiplication between $(typeof(x)) and $(typeof(y)) is not defined; maybe you are looking for a tensor product `tensor`"))
4546
Base.:(/)(x::Symbolic{T}, c::Number) where {T<:QObj} = iszero(c) ? throw(DomainError(c,"cannot divide QSymbolics expressions by zero")) : (1/c)*x

src/QSymbolicsBase/express.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#
44
# The main function is `express`, which takes a quantum object and a representation and returns an expression of the object in that representation.
55
##
6-
6+
77
export express, express_nolookup, consistent_representation
88

99
import SymbolicUtils: Symbolic
@@ -28,7 +28,9 @@ julia> express(X1, CliffordRepr())
2828
2929
julia> express(QuantumSymbolics.X)
3030
Operator(dim=2x2)
31-
basis: Spin(1/2)sparse([2, 1], [1, 2], ComplexF64[1.0 + 0.0im, 1.0 + 0.0im], 2, 2)
31+
basis: Spin(1/2)
32+
⋅ 1.0 + 0.0im
33+
1.0 + 0.0im ⋅
3234
3335
julia> express(QuantumSymbolics.X, CliffordRepr(), UseAsOperation())
3436
sX

src/QSymbolicsBase/literal_objects.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@ Base.show(io::IO, x::SymQObj) = print(io, symbollabel(x)) # fallback that probab
143143

144144
struct SZero{T<:QObj} <: Symbolic{T} end
145145

146+
function Base.zero(x::Symbolic{T}) where T<:QObj
147+
return SZero{T}()
148+
end
149+
146150
"""Symbolic zero bra"""
147151
const SZeroBra = SZero{AbstractBra}
148152

src/QSymbolicsBase/predefined.jl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,16 +160,20 @@ julia> MixedState(X1⊗X2)
160160
161161
julia> express(MixedState(X1⊗X2))
162162
Operator(dim=4x4)
163-
basis: [Spin(1/2) ⊗ Spin(1/2)]sparse([1, 2, 3, 4], [1, 2, 3, 4], ComplexF64[0.25 + 0.0im, 0.25 + 0.0im, 0.25 + 0.0im, 0.25 + 0.0im], 4, 4)
163+
basis: [Spin(1/2) ⊗ Spin(1/2)]
164+
0.25 + 0.0im ⋅ ⋅ ⋅
165+
⋅ 0.25 + 0.0im ⋅ ⋅
166+
⋅ ⋅ 0.25 + 0.0im ⋅
167+
⋅ ⋅ ⋅ 0.25 + 0.0im
164168
165169
julia> express(MixedState(X1⊗X2), CliffordRepr())
166170
𝒟ℯ𝓈𝓉𝒶𝒷
167-
171+
168172
𝒳ₗ━━
169173
+ X_
170174
+ _X
171175
𝒮𝓉𝒶𝒷
172-
176+
173177
𝒵ₗ━━
174178
+ Z_
175179
+ _Z

test/test_misc_linalg.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
@op A; @op B;
33
O = SZeroOperator()
44

5+
@bra p
6+
@bra q
7+
8+
@ket m
9+
@ket n
10+
511
@testset "Complex Conjugate" begin
612
@test isequal(conj(O), O)
713
@test isequal(conj(conj(A)), A)
@@ -25,4 +31,17 @@
2531
@testset "Exponential" begin
2632
@test isequal(exp(A), SExpOperator(A))
2733
end
34+
35+
@testset "Vector of Operators" begin
36+
@test isequal([1 1;-im im]*[A;B], [A + B;im*B-im*A])
37+
end
38+
39+
@testset "Vector of Kets" begin
40+
@test isequal([1 1;-im im] *[p;q], [p + q;im*q-im*p])
41+
end
42+
43+
@testset "Vector of Bras" begin
44+
@test isequal([1 1;-im im] *[m;n], [m + n;im*n-im*m])
45+
end
46+
2847
end

test/test_zero_obj.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
@test isequal(anticommutator(A, Oop), Oop) && isequal(anticommutator(Oop, A), Oop) && isequal(anticommutator(Oop, Oop), Oop)
1818
@test isequal(projector(Ok), Oop)
1919
@test isequal(dagger(Oop), Oop)
20+
@test isequal(zero(A), Oop)
2021
end
2122

2223
@testset "zero bra and ket tests" begin

0 commit comments

Comments
 (0)