bsc-quantic / Tenet.jl

Composable Tensor Network library in Julia
https://bsc-quantic.github.io/Tenet.jl/
Apache License 2.0
17 stars 1 forks source link

Implement `ColumnReduction` transformation for `TensorNetwork` #63

Closed jofrevalles closed 1 year ago

jofrevalles commented 1 year ago

Summary

This PR addresses the issue #17 (resolves #17) by introducing the ColumnReduction transformation in the transform! function for TensorNetworks. This transformation applies the column reduction (as defined here) to simplify the tensor network. The key idea is to reduce the rank of the tensors that have a dimension where all but a "column" is filled with zeros.

Furthermore, we have extended the scope of this method to handle cases where more than one column contains non-zeros but specific columns are entirely zero-filled. This allows the reduction of the index dimensionality where columns are filled with zeros.

In addition to implementing the transformation, this PR includes tests to ensure the correctness and robustness of the new method.

Example

The primary function of ColumnReduction is to decrease tensor rank where a dimension features only one non-zero 'column'. The following example illustrates this operation:

julia> using Tenet

julia> data = rand(3, 3, 3)
...

julia> data[:,1:2,:] .= 0 # 1st and 2nd column of the 2nd dimension are zero
...

julia> data # only 3rd columns is non-zero
3×3×3 Array{Float64, 3}:
[:, :, 1] =
 0.0  0.0  0.350011
 0.0  0.0  0.990275
 0.0  0.0  0.295394

[:, :, 2] =
 0.0  0.0  0.538989
 0.0  0.0  0.141018
 0.0  0.0  0.687099

[:, :, 3] =
 0.0  0.0  0.291134
 0.0  0.0  0.0972771
 0.0  0.0  0.614801

julia> A = Tensor(data, (:i, :j, :k)); B = Tensor(rand(3, 3), (:j, :l)); C = Tensor(rand(3, 3), (:j, :m))
3×3×3 Tensor{Float64, 3, Array{Float64, 3}}:
...

julia> tn = TensorNetwork([A, B, C])
TensorNetwork{Arbitrary}(#tensors=3, #inds=5)

julia> reduced = transform(tn, ColumnReduction)
TensorNetwork{Arbitrary}(#tensors=3, #inds=4)

julia> [labels(tensor) for tensor in reduced.tensors]
3-element Vector{Tuple{Symbol, Vararg{Symbol}}}:
 (:i, :k)
 (:l,)
 (:m,)

In this example, the column reduction transformation results in a network where the tensors are unconnected.

We now show how we enhanced the transformation to operate in scenarios where more than one non-zero column exists. The example below highlights such an instance:

julia> using Tenet

julia> data = rand(3, 3, 3)
...

julia> data[:,2,:] .= 0 # 2nd column of the 2nd dimension can be reduced
...

julia> data # 1st and 3rd column of the 2nd dimension are non-zero
3×3×3 Array{Float64, 3}:
[:, :, 1] =
 0.615815  0.0  0.245444
 0.26432   0.0  0.0888118
 0.812256  0.0  0.294028

[:, :, 2] =
 0.998973  0.0  0.0581316
 0.532386  0.0  0.764539
 0.503775  0.0  0.812196

[:, :, 3] =
 0.959305  0.0  0.812671
 0.717794  0.0  0.925235
 0.812287  0.0  0.54355

julia> A = Tensor(data, (:i, :j, :k)); B = Tensor(rand(3, 3), (:j, :l)); C = Tensor(rand(3, 3), (:j, :m))
3×3×3 Tensor{Float64, 3, Array{Float64, 3}}:
...

julia> tn = TensorNetwork([A, B, C])
TensorNetwork{Arbitrary}(#tensors=3, #inds=5)

julia> reduced = transform(tn, ColumnReduction)
TensorNetwork{Arbitrary}(#tensors=3, #inds=5)

julia> [size(tensor) for tensor in reduced.tensors]
3-element Vector{Tuple{Int64, Int64, Vararg{Int64}}}:
 (3, 2, 3)
 (2, 3)
 (2, 3)

In this example we show that when we have only one zero column in an index, we can apply a transformation to reduce the dimension of that index.

codecov[bot] commented 1 year ago

Codecov Report

Merging #63 (ada6a56) into master (67b417f) will increase coverage by 0.68%. The diff coverage is 95.55%.

@@            Coverage Diff             @@
##           master      #63      +/-   ##
==========================================
+ Coverage   84.21%   84.89%   +0.68%     
==========================================
  Files          12       12              
  Lines         703      748      +45     
==========================================
+ Hits          592      635      +43     
- Misses        111      113       +2     
Impacted Files Coverage Δ
src/Transformations.jl 98.07% <95.55%> (-1.03%) :arrow_down: