-
Notifications
You must be signed in to change notification settings - Fork 32
/
types.jl
325 lines (251 loc) · 6.48 KB
/
types.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# a linear map for low-level hamiltonian representation
"""
struct Hamiltonian
`Hamiltonian` stores the dynamic prefactors of each term.
The actual hamiltonian is the sum of `f_i(t) * t_i` where
`f_i` and `t_i` are entries of `fs` and `ts`.
"""
struct Hamiltonian{FS <: Tuple, TS <: Tuple}
fs::FS # prefactor of each term
ts::TS # const linear map of each term
function Hamiltonian(fs, ts)
all(x->size(x)==size(ts[1]), ts) || throw(ArgumentError("matrix term should have the same size"))
new{typeof(fs), typeof(ts)}(fs, ts)
end
end
Base.size(h::Hamiltonian) = size(h.ts[1])
Base.size(h::Hamiltonian, idx::Int) = size(h.ts[1], idx)
Adapt.@adapt_structure Hamiltonian
"""
struct StepHamiltonian
A low-level linear-map object that encodes time-dependent
hamiltonian at time step `t`. This object supports the
linear map interface `mul!(Y, H, X)`.
"""
struct StepHamiltonian{T, FS, TS}
t::T # clock
h::Hamiltonian{FS, TS}
end
Base.size(h::StepHamiltonian, idx::Int) = size(h.h, idx)
Base.size(h::StepHamiltonian) = size(h.h)
function to_matrix(h::StepHamiltonian)
return sum(zip(h.h.fs, h.h.ts)) do (f, t)
f(h.t) * t
end
end
function LinearAlgebra.opnorm(h::StepHamiltonian, p=2)
return opnorm(to_matrix(h), p)
end
(h::Hamiltonian)(t::Real) = StepHamiltonian(t, h)
# NOTE: these high level expression
# will be converted to lower level Hamiltonian object
# before simulation no need to aggressively specialize on them
# the following operators are mainly used to interface
# with Yao
"""
XPhase{T} <: PrimitiveBlock{2}
XPhase operator.
```math
e^{ϕ ⋅ im} |0⟩⟨1| + e^{-ϕ ⋅ im} |1⟩⟨0|
```
"""
struct XPhase{T} <: PrimitiveBlock{2}
ϕ::T
end
struct PuPhase{T} <: PrimitiveBlock{2}
ϕ::T
end
struct PdPhase{T} <: PrimitiveBlock{2}
ϕ::T
end
"""
AbstractTerm <: PrimitiveBlock{2}
Abstract term for local hamiltonian terms.
"""
abstract type AbstractTerm <: PrimitiveBlock{2} end
"""
struct RydInteract <: AbstractTerm
RydInteract(;atoms, C=2π * 862690MHz⋅μm^6)
Type for Rydberg interactive term.
# Expression
```math
\\sum_{i, j} \\frac{C}{|r_i - r_j|^6} n_i n_j
```
# Keyword Arguments
- `atoms`: a list of atom positions, must be type `RydAtom`, default unit is `μm`.
- `C`: the interaction strength, default unit is `MHz⋅μm^6`. default value is `2π * 862690 * MHz*µm^6`.
"""
Base.@kwdef struct RydInteract <: AbstractTerm
atoms::Vector
C::Real = 2π * 862690
function RydInteract(atoms, C)
new(atoms, default_unit(MHz * µm^6, C))
end
end
"""
struct SumOfX <: AbstractTerm
SumOfX(nsites, Ω)
Term for sum of X operators.
The following two expressions are equivalent
```jldoctest; setup=:(using BloqadeExpr, YaoBlocks)
julia> SumOfX(nsites=5)
∑ σ^x_i
julia> sum([X for _ in 1:5])
nqudits: 1
+
├─ X
├─ X
├─ X
├─ X
└─ X
```
# Expression
```math
\\sum_i Ω σ^x_i
```
"""
Base.@kwdef struct SumOfX <: AbstractTerm
nsites::Int
Ω = 1
function SumOfX(nsites, Ω)
assert_param(nsites, Ω, :Ω)
Ω = default_unit(MHz, Ω)
new(nsites, Ω)
end
end
SumOfX(n::Int) = SumOfX(n, 1)
"""
struct SumOfXPhase <: AbstractTerm
SumOfXPhase(;nsites, Ω=1, ϕ)
Sum of `XPhase` operators.
The following two expressions are equivalent
```jldoctest; setup=:(using BloqadeExpr)
julia> SumOfXPhase(nsites=5, ϕ=0.1)
1.0 ⋅ ∑ e^{0.1 ⋅ im} |0⟩⟨1| + e^{-0.1 ⋅ im} |1⟩⟨0|
julia> sum([XPhase(0.1) for _ in 1:5])
nqudits: 1
+
├─ XPhase(0.1)
├─ XPhase(0.1)
├─ XPhase(0.1)
├─ XPhase(0.1)
└─ XPhase(0.1)
```
But may provide extra speed up.
# Expression
```math
\\sum_i Ω ⋅ (e^{ϕ ⋅ im} |0⟩⟨1| + e^{-ϕ ⋅ im} |1⟩⟨0|)
```
"""
Base.@kwdef struct SumOfXPhase <: AbstractTerm
nsites::Int
Ω = 1
ϕ
function SumOfXPhase(nsites, Ω, ϕ)
assert_param(nsites, Ω, :Ω)
assert_param(nsites, ϕ, :ϕ)
Ω = default_unit(MHz, Ω)
ϕ = default_unit(NoUnits, ϕ)
new(nsites, Ω, ϕ)
end
end
"""
struct SumOfN <: AbstractTerm
SumOfN(;nsites[, Δ=1])
Sum of N operators.
The following two expression are equivalent
```jldoctest; setup=:(using BloqadeExpr)
julia> SumOfN(nsites=5)
∑ n_i
julia> sum([Op.n for _ in 1:5])
nqudits: 1
+
├─ P1
├─ P1
├─ P1
├─ P1
└─ P1
```
But may provide extra speed up.
# Expression
```math
\\sum_i Δ ⋅ n_i
```
"""
Base.@kwdef struct SumOfN <: AbstractTerm
nsites::Int
Δ = 1
function SumOfN(nsites, Δ)
assert_param(nsites, Δ, :Δ)
new(nsites, default_unit(MHz, Δ))
end
end
SumOfN(n::Int) = SumOfN(n, 1)
"""
struct SumOfZ <: AbstractTerm
SumOfZ(;nsites, Δ=1)
Sum of Pauli Z operators.
The following two expression are equivalent
```jldoctest; setup=:(using BloqadeExpr, YaoBlocks)
julia> SumOfZ(nsites=5)
∑ σ^z_i
julia> sum([Z for _ in 1:5])
nqudits: 1
+
├─ Z
├─ Z
├─ Z
├─ Z
└─ Z
```
# Expression
```math
\\sum_i Δ ⋅ σ^z_i
```
"""
Base.@kwdef struct SumOfZ <: AbstractTerm
nsites::Int
Δ = 1
function SumOfZ(nsites, Δ)
assert_param(nsites, Δ, :Δ)
new(nsites, default_unit(MHz, Δ))
end
end
SumOfZ(n::Int) = SumOfZ(n, 1)
YaoAPI.nqudits(::XPhase) = 1
YaoAPI.nqudits(::PdPhase) = 1
YaoAPI.nqudits(::PuPhase) = 1
YaoAPI.nqudits(h::RydInteract) = length(h.atoms)
YaoAPI.nqudits(h::SumOfX) = h.nsites
YaoAPI.nqudits(h::SumOfXPhase) = h.nsites
YaoAPI.nqudits(h::SumOfZ) = h.nsites
YaoAPI.nqudits(h::SumOfN) = h.nsites
function Base.:(==)(lhs::RydInteract, rhs::RydInteract)
lhs.C == rhs.C && lhs.atoms == rhs.atoms
end
function Base.:(==)(lhs::SumOfX, rhs::SumOfX)
lhs.nsites == rhs.nsites && lhs.Ω == rhs.Ω
end
function Base.:(==)(lhs::SumOfZ, rhs::SumOfZ)
lhs.nsites == rhs.nsites && lhs.Δ == rhs.Δ
end
function Base.:(==)(lhs::SumOfN, rhs::SumOfN)
lhs.nsites == rhs.nsites && lhs.Δ == rhs.Δ
end
function Base.:(==)(lhs::SumOfXPhase, rhs::SumOfXPhase)
lhs.nsites == rhs.nsites && lhs.Ω == rhs.Ω && lhs.ϕ == rhs.ϕ
end
Base.isreal(::RydInteract) = true
Base.isreal(::SumOfN) = true
Base.isreal(::SumOfX) = true
Base.isreal(::SumOfZ) = true
Base.isreal(::SumOfXPhase) = false
Base.isreal(h::Add) = all(isreal, subblocks(h))
Base.isreal(h::Scale) = isreal(factor(h)) && isreal(content(h))
storage_size(x) = sizeof(x)
function storage_size(h::Hamiltonian)
sum(storage_size, h.ts)
end
function storage_size(H::SparseMatrixCSC)
return sizeof(H.colptr) + sizeof(H.rowval) + sizeof(H.nzval)
end