Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reimplement MPS and MPO #232

Merged
merged 72 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from 41 commits
Commits
Show all changes
72 commits
Select commit Hold shift + click to select a range
530069f
Prototype `MPS`, `MPO`
mofeing Aug 9, 2024
823da0e
Implement `rand`, `adjoint`, `defaultorder`, `boundary`, `form` for `…
mofeing Sep 12, 2024
789f101
Implement conversion from `Product` to `MPS`, `MPO`
mofeing Sep 12, 2024
bf96569
Refactor `MPS`, `MPO` on top of new `Ansatz` type
mofeing Sep 16, 2024
8115965
Move `Chain` code to `AbstractAnsatz` and `MPS`
mofeing Nov 7, 2024
71feb81
Fix `sites` method for `MPS`
mofeing Sep 18, 2024
f89773a
Fix `inds` method for `MPS`
mofeing Sep 18, 2024
82d8183
Refactor `adapt_structure` method to support additional types
mofeing Nov 7, 2024
eafc6de
Refactor `Reactant.make_tracer`, `Reactant.create_result` methods on …
mofeing Nov 7, 2024
80e9455
Refactor `ChainRules` methods on top of new types
mofeing Nov 7, 2024
c46ac90
Refactor `rand` for `MPS`, `MPO`
mofeing Sep 18, 2024
7d1d254
Refactor `Chain` tests on top of `MPS`, `MPO`
mofeing Sep 18, 2024
830c8f4
Try using more `@site_str` instead of `Site` in MPS tests
mofeing Sep 18, 2024
1e82cc9
Implement some `sites`, `inds` methods for `MPO`
mofeing Sep 18, 2024
2dbe92e
Try using more `@site_str` in MPO tests
mofeing Sep 18, 2024
8df6213
Fix typo in `mixed_canonize!`
mofeing Sep 18, 2024
f030054
Fix `truncate` tests on `MPS`
mofeing Sep 18, 2024
775c6af
Refactor some tests of `MPS` to simplify
mofeing Sep 18, 2024
bf48d50
Fix typo in `normalize!` on `MPS` method
mofeing Sep 18, 2024
6cd0ef8
Fix typo
mofeing Sep 18, 2024
ec68b2e
Deprecate `isleftcanonical`, `isrightcanonical` in favor of `isisometry`
mofeing Sep 18, 2024
bdd2e26
Fix `isleftcanonical`, `isrightcanonical` tests on boundary sites
mofeing Sep 19, 2024
afddb6d
Fix `evolve!` calls in tests
mofeing Sep 19, 2024
b9a5148
Refactor MPO tests
mofeing Sep 22, 2024
5e04993
Stop orthogonalization to index on `mixed_canonize!`
mofeing Sep 26, 2024
6f84deb
Aesthetic name fix
mofeing Sep 26, 2024
e630527
Stop using `IdDict` on Reactant extension
mofeing Sep 28, 2024
5020d20
Fix `create_result` on `MPS`, `MPO`
mofeing Sep 30, 2024
cb1c8f4
Refactor lattice generation in constructors of `Dense`, `Product`, `M…
mofeing Sep 30, 2024
be708b8
Implement an MPS method initializing the tensors to identity (copy-te…
Todorbsc Oct 14, 2024
1b977fa
move files
mofeing Nov 7, 2024
a38ed16
fix constructors
mofeing Nov 7, 2024
7c12dab
Document types
mofeing Nov 7, 2024
c445787
Remove unimplemented `evolve!` method
mofeing Nov 7, 2024
6476930
fix mutability of `MPO`
mofeing Nov 8, 2024
a0e48f7
Move `MPO` code to "MPS.jl" and refactor common code
mofeing Nov 8, 2024
f97d33c
document `MPS`, `MPO` constructors
mofeing Nov 8, 2024
19264cf
document `rand` on `MPS`, `MPO`
mofeing Nov 8, 2024
1ec32bb
move some docstrings to `AbstractAnsatz`
mofeing Nov 8, 2024
b69d744
Refactor `normalize!`
mofeing Nov 8, 2024
c35ea9f
Fix `defaultorder`
mofeing Nov 8, 2024
109c9e5
fix `normalize!`
mofeing Nov 8, 2024
fc9c48a
apply `isisometry` docstring suggestion by @starsfordummies
mofeing Nov 8, 2024
84f422e
add shortcut for `normalize!` with mixed canonization
mofeing Nov 8, 2024
9b82c00
fix MPS identity constructor test
mofeing Nov 8, 2024
0a9de7b
implement shortcut `Quantum` constructor for simple gates
mofeing Nov 8, 2024
c932b7a
fix test
mofeing Nov 8, 2024
ae66855
refactor exported names
mofeing Nov 8, 2024
acf064e
fix `mixed_canonize!` tests
mofeing Nov 8, 2024
dcde266
fix `canonize!`, `mixed_canonize!`
mofeing Nov 8, 2024
3f5f7f8
import missing symbols to tests
mofeing Nov 8, 2024
a421a62
fix field name of `MixedCanonical`
mofeing Nov 10, 2024
ace97f1
fix namespace clash with `truncate`
mofeing Nov 10, 2024
2b31aeb
fix `truncate!`
mofeing Nov 10, 2024
f0b4d31
fix tests
mofeing Nov 10, 2024
5fae38c
try fix `mixed_canonize!`, `normalize!`
mofeing Nov 10, 2024
837ba8e
fix keyword args of `simple_update!` call
mofeing Nov 10, 2024
8186bc7
comment
mofeing Nov 10, 2024
16c5cbc
fix `MPO` test
mofeing Nov 10, 2024
a1a8b33
more fixes
mofeing Nov 10, 2024
293dae9
fix test
mofeing Nov 11, 2024
f8d4a91
refactor legacy `simple_update!` on `Canonical` form
mofeing Nov 11, 2024
de031b2
fix symbol in test
mofeing Nov 11, 2024
f38876d
rename testset
mofeing Nov 11, 2024
d97d154
fix `reindex!`
mofeing Nov 11, 2024
3b1bcc4
Remove legacy `@show`
mofeing Nov 11, 2024
ddbf909
format code
mofeing Nov 11, 2024
71defdb
refactor `evolve!` tests
mofeing Nov 11, 2024
fff1cb8
refactor `evolve!` tests again
mofeing Nov 11, 2024
c725495
fix indexing in `simple_update!`
mofeing Nov 11, 2024
c248c52
fix wrong call to `canonize!`
mofeing Nov 11, 2024
c9d3f52
try fix forward-mode diff of `MPS`, `MPO` constructors
mofeing Nov 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ext/TenetAdaptExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ Adapt.adapt_structure(to, x::TensorNetwork) = TensorNetwork(adapt.(Ref(to), tens
Adapt.adapt_structure(to, x::Quantum) = Quantum(adapt(to, TensorNetwork(x)), x.sites)
Adapt.adapt_structure(to, x::Ansatz) = Ansatz(adapt(to, Quantum(x)), Tenet.lattice(x))
Adapt.adapt_structure(to, x::Product) = Product(adapt(to, Ansatz(x)))
Adapt.adapt_structure(to, x::MPS) = MPS(adapt(to, Ansatz(x)), form(x))
Adapt.adapt_structure(to, x::MPO) = MPO(adapt(to, Ansatz(x)), form(x))

end
2 changes: 2 additions & 0 deletions ext/TenetChainRulesCoreExt/frules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ end

# `AbstractAnsatz`-subtype constructors
ChainRulesCore.frule((_, ẋ), ::Type{Product}, x::Ansatz) = Product(x), Tangent{Product}(; tn=ẋ)
ChainRulesCore.frule((_, ẋ), ::Type{MPS}, x::Ansatz, form) = MPS(x, form), Tangent{MPS}(; tn=ẋ, lattice=NoTangent())
ChainRulesCore.frule((_, ẋ), ::Type{MPO}, x::Ansatz, form) = MPO(x, form), Tangent{MPO}(; tn=ẋ, lattice=NoTangent())

# `Base.conj` methods
ChainRulesCore.frule((_, Δ), ::typeof(Base.conj), tn::Tensor) = conj(tn), conj(Δ)
Expand Down
8 changes: 8 additions & 0 deletions ext/TenetChainRulesCoreExt/rrules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ Product_pullback(ȳ) = (NoTangent(), ȳ.tn)
Product_pullback(ȳ::AbstractThunk) = Product_pullback(unthunk(ȳ))
ChainRulesCore.rrule(::Type{Product}, x::Ansatz) = Product(x), Product_pullback

MPS_pullback(ȳ) = (NoTangent(), ȳ.tn, NoTangent())
MPS_pullback(ȳ::AbstractThunk) = MPS_pullback(unthunk(ȳ))
ChainRulesCore.rrule(::Type{MPS}, x::Ansatz, form) = MPS(x, form), MPS_pullback

MPO_pullback(ȳ) = (NoTangent(), ȳ.tn, NoTangent())
MPO_pullback(ȳ::AbstractThunk) = MPO_pullback(unthunk(ȳ))
ChainRulesCore.rrule(::Type{MPO}, x::Ansatz, form) = MPO(x, form), MPO_pullback

# `Base.conj` methods
conj_pullback(Δ::Tensor) = (NoTangent(), conj(Δ))
conj_pullback(Δ::Tangent{Tensor}) = (NoTangent(), conj(Δ))
Expand Down
14 changes: 14 additions & 0 deletions ext/TenetReactantExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ function Reactant.make_tracer(seen, prev::Tenet.Product, path::Tuple, mode::Reac
return Tenet.Product(tracetn)
end

for A in (MPS, MPO)
@eval function Reactant.make_tracer(seen, prev::$A, path::Tuple, mode::Reactant.TraceMode; kwargs...)
tracetn = Reactant.make_tracer(seen, Ansatz(prev), Reactant.append_path(path, :tn), mode; kwargs...)
return $A(tracetn, form(prev))
end
end

function Reactant.create_result(@nospecialize(tocopy::Tensor), @nospecialize(path), result_stores)
data = Reactant.create_result(parent(tocopy), Reactant.append_path(path, :data), result_stores)
return :($Tensor($data, $(inds(tocopy))))
Expand Down Expand Up @@ -70,6 +77,13 @@ function Reactant.create_result(tocopy::Tenet.Product, @nospecialize(path), resu
return :($(Tenet.Product)($tn))
end

for A in (MPS, MPO)
@eval function Reactant.create_result(tocopy::A, @nospecialize(path), result_stores) where {A<:$A}
tn = Reactant.create_result(Ansatz(tocopy), Reactant.append_path(path, :tn), result_stores)
return :($A($tn, $(Tenet.form(tocopy))))
end
end

# TODO try rely on generic fallback for ansatzes
# function Reactant.create_result(tocopy::Tenet.Product, @nospecialize(path), result_stores)
# tn = Reactant.create_result(Ansatz(tocopy), Reactant.append_path(path, :tn), result_stores)
Expand Down
38 changes: 37 additions & 1 deletion src/Ansatz.jl
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,49 @@ Contract the virtual bond between two [`Site`](@ref)s in a [`AbstractAnsatz`](@r
"""
@kwmethod contract!(tn::AbstractAnsatz; bond) = contract!(tn, inds(tn; bond))

"""
canonize!(tn::AbstractAnsatz)

Transform an [`AbstractAnsatz`](@ref) Tensor Network into the canonical form (aka Vidal gauge); i.e. the singular values matrix Λᵢ between each tensor Γᵢ₋₁ and Γᵢ.
"""
function canonize! end

"""
canonize(tn::AbstractAnsatz)

Like [`canonize!`](@ref), but returns a new Tensor Network instead of modifying the original one.
"""
canonize(tn::AbstractAnsatz, args...; kwargs...) = canonize!(deepcopy(tn), args...; kwargs...)

"""
mixed_canonize!(tn::AbstractAnsatz, orthog_center)

Transform an [`AbstractAnsatz`](@ref) Tensor Network into the mixed-canonical form, that is,
for `i < orthog_center` the tensors are left-canonical and for `i >= orthog_center` the tensors are right-canonical,
and in the `orthog_center` there is a tensor with the Schmidt coefficients in it.
"""
function mixed_canonize! end

"""
mixed_canonize(tn::AbstractAnsatz, orthog_center)

Like [`mixed_canonize!`](@ref), but returns a new Tensor Network instead of modifying the original one.
"""
mixed_canonize(tn::AbstractAnsatz, args...; kwargs...) = mixed_canonize!(deepcopy(tn), args...; kwargs...)

canonize_site(tn::AbstractAnsatz, args...; kwargs...) = canonize_site!(deepcopy(tn), args...; kwargs...)

"""
isisometry(tn::AbstractAnsatz, site; kwargs...)

Check if the tensor at a given [`Site`](@ref) in a [`AbstractAnsatz`](@ref) Tensor Network is an isometry.
mofeing marked this conversation as resolved.
Show resolved Hide resolved
"""
function isisometry end

"""
truncate(tn::AbstractAnsatz, bond; threshold = nothing, maxdim = nothing)

Like [`truncate!`](@ref), but returns a new tensor network instead of modifying the original one.
Like [`truncate!`](@ref), but returns a new Tensor Network instead of modifying the original one.
"""
truncate(tn::AbstractAnsatz, args...; kwargs...) = truncate!(deepcopy(tn), args...; kwargs...)

Expand Down
Loading
Loading