In [1]:
%pylab inline
In [2]:
from qutip import *
In [3]:
import time
In [4]:
reps = 1
In [5]:
def plot_bm_mat(bm_mat, N_vec, solvers):
fig, ax = subplots(figsize=(12, 8))
for idx, solver in enumerate(solvers):
solver_name, solver_func = solver
ax.plot(N_vec, bm_mat[:, idx], label=solver_name, linewidth=2)
ax.set_yscale('log')
ax.set_xlabel("Hilbert space size (N)", fontsize=18)
ax.set_ylabel("Elapsed time", fontsize=18)
ax.legend(loc=0)
ax.axis('tight')
In [6]:
def benchmark_steadystate_solvers(N_vec, solvers, problem_func):
bm_mat = zeros((len(N_vec), len(solvers)))
for N_idx, N in enumerate(N_vec):
H, c_ops = problem_func(N)
for sol_idx, solver in enumerate(solvers):
solver_name, solver_func = solver
try:
t1 = time.time()
for r in range(reps):
rhoss = solver_func(H, c_ops)
t2 = time.time()
bm_mat[N_idx, sol_idx] = (t2 - t1)/reps
except:
bm_mat[N_idx, sol_idx] = 0
return bm_mat
In [7]:
solvers = [
["power use_umfpack=True",
lambda H, c_ops: steadystate(H, c_ops, method='power', use_umfpack=True)],
["power use_umfpack=False",
lambda H, c_ops: steadystate(H, c_ops, method='power', use_umfpack=False)],
["direct sparse use_umfpack=True",
lambda H, c_ops: steadystate(H, c_ops, use_umfpack=True, sparse=True)],
["direct sparse use_umfpack=False",
lambda H, c_ops: steadystate(H, c_ops, use_umfpack=False, sparse=True)],
["iterative use_precond=True",
lambda H, c_ops: steadystate(H, c_ops, method='iterative', use_precond=True)],
["iterative use_precond=False",
lambda H, c_ops: steadystate(H, c_ops, method='iterative', use_precond=False)],
["iterative-bicg use_precond=True",
lambda H, c_ops: steadystate(H, c_ops, method='iterative-bicg', use_precond=True)],
["iterative-bicg use_precond=False",
lambda H, c_ops: steadystate(H, c_ops, method='iterative-bicg', use_precond=False)],
["direct dense use_umfpack=True",
lambda H, c_ops: steadystate(H, c_ops, use_umfpack=True, sparse=False)],
["direct dense use_umfpack=False",
lambda H, c_ops: steadystate(H, c_ops, use_umfpack=False, sparse=False)],
["svd_dense",
lambda H, c_ops: steadystate(H, c_ops, method='svd')],
["lu",
lambda H, c_ops: steadystate(H, c_ops, method='lu')],
]
In [8]:
large_solvers = [
["power use_umfpack=True",
lambda H, c_ops: steadystate(H, c_ops, method='power', use_umfpack=True)],
["power use_umfpack=False",
lambda H, c_ops: steadystate(H, c_ops, method='power', use_umfpack=False)],
["direct sparse use_umfpack=True",
lambda H, c_ops: steadystate(H, c_ops, use_umfpack=True, sparse=True)],
["direct sparse use_umfpack=False",
lambda H, c_ops: steadystate(H, c_ops, use_umfpack=False, sparse=True)],
["iterative use_precond=True",
lambda H, c_ops: steadystate(H, c_ops, method='iterative', use_precond=True)],
["iterative-bicg use_precond=True",
lambda H, c_ops: steadystate(H, c_ops, method='iterative-bicg', use_precond=True)]
]
In [9]:
def bm_problem1(N):
a = tensor(destroy(N), identity(2))
b = tensor(identity(N), destroy(2))
H = a.dag() * a + b.dag() * b + 0.25 * (a + a.dag()) * (b + b.dag())
c_ops = [sqrt(0.1) * a, sqrt(0.075) * a.dag(), sqrt(0.1) * b]
return H, c_ops
In [10]:
N_vec = array([2, 5, 10, 15, 20, 25, 30, 35])
In [11]:
bm_mat = benchmark_steadystate_solvers(N_vec, solvers, bm_problem1)
In [12]:
plot_bm_mat(bm_mat, 2 * N_vec, solvers)
In [13]:
N_vec = arange(25, 400, 25)
In [14]:
bm_mat = benchmark_steadystate_solvers(N_vec, large_solvers, bm_problem1)
In [15]:
plot_bm_mat(bm_mat, 2 * N_vec, large_solvers)
In [16]:
def bm_problem2(N):
a = destroy(N)
H = a.dag() * a
nth = N / 4
gamma = 0.05
c_ops = [sqrt(gamma * (nth + 1)) * a, sqrt(gamma * nth) * a.dag()]
return H, c_ops
In [17]:
N_vec = array([2, 5, 10, 15, 20, 25])
In [18]:
bm_mat = benchmark_steadystate_solvers(N_vec, solvers, bm_problem2)
In [19]:
plot_bm_mat(bm_mat, N_vec, solvers)
In [20]:
N_vec = arange(25, 300, 25)
In [21]:
bm_mat = benchmark_steadystate_solvers(N_vec, large_solvers, bm_problem2)
In [22]:
plot_bm_mat(bm_mat, N_vec, large_solvers)
In [23]:
def bm_problem3(N):
a = tensor(destroy(N), identity(N))
b = tensor(identity(N), destroy(N))
H = a.dag() * a + 0.25 * b.dag() * b + 0.05 * a.dag() * a * (b + b.dag()) + 0.1 * (a + a.dag())
c_ops = [sqrt(0.05) * a, sqrt(0.015) * a.dag(), sqrt(0.1) * b, sqrt(0.075) * b.dag()]
return H, c_ops
In [24]:
N_vec = array([2, 4, 6, 8, 10])
In [25]:
bm_mat = benchmark_steadystate_solvers(N_vec, large_solvers, bm_problem3)
In [26]:
plot_bm_mat(bm_mat, N_vec ** 2, large_solvers)
In [27]:
def bm_problem4(N=1):
H = 0
for m in range(N):
H += tensor([sigmaz() if n == m else identity(2) for n in range(N)])
for m in range(N-1):
H += tensor([sigmax() if n in [m,m+1] else identity(2) for n in range(N)])
c_ops = [sqrt(0.05) * tensor([sigmam() if n == m else identity(2) for n in range(N)])
for m in range(N)]
return H, c_ops
In [28]:
N_vec = array([2, 3, 4, 5, 6])
In [29]:
bm_mat = benchmark_steadystate_solvers(N_vec, large_solvers, bm_problem3)
In [30]:
plot_bm_mat(bm_mat, N_vec, large_solvers)
In [31]:
H, c_ops = bm_problem1(300)
In [32]:
%%prun -q -T steadystate.txt
steadystate(H, c_ops)
In [33]:
!head -n 20 steadystate.txt
In [34]:
%%prun -q -T steadystate_direct.txt
steadystate(H, c_ops, method="direct")
In [35]:
!head -n 20 steadystate_direct.txt
In [36]:
%%prun -q -T steadystate_iterative.txt
steadystate(H, c_ops, method="iterative")
In [37]:
!head -n 20 steadystate_iterative.txt
In [38]:
%%prun -q -T steadystate_iterative.txt
steadystate(H, c_ops, method="iterative-bicg")
In [39]:
!head -n 20 steadystate_iterative.txt
In [40]:
from qutip.ipynbtools import version_table
version_table()
Out[40]: