MCPcopy Create free account
hub / github.com/Apress/quantum-machine-learning-python / expectation

Method expectation

Chapter_7/listing7_3/QAOA.py:68–87  ·  view source on GitHub ↗
(self,waveform)

Source from the content-addressed store, hash-verified

66
67
68 def expectation(self,waveform):
69
70 expectation = 0
71 prob_from_waveform = (np.absolute(waveform.final_state))**2
72 #print(prob_from_waveform)
73 for i in range(len(prob_from_waveform)):
74 base = bin(i).replace("0b", "")
75 base = (self.num_elems - len(base))*'0' + base
76 base_array = []
77 for b in base:
78 if int(b) == 0:
79 base_array.append(-1)
80 else:
81 base_array.append(1)
82
83 base_array = np.array(base_array)
84 base_interactions = np.outer(base_array, base_array)
85 #print(i, prob_from_waveform[i], np.sum(np.multiply(base_interactions,self.hamiltonian_interactions)))
86 expectation =+ prob_from_waveform[i]*np.sum(np.multiply(base_interactions,self.hamiltonian_interactions))
87 return expectation
88
89 def optimize_params(self, gammas, betas, verbose=True):
90 expectation_dict = {}

Callers 1

optimize_paramsMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected