-
Notifications
You must be signed in to change notification settings - Fork 0
/
hamLearning_RandomIsing_varb.jl
149 lines (129 loc) · 4 KB
/
hamLearning_RandomIsing_varb.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# generate the result for reconstruction
# of random transverse-field Ising Hamiltonian
#
# fix n=4
# for each b in 1, 2, 4, 8
# generate [totalHams] TFIsing Hamiltonians (see hamsGen.jl)
# each is reconstructed [eachRounds] times
# output as json
#
# usage: julia hamLearning_RandomIsing.jl [verbose (true|false)]
#
#
import Distributions.Normal
using Distributed
include("./utils.jl")
include("./noisy_oracle.jl")
include("./hamsGen.jl")
using LinearAlgebra, Statistics, .hamLearning_utils, .noisyOracle
using FileIO, JSON
function printboth(s, fs)
maps = (k) -> (haskey(s, k) ? s[k] : nothing)
mapfs = (k) -> (haskey(fs, k) ? fs[k] : nothing)
ks = union(keys(s), keys(fs))
for k in ks
println(k, "=>", "(", maps(k), ", ", mapfs(k), ")")
end
end
function parameterDis(s, s_re)
maps = (k) -> (haskey(s, k) ? s[k] : 0.0)
mapsre = (k) -> (haskey(s_re, k) ? s_re[k] : 0.0)
ans = 0.0
for key in union(keys(s), keys(s_re))
ans+=abs(maps(key) - mapsre(key))
end
idea=0.0
for (key,val) in s
idea+= abs(val)
end
return (ans)/(idea)
end
# modifiedParams:
# (singleSensitivity, linesInPhase2, zeroSensitivity, b)
modifiedParams = [
(2e-2, 100, 4e-2, 2),
(1e-2, 100, 3e-2, 3),
(1e-2, 100, 3e-2, 4),
(1e-2, 100, 3e-2, 5),
(1e-2, 100, 3e-2, 6),
(8e-3, 100, 5e-2, 7),
(1e-2, 200, 5e-2, 8)
]
verbose = false
params = Dict(
"Cutoff"=>0.995,
"Zerosensitivity"=>2e-2,
"noise"=>0.001,
"largeEnoughOffset"=>100,
"single"=>1e-3,
"phase2Lines"=>200
);
A = params["largeEnoughOffset"]
totalHams = 50
eachRounds = 10
n = 6
for j in 6:6
savingItems = [] # will be the json output
params["single"] = modifiedParams[j][1]
params["phase2Lines"] = modifiedParams[j][2]
params["Zerosensitivity"] = modifiedParams[j][3]
b = modifiedParams[j][4]
failCount = 0
ave=0.0
for hamsCount in 1:totalHams
s = TFIsingHamiltonian1d(n)
push!(savingItems, s);
ham0, oracle_f = construct_oracle_f(s, n, params["noise"])
# real oracle being actually used
# total calls is counted
function fOracle(k)
if haskey(F,k)
return F[k]
end
global totalCall = totalCall + 1
F[k] = oracle_f(k+1) + A
return F[k]
end
oracle_s = construct_oracle_s(ham0, n, params["noise"])
function sOracle(β, γ)
global totalCall2 = totalCall2 + 1
return oracle_s(β, γ)
end
mainProc = hamLearningProc(n)
for i=1:eachRounds
global F = Dict()
global totalCall = 0
global totalCall2 = 0
try
global ps_re = mainProc.dopeel(b, fOracle, params, 3, 10, verbose)
if verbose
println("=======================")
end
nonZeroAlphas = filter!(x->x≠0, [a for a in keys(ps_re)])
global s_re = pauliparametersReconstruction(nonZeroAlphas, params["phase2Lines"], sOracle)
catch
println("error in do peel")
failCount += 1
i = i-1
continue
end
if verbose
println("total Calls to oracle at round ",i," with qubit ",n ,": " ,totalCall+totalCall2)
end
fs = reconstructedParameters(ps_re, s_re)
if verbose
printboth(s, fs)
println("distance at round ",(hamsCount-1)*eachRounds + i,": ",parameterDis(s,fs))
println("=======================")
end
ave += parameterDis(s,fs)
push!(savingItems, (fs, totalCall, totalCall2))
end
end
ave/=(eachRounds * totalHams)
println("average error: ", ave)
println("fail rate ", failCount/(eachRounds * totalHams))
open("./data/strictly"*string(n)*"RandomIsingRounds=500_b="*string(b)*".json", "w") do f
JSON.print(f, savingItems, 4)
end
end