Here's part of the code I am working on. My goal is to start from the empty list D_E
, then accumulate results of de
into D_E
, from the for
loops.
D_E = []
for num_flip in range (0, N):
probsu = []
de = []
for i in range (L):
probsui = []
for j in range (nuni):
counts = results.get_counts((nuni*i) j)
prob = absorb_counts(counts)[num_flip]/sum(counts.values())
probsui.append(prob)
pz_bar = np.average(probsui)
probsu.append(pz_bar)
de.append(np.abs(5 - math.comb(N, num_flip)))
spec = dict(zip(EW,probsu))
# Here, I want to perform an iterative update of D_E
D_E = list(map(add, D_E, de))
D_E = dict(zip(EW, [0.5*i for i in D_E]))
print(D_E)
I initialized D_E
to be an empty list, and in my for
loop for num_flip
, I am trying to update D_E
by adding de
, another list. I am not appending de
, but want to add all its elements to D_E
. Since I am repeating this procedure, I wonder if I could use map
in the for loop, and update D_E
. However, currently my final result of D_E
is just {}
. I don't know where I am doing wrong.
I tried a simpler example: D_E = dict(zip([1,2,3], [0.5*i for i in [1,2,3]]))
and that worked well, I wonder how can I use map
in the for loop in my case?
CodePudding user response:
Based on the discussion on the comments, you need to initialise D_E
before summing the new values. The following code should work (I left the rest of the code as is, even the parts that are never used, such as spec
).
D_E = [0 for _ in range(L)]
for num_flip in range (0, N): # Note: range normally starts from 0, so range(N) is enough
probsu = []
de = []
for i in range (L):
probsui = []
for j in range (nuni):
counts = results.get_counts((nuni*i) j)
prob = absorb_counts(counts)[num_flip]/sum(counts.values())
probsui.append(prob)
pz_bar = np.average(probsui)
probsu.append(pz_bar) # Note: this is never used
de.append(np.abs(5 - math.comb(N, num_flip)))
spec = dict(zip(EW,probsu)) # Note: this is never used
# Here, I want to perform an iterative update of D_E
D_E = list(map(add, D_E, de))
D_E = dict(zip(EW, [0.5*i for i in D_E]))
print(D_E)