forked from JoeyT1994/TensorNetworkQuantumSimulator.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinner.jl
More file actions
80 lines (67 loc) · 3.77 KB
/
Copy pathinner.jl
File metadata and controls
80 lines (67 loc) · 3.77 KB
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
"""
inner(ψ::TensorNetworkState, ϕ::TensorNetworkState; alg, kwargs...)
Compute the inner product ⟨ψ|ϕ⟩ between two `TensorNetworkState`s using the specified algorithm. The two states must have the same graph structure and physical indices on each site. For the squared norm of a single state, use `norm_sqr(ψ; alg, kwargs...)` instead.
# Arguments
- `ψ::TensorNetworkState`: The first tensor network state.
- `ϕ::TensorNetworkState`: The second tensor network state.
# Keyword Arguments
- `alg`: The algorithm to use for the inner product calculation. Options include:
- `"exact"`: Exact contraction of the tensor network.
- `"bp"`: Belief propagation approximation.
- `"boundarymps"`: Boundary MPS approximation (requires `mps_bond_dimension`).
- `"loopcorrections"`: Loop corrections to belief propagation.
- Extra kwargs for `alg = "boundarymps"`:
- `mps_bond_dimension::Integer`: The bond dimension for the boundary MPS approximation.
- `partition_by`: How to partition the graph for boundary MPS (default is `"row"`).
- `cache_update_kwargs`: Additional keyword arguments for updating the cache.
- Extra kwargs for `alg = "bp"` or `"loopcorrections"`:
- `cache_update_kwargs`: Additional keyword arguments for updating the cache.
- `max_configuration_size`: Maximum configuration size for loop corrections (only for `"loopcorrections"`).
# Returns
- The computed inner product as a scalar value.
# Example
```julia
s = siteinds("S=1/2", g)
ψ = random_tensornetworkstate(ComplexF32, g, s; bond_dimension = 4)
ϕ = random_tensornetworkstate(ComplexF32, g, s; bond_dimension = 4)
# Exact inner product
ip_exact = ITensors.inner(ψ, ϕ; alg = "exact")
# Belief propagation inner product
ip_bp = ITensors.inner(ψ, ϕ; alg = "bp")
# Boundary MPS inner product with bond dimension 10
ip_bmps = ITensors.inner(ψ, ϕ; alg = "boundarymps", mps_bond_dimension = 10)
```
"""
function ITensors.inner(ψ::TensorNetworkState, ϕ::TensorNetworkState; alg, kwargs...)
algorithm_check(ψ, "inner", alg)
algorithm_check(ϕ, "inner", alg)
return inner(Algorithm(alg), ψ, ϕ; kwargs...)
end
function ITensors.inner(
alg::Algorithm"exact", blf::BilinearForm;
contraction_sequence_kwargs = (; alg = "omeinsum", optimizer = GreedyMethod())
)
blf_tensors = bp_factors(blf, collect(vertices(ket(blf))))
seq = contraction_sequence(blf_tensors; contraction_sequence_kwargs...)
return scalar(contract(blf_tensors; sequence = seq))
end
function ITensors.inner(alg::Algorithm, cache::AbstractBeliefPropagationCache; max_configuration_size = nothing)
tn = network(cache)
z = cache_partitionfunction(alg, cache; max_configuration_size)
tn isa BilinearForm && return z
return state_error("BilinearForm")
end
function ITensors.inner(alg::Union{Algorithm"bp", Algorithm"loopcorrections"}, ψ::TensorNetworkState, ϕ::TensorNetworkState; cache_update_kwargs = (;), kwargs...)
ψϕ_bpc = BeliefPropagationCache(BilinearForm(ψ, ϕ))
ψϕ_bpc = update(ψϕ_bpc; cache_update_kwargs...)
return inner(alg, ψϕ_bpc; kwargs...)
end
function ITensors.inner(alg::Algorithm"boundarymps", ψ::TensorNetworkState, ϕ::TensorNetworkState; mps_bond_dimension::Integer, partition_by = "row", cache_update_kwargs = (;), kwargs...)
ψϕ_bmps = BoundaryMPSCache(BilinearForm(ψ, ϕ), mps_bond_dimension; partition_by)
cache_update_kwargs = with_default_maxiter(cache_update_kwargs, ψϕ_bmps)
ψϕ_bmps = update(ψϕ_bmps; cache_update_kwargs...)
return inner(alg, ψϕ_bmps; kwargs...)
end
function ITensors.inner(alg::Algorithm"exact", ψ::TensorNetworkState, ϕ::TensorNetworkState)
return inner(alg, BilinearForm(ψ, ϕ))
end