-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor expect (single site) (#162)
- Loading branch information
Showing
11 changed files
with
161 additions
and
196 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
MIT License | ||
|
||
Copyright (c) 2021 Matthew Fishman <[email protected]> and contributors | ||
Copyright (c) 2021 Matthew Fishman <[email protected]>, Joseph Tindall <[email protected]> and contributors | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
name = "ITensorNetworks" | ||
uuid = "2919e153-833c-4bdc-8836-1ea460a35fc7" | ||
authors = ["Matthew Fishman <[email protected]> and contributors"] | ||
authors = ["Matthew Fishman <[email protected]>, Joseph Tindall <[email protected]> and contributors"] | ||
version = "0.10.2" | ||
|
||
[deps] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ DocMeta.setdocmeta!( | |
|
||
makedocs(; | ||
modules=[ITensorNetworks], | ||
authors="Matthew Fishman <[email protected]> and contributors", | ||
authors="Matthew Fishman <[email protected]>, Joseph Tindall <[email protected]> and contributors", | ||
repo="https://github.com/mtfishman/ITensorNetworks.jl/blob/{commit}{path}#{line}", | ||
sitename="ITensorNetworks.jl", | ||
format=Documenter.HTML(; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,63 @@ | ||
using ITensors.ITensorMPS: ITensorMPS, expect, promote_itensor_eltype, OpSum | ||
using Dictionaries: Dictionary, set! | ||
using ITensors: Op, op, contract, siteinds, which_op | ||
using ITensors.ITensorMPS: ITensorMPS, expect | ||
|
||
default_expect_alg() = "bp" | ||
|
||
function ITensorMPS.expect(ψIψ::AbstractFormNetwork, op::Op; contract_kwargs=(;), kwargs...) | ||
v = only(op.sites) | ||
ψIψ_v = ψIψ[operator_vertex(ψIψ, v)] | ||
s = commonind(ψIψ[ket_vertex(ψIψ, v)], ψIψ_v) | ||
operator = ITensors.op(op.which_op, s) | ||
∂ψIψ_∂v = environment(ψIψ, operator_vertices(ψIψ, [v]); kwargs...) | ||
numerator = contract(vcat(∂ψIψ_∂v, operator); contract_kwargs...)[] | ||
denominator = contract(vcat(∂ψIψ_∂v, ψIψ_v); contract_kwargs...)[] | ||
|
||
return numerator / denominator | ||
end | ||
|
||
function ITensorMPS.expect( | ||
op::String, | ||
ψ::AbstractITensorNetwork; | ||
cutoff=nothing, | ||
maxdim=nothing, | ||
ortho=false, | ||
sequence=nothing, | ||
vertices=vertices(ψ), | ||
alg::Algorithm, | ||
ψ::AbstractITensorNetwork, | ||
ops; | ||
(cache!)=nothing, | ||
update_cache=isnothing(cache!), | ||
cache_update_kwargs=default_cache_update_kwargs(cache!), | ||
cache_construction_function=tn -> | ||
cache(alg, tn; default_cache_construction_kwargs(alg, tn)...), | ||
kwargs..., | ||
) | ||
s = siteinds(ψ) | ||
ElT = promote_itensor_eltype(ψ) | ||
# ElT = ishermitian(ITensors.op(op, s[vertices[1]])) ? real(ElT) : ElT | ||
res = Dictionary(vertices, Vector{ElT}(undef, length(vertices))) | ||
if isnothing(sequence) | ||
sequence = contraction_sequence(inner_network(ψ, ψ)) | ||
ψIψ = inner_network(ψ, ψ) | ||
if isnothing(cache!) | ||
cache! = Ref(cache_construction_function(ψIψ)) | ||
end | ||
normψ² = norm_sqr(ψ; alg="exact", sequence) | ||
for v in vertices | ||
O = ITensor(Op(op, v), s) | ||
Oψ = apply(O, ψ; cutoff, maxdim, ortho) | ||
res[v] = inner(ψ, Oψ; alg="exact", sequence) / normψ² | ||
|
||
if update_cache | ||
cache![] = update(cache![]; cache_update_kwargs...) | ||
end | ||
return res | ||
|
||
return map(op -> expect(ψIψ, op; alg, cache!, update_cache=false, kwargs...), ops) | ||
end | ||
|
||
function ITensorMPS.expect(alg::Algorithm"exact", ψ::AbstractITensorNetwork, ops; kwargs...) | ||
ψIψ = inner_network(ψ, ψ) | ||
return map(op -> expect(ψIψ, op; alg, kwargs...), ops) | ||
end | ||
|
||
function ITensorMPS.expect( | ||
ℋ::OpSum, | ||
ψ::AbstractITensorNetwork; | ||
cutoff=nothing, | ||
maxdim=nothing, | ||
ortho=false, | ||
sequence=nothing, | ||
ψ::AbstractITensorNetwork, op::Op; alg=default_expect_alg(), kwargs... | ||
) | ||
s = siteinds(ψ) | ||
# h⃗ = Vector{ITensor}(ℋ, s) | ||
if isnothing(sequence) | ||
sequence = contraction_sequence(inner_network(ψ, ψ)) | ||
end | ||
h⃗ψ = [apply(hᵢ, ψ; cutoff, maxdim, ortho) for hᵢ in ITensors.terms(ℋ)] | ||
ψhᵢψ = [inner(ψ, hᵢψ; alg="exact", sequence) for hᵢψ in h⃗ψ] | ||
ψh⃗ψ = sum(ψhᵢψ) | ||
ψψ = norm_sqr(ψ; alg="exact", sequence) | ||
return ψh⃗ψ / ψψ | ||
return expect(Algorithm(alg), ψ, [op]; kwargs...) | ||
end | ||
|
||
function ITensorMPS.expect( | ||
ψ::AbstractITensorNetwork, op::String, vertices; alg=default_expect_alg(), kwargs... | ||
) | ||
return expect(Algorithm(alg), ψ, [Op(op, vertex) for vertex in vertices]; kwargs...) | ||
end | ||
|
||
function ITensorMPS.expect( | ||
opsum_sum::Sum{<:OpSum}, | ||
ψ::AbstractITensorNetwork; | ||
cutoff=nothing, | ||
maxdim=nothing, | ||
ortho=true, | ||
sequence=nothing, | ||
ψ::AbstractITensorNetwork, op::String; alg=default_expect_alg(), kwargs... | ||
) | ||
return expect(sum(Ops.terms(opsum_sum)), ψ; cutoff, maxdim, ortho, sequence) | ||
return expect(ψ, op, vertices(ψ); alg, kwargs...) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.