Issue 1:
The C function malloc() returns NULL (i.e. 0) if it fails to allocate memory. The return value of a malloc() call must be checked for this value before attempting to use the result. Here are places in the scipy code where this check is not made. If any of these calls fails, the result is likely to be a segmentation fault.

Cython

cluster

In the file _optimal_leaf_ordering.pyx, in the function identify_swaps

interpolate

In the file _ppoly.pyx, in the functions real_roots, croots_poly1 and _croots_poly1

spatial

In setlist.pxd, in the function init, in the statement setlists.sets[j] = ...

sparse.csgraph

File _shortest_path.pyx, in the code that creates nodes in the functions _dijkstra_directed, _dijkstra_directed_multi, _dijkstra_undirected, _dijkstra_undirected_multi

_lib

In the file _ccallback_c.pyx, the result of strdup is used without checking for NULL. strdup can fail to allocate memory for the duplicated string. In that case, it returns NULL. (Note: this might be harmless. I haven't checked to see if the pointer is ever dereferenced while the capsule is in use.)

C

fftpack

In the file fftpack.h, the macro GEN_CACHE is defined. It is used in the files convolve.c, drfft.c, zfft.c,zfftnd.c

ndimage

In the file src/ni_morphology.c, at line 624 (at the time of this writing): temp->coordinates = malloc(...)

signal

File sigtoolsmodule.c, lines 914--927 (8 unchecked calls to malloc) and line 1096

We have vendored code in sparse and spatial that use malloc:

The SuperLU code in linalg/dsolve/SuperLU/SRC has its own memory management code. I didn't spend time trying to figure out the memory management here. The file cmemory.c is the most relevant, and there appear to be calls to intMalloc() and user_malloc() that do not check for failure, but I didn't dig deeper to see if those functions check for failure before returning.

spatial provides a wrapper for QHull. I didn't look very deeply into how the qhull code manages memory. It looks like the function qh_memalloc() in mem_r.c will print an error and exit if it fails.

We should be aware of these vendored packages using malloc, but I think a thorough review of their memory management is not required. Someone can do a deep dive into either of these libraries in the future if there is ever a suspicion that they are leaking memory.

Issue 2:
My issue is about the error LinAlgError: Schur form not found. Possibly ill-conditioned raised when I use the following code. The problem is that it only happens in the first execution of the script, then it disappears. It also happens when I try to use scipy.linalg.expm and scipy.linalg.logm as it has to do with the Schur decomposition.

Reproducing code example:

""" Obtains the fidelity for the QFT for n=3 for Qiskit noise model """

import numpy as np

def fidelity(a,b):
''' Computes the fidelity of two density matrices

Args:
a (array): density matrix
b (array): density matrix

Returns:
float: fidelity of a and b
'''
return (np.matrix.trace(scipy.linalg.sqrtm(np.matmul(scipy.linalg.sqrtm(a),np.matmul(b,scipy.linalg.sqrtm(a))))))**2

rho0=np.array([[0.23394436+0.j , 0.11036513+0.07068888j,
0.03302341+0.1088189j , 0.02706909+0.0881958j ,
0.05815633-0.01072523j, 0.02037557-0.02607218j,
0.1016744 +0.00529989j, 0.12719727+0.00524902j],
[0.11036513-0.07068888j, 0.13571167+0.j ,
0.05817668+0.06459554j, 0.05001153+0.03409153j,
0.01467896-0.03614299j, 0.02297974-0.02354262j,
0.05975342-0.05200195j, 0.07036336-0.04786174j],
[0.03302341-0.1088189j , 0.05817668-0.06459554j,
0.14170668+0.j , 0.09006076-0.00379435j,
0.0081075 -0.02009074j, 0.00140381-0.01287842j,
0.01685588-0.05481296j, 0.02995809-0.0568339j ],
[0.02706909-0.0881958j , 0.05001153-0.03409153j,
0.09006076+0.00379435j, 0.1104499 +0.j ,
0.0189209 -0.01251221j, 0.00768026-0.02039591j,
0.03121948-0.0254008j , 0.03673299-0.03857761j],
[0.05815633+0.01072523j, 0.01467896+0.03614299j,
0.0081075 +0.02009074j, 0.0189209 +0.01251221j,
0.0863071 +0.j , 0.04125298+0.00418091j,
0.01762221-0.00102403j, 0.03347778+0.00219727j],
[0.02037557+0.02607218j, 0.02297974+0.02354262j,
0.00140381+0.01287842j, 0.00768026+0.02039591j,
0.04125298-0.00418091j, 0.06473456+0.j ,
0.0410258 +0.00813802j, 0.01971775+0.01372613j],
[0.1016744 -0.00529989j, 0.05975342+0.05200195j,
0.01685588+0.05481296j, 0.03121948+0.0254008j ,
0.01762221+0.00102403j, 0.0410258 -0.00813802j,
0.11333211+0.j , 0.06110975+0.02015177j],
[0.12719727-0.00524902j, 0.07036336+0.04786174j,
0.02995809+0.0568339j , 0.03673299+0.03857761j,
0.03347778-0.00219727j, 0.01971775-0.01372613j,
0.06110975-0.02015177j, 0.11381361+0.j ]])

rho=np.array([[ 0.17706638+0.j , 0.06659953+0.01608955j,
0.0297682 +0.01830716j, 0.00812785+0.01222738j,
0.00753445-0.00309584j, 0.00878906-0.01509603j,
0.02885946-0.0179952j , 0.04318237-0.01519775j],
[ 0.06659953-0.01608955j, 0.13491143+0.j ,
0.03180949+0.01096598j, 0.01931085+0.01529609j,
0.00876872-0.00343831j, 0.00969103-0.01005385j,
0.01644897-0.02258301j, 0.02629598-0.01775106j],
[ 0.0297682 -0.01830716j, 0.03180949-0.01096598j,
0.12190416+0.j , 0.03262329+0.01110501j,
0.0140686 -0.00460815j, 0.00094604-0.01165771j,
0.00431993-0.02871026j, 0.01617432-0.02577718j],
[ 0.00812785-0.01222738j, 0.01931085-0.01529609j,
0.03262329-0.01110501j, 0.10249498+0.j ,
0.00314331-0.00964355j, 0.00564575+0.00064087j,
-0.00264486-0.00935872j, 0.00440131-0.01796807j],
[ 0.00753445+0.00309584j, 0.00876872+0.00343831j,
0.0140686 +0.00460815j, 0.00314331+0.00964355j,
0.10799493+0.j , 0.02676392+0.0035163j ,
0.01410251+0.00034248j, 0.00727336+0.00429281j],
[ 0.00878906+0.01509603j, 0.00969103+0.01005385j,
0.00094604+0.01165771j, 0.00564575-0.00064087j,
0.02676392-0.0035163j , 0.10673353+0.j ,
0.01838175+0.00315348j, 0.02171156+0.01295641j],
[ 0.02885946+0.0179952j , 0.01644897+0.02258301j,
0.00431993+0.02871026j, -0.00264486+0.00935872j,
0.01410251-0.00034248j, 0.01838175-0.00315348j,
0.12371487+0.j , 0.0449117 +0.01037259j],
[ 0.04318237+0.01519775j, 0.02629598+0.01775106j,
0.01617432+0.02577718j, 0.00440131+0.01796807j,
0.00727336-0.00429281j, 0.02171156-0.01295641j,
0.0449117 -0.01037259j, 0.12517971+0.j ]])
fidelity=DAQC.fidelity(rho0,rho)

Error message:

File "C:\Users\Paula\Dropbox\Master thesis\FinalCode\ibmq_estimation\DAQC.py", line 37, in fidelity
return (np.matrix.trace(scipy.linalg.sqrtm(np.matmul(scipy.linalg.sqrtm(a),np.matmul(b,scipy.linalg.sqrtm(a))))))**2

File "C:\Users\Paula\Anaconda3\envs\test_env\lib\site-packages\scipy\linalg\_matfuncs_sqrtm.py", line 172, in sqrtm
T, Z = schur(A, output='complex')

File "C:\Users\Paula\Anaconda3\envs\test_env\lib\site-packages\scipy\linalg\decomp_schur.py", line 170, in schur
raise LinAlgError("Schur form not found. Possibly ill-conditioned.")

LinAlgError: Schur form not found. Possibly ill-conditioned.
...

Scipy/Numpy/Python version information:

import sys, scipy, numpy; print(scipy.__version__, numpy.__version__, sys.version_info)

1.5.2 1.18.5 sys.version_info(major=3, minor=7, micro=7, releaselevel='final', serial=0)