fmin_cobyla
我遇到了一些麻烦。我的约束函数是问题,因为它在那里产生错误。我收到以下错误消息:
File "C:/Users/daniel/Documents/Python Scripts/GST_physics\ML_GST.py", line 85, in ML_GST
x_opt=scipy.optimize.fmin_cobyla(objectiveFunction,parameters,ConstrainFunction)
File "C:\Users\daniel\Anaconda\lib\site-packages\scipy\optimize\cobyla.py", line 171, in fmin_cobyla **opts)
File "C:\Users\daniel\Anaconda\lib\site-packages\scipy\optimize\cobyla.py", line 246, in _minimize_cobyla
dinfo=info)
File "C:\Users\daniel\Anaconda\lib\site-packages\scipy\optimize\cobyla.py", line 240, in calcfc con[k] = c['fun'](x, *c['args'])
ValueError: setting an array element with a sequence.
这是约束函数的代码。我没有发布所有代码,因为有很多用户定义的函数没有意义。大部分代码都涉及量化计算工作的qutip
包。
此功能出现问题。 G
是一个矩阵列表,rho
和E
是数组,pauli_basis
是Qobjs
的列表。
def ConstrainFunction(x):
[G_chol,r,E]=ParamToGate(x,parameterSizes)
G=[qptInv(np.transpose(np.conj(g))*g,op_basis) for g in G_chol]
G=[np.transpose(np.conj(g))*g for g in G]
c=[]
ceq=np.zeros((len(pauli_basis),len(G)))
for ii in range(len(G)):
for rr in range(len(pauli_basis)):
for mm in range(len(G)):
for nn in range(len(G)):
ceq[rr,ii]=ceq[rr,ii]+G[ii][mm,nn]*(pauli_basis[mm]*pauli_basis[rr]*pauli_basis[nn]).tr()
ceq[rr,ii]=ceq[rr,ii]-kroneckerDelta(0,rr)*(2**(num_qubits*2))
ceq=np.matrix(ceq)
ceq=np.array(mat2vec(ceq))
lengthCEQ=len(ceq)
rho=np.transpose(np.conj(r))*r
traceerror=np.trace(rho)-1.0
ceq=np.vstack([np.array(ceq),np.array([[traceerror.real]])])
return ceq
x_opt=scipy.optimize.fmin_cobyla(objectiveFunction,parameters,ConstrainFunction)