-
Notifications
You must be signed in to change notification settings - Fork 1
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
Implement an MPS method initializing the tensors to identity (copy-tensors) #218
Implement an MPS method initializing the tensors to identity (copy-tensors) #218
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! But could you clarify a lil bit how it works?
test/MPS_test.jl
Outdated
dispatch_arraysdims = [ | ||
([2, 4], [5, 4, 3], [2, 3]), | ||
[(2, 4), (5, 4, 3), (2, 3)], | ||
((2, 4), (5, 4, 3), (2, 3)), | ||
[[2, 4], [5, 4, 3], [2, 3]], | ||
] | ||
for arraysdims in dispatch_arraysdims | ||
ψ = MPS(arraysdims) # Default order (:o, :l, :r) | ||
@test size(tensors(ψ; at=site"1")) == (2, 4) | ||
@test size(tensors(ψ; at=site"2")) == (5, 4, 3) | ||
@test size(tensors(ψ; at=site"3")) == (2, 3) | ||
t1 = tensors(ψ; at=site"1") | ||
@test t1[1, 1] == t1[2, 2] == 1 | ||
@test sum(t1) == 2 | ||
t2 = tensors(ψ; at=site"2") | ||
@test t2[1, 1, 1] == t2[2, 2, 2] == t2[3, 3, 3] == 1 | ||
@test sum(t2) == 3 | ||
t3 = tensors(ψ; at=site"3") | ||
@test t3[1, 1] == t3[2, 2] == 1 | ||
@test sum(t3) == 2 | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrap this into a @testset
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
src/Ansatz/MPS.jl
Outdated
MPS(arraysdims::NTuple{N,<:Vector{Int}}; order=defaultorder(MPS)) where {N} = MPS(collect(arraysdims); order=order) | ||
MPS(arraysdims::Vector{<:Tuple{Vararg{Int}}}; order=defaultorder(MPS)) = MPS(collect.(arraysdims); order=order) | ||
function MPS(arraysdims::NTuple{N,Tuple{Vararg{Int}}}; order=defaultorder(MPS)) where {N} | ||
return MPS(collect(collect.(arraysdims)); order=order) | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would currently just delete these methods for now...
MPS(arraysdims::NTuple{N,<:Vector{Int}}; order=defaultorder(MPS)) where {N} = MPS(collect(arraysdims); order=order) | |
MPS(arraysdims::Vector{<:Tuple{Vararg{Int}}}; order=defaultorder(MPS)) = MPS(collect.(arraysdims); order=order) | |
function MPS(arraysdims::NTuple{N,Tuple{Vararg{Int}}}; order=defaultorder(MPS)) where {N} | |
return MPS(collect(collect.(arraysdims)); order=order) | |
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed them
src/Ansatz/MPS.jl
Outdated
function MPS(arraysdims::NTuple{N,Tuple{Vararg{Int}}}; order=defaultorder(MPS)) where {N} | ||
return MPS(collect(collect.(arraysdims)); order=order) | ||
end | ||
function MPS(arraysdims::Vector{<:Vector{Int}}; order=defaultorder(MPS)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would use Base.identity
, not a constructor since you're not passing arrays.
function MPS(arraysdims::Vector{<:Vector{Int}}; order=defaultorder(MPS)) | |
function Base.identity(::Type{MPS}, arraysdims; order=defaultorder(MPS)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was one possibility along with the one @jofrevalles suggested below of using ones
. I replaced the constructor by identity
as I think it fits better than ones
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @Todorbsc
src/Ansatz/MPS.jl
Outdated
MPS(arraysdims::NTuple{N,<:Vector{Int}}; order=defaultorder(MPS)) where {N} = MPS(collect(arraysdims); order=order) | ||
MPS(arraysdims::Vector{<:Tuple{Vararg{Int}}}; order=defaultorder(MPS)) = MPS(collect.(arraysdims); order=order) | ||
function MPS(arraysdims::NTuple{N,Tuple{Vararg{Int}}}; order=defaultorder(MPS)) where {N} | ||
return MPS(collect(collect.(arraysdims)); order=order) | ||
end | ||
function MPS(arraysdims::Vector{<:Vector{Int}}; order=defaultorder(MPS)) | ||
@assert length(arraysdims[1]) == 2 "First array must have 2 dimensions" | ||
@assert all(==(3) ∘ length, arraysdims[2:(end - 1)]) "All arrays must have 3 dimensions" | ||
@assert length(arraysdims[end]) == 2 "Last array must have 2 dimensions" | ||
issetequal(order, defaultorder(MPS)) || | ||
throw(ArgumentError("order must be a permutation of $(String.(defaultorder(MPS)))")) | ||
|
||
return MPS( | ||
map(arraysdims) do arrdims | ||
mindim = minimum(arrdims) | ||
arr = zeros(ComplexF64, arrdims...) | ||
deltas = ntuple(x -> ntuple(_ -> x, length(arrdims)), mindim) | ||
broadcast(delta -> arr[delta...] = 1.0, deltas) | ||
arr | ||
end; | ||
order=order, | ||
) | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would not create another constructor for MPS
, since that may be misleading to the user who wants to directly create an MPS
with some specific arrays. Instead, you could extend the function ones
or one similiar, to make that when we call ones(MPS, arraydims ... )
we get this kind of MPS
. Something similar to the things we do with rand
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I said above, I agree it is better to not implement another constructor. The option that fits the best for me is Base.identity
, check it out :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, in general I agree with @jofrevalles but I think ones
might be misleading since it could mean you're initializing the MPS to a
I think identity
is better because contracting against another "identity" MPS gives you 1, all the tensors are just COPY-tensors and "identity initialization" is how is known in some packages (it's also known as eye
-initialization but eye
is how the identity is known on those languages).
Also note that, as we discussed, we should add a |
Sure! The point is to create an identity |
Thanks for clarifying but I think having custom dimensions is not so relevant and can be tedious for the user to generate all the tuples... IMO it would be better to accept a signature like Base.identity(::Type{MPS}, n; maxdim=2^(n ÷ 2), physdim=2) where |
@mofeing What happens in the case where |
the order or num dims of the tensors is not affected. just the size of the virtual indices is upper bounded. so yeah, you can use this code in here to compute the virtual index sizes for an open MPS Lines 233 to 239 in 4c16d2f
|
Perfect, thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! I would add some empty lines to make the testset clearer, but this is ready to merge soon.
Co-authored-by: Jofre Vallès Muns <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
…nsors) (#218) * Format code * Implement MPS identity initialization * Add tests for all dispatches of MPS identity init * Format julia code * Rename function header & add docstring * Fix test set for identity MPS * Format code * Rewrite MPS identity init function to nsites instead of arrays' dimensions * Format julia code * Update docstring of identity * Clean code in test (suggested by Jofre) Co-authored-by: Jofre Vallès Muns <[email protected]> * Format julia code * Refactor virtualdims in identity (suggested by Sergio) * Update src/Ansatz/MPS.jl * Restrict to default order in identity MPS * Update src/Ansatz/MPS.jl * Remove order parameter in identity --------- Co-authored-by: Jofre Vallès Muns <[email protected]> Co-authored-by: Sergio Sánchez Ramírez <[email protected]>
* Prototype `MPS`, `MPO` * Implement `rand`, `adjoint`, `defaultorder`, `boundary`, `form` for `MPS`, `MPO` * Implement conversion from `Product` to `MPS`, `MPO` * Refactor `MPS`, `MPO` on top of new `Ansatz` type * Move `Chain` code to `AbstractAnsatz` and `MPS` * Fix `sites` method for `MPS` * Fix `inds` method for `MPS` * Refactor `adapt_structure` method to support additional types * Refactor `Reactant.make_tracer`, `Reactant.create_result` methods on top of recent changes * Refactor `ChainRules` methods on top of new types * Refactor `rand` for `MPS`, `MPO` * Refactor `Chain` tests on top of `MPS`, `MPO` * Try using more `@site_str` instead of `Site` in MPS tests * Implement some `sites`, `inds` methods for `MPO` * Try using more `@site_str` in MPO tests * Fix typo in `mixed_canonize!` * Fix `truncate` tests on `MPS` * Refactor some tests of `MPS` to simplify * Fix typo in `normalize!` on `MPS` method * Fix typo * Deprecate `isleftcanonical`, `isrightcanonical` in favor of `isisometry` * Fix `isleftcanonical`, `isrightcanonical` tests on boundary sites * Fix `evolve!` calls in tests * Refactor MPO tests * Stop orthogonalization to index on `mixed_canonize!` * Aesthetic name fix * Stop using `IdDict` on Reactant extension * Fix `create_result` on `MPS`, `MPO` * Refactor lattice generation in constructors of `Dense`, `Product`, `MPS`, `MPO`, `PEPS` * Implement an MPS method initializing the tensors to identity (copy-tensors) (#218) * Format code * Implement MPS identity initialization * Add tests for all dispatches of MPS identity init * Format julia code * Rename function header & add docstring * Fix test set for identity MPS * Format code * Rewrite MPS identity init function to nsites instead of arrays' dimensions * Format julia code * Update docstring of identity * Clean code in test (suggested by Jofre) Co-authored-by: Jofre Vallès Muns <[email protected]> * Format julia code * Refactor virtualdims in identity (suggested by Sergio) * Update src/Ansatz/MPS.jl * Restrict to default order in identity MPS * Update src/Ansatz/MPS.jl * Remove order parameter in identity --------- Co-authored-by: Jofre Vallès Muns <[email protected]> Co-authored-by: Sergio Sánchez Ramírez <[email protected]> * move files * fix constructors * Document types * Remove unimplemented `evolve!` method * fix mutability of `MPO` * Move `MPO` code to "MPS.jl" and refactor common code * document `MPS`, `MPO` constructors * document `rand` on `MPS`, `MPO` * move some docstrings to `AbstractAnsatz` * Refactor `normalize!` * Fix `defaultorder` * fix `normalize!` * apply `isisometry` docstring suggestion by @starsfordummies * add shortcut for `normalize!` with mixed canonization * fix MPS identity constructor test * implement shortcut `Quantum` constructor for simple gates * fix test * refactor exported names * fix `mixed_canonize!` tests * fix `canonize!`, `mixed_canonize!` * import missing symbols to tests * fix field name of `MixedCanonical` * fix namespace clash with `truncate` * fix `truncate!` * fix tests * try fix `mixed_canonize!`, `normalize!` * fix keyword args of `simple_update!` call * comment * fix `MPO` test * more fixes * fix test * refactor legacy `simple_update!` on `Canonical` form * fix symbol in test * rename testset * fix `reindex!` * Remove legacy `@show` * format code * refactor `evolve!` tests * refactor `evolve!` tests again * fix indexing in `simple_update!` * fix wrong call to `canonize!` * try fix forward-mode diff of `MPS`, `MPO` constructors --------- Co-authored-by: Todorbsc <[email protected]> Co-authored-by: Jofre Vallès Muns <[email protected]>
Closes #165