Issue with optimize_pulse_unitary for large sparse matrices
See original GitHub issueI am using GRAPE to identify optimal pulse sequences to produce a desired unitary, and am running into an error when I use optimize_pulse_unitary for sparse matrices of size 1296x1296 and larger.
For example, the following code works for N=2 but fails for N=>3:
import numpy as np
from qutip import *
from scipy.sparse import csr_matrix
import qutip.logging_utils as logging
logger = logging.get_logger()
#Set this to None or logging.WARN for 'quiet' execution
log_level = logging.INFO
#QuTiP control modules
import qutip.control.pulseoptim as cpo
import math
# Set parameters
chi = 2.2 * 2 * np.pi # coupling strength
N = 3 # number of cavity states
n_ts = 21 # number of discrete time steps
evo_time = 4. # Length of time
fid_err_targ = 1e-10 # Target fidelity error
max_iter = 0 # # Maximum iterations for the optimization algorithm
max_wall_time = 0 # Maximum (elapsed) time
min_grad = 1e-20 # Minimum gradient
p_type = 'SINE' # pulse type (RND|ZERO|LIN|SINE|SQUARE|SAW|TRIANGLE)
f_ext = "pulsedata.txt" # output file
# Set operators and Hamiltonians
# operators
a = tensor(destroy(N), qeye(N), qeye(N), qeye(N), qeye(2), qeye(2), qeye(2), qeye(2))
b = tensor(qeye(N), destroy(N), qeye(N), qeye(N), qeye(2), qeye(2), qeye(2), qeye(2))
c = tensor(qeye(N), qeye(N), destroy(N), qeye(N), qeye(2), qeye(2), qeye(2), qeye(2))
d = tensor(qeye(N), qeye(N), qeye(N), destroy(N), qeye(2), qeye(2), qeye(2), qeye(2))
sm = tensor(qeye(N), qeye(N), qeye(N), qeye(N), destroy(2), qeye(2), qeye(2), qeye(2))
zm = tensor(qeye(N), qeye(N), qeye(N), qeye(N), qeye(2), destroy(2), qeye(2), qeye(2))
tm = tensor(qeye(N), qeye(N), qeye(N), qeye(N), qeye(2), qeye(2), destroy(2), qeye(2))
qm = tensor(qeye(N), qeye(N), qeye(N), qeye(N), qeye(2), qeye(2), qeye(2), destroy(2))
P_se = tensor(qeye(N), qeye(N), qeye(N), qeye(N), basis(2, 1) * basis(2, 1).dag(), qeye(2), qeye(2), qeye(2))
P_ze = tensor(qeye(N), qeye(N), qeye(N), qeye(N), qeye(2), basis(2, 1) * basis(2, 1).dag(), qeye(2), qeye(2))
P_te = tensor(qeye(N), qeye(N), qeye(N), qeye(N), qeye(2), qeye(2), basis(2, 1) * basis(2, 1).dag(), qeye(2))
P_qe = tensor(qeye(N), qeye(N), qeye(N), qeye(N), qeye(2), qeye(2), qeye(2), basis(2, 1) * basis(2, 1).dag())
# Hamiltonians
drift = chi * (a.dag() * a * P_se) + chi * (b.dag() * b * P_ze) + chi * (c.dag() * c * P_te) + chi * (d.dag() * d * P_qe)
row = np.array([1,1,2,2,3,3,3,3,4,4,4,4,5,5,6,6])
column = np.array([3,4,3,4,1,2,5,6,1,2,5,6,3,4,3,4])
data = np.array([-1,1,-1,-1,-1,-1,-1,-1,1,-1,-1,1,-1,-1,-1,1])
mapped=csr_matrix((data,(row,column)),shape=(N**4*2*2*2*2,N**4*2*2*2*2))
mapped=Qobj(mapped)
n_ctrls=4 # Controls
ctrls = [sm.dag()*a+sm*a.dag()+sm.dag()*b+sm*b.dag()+zm.dag()*b+zm*b.dag()+zm.dag()*c+zm*c.dag()+tm.dag()*c+tm*c.dag()+tm.dag()*d+tm*d.dag()+qm.dag()*d+qm*d.dag()+qm.dag()*a+qm*a.dag(),1j*(sm.dag()*a-sm*a.dag())+1j*(sm.dag()*b-sm*b.dag())+1j*(zm.dag()*b-zm*b.dag())+1j*(zm.dag()*c-zm*c.dag())+1j*(tm.dag()*c-tm*c.dag())+1j*(tm.dag()*d-tm*d.dag())+1j*(qm.dag()*d-qm*d.dag())+1j*(qm.dag()*a-qm*a.dag()),sm+sm.dag()+zm+zm.dag()+tm+tm.dag()+qm+qm.dag(), 1j*(sm-sm.dag())+1j*(zm-zm.dag())+1j*(tm-tm.dag())+1j*(qm-qm.dag())]
# Set optimization parameters
U_0=tensor(qeye(N), qeye(N), qeye(N), qeye(N), qeye(2), qeye(2), qeye(2), qeye(2)) # Initial gate
U_targ=mapped # Target gates
# Run the optimization
result = cpo.optimize_pulse_unitary(drift, ctrls, U_0, U_targ, n_ts, evo_time, fid_err_targ=fid_err_targ, min_grad=min_grad, max_iter=max_iter, max_wall_time=max_wall_time, out_file_ext=f_ext, init_pulse_type=p_type, log_level=log_level,gen_stats=True,dyn_params=dict(sparse_eigen_decomp=True))
For N=>3, instead of getting a printout of the optimized controls, I receive the following terminal output error:
Traceback (most recent call last):
File "MinimalErrorExample.py", line 59, in <module>
result = cpo.optimize_pulse_unitary(drift, ctrls, U_0, U_targ, n_ts, evo_time, fid_err_targ=fid_err_targ, min_grad=min_grad, max_iter=max_iter, max_wall_time=max_wall_time, out_file_ext=f_ext, init_pulse_type=p_type, log_level=log_level,gen_stats=True,dyn_params=dict(sparse_eigen_decomp=True))
File "/Users/msoley/opt/anaconda3/lib/python3.7/site-packages/qutip/control/pulseoptim.py", line 829, in optimize_pulse_unitary
gen_stats=gen_stats)
File "/Users/msoley/opt/anaconda3/lib/python3.7/site-packages/qutip/control/pulseoptim.py", line 510, in optimize_pulse
result = optim.run_optimization()
File "/Users/msoley/opt/anaconda3/lib/python3.7/site-packages/qutip/control/optimizer.py", line 970, in run_optimization
result = self._create_result()
File "/Users/msoley/opt/anaconda3/lib/python3.7/site-packages/qutip/control/optimizer.py", line 375, in _create_result
result.initial_fid_err = self.dynamics.fid_computer.get_fid_err()
File "/Users/msoley/opt/anaconda3/lib/python3.7/site-packages/qutip/control/fidcomp.py", line 448, in get_fid_err
return np.abs(1 - self.get_fidelity())
File "/Users/msoley/opt/anaconda3/lib/python3.7/site-packages/qutip/control/fidcomp.py", line 458, in get_fidelity
self.fid_norm_func(self.get_fidelity_prenorm())
File "/Users/msoley/opt/anaconda3/lib/python3.7/site-packages/qutip/control/fidcomp.py", line 474, in get_fidelity_prenorm
dyn.compute_evolution()
File "/Users/msoley/opt/anaconda3/lib/python3.7/site-packages/qutip/control/dynamics.py", line 1421, in compute_evolution
self.tslot_computer.recompute_evolution()
File "/Users/msoley/opt/anaconda3/lib/python3.7/site-packages/qutip/control/tslotcomp.py", line 299, in recompute_evolution
prop_comp._compute_prop_grad(k, j)
File "/Users/msoley/opt/anaconda3/lib/python3.7/site-packages/qutip/control/propcomp.py", line 272, in _compute_prop_grad
dyn._ensure_decomp_curr(k)
File "/Users/msoley/opt/anaconda3/lib/python3.7/site-packages/qutip/control/dynamics.py", line 1437, in _ensure_decomp_curr
self._spectral_decomp(k)
File "/Users/msoley/opt/anaconda3/lib/python3.7/site-packages/qutip/control/dynamics.py", line 1654, in _spectral_decomp
dims=self.dyn_dims)
File "/Users/msoley/opt/anaconda3/lib/python3.7/site-packages/qutip/qobj.py", line 292, in __init__
_tmp = sp.csr_matrix(inpt, dtype=complex, copy=do_copy)
File "/Users/msoley/opt/anaconda3/lib/python3.7/site-packages/scipy/sparse/compressed.py", line 88, in __init__
self._set_self(self.__class__(coo_matrix(arg1, dtype=dtype)))
File "/Users/msoley/opt/anaconda3/lib/python3.7/site-packages/scipy/sparse/coo.py", line 191, in __init__
self.row, self.col = M.nonzero()
File "/Users/msoley/opt/anaconda3/lib/python3.7/site-packages/scipy/sparse/base.py", line 287, in __bool__
raise ValueError("The truth value of an array with more than one "
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all().
I am using the following QuTiP environment: QuTiP Version: 4.4.1 Numpy Version: 1.21.0 Scipy Version: 1.4.1 Cython Version: 0.29.15 Matplotlib Version: 3.4.2 Python Version: 3.7.6 Number of CPUs: 2 BLAS Info: OPENBLAS OPENMP Installed: False INTEL MKL Ext: False Platform Info: Darwin (x86_64)
Thank you for your help.
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Large sparse matrices of big integers lose information #13329
When I create a pretty large sparse matrix of big integers, somehow I get slightly different values stored in the matrix, even for...
Read more >Sparse matrix methods in optimization - Stanford University
We review various sparse matrices that arise in optimization, ... large LP problems seem to havepredated codes for sparse linear equations (even.
Read more >A Gentle Introduction to Sparse Matrices for Machine Learning
Large sparse matrices are common in general and especially in ... In this tutorial, you will discover sparse matrices, the issues they ...
Read more >High-performance computation of the exponential of a large ...
Abstract: Computation of the large sparse matrix exponential has been an important topic in many fields, such as network and finite-element ...
Read more >Reduction of Huge, Sparse Matrices over Finite Fields Via ...
a large, sparse bit matrix to a smaller, dense bit matrix that ... of choice for sudffi ciently large problems, for our ma-...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thank you for all of your help!
#1621 has been merged and should be included in the 4.6.3 release.