/
state.jl
302 lines (268 loc) · 9.41 KB
/
state.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
using Test
using LinearAlgebra
"""Check that `state` is a valid element of a Hilbert space.
```julia
@test check_state(state;
for_immutable_state=true, for_mutable_state=true,
normalized=false, atol=1e-15, quiet=false)
```
verifies the following requirements:
* The inner product (`LinearAlgebra.dot`) of two states must return a Complex
number type.
* The `LinearAlgebra.norm` of `state` must be defined via the inner product.
This is the *definition* of a Hilbert space, a.k.a a complete inner product
space or more precisely a Banach space (normed vector space) where the
norm is induced by an inner product.
If `for_immutable_state`:
* `state + state` and `state - state` must be defined
* `copy(state)` must be defined
* `c * state` for a scalar `c` must be defined
* `norm(state + state)` must fulfill the triangle inequality
* `0.0 * state` must produce a state with norm 0
* `copy(state) - state` must have norm 0
* `norm(state)` must have absolute homogeneity: `norm(s * state) = s *
norm(state)`
If `for_mutable_state`:
* `similar(state)` must be defined and return a valid state
* `copyto!(other, state)` must be defined
* `LinearAlgebra.lmul!(c, state)` for a scalar `c` must be defined
* `LinearAlgebra.axpy!(c, state, other)` must be defined
* `norm(state)` must fulfill the same general mathematical norm properties as
with `for_immutable_state`.
If `normalized` (not required by default):
* `LinearAlgebra.norm(state)` must be 1
It is strongly recommended to always support immutable operations (also for
mutable states)
The function returns `true` for a valid state and `false` for an invalid state.
Unless `quiet=true`, it will log an error to indicate which of the conditions
failed.
"""
function check_state(
state;
for_immutable_state=true,
for_mutable_state=true,
normalized=false,
atol=1e-14,
quiet=false,
_check_similar=true, # to avoid infinite recursion
_message_prefix="" # for recursive calling
)
≈(a, b) = isapprox(a, b; atol)
px = _message_prefix
success = true
try
c = state ⋅ state
if !(c isa Complex)
quiet ||
@error "$(px)`state ⋅ state` must return a Complex number type, not $(typeof(c))"
success = false
end
catch exc
quiet || @error(
"$(px)the inner product of two states must be a complex number.",
exception = (exc, catch_abbreviated_backtrace())
)
success = false
end
try
r1 = norm(state)
r2 = sqrt(real(state ⋅ state))
Δ = abs(r1 - r2)
if Δ > atol
quiet || @error "$(px)`norm(state)=$r1)` must match `√(state⋅state)=$r2` (Δ=$Δ)"
success = false
end
catch exc
quiet || @error(
"$(px)the norm of a state must be defined via the inner product.",
exception = (exc, catch_abbreviated_backtrace())
)
success = false
end
if for_immutable_state
try
Δ = norm(state - state)
if Δ > atol
quiet || @error "`$(px)state - state` must have norm 0"
success = false
end
η = norm(state + state)
if η > (2 * norm(state) + atol)
quiet ||
@error "`$(px)norm(state + state)` must fulfill the triangle inequality"
success = false
end
catch exc
quiet || @error(
"$(px)`state + state` and `state - state` must be defined.",
exception = (exc, catch_abbreviated_backtrace())
)
success = false
end
try
ϕ = copy(state)
if norm(ϕ - state) > atol
quiet || @error "$(px)`copy(state) - state` must have norm 0"
success = false
end
catch exc
quiet || @error(
"$(px)copy(state) must be defined.",
exception = (exc, catch_abbreviated_backtrace())
)
success = false
end
try
ϕ = 0.5 * state
if abs(norm(ϕ) - 0.5 * norm(state)) > atol
quiet ||
@error "$(px)`norm(state)` must have absolute homogeneity: `norm(s * state) = s * norm(state)`"
success = false
end
catch exc
quiet || @error(
"$(px)`c * state` for a scalar `c` must be defined.",
exception = (exc, catch_abbreviated_backtrace())
)
success = false
end
try
if norm(0.0 * state) > atol
quiet || @error "$(px)`0.0 * state` must produce a state with norm 0"
success = false
end
catch exc
quiet || @error(
"$(px)`0.0 * state` must produce a state with norm 0.",
exception = (exc, catch_abbreviated_backtrace())
)
success = false
end
end
if for_mutable_state
has_similar = true
similar_is_valid = true
try
ϕ = similar(state)
catch exc
quiet || @error(
"$(px)`similar(state)` must be defined.",
exception = (exc, catch_abbreviated_backtrace())
)
has_similar = false
success = false
end
try
if has_similar
ϕ = similar(state)
copyto!(ϕ, state)
if _check_similar
# we only check ϕ after `copyto!`, because just `similar`
# might have NaNs in the amplitude, which screws up lots of
# the checks
if !check_state(
ϕ;
for_immutable_state,
for_mutable_state,
normalized=false,
atol,
_check_similar=false,
_message_prefix="On `similar(state)`: ",
quiet
)
quiet || @error("$(px)`similar(state)` must return a valid state")
similar_is_valid = false
success = false
end
end
if for_immutable_state
if norm(ϕ - state) > atol
quiet ||
@error "$(px)`ϕ - state` must have norm 0, where `ϕ = similar(state); copyto!(ϕ, state)`"
success = false
end
end
end
catch exc
quiet || @error(
"$(px)`copyto!(other, state)` must be defined.",
exception = (exc, catch_abbreviated_backtrace())
)
success = false
end
try
if has_similar && similar_is_valid
ϕ = similar(state)
copyto!(ϕ, state)
ϕ = lmul!(1im, ϕ)
if for_immutable_state
if norm(ϕ - 1im * state) > atol
quiet ||
@error "$(px)`norm(state)` must have absolute homogeneity: `norm(s * state) = s * norm(state)`"
success = false
end
end
end
catch exc
quiet || @error(
"$(px)`lmul!(c, state)` for a scalar `c` must be defined.",
exception = (exc, catch_abbreviated_backtrace())
)
success = false
end
try
if has_similar && similar_is_valid
ϕ = similar(state)
copyto!(ϕ, state)
ϕ = lmul!(0.0, ϕ)
if norm(ϕ) > atol
quiet ||
@error "$(px)`lmul!(0.0, state)` must produce a state with norm 0"
success = false
end
end
catch exc
quiet || @error(
"$(px)`lmul!(0.0, state)` must produce a state with norm 0.",
exception = (exc, catch_abbreviated_backtrace())
)
success = false
end
try
if has_similar && similar_is_valid
ϕ = similar(state)
copyto!(ϕ, state)
ϕ = axpy!(1im, state, ϕ)
if for_immutable_state
if norm(ϕ - (state + 1im * state)) > atol
quiet ||
@error "$(px)`axpy!(a, state, ϕ)` must match `ϕ += a * state`"
success = false
end
end
end
catch exc
quiet || @error(
"$(px)`axpy!(c, state, other)` must be defined.",
exception = (exc, catch_abbreviated_backtrace())
)
success = false
end
end
if normalized
try
η = norm(state)
if abs(η - 1) > atol
quiet || @error "$(px)`norm(state)` must be 1, not $η"
success = false
end
catch exc
quiet || @error(
"$(px)`norm(state)` must be 1.",
exception = (exc, catch_abbreviated_backtrace())
)
success = false
end
end
return success
end