using CVChannel
using Test
using DelimitedFiles

"""
This obtains data about the Werner-Holevo channel when tensored
with the Siddhu channel.
"""

print("\nThis script shows that there is non-multiplicativity of")
print("cvPPT for the Werner-Holevo channel tensored with the Siddhu channel.")

@testset "Werner-Holevo with Siddhu is Non-Multiplicative" begin
    println("Now we actually verify non-multiplicativity.")
    λ_range = [0:0.025:0.325;]
    s_range = [0:0.1:0.5;]
    λ_ctr, s_ctr = 1,1
    data_table = zeros(length(s_range),length(λ_range))
    for λ in λ_range
        println("---Now scanning λ=",λ,"---")
        A,B,g,a = generalWHLPConstraints(1,3,λ*ones(1))
        cvWH, v1 =wernerHolevoCVPPT(1,3,A,B,g,a)
        whChan(X) = wernerHolevoChannel(X,λ)
        wh_chan = Choi(whChan,3,3)
        for s in s_range
            println("Now scanning s=",s)
            target_val = 2*cvWH
            sidchan(X) = siddhuChannel(X,s)
            sid_chan= Choi(sidchan,3,3)

            par_cv, = pptCV(parChoi(wh_chan, sid_chan), :dual)

            non_mult = par_cv - target_val
            isapprox(non_mult,0,atol=3e-6) ?
                data_table[s_ctr,λ_ctr] = 0 :
                    data_table[s_ctr,λ_ctr] = non_mult

            s_ctr += 1
            end
        λ_ctr += 1
        s_ctr = 1
    end

    println("\n Here is the data.")
    show(stdout, "text/plain", data_table)
    println("Note that the non-multiplicativity is the same for all s.")
    println("Also note that as λ increases, the non-multiplicativity decreases.")

    header, s_label = hcat("s|λ:",λ_range'), s_range
    data_to_save = vcat(header,hcat(s_label, data_table))

    info_vec = Vector{Union{Nothing,String}}(nothing, size(data_to_save)[1])
    info_vec[1] = "INFO:"
    info_vec[2] = "Generated by werner-holevo-with-siddhu.jl"
    info_vec[4] = "λ_range ="*string(λ_range)
    info_vec[5] = "s_range="*string(s_range)
    data_to_save = hcat(data_to_save,info_vec)

    println("Please name the file you'd like to write the results to: \n")
    file_name = readline()
    file_to_open = string(file_name,".csv")
    writedlm(file_to_open, data_to_save, ',')

    #This is sufficient for checking that there is non-multiplicativity somewhere
    @test data_table[1:1:1]>[0.1]
end
