Coverage for src/driada/network/spectral.py: 13.33%

30 statements  

« prev     ^ index     » next       coverage.py v7.9.2, created at 2025-07-25 15:40 +0300

1import numpy as np 

2 

3 

4def free_entropy(spectrum, t): 

5 # eq.2 in https://www.nature.com/articles/s42005-021-00582-8#Sec10 

6 eigenvalues = np.exp(-t * spectrum) 

7 F = np.log2(np.real(np.sum(eigenvalues))) 

8 return F 

9 

10 

11def q_entropy(spectrum, t, q=1): 

12 """ 

13 

14 Args: 

15 spectrum: 

16 t: 

17 q: 

18 

19 Returns: 

20 

21 """ 

22 # https://journals.aps.org/prx/abstract/10.1103/PhysRevX.6.041062 

23 

24 if q <= 0: 

25 raise Exception('q must be >0') 

26 else: 

27 Z = np.sum(np.exp(-t * spectrum)) 

28 if q != 1: 

29 eigenvalues = np.exp(-t * q * spectrum) 

30 S = 1/(1-q) * np.log(Z**(-q) * np.sum(eigenvalues)) 

31 else: 

32 S = spectral_entropy(spectrum, t, verbose=0) 

33 

34 if np.imag(S) != 0: 

35 raise Exception(f'Imaginary entropy detected: t={t}, q={q}, S={S}!') 

36 

37 return S 

38 

39 

40def spectral_entropy(spectrum, t, verbose=0): 

41 eigenvalues = np.exp(-t * spectrum) 

42 norm_eigenvalues = np.trim_zeros(eigenvalues / np.sum(eigenvalues)) 

43 S = -np.real(np.sum(np.multiply(norm_eigenvalues, np.log2(norm_eigenvalues)))) 

44 

45 if verbose: 

46 print('initial eigenvalues:') 

47 print(spectrum) 

48 print('exp eigenvalues:') 

49 print(eigenvalues) 

50 print('norm exp eigenvalues:') 

51 print(norm_eigenvalues) 

52 print('logs:') 

53 print(np.log2(norm_eigenvalues)) 

54 

55 return S