Page MenuHomec4science

minimizer.py
No OneTemporary

File Metadata

Created
Mon, Sep 30, 16:23

minimizer.py

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 10 13:45:12 2020
@author: david
"""
import qutip
from scipy.optimize import minimize, basinhopping, shgo
from hm import hamiltonian, a_tensor_list
from QRSP_qutip import aa_mixed, constraint, error_fidelity
import numpy as np
import multiprocessing, sys, os
from itertools import product
F = np.logspace(-3, 2, 60)*np.cos(np.pi/4)
U = np.logspace(-3, 2, 60)
n_cores = 6
""""system parameters:"""
delta = 0
K = 0
# U = 25
J = 0
Nmodes = 2
Nmax = 6
# list of collapse operators
aa = a_tensor_list(Nmodes = 2, Nmax = Nmax)
# target state
rho_target = qutip.fock_dm(Nmax + 1, 1) # Single-photon Fockstate
def minimizing_function(U, F):
print("pump: ", F, " U:", U)
# construct hamiltonian
h = hamiltonian(delta, U, K, J, F, Nmodes, Nmax, verbose=False,
trunc = 1e-14)
# print(h.dims)
# print(aa[0].dims)
rho_ss = qutip.steadystate(h, aa, solver='scipy')
# somhow solver='mkl' gives an error for exceptional parameter choices
# initial random mixing of modes
x0 = np.random.uniform(-1, 1, (Nmodes * 2))
x0/= np.sqrt(sum(abs(x0)**2))
optimizer_args = {'method':'SLSQP', 'options':{'disp': True},
'args':(rho_ss, rho_target, aa),
'constraints' : {"type":"eq", "fun": constraint}}
bounds = list(zip(-np.ones(2*Nmodes), np.ones(2*Nmodes)))
res = shgo(error_fidelity, bounds, args = (rho_ss, rho_target, aa),
constraints = {"type":"eq", "fun": constraint}, iters = 3,
minimizer_kwargs = optimizer_args)#, options = {'disp' : True,
# 'minimize_every_iter' : True, 'symmetry': False})
# res = basinhopping(error_fidelity, x0 = x0, niter = 15,
# minimizer_kwargs = optimizer_args, disp = True)
fid = 1-res.fun
n_out_opt = (aa_mixed(res.x, aa).dag() * aa_mixed(res.x, aa) * rho_ss).tr()
G2 = (aa_mixed(res.x, aa).dag() * aa_mixed(res.x, aa).dag() \
* aa_mixed(res.x, aa) * aa_mixed(res.x, aa) * rho_ss).tr()
g2 = G2/n_out_opt**2
n1 = (aa[0].dag() * aa[0] * rho_ss).tr()
n2 = (aa[1].dag() * aa[1] * rho_ss).tr()
print("opt fidelity: ", fid)
print("opt n_out: ", n_out_opt)
print("opt g2: ", g2)
sys.stdout.flush()
return U, F, res.x, fid, n_out_opt.real, g2.real, n1.real, n2.real
# rho_opt = rho_out(rho_ss, res.x, aa)
# n_out_opt = (aa_mixed(res.x, aa).dag() * aa_mixed(res.x, aa) * rho_ss).tr()
""""Program Execution:"""
if __name__ == "__main__":
sys.path.append(os.getcwd())
try:
ncpus = int(os.environ["SLURM_JOB_CPUS_PER_NODE"])
except KeyError:
ncpus = multiprocessing.cpu_count()
print("calculating parallel with ", ncpus, " cpus.")
p = multiprocessing.Pool(ncpus)
result = p.starmap_async(minimizing_function, product(U,F))
with open("U_F_fidelity.npy", "wb") as f:
np.save(f, np.array(list(product(U,F))))
np.save(f, np.array(result.get()))

Event Timeline