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

Avoid in-place operations in adjoint and conj functions #226

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 4 additions & 4 deletions src/Quantum.jl
Original file line number Diff line number Diff line change
Expand Up @@ -335,12 +335,12 @@ end

Returns the adjoint of a [`Quantum`](@ref) Tensor Network; i.e. the conjugate Tensor Network with the inputs and outputs swapped.
"""
Base.adjoint(tn::AbstractQuantum) = adjoint!(deepcopy(tn))
Base.adjoint(tn::AbstractQuantum) = adjoint_sites!(conj(tn))

function LinearAlgebra.adjoint!(tn::AbstractQuantum)
conj!(tn)
LinearAlgebra.adjoint!(tn::AbstractQuantum) = adjoint_sites!(conj!(tn))

# update site information
# update site information and rename inner indices
function adjoint_sites!(tn::AbstractQuantum)
oldsites = copy(Quantum(tn).sites)
empty!(Quantum(tn).sites)
for (site, index) in oldsites
Expand Down
12 changes: 11 additions & 1 deletion src/TensorNetwork.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,17 @@ end

Base.eltype(tn::AbstractTensorNetwork) = promote_type(eltype.(tensors(tn))...)

Base.conj(tn::AbstractTensorNetwork) = conj!(deepcopy(tn))
"""
conj(tn::AbstractTensorNetwork)

Return a copy of the [`AbstractTensorNetwork`](@ref) with all tensors conjugated.
"""
function Base.conj(tn::AbstractTensorNetwork)
tn = copy(tn)
replace!(tn, tensors(tn) .=> conj.(tensors(tn)))
return tn
end

function Base.conj!(tn::AbstractTensorNetwork)
foreach(conj!, tensors(tn))
return tn
Expand Down
19 changes: 19 additions & 0 deletions test/Quantum_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,25 @@
@test_throws ErrorException Quantum(tn, Dict(site"1" => :j))
@test_throws ErrorException Quantum(tn, Dict(site"1" => :i))

@testset "Base.adjoint" begin
_tensors = Tensor[
Tensor(rand(ComplexF64, 2, 4, 2), [:i, :link, :j]), Tensor(rand(ComplexF64, 2, 4, 2), [:k, :link, :l])
]
tn = TensorNetwork(_tensors)
qtn = Quantum(tn, Dict(site"1" => :i, site"2" => :k, site"1'" => :j, site"2'" => :l))

adjoint_qtn = adjoint(qtn)

@test nsites(adjoint_qtn; set=:inputs) == nsites(adjoint_qtn; set=:outputs) == 2
@test issetequal(sites(adjoint_qtn), [site"1", site"2", site"1'", site"2'"])
@test socket(adjoint_qtn) == Operator()
@test inds(adjoint_qtn; at=site"1'") == :i # now the indices are flipped
@test inds(adjoint_qtn; at=site"1") == :j
@test inds(adjoint_qtn; at=site"2'") == :k
@test inds(adjoint_qtn; at=site"2") == :l
@test isapprox(tensors(adjoint_qtn), replace.(conj.(_tensors), :link => Symbol(:link, "'")))
end

@testset "reindex!" begin
@testset "manual indices" begin
# mps-like tensor network
Expand Down
8 changes: 8 additions & 0 deletions test/TensorNetwork_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,14 @@
@test issetequal(inds(projvirttn), [:i, :k])
end

@testset "Base.conj" begin
tensor1 = Tensor(rand(ComplexF64, 3, 4), (:i, :j))
tensor2 = Tensor(rand(ComplexF64, 4, 5), (:j, :k))
complextn = TensorNetwork([tensor1, tensor2])

@test -imag.(tensors(complextn)) == imag.(tensors(conj(complextn)))
end

@testset "Base.conj!" begin
@testset "for complex" begin
tensor1 = Tensor(rand(ComplexF64, 3, 4), (:i, :j))
Expand Down
Loading