Skip to content

Complex entries need to be conjugated after cost_and_gradient #98

Closed
@fliingelephant

Description

@fliingelephant

Cases

Failed to pass these two tests if change Float64 to ComplexF64.

@testset "belief propagation" begin
n = 5
chi = 3
mps_uai = TensorInference.random_tensor_train_uai(Float64, n, chi)
bp = BeliefPropgation(mps_uai)
@test TensorInference.initial_state(bp) isa TensorInference.BPState
state, info = belief_propagate(bp)
@test info.converged
@test info.iterations < 20
mars = marginals(state)
tnet = TensorNetworkModel(mps_uai)
mars_tnet = marginals(tnet)
for v in 1:TensorInference.num_variables(bp)
@test mars[[v]] mars_tnet[[v]] atol=1e-6
end
end
@testset "belief propagation on circle" begin
n = 10
chi = 3
mps_uai = TensorInference.random_tensor_train_uai(Float64, n, chi; periodic=true)
bp = BeliefPropgation(mps_uai)
@test TensorInference.initial_state(bp) isa TensorInference.BPState
state, info = belief_propagate(bp; max_iter=100, tol=1e-6)
@test info.converged
@test info.iterations < 100
contraction_res = TensorInference.contraction_results(state)
tnet = TensorNetworkModel(mps_uai)
mars = marginals(state)
mars_tnet = marginals(tnet)
for v in 1:TensorInference.num_variables(bp)
@test mars[[v]] mars_tnet[[v]] atol=1e-4
end
end

Also there is some problem in using BP to approximately contracting MPS yuqingrong/IsoPEPS.jl#25

Possible bugs

It seems that it is because cost_and_gradient is used to calculate a tensor network without certain tensors:

cost, gradient = cost_and_gradient(code, (t, vectors_in...))

One needs to do conj instead of direct usage:

using OMEinsum
A = rand(ComplexF64, 4, 4)
B = rand(ComplexF64, 4, 4)
C = rand(ComplexF64, 4, 4)
code = ein"(ij, jk), ki->"
cost, grad = cost_and_gradient(code, (A, B, C))

@assert !isapprox(ein"ij, ij->"(grad[1], A)[], cost[])
@assert isapprox(ein"ij, ij->"(conj(grad[1]), A)[], cost[])

Fix

After adding conj in the BP part and marginal function:

cost, grads = cost_and_gradient(tn.code, (adapt_tensors(tn; usecuda, rescale)...,))

the first test aforementioned is passed, but the second one ("belief propagation on circle") still fail. There might be some other issues.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions