Skip to content

Commit af105ed

Browse files
committed
More simplifications
1 parent 3a9569c commit af105ed

File tree

3 files changed

+72
-111
lines changed

3 files changed

+72
-111
lines changed

src/sitetypes/aliases.jl

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ end
2626
# Pauli eingenstates
2727
@state_alias "X+" "+"
2828
@state_alias "Xp" "+"
29-
@state_alias "X-" "+"
30-
@state_alias "Xm" "+"
29+
@state_alias "X-" "-"
30+
@state_alias "Xm" "-"
3131

3232
@state_alias "Y+" "i"
3333
@state_alias "Yp" "i"
@@ -47,6 +47,9 @@ end
4747
@op_alias "σ⁰" "Id"
4848
@op_alias "σ₀" "Id"
4949

50+
alias(::OpName"X") = (OpName"σ⁺"() + OpName"σ⁻"()) / 2
51+
alias(::OpName"Y") = -im * (OpName"σ⁺"() - OpName"σ⁻"()) / 2
52+
5053
@op_alias "σx" "X"
5154
@op_alias "σˣ" "X"
5255
@op_alias "σₓ" "X"
@@ -70,7 +73,7 @@ end
7073
@op_alias "σz" "Z"
7174
# TODO: No subsript `\_z` available
7275
# in unicode.
73-
@op_alias "σᶻ" "Z"
76+
@op_alias "Z" "σᶻ"
7477
@op_alias "σ3" "Z"
7578
@op_alias "σ³" "Z"
7679
@op_alias "σ₃" "Z"
@@ -84,12 +87,16 @@ alias(n::OpName"iSy") = OpName("iY") / 2
8487
@op_alias "iSʸ" "iSy"
8588
alias(n::OpName"Sz") = OpName("Z") / 2
8689
@op_alias "Sᶻ" "Sz"
87-
@op_alias "S⁻" "S-"
90+
alias(::OpName"S⁻") = OpName("σ⁻") / 2
91+
@op_alias "S-" "S⁻"
8892
@op_alias "Sminus" "S-"
8993
@op_alias "Sm" "S-"
90-
@op_alias "S⁺" "S+"
94+
alias(::OpName"S⁺") = OpName("σ⁺") / 2
95+
@op_alias "S+" "S⁺"
9196
@op_alias "Splus" "S+"
9297
@op_alias "Sp" "S+"
98+
99+
# TODO: Is this the correct general definition?
93100
alias(n::OpName"S2") = 3 * OpName("I") / 4
94101
@op_alias "" "S2"
95102

src/sitetypes/qubit.jl

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@ using LinearAlgebra: I
22

33
Base.length(::SiteType"Qubit") = 2
44

5+
# `eigvecs(Z)`
56
Base.AbstractArray(::StateName"0", ::Tuple{SiteType"Qubit"}) = [1, 0]
67
Base.AbstractArray(::StateName"1", ::Tuple{SiteType"Qubit"}) = [0, 1]
8+
9+
# `eigvecs(X)`
710
Base.AbstractArray(::StateName"+", ::Tuple{SiteType"Qubit"}) = [1, 1] / 2
811
Base.AbstractArray(::StateName"-", ::Tuple{SiteType"Qubit"}) = [1, -1] / 2
12+
13+
# `eigvecs(Y)`
914
Base.AbstractArray(::StateName"i", ::Tuple{SiteType"Qubit"}) = [1, im] / 2
1015
Base.AbstractArray(::StateName"-i", ::Tuple{SiteType"Qubit"}) = [1, -im] / 2
1116

@@ -30,49 +35,60 @@ end
3035
#
3136
# 1-Qubit gates
3237
#
33-
Base.AbstractArray(::OpName"X", ::Tuple{SiteType"Qubit"}) = [
34-
0 1
35-
1 0
36-
]
37-
38-
Base.AbstractArray(::OpName"Y", ::Tuple{SiteType"Qubit"}) = [
39-
0 -im
40-
im 0
41-
]
42-
43-
Base.AbstractArray(::OpName"Z", ::Tuple{SiteType"Qubit"}) = [
44-
1 0
45-
0 -1
46-
]
4738

48-
Base.AbstractArray(::OpName"S+", ::Tuple{SiteType"Qubit"}) = [
49-
0 1
39+
Base.AbstractArray(::OpName"σ⁺", ::Tuple{SiteType"Qubit"}) = [
40+
0 2
5041
0 0
5142
]
52-
Base.AbstractArray(::OpName"S-", ::Tuple{SiteType"Qubit"}) = [
43+
Base.AbstractArray(::OpName"σ⁻", ::Tuple{SiteType"Qubit"}) = [
5344
0 0
45+
2 0
46+
]
47+
Base.AbstractArray(::OpName"σᶻ", ::Tuple{SiteType"Qubit"}) = [
5448
1 0
49+
0 -1
5550
]
51+
52+
## # TODO: Write as `σ⁺ / 2`.
53+
## Base.AbstractArray(::OpName"S+", ::Tuple{SiteType"Qubit"}) = [
54+
## 0 1
55+
## 0 0
56+
## ]
57+
##
58+
## # TODO: Write as `σ⁻ / 2`.
59+
## Base.AbstractArray(::OpName"S-", ::Tuple{SiteType"Qubit"}) = [
60+
## 0 0
61+
## 1 0
62+
## ]
63+
64+
# TODO: Write as `(I + σᶻ) / 2`?
5665
Base.AbstractArray(::OpName"ProjUp", ::Tuple{SiteType"Qubit"}) = [
5766
1 0
5867
0 0
5968
]
69+
70+
# TODO: Define as `σ⁺ * σ−`?
71+
# TODO: Write as `(I - σᶻ) / 2`?
6072
Base.AbstractArray(::OpName"ProjDn", ::Tuple{SiteType"Qubit"}) = [
6173
0 0
6274
0 1
6375
]
6476

77+
# TODO: Determine a general spin definition.
78+
# `eigvecs(X)`
6579
Base.AbstractArray(::OpName"H", ::Tuple{SiteType"Qubit"}) = [
6680
1/√2 1/√2
6781
1/√2 -1/√2
6882
]
6983

84+
# exp(-im * n.θ / 2 * Z) * exp(im * n.θ)
7085
Base.AbstractArray(n::OpName"Phase", ::Tuple{SiteType"Qubit"}) = [
7186
1 0
7287
0 exp(im * n.θ)
7388
]
7489

7590
# Rotation around X-axis
91+
# exp(-im * n.θ / 2 * X)
7692
function Base.AbstractArray(n::OpName"Rx", ::Tuple{SiteType"Qubit"})
7793
return [
7894
cos(n.θ / 2) -im*sin(n.θ / 2)
@@ -81,6 +97,7 @@ function Base.AbstractArray(n::OpName"Rx", ::Tuple{SiteType"Qubit"})
8197
end
8298

8399
# Rotation around Y-axis
100+
# exp(-im * n.θ / 2 * Y)
84101
function Base.AbstractArray(n::OpName"Ry", ::Tuple{SiteType"Qubit"})
85102
return [
86103
cos(n.θ / 2) -sin(n.θ / 2)
@@ -89,6 +106,7 @@ function Base.AbstractArray(n::OpName"Ry", ::Tuple{SiteType"Qubit"})
89106
end
90107

91108
# Rotation around Z-axis
109+
# exp(-im * n.θ / 2 * Z)
92110
function Base.AbstractArray(n::OpName"Rz", ::Tuple{SiteType"Qubit"})
93111
return [
94112
exp(-im * n.θ / 2) 0
@@ -97,6 +115,7 @@ function Base.AbstractArray(n::OpName"Rz", ::Tuple{SiteType"Qubit"})
97115
end
98116

99117
# Rotation around generic axis n̂
118+
# exp(-im * n.θ / 2 * n̂ ⋅ σ⃗)
100119
#=
101120
TODO: Define R-gate when `λ == -ϕ`, i.e.:
102121
```julia

src/sitetypes/qudit.jl

Lines changed: 24 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,36 @@
1-
using ChainRulesCore: @non_differentiable
2-
3-
alias(t::SiteType"Qudit") = t
41
Base.length(t::SiteType"Qudit") = t.length
52

6-
"""
7-
space(::SiteType"Qudit")
8-
9-
Create the Hilbert space for a site of type "Qudit".
10-
11-
Optionally specify the conserved symmetries and their quantum number labels.
12-
"""
13-
function space(::SiteType"Qudit"; dim=2)
14-
return dim
15-
end
16-
17-
function val(::ValName{N}, ::SiteType"Qudit") where {N}
18-
return parse(Int, String(N)) + 1
19-
end
3+
# TODO: Add this.
4+
## function Base.Integer(::StateName{N}) where {N}
5+
## return parse(Int, String(N))
6+
## end
207

21-
function state(::StateName{N}, ::SiteType"Qudit") where {N}
8+
function Base.AbstractArray(n::StateName{N}, ::Tuple{SiteType"Qudit"}) where {N}
9+
# TODO: Use `Integer(n)`.
2210
n = parse(Int, String(N))
23-
st = zeros(dim(s))
24-
st[n + 1] = 1.0
25-
return st
26-
end
27-
28-
# one-body operators
29-
function op(::OpName"Id", ::SiteType"Qudit", ds::Int...)
30-
d = prod(ds)
31-
return Matrix(1.0I, d, d)
32-
end
33-
op(on::OpName"I", st::SiteType"Qudit", ds::Int...) = op(alias(on), st, ds...)
34-
op(on::OpName"F", st::SiteType"Qudit", ds::Int...) = op(OpName"Id"(), st, ds...)
35-
36-
function op(::OpName"Adag", ::SiteType"Qudit", d::Int)
37-
mat = zeros(d, d)
38-
for k in 1:(d - 1)
39-
mat[k + 1, k] = k
40-
end
41-
return mat
11+
a = falses(dim(s))
12+
a[n + 1] = one(Bool)
13+
return a
4214
end
43-
op(on::OpName"adag", st::SiteType"Qudit", d::Int) = op(alias(on), st, d)
44-
op(on::OpName"a†", st::SiteType"Qudit", d::Int) = op(alias(on), st, d)
4515

46-
function op(::OpName"A", ::SiteType"Qudit", d::Int)
47-
mat = zeros(d, d)
16+
function Base.AbstractArray(n::OpName"a†", t::Tuple{SiteType"Qudit"})
17+
d = length(t)
18+
a = zeros(d, d)
4819
for k in 1:(d - 1)
49-
mat[k, k + 1] = k
20+
a[k + 1, k] = k
5021
end
51-
return mat
22+
return a
5223
end
53-
op(on::OpName"a", st::SiteType"Qudit", d::Int) = op(alias(on), st, d)
24+
@op_alias "Adag" "a†"
25+
@op_alias "adag" "a†"
5426

55-
function op(::OpName"N", ::SiteType"Qudit", d::Int)
56-
mat = zeros(d, d)
57-
for k in 1:d
58-
mat[k, k] = k - 1
59-
end
60-
return mat
61-
end
62-
op(on::OpName"n", st::SiteType"Qudit", d::Int) = op(alias(on), st, d)
63-
64-
# two-body operators
65-
function op(::OpName"ab", st::SiteType"Qudit", d1::Int, d2::Int)
66-
return kron(op(OpName("a"), st, d1), op(OpName("a"), st, d2))
67-
end
27+
alias(::OpName"a") = OpName"a†"()'
28+
@op_alias "A" "a"
6829

69-
function op(::OpName"a†b", st::SiteType"Qudit", d1::Int, d2::Int)
70-
return kron(op(OpName("a†"), st, d1), op(OpName("a"), st, d2))
71-
end
72-
73-
function op(::OpName"ab†", st::SiteType"Qudit", d1::Int, d2::Int)
74-
return kron(op(OpName("a"), st, d1), op(OpName("a†"), st, d2))
75-
end
76-
77-
function op(::OpName"a†b†", st::SiteType"Qudit", d1::Int, d2::Int)
78-
return kron(op(OpName("a†"), st, d1), op(OpName("a†"), st, d2))
79-
end
80-
81-
## TODO: Make this logic more general.
82-
## # interface
83-
## function op(on::OpName, st::SiteType"Qudit", s1::Index, s_tail::Index...; kwargs...)
84-
## rs = reverse((s1, s_tail...))
85-
## ds = dim.(rs)
86-
## opmat = op(on, st, ds...; kwargs...)
87-
## return itensor(opmat, prime.(rs)..., dag.(rs)...)
88-
## end
89-
90-
function op(on::OpName, st::SiteType"Qudit"; kwargs...)
91-
return error("`op` can't be called without indices or dimensions.")
92-
end
30+
alias(::OpName"n") = OpName"a†"() * OpName"a"()
31+
@op_alias "N" "n"
9332

94-
# Zygote
95-
@non_differentiable op(::OpName"ab", ::SiteType"Qudit", ::Int, ::Int)
96-
@non_differentiable op(::OpName"a†b", ::SiteType"Qudit", ::Int, ::Int)
97-
@non_differentiable op(::OpName"ab†", ::SiteType"Qudit", ::Int, ::Int)
98-
@non_differentiable op(::OpName"a†b†", ::SiteType"Qudit", ::Int, ::Int)
99-
@non_differentiable op(::OpName"a", ::SiteType"Qudit", ::Int)
100-
@non_differentiable op(::OpName"a†", ::SiteType"Qudit", ::Int)
101-
@non_differentiable op(::OpName"N", ::SiteType"Qudit", ::Int)
33+
alias(::OpName"aa") = OpName("a") OpName("a")
34+
alias(::OpName"a†a") = OpName("a†") OpName("a")
35+
alias(::OpName"aa†") = OpName("a") OpName("a†")
36+
alias(::OpName"a†a†") = OpName("a†") OpName("a†")

0 commit comments

Comments
 (0)