I have written a function using a quantum simulation class QuTIP that returns a float. Next, I called scipy.optimize.fmin_cg on the function. I keep getting the error:
AttributeError: 'numpy.ndarray' object has no attribute 'expm'
on the line:
U_sq = H_sq.expm
But H_sq is an instance of Qobj, not an ndarray. If I run the function outside of scipy.optimize.fmin_cg, it returns the type as ‘instance’; when it runs inside of fmin_cg it returns the type as ‘ndarray’.
Why does it do this? Is there a optimization function in python that will respect using instances like this?
Here is the code:
from qutip import *
from numpy import *
import scipy.optimize
def sq_fidelity(eps,N=7):
H_sq = squeez(N,eps);
print type(H_sq);
one_ph = basis(N,1);
U_sq = H_sq.expm();
squ = U_sq*one_ph;
fidelity = expect(fock_dm(N,1),squ);
return float(fidelity)
if __name__=='__main__':
print sq_fidelity(0.2);
eps = scipy.optimize.fmin_cg(sq_fidelity, x0=0.2, args=(7,));
The issue here is that
fmin_cgis passing anndarray(of length 1) to your objective function. You can extract the scalar value by just changing the first line ofsq_fidelityto: