/
sitetype.jl
834 lines (685 loc) · 22.5 KB
/
sitetype.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
using ChainRulesCore: @ignore_derivatives
using ..ITensors:
ITensors, Index, ITensor, itensor, dag, onehot, prime, product, swapprime, tags
using ..SmallStrings: SmallString
using ..TagSets: TagSets, TagSet, addtags, commontags
@eval struct SiteType{T}
(f::Type{<:SiteType})() = $(Expr(:new, :f))
end
# Note that the complicated definition of
# SiteType above is a workaround for performance
# issues when creating parameterized types
# in Julia 1.4 and 1.5-beta. Ideally we
# can just use the following in the future:
# struct SiteType{T}
# end
"""
SiteType is a parameterized type which allows
making Index tags into Julia types. Use cases
include overloading functions such as `op`,
`siteinds`, and `state` which generate custom
operators, Index arrays, and IndexVals associated
with Index objects having a certain tag.
To make a SiteType type, you can use the string
macro notation: `SiteType"MyTag"`
To make a SiteType value or object, you can use
the notation: `SiteType("MyTag")`
There are currently a few built-in site types
recognized by `jl`. The system is easily extensible
by users. To add new operators to an existing site type,
or to create new site types, you can follow the instructions
[here](https://itensor.github.io/jl/stable/examples/Physics.html).
The current built-in site types are:
- `SiteType"S=1/2"` (or `SiteType"S=½"`)
- `SiteType"S=1"`
- `SiteType"Qubit"`
- `SiteType"Qudit"`
- `SiteType"Boson"`
- `SiteType"Fermion"`
- `SiteType"tJ"`
- `SiteType"Electron"`
# Examples
Tags on indices get turned into SiteTypes internally, and then
we search for overloads of functions like `op` and `siteind`.
For example:
```julia
julia> s = siteind("S=1/2")
(dim=2|id=862|"S=1/2,Site")
julia> @show op("Sz", s);
op(s, "Sz") = ITensor ord=2
Dim 1: (dim=2|id=862|"S=1/2,Site")'
Dim 2: (dim=2|id=862|"S=1/2,Site")
NDTensors.Dense{Float64,Array{Float64,1}}
2×2
0.5 0.0
0.0 -0.5
julia> @show op("Sx", s);
op(s, "Sx") = ITensor ord=2
Dim 1: (dim=2|id=862|"S=1/2,Site")'
Dim 2: (dim=2|id=862|"S=1/2,Site")
NDTensors.Dense{Float64,Array{Float64,1}}
2×2
0.0 0.5
0.5 0.0
julia> @show op("Sy", s);
op(s, "Sy") = ITensor ord=2
Dim 1: (dim=2|id=862|"S=1/2,Site")'
Dim 2: (dim=2|id=862|"S=1/2,Site")
NDTensors.Dense{Complex{Float64},Array{Complex{Float64},1}}
2×2
0.0 + 0.0im -0.0 - 0.5im
0.0 + 0.5im 0.0 + 0.0im
julia> s = siteind("Electron")
(dim=4|id=734|"Electron,Site")
julia> @show op("Nup", s);
op(s, "Nup") = ITensor ord=2
Dim 1: (dim=4|id=734|"Electron,Site")'
Dim 2: (dim=4|id=734|"Electron,Site")
NDTensors.Dense{Float64,Array{Float64,1}}
4×4
0.0 0.0 0.0 0.0
0.0 1.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 1.0
```
Many operators are available, for example:
- `SiteType"S=1/2"`: `"Sz"`, `"Sx"`, `"Sy"`, `"S+"`, `"S-"`, ...
- `SiteType"Electron"`: `"Nup"`, `"Ndn"`, `"Nupdn"`, `"Ntot"`, `"Cup"`,
`"Cdagup"`, `"Cdn"`, `"Cdagdn"`, `"Sz"`, `"Sx"`, `"Sy"`, `"S+"`, `"S-"`, ...
- ...
You can view the source code for the internal SiteType definitions
and operators that are defined [here](https://github.com/ITensor/jl/tree/main/src/physics/site_types).
"""
SiteType(s::AbstractString) = SiteType{SmallString(s)}()
SiteType(t::Integer) = SiteType{SmallString(t)}()
SiteType(t::SmallString) = SiteType{t}()
tag(::SiteType{T}) where {T} = T
macro SiteType_str(s)
return SiteType{SmallString(s)}
end
# Keep TagType defined for backwards
# compatibility; will be deprecated later
const TagType = SiteType
macro TagType_str(s)
return TagType{SmallString(s)}
end
#---------------------------------------
#
# op system
#
#---------------------------------------
@eval struct OpName{Name}
(f::Type{<:OpName})() = $(Expr(:new, :f))
end
# Note that the complicated definition of
# OpName above is a workaround for performance
# issues when creating parameterized types
# in Julia 1.4 and 1.5-beta. Ideally we
# can just use the following in the future:
# struct OpName{Name}
# end
"""
OpName is a parameterized type which allows
making strings into Julia types for the purpose
of representing operator names.
The main use of OpName is overloading the
`op!` method which generates operators
for indices with certain tags such as "S=1/2".
To make a OpName type, you can use the string
macro notation: `OpName"MyTag"`.
To make an OpName value or object, you can use
the notation: `OpName("myop")`
"""
OpName(s::AbstractString) = OpName{Symbol(s)}()
OpName(s::Symbol) = OpName{s}()
# TODO: Avoid overloading `ITensors` version.
ITensors.name(::OpName{N}) where {N} = N
macro OpName_str(s)
return OpName{Symbol(s)}
end
# Default implementations of op and op!
op(::OpName; kwargs...) = nothing
op(::OpName, ::SiteType; kwargs...) = nothing
op(::OpName, ::SiteType, ::Index...; kwargs...) = nothing
function op(
::OpName, ::SiteType, ::SiteType, sitetypes_inds::Union{SiteType,Index}...; kwargs...
)
return nothing
end
op!(::ITensor, ::OpName, ::SiteType, ::Index...; kwargs...) = nothing
function op!(
::ITensor,
::OpName,
::SiteType,
::SiteType,
sitetypes_inds::Union{SiteType,Index}...;
kwargs...,
)
return nothing
end
# Deprecated version, for backwards compatibility
op(::SiteType, ::Index, ::AbstractString; kwargs...) = nothing
function _sitetypes(ts::TagSet)
Ntags = length(ts)
return SiteType[SiteType(TagSets.data(ts)[n]) for n in 1:Ntags]
end
_sitetypes(i::Index) = _sitetypes(tags(i))
"""
op(opname::String, s::Index; kwargs...)
Return an ITensor corresponding to the operator
named `opname` for the Index `s`. The operator
is constructed by calling an overload of either
the `op` or `op!` methods which take a `SiteType`
argument that corresponds to one of the tags of
the Index `s` and an `OpName"opname"` argument
that corresponds to the input operator name.
Operator names can be combined using the `"*"`
symbol, for example `"S+*S-"` or `"Sz*Sz*Sz"`.
The result is an ITensor made by forming each operator
then contracting them together in a way corresponding
to the usual operator product or matrix multiplication.
The `op` system is used by the OpSum
system to convert operator names into ITensors,
and can be used directly such as for applying
operators to MPS.
# Example
```julia
s = Index(2, "Site,S=1/2")
Sz = op("Sz", s)
```
To see all of the operator names defined for the site types included with
ITensor, please view the [source code](https://github.com/ITensor/jl/tree/main/src/physics/site_types)
for each site type. Note that some site types such as "S=1/2" and "Qubit"
are aliases for each other and share operator definitions.
"""
function op(name::AbstractString, s::Index...; adjoint::Bool=false, kwargs...)
name = strip(name)
# TODO: filter out only commons tags
# if there are multiple indices
commontags_s = commontags(s...)
# first we handle the + and - algebra, which requires a space between ops to avoid clashing
name_split = nothing
@ignore_derivatives name_split = String.(split(name, " "))
oplocs = findall(x -> x ∈ ("+", "-"), name_split)
if !isempty(oplocs)
@ignore_derivatives !isempty(kwargs) &&
error("Lazy algebra on parametric gates not allowed")
# the string representation of algebra ops: ex ["+", "-", "+"]
labels = name_split[oplocs]
# assign coefficients to each term: ex [+1, -1, +1]
coeffs = [1, [(-1)^Int(label == "-") for label in labels]...]
# grad the name of each operator block separated by an algebra op, and do so by
# making sure blank spaces between opnames are kept when building the new block.
start, opnames = 0, String[]
for oploc in oplocs
finish = oploc
opnames = vcat(
opnames, [prod([name_split[k] * " " for k in (start + 1):(finish - 1)])]
)
start = oploc
end
opnames = vcat(
opnames, [prod([name_split[k] * " " for k in (start + 1):length(name_split)])]
)
# build the vector of blocks and sum
op_list = [
coeff * (op(opname, s...; kwargs...)) for (coeff, opname) in zip(coeffs, opnames)
]
return sum(op_list)
end
# the the multiplication come after
oploc = findfirst("*", name)
if !isnothing(oploc)
op1, op2 = nothing, nothing
@ignore_derivatives begin
op1 = name[1:prevind(name, oploc.start)]
op2 = name[nextind(name, oploc.start):end]
if !(op1[end] == ' ' && op2[1] == ' ')
@warn "($op1*$op2) composite op definition `A*B` deprecated: please use `A * B` instead (with spaces)"
end
end
return product(op(op1, s...; kwargs...), op(op2, s...; kwargs...))
end
common_stypes = _sitetypes(commontags_s)
@ignore_derivatives push!(common_stypes, SiteType("Generic"))
opn = OpName(name)
#
# Try calling a function of the form:
# op(::OpName, ::SiteType, ::Index...; kwargs...)
#
for st in common_stypes
res = op(opn, st, s...; kwargs...)
if !isnothing(res)
adjoint && return swapprime(dag(res), 0 => 1)
return res
end
end
#
# Try calling a function of the form:
# op(::OpName; kwargs...)
# for backward compatibility with previous
# gate system in PastaQ.jl
#
op_mat = op(opn; kwargs...)
if !isnothing(op_mat)
rs = reverse(s)
res = itensor(op_mat, prime.(rs)..., dag.(rs)...)
adjoint && return swapprime(dag(res), 0 => 1)
return res
end
#
# otherwise try calling a function of the form:
# op(::OpName, ::SiteType; kwargs...)
# which returns a Julia matrix
#
for st in common_stypes
op_mat = op(opn, st; kwargs...)
if !isnothing(op_mat)
rs = reverse(s)
#return itensor(op_mat, prime.(rs)..., dag.(rs)...)
res = itensor(op_mat, prime.(rs)..., dag.(rs)...)
adjoint && return swapprime(dag(res), 0 => 1)
return res
end
end
# otherwise try calling a function of the form:
# op!(::ITensor, ::OpName, ::SiteType, ::Index...; kwargs...)
#
Op = ITensor(prime.(s)..., dag.(s)...)
for st in common_stypes
op!(Op, opn, st, s...; kwargs...)
if !isempty(Op)
adjoint && return swapprime(dag(Op), 0 => 1)
return Op
end
end
if length(s) > 1
# No overloads for common tags found. It might be a
# case of making an operator with mixed site types,
# searching for overloads like:
# op(::OpName,
# ::SiteType...,
# ::Index...;
# kwargs...)
# op!(::ITensor, ::OpName,
# ::SiteType...,
# ::Index...;
# kwargs...)
stypes = _sitetypes.(s)
for st in Iterators.product(stypes...)
res = op(opn, st..., s...; kwargs...)
if !isnothing(res)
adjoint && return swapprime(dag(res), 0 => 1)
return res
end
end
Op = ITensor(prime.(s)..., dag.(s)...)
for st in Iterators.product(stypes...)
op!(Op, opn, st..., s...; kwargs...)
if !isempty(Op)
adjoint && return swapprime(dag(Op), 0 => 1)
return Op
end
end
throw(
ArgumentError(
"Overload of \"op\" or \"op!\" functions not found for operator name \"$name\" and Index tags: $(tags.(s)).",
),
)
end
#
# otherwise try calling a function of the form:
# op(::SiteType, ::Index, ::AbstractString)
#
# (Note: this version is for backwards compatibility
# after version 0.1.10, and may be eventually
# deprecated)
#
for st in common_stypes
res = op(st, s[1], name; kwargs...)
if !isnothing(res)
adjoint && return dag(res)
return res
end
end
return throw(
ArgumentError(
"Overload of \"op\" or \"op!\" functions not found for operator name \"$name\" and Index tags: $(tags.(s)).",
),
)
end
op(name::AbstractString; kwargs...) = error("Must input indices when creating an `op`.")
"""
op(X::AbstractArray, s::Index...)
op(M::Matrix, s::Index...)
Given a matrix M and a set of indices s,t,...
return an operator ITensor with matrix elements given
by M and indices s, s', t, t'
# Example
```julia
julia> s = siteind("S=1/2")
(dim=2|id=575|"S=1/2,Site")
julia> Sz = op([1/2 0; 0 -1/2],s)
ITensor ord=2 (dim=2|id=575|"S=1/2,Site")' (dim=2|id=575|"S=1/2,Site")
NDTensors.Dense{Float64, Vector{Float64}}
julia> @show Sz
Sz = ITensor ord=2
Dim 1: (dim=2|id=575|"S=1/2,Site")'
Dim 2: (dim=2|id=575|"S=1/2,Site")
NDTensors.Dense{Float64, Vector{Float64}}
2×2
0.5 0.0
0.0 -0.5
ITensor ord=2 (dim=2|id=575|"S=1/2,Site")' (dim=2|id=575|"S=1/2,Site")
NDTensors.Dense{Float64, Vector{Float64}}
```
"""
op(X::AbstractArray, s::Index...) = itensor(X, prime.([s...]), dag.([s...]))
op(opname, s::Vector{<:Index}; kwargs...) = op(opname, s...; kwargs...)
op(s::Vector{<:Index}, opname; kwargs...) = op(opname, s...; kwargs...)
# For backwards compatibility, version of `op`
# taking the arguments in the other order:
op(s::Index, opname; kwargs...) = op(opname, s; kwargs...)
# To ease calling of other op overloads,
# allow passing a string as the op name
op(opname::AbstractString, t::SiteType; kwargs...) = op(OpName(opname), t; kwargs...)
"""
op(opname::String,sites::Vector{<:Index},n::Int; kwargs...)
Return an ITensor corresponding to the operator
named `opname` for the n'th Index in the array
`sites`.
# Example
```julia
s = siteinds("S=1/2", 4)
Sz2 = op("Sz", s, 2)
```
"""
function op(opname, s::Vector{<:Index}, ns::NTuple{N,Integer}; kwargs...) where {N}
return op(opname, ntuple(n -> s[ns[n]], Val(N))...; kwargs...)
end
function op(opname, s::Vector{<:Index}, ns::Vararg{Integer}; kwargs...)
return op(opname, s, ns; kwargs...)
end
function op(s::Vector{<:Index}, opname, ns::Tuple{Vararg{Integer}}; kwargs...)
return op(opname, s, ns...; kwargs...)
end
function op(s::Vector{<:Index}, opname, ns::Integer...; kwargs...)
return op(opname, s, ns; kwargs...)
end
function op(s::Vector{<:Index}, opname, ns::Tuple{Vararg{Integer}}, kwargs::NamedTuple)
return op(opname, s, ns; kwargs...)
end
function op(s::Vector{<:Index}, opname, ns::Integer, kwargs::NamedTuple)
return op(opname, s, (ns,); kwargs...)
end
op(s::Vector{<:Index}, o::Tuple) = op(s, o...)
op(o::Tuple, s::Vector{<:Index}) = op(s, o...)
op(f::Function, args...; kwargs...) = f(op(args...; kwargs...))
function op(
s::Vector{<:Index},
f::Function,
opname::AbstractString,
ns::Tuple{Vararg{Integer}};
kwargs...,
)
return f(op(opname, s, ns...; kwargs...))
end
function op(
s::Vector{<:Index}, f::Function, opname::AbstractString, ns::Integer...; kwargs...
)
return f(op(opname, s, ns; kwargs...))
end
# Here, Ref is used to not broadcast over the vector of indices
# TODO: consider overloading broadcast for `op` with the example
# here: https://discourse.julialang.org/t/how-to-broadcast-over-only-certain-function-arguments/19274/5
# so that `Ref` isn't needed.
ops(s::Vector{<:Index}, os::AbstractArray) = [op(oₙ, s) for oₙ in os]
ops(os::AbstractVector, s::Vector{<:Index}) = [op(oₙ, s) for oₙ in os]
@doc """
ops(s::Vector{<:Index}, os::Vector)
ops(os::Vector, s::Vector{<:Index})
Given a list of operators, create ITensors using the collection
of indices.
# Examples
```julia
s = siteinds("Qubit", 4)
os = [("H", 1), ("X", 2), ("CX", 2, 4)]
# gates = ops(s, os)
gates = ops(os, s)
```
""" ops(::Vector{<:Index}, ::AbstractArray)
#---------------------------------------
#
# state system
#
#---------------------------------------
@eval struct StateName{Name}
(f::Type{<:StateName})() = $(Expr(:new, :f))
end
StateName(s::AbstractString) = StateName{SmallString(s)}()
StateName(s::SmallString) = StateName{s}()
# TODO: Avoid overloading `ITensors` version.
ITensors.name(::StateName{N}) where {N} = N
macro StateName_str(s)
return StateName{SmallString(s)}
end
state(::StateName, ::SiteType; kwargs...) = nothing
state(::StateName, ::SiteType, ::Index; kwargs...) = nothing
state!(::ITensor, ::StateName, ::SiteType, ::Index; kwargs...) = nothing
# Syntax `state("Up", Index(2, "S=1/2"))`
state(sn::String, i::Index; kwargs...) = state(i, sn; kwargs...)
"""
state(s::Index, name::String; kwargs...)
Return an ITensor corresponding to the state
named `name` for the Index `s`. The returned
ITensor will have `s` as its only index.
The terminology here is based on the idea of a
single-site state or wavefunction in physics.
The `state` function is implemented for various
Index tags by overloading either the
`state` or `state!` methods which take a `SiteType`
argument corresponding to one of the tags of
the Index `s` and an `StateName"name"` argument
that corresponds to the input state name.
The `state` system is used by the MPS type
to construct product-state MPS and for other purposes.
# Example
```julia
s = Index(2, "Site,S=1/2")
sup = state(s,"Up")
sdn = state(s,"Dn")
sxp = state(s,"X+")
sxm = state(s,"X-")
```
"""
function state(s::Index, name::AbstractString; kwargs...)::ITensor
stypes = _sitetypes(s)
sname = StateName(name)
# Try calling state(::StateName"Name",::SiteType"Tag",s::Index; kwargs...)
for st in stypes
v = state(sname, st, s; kwargs...)
if !isnothing(v)
if v isa ITensor
return v
else
# TODO: deprecate, only for backwards compatibility.
return itensor(v, s)
end
end
end
# Try calling state!(::ITensor,::StateName"Name",::SiteType"Tag",s::Index;kwargs...)
T = ITensor(s)
for st in stypes
state!(T, sname, st, s; kwargs...)
!isempty(T) && return T
end
#
# otherwise try calling a function of the form:
# state(::StateName"Name", ::SiteType"Tag"; kwargs...)
# which returns a Julia vector
#
for st in stypes
v = state(sname, st; kwargs...)
!isnothing(v) && return itensor(v, s)
end
return throw(
ArgumentError(
"Overload of \"state\" or \"state!\" functions not found for state name \"$name\" and Index tags $(tags(s))",
),
)
end
state(s::Index, n::Integer) = onehot(s => n)
state(sset::Vector{<:Index}, j::Integer, st; kwargs...) = state(sset[j], st; kwargs...)
#---------------------------------------
#
# val system
#
#---------------------------------------
@eval struct ValName{Name}
(f::Type{<:ValName})() = $(Expr(:new, :f))
end
ValName(s::AbstractString) = ValName{SmallString(s)}()
ValName(s::Symbol) = ValName{s}()
# TODO: Avoid overloading `ITensors` version.
ITensors.name(::ValName{N}) where {N} = N
macro ValName_str(s)
return ValName{SmallString(s)}
end
val(::ValName, ::SiteType) = nothing
val(::AbstractString, ::SiteType) = nothing
"""
val(s::Index, name::String)
Return an integer corresponding to the `name`
of a certain value the Index `s` can take.
In other words, the `val` function maps strings
to specific integer values within the range `1:dim(s)`.
The `val` function is implemented for various
Index tags by overloading methods named `val`
which take a `SiteType` argument corresponding to
one of the tags of the Index `s` and an `ValName"name"`
argument that corresponds to the input name.
# Example
```julia
s = Index(2, "Site,S=1/2")
val(s,"Up") == 1
val(s,"Dn") == 2
s = Index(2, "Site,Fermion")
val(s,"Emp") == 1
val(s,"Occ") == 2
```
"""
function val(s::Index, name::AbstractString)::Int
stypes = _sitetypes(s)
sname = ValName(name)
# Try calling val(::StateName"Name",::SiteType"Tag",)
for st in stypes
res = val(sname, st)
!isnothing(res) && return res
end
return throw(
ArgumentError("Overload of \"val\" function not found for Index tags $(tags(s))")
)
end
val(s::Index, n::Integer) = n
val(sset::Vector{<:Index}, j::Integer, st) = val(sset[j], st)
#---------------------------------------
#
# siteind system
#
#---------------------------------------
space(st::SiteType; kwargs...) = nothing
space(st::SiteType, n::Int; kwargs...) = space(st; kwargs...)
function space_error_message(st::SiteType)
return "Overload of \"space\",\"siteind\", or \"siteinds\" functions not found for Index tag: $(tag(st))"
end
function siteind(st::SiteType; addtags="", kwargs...)
sp = space(st; kwargs...)
isnothing(sp) && return nothing
return Index(sp, "Site, $(tag(st)), $addtags")
end
function siteind(st::SiteType, n; kwargs...)
s = siteind(st; kwargs...)
!isnothing(s) && return addtags(s, "n=$n")
sp = space(st, n; kwargs...)
isnothing(sp) && error(space_error_message(st))
return Index(sp, "Site, $(tag(st)), n=$n")
end
siteind(tag::String; kwargs...) = siteind(SiteType(tag); kwargs...)
siteind(tag::String, n; kwargs...) = siteind(SiteType(tag), n; kwargs...)
# Special case of `siteind` where integer (dim) provided
# instead of a tag string
#siteind(d::Integer, n::Integer; kwargs...) = Index(d, "Site,n=$n")
function siteind(d::Integer, n::Integer; addtags="", kwargs...)
return Index(d, "Site,n=$n, $addtags")
end
#---------------------------------------
#
# siteinds system
#
#---------------------------------------
siteinds(::SiteType, N; kwargs...) = nothing
"""
siteinds(tag::String, N::Integer; kwargs...)
Create an array of `N` physical site indices of type `tag`.
Keyword arguments can be used to specify quantum number conservation,
see the `space` function corresponding to the site type `tag` for
supported keyword arguments.
# Example
```julia
N = 10
s = siteinds("S=1/2", N; conserve_qns=true)
```
"""
function siteinds(tag::String, N::Integer; kwargs...)
st = SiteType(tag)
si = siteinds(st, N; kwargs...)
if !isnothing(si)
return si
end
return [siteind(st, j; kwargs...) for j in 1:N]
end
"""
siteinds(f::Function, N::Integer; kwargs...)
Create an array of `N` physical site indices where the site type at site `n` is given
by `f(n)` (`f` should return a string).
"""
function siteinds(f::Function, N::Integer; kwargs...)
return [siteind(f(n), n; kwargs...) for n in 1:N]
end
# Special case of `siteinds` where integer (dim)
# provided instead of a tag string
"""
siteinds(d::Integer, N::Integer; kwargs...)
Create an array of `N` site indices, each of dimension `d`.
# Keywords
- `addtags::String`: additional tags to be added to all indices
"""
function siteinds(d::Integer, N::Integer; kwargs...)
return [siteind(d, n; kwargs...) for n in 1:N]
end
#---------------------------------------
#
# has_fermion_string system
#
#---------------------------------------
has_fermion_string(operator::AbstractArray{<:Number}, s::Index; kwargs...)::Bool = false
has_fermion_string(::OpName, ::SiteType) = nothing
function has_fermion_string(opname::AbstractString, s::Index; kwargs...)::Bool
opname = strip(opname)
# Interpret operator names joined by *
# as acting sequentially on the same site
starpos = findfirst(isequal('*'), opname)
if !isnothing(starpos)
op1 = opname[1:prevind(opname, starpos)]
op2 = opname[nextind(opname, starpos):end]
return xor(has_fermion_string(op1, s; kwargs...), has_fermion_string(op2, s; kwargs...))
end
Ntags = length(tags(s))
stypes = _sitetypes(s)
opn = OpName(opname)
for st in stypes
res = has_fermion_string(opn, st)
!isnothing(res) && return res
end
return false
end