Skip to content

Commit 0fb51dc

Browse files
committed
add ansatz
1 parent 6ea81bc commit 0fb51dc

8 files changed

Lines changed: 122 additions & 1 deletion

File tree

Project.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,18 @@ uuid = "1c4bdfd3-3b3e-42fc-86f5-70ab4d610eac"
33
authors = ["ArrogantGao <xz.gao@connect.ust.hk> and contributors"]
44
version = "1.0.0-DEV"
55

6+
[deps]
7+
GenericMessagePassing = "09522ef3-fe1e-4698-a056-61d75781c616"
8+
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
9+
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
10+
OMEinsum = "ebe7aa44-baf0-506c-a96f-8464559b3922"
11+
Yao = "5872b779-8223-5990-8dd0-5abbb0748c8c"
12+
613
[compat]
14+
Graphs = "1.12.1"
15+
LinearAlgebra = "1.11.0"
16+
OMEinsum = "0.8.7"
17+
Yao = "0.9.1"
718
julia = "1.6.7"
819

920
[extras]

src/BPGauge.jl

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
module BPGauge
22

3-
# Write your package code here.
3+
using LinearAlgebra, Graphs
4+
using OMEinsum
5+
using Yao
6+
using GenericMessagePassing # provide the `bp` backend
7+
8+
export TensorNetworkAnsatz
9+
10+
include("eins.jl")
11+
12+
# define and construct the network
13+
include("ansatz.jl")
14+
include("rydberg.jl")
15+
16+
# bp on the tensor network ansatz
17+
include("bp.jl")
418

519
end

src/ansatz.jl

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# the tensor network ansatz, |ket>
2+
struct TensorNetworkAnsatz{TA}
3+
g::SimpleGraph{Int} # g is used to store the connections between the tensors
4+
site_tensors::Vector{TA} # the tensors are stored as vector of arrays, corresponding to vertices of the graph. The virtual bounds are in order of its neighbors, the last dimension is the open dimension
5+
gauge_tensors::Vector{TA} # corresponding to edges of the graph, listed in the order of edges(g), and enforce e.src < e.dst
6+
gauge_tensors_map::Dict{Tuple{Int, Int}, Int} # maps between the edge and the index of the gauge tensor
7+
8+
function TensorNetworkAnsatz(g::SimpleGraph{Int}, d_virtual::Int, d_open::Int)
9+
TA = Array{ComplexF64}
10+
11+
site_tensors = Vector{TA}()
12+
gauge_tensors = Vector{TA}()
13+
14+
for v in vertices(g)
15+
t = zeros(ComplexF64, [d_virtual for _ in 1:length(neighbors(g, v))]..., d_open)
16+
push!(site_tensors, t)
17+
end
18+
19+
for e in edges(g)
20+
t = (one(ComplexF64) * I)(d_virtual)
21+
push!(gauge_tensors, t)
22+
end
23+
24+
map = Dict{Tuple{Int, Int}, Int}()
25+
for (i, e) in enumerate(edges(g))
26+
src, dst = minmax(e.src, e.dst)
27+
map[(src, dst)] = i
28+
end
29+
30+
new{Array{ComplexF64}}(g, site_tensors, gauge_tensors, map)
31+
end
32+
end
33+
34+
Base.show(io::IO, ansatz::TensorNetworkAnsatz{TA}) where {TA} = print(io, "TensorNetworkAnsatz{$(TA)} with $(nv(ansatz.g)) sites and $(ne(ansatz.g)) edges")
35+
36+
function inner_product(bra::TensorNetworkAnsatz{TA}, ket::TensorNetworkAnsatz{TA}; optimizer = GreedyMethod()) where {TA}
37+
@assert bra.g == ket.g
38+
39+
# 1:nv(g) are the indices of open indices
40+
count = nv(bra.g) + 1
41+
ixs_bra, count = all_eins(bra.g, count)
42+
ixs_ket, count = all_eins(ket.g, count)
43+
44+
ixs = vcat(ixs_bra, ixs_ket)
45+
raw_code = EinCode(ixs, Int[])
46+
47+
xs = [bra.site_tensors..., bra.gauge_tensors..., ket.site_tensors..., ket.gauge_tensors...]
48+
size_dict = OMEinsum.get_size_dict!(ixs, xs, Dict{Int, Int}())
49+
50+
opt_code = optimize_code(raw_code, size_dict, optimizer)
51+
@info "contraction complexity: $(contraction_complexity(opt_code, size_dict))"
52+
53+
res = opt_code(xs...)
54+
return res[]
55+
end

src/bp.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# bp on the TensorNetworkAnsatz
2+
3+
struct BPState{TA}
4+
message_e2v::Dict{Tuple{Int, Int}, TA}
5+
message_v2e::Dict{Tuple{Int, Int}, TA}
6+
end

src/eins.jl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# codes about constructing the einsum expression
2+
3+
function all_eins(g::SimpleGraph{Int}, count::Int)
4+
indices_dict = Dict{Tuple{Int, Int}, Int}()
5+
ixs = Vector{Vector{Int}}()
6+
7+
# site tensors
8+
for v in vertices(g)
9+
ix = Int[]
10+
for n in neighbors(g, v)
11+
push!(ix, count)
12+
indices_dict[(v, n)] = count
13+
count += 1
14+
end
15+
push!(ix, v)
16+
push!(ixs, ix)
17+
end
18+
19+
# gauge tensors
20+
for e in edges(g)
21+
src, dst = minmax(e.src, e.dst)
22+
push!(ixs, [indices_dict[(src, dst)], indices_dict[(dst, src)]])
23+
end
24+
25+
return ixs, count
26+
end

src/guage.jl

Whitespace-only changes.

src/rydberg.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# initialize the state for rydberg system, all sites are in the |0> state
2+
function zero_state(g::SimpleGraph{Int}; d_virtual::Int = 1)
3+
phi = TensorNetworkAnsatz(g, d_virtual, 2)
4+
# set all to the |0> state
5+
for tensor in phi.site_tensors
6+
tensor[1] = ComplexF64(1.0)
7+
end
8+
return phi
9+
end

src/su.jl

Whitespace-only changes.

0 commit comments

Comments
 (0)