mutual_information.py 4.08 KB
Newer Older
mgassner's avatar
mgassner committed
1
from ast import parse
mgassner's avatar
mgassner committed
2
3
4
5
6
7
8
9
import numpy as np
import yaml
import time
import matplotlib.pyplot as plt

from create_model import spin_model
from entropy_new import compute_MI, compute_entropy, reduced_dm

mgassner's avatar
mgassner committed
10
11
12
13
14
15
16
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--set_number_spins", "-d", dest='number_spins',type=int, required=True)
parser.add_argument("--set_param_range_and_steps", '-p', dest='param_range', nargs='+', type=float)
parser.add_argument("--model_name", "-m", dest='model_name' ,type=str, required=True)
args = parser.parse_args()

mgassner's avatar
mgassner committed
17
def test_mutual_information():
mgassner's avatar
mgassner committed
18
    # change return in compute_MI to run this function [S_A, S_B, S_AB] instead of S_A + S_B - S_AB
mgassner's avatar
mgassner committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
    model_name = 'xxz'
    number_spins = 10
    periodic = False
    spin_inversion = None
    param = 1.0
    if param < -1.0:
        hamming_weight = None
    else: 
        hamming_weight = number_spins // 2
    model = spin_model(model_name=model_name, number_spins=number_spins, periodic=periodic, 
                            param=param, hamming_weight=hamming_weight ,spin_inversion=spin_inversion)
    model.compute_ew_and_ev()
    print('EIGENSTATE at h/J = ', param, 'is: ', model.eigenstates[:,0])
    sub_dim = 4
    first_trace_spin = 4
    basis_states = model.basis.states
    gs = model.eigenstates[:,0]
    print('Number Spins: ', model.basis.number_spins)
    print('States', model.basis.states)
    print('Sub Dimension is ', sub_dim)
    entropies = compute_MI(sub_dim, number_spins, hamming_weight, gs, basis_states, spin_inversion=None, first_trace_spin=first_trace_spin)
    MI = entropies[0] + entropies[1] - entropies[2]
    print(MI)
    print(entropies)
    print('--------------------Test-----------------------')
    rhos4 = reduced_dm(4, number_spins, hamming_weight, gs, basis_states)
mgassner's avatar
mgassner committed
45
46
47
    rhos6 = reduced_dm(6, number_spins, hamming_weight, gs, basis_states)
    print(compute_entropy(rhos4), compute_entropy(rhos6))

mgassner's avatar
mgassner committed
48
def MI_vs_Entropy(model_name, number_spins, param_range):
mgassner's avatar
mgassner committed
49

mgassner's avatar
mgassner committed
50
    model_name = model_name
mgassner's avatar
mgassner committed
51
    number_spins = number_spins
mgassner's avatar
mgassner committed
52
53
    periodic = False
    spin_inversion = None
mgassner's avatar
mgassner committed
54
    params = np.linspace(param_range[0], param_range[1], int(param_range[2]))
mgassner's avatar
mgassner committed
55

mgassner's avatar
mgassner committed
56
57
    sub_dim = 1
    first_trace_spin = 1
mgassner's avatar
mgassner committed
58
59
60
    MIs = []
    Entropies = []

mgassner's avatar
mgassner committed
61
    print('Number Spins: ', number_spins)
mgassner's avatar
mgassner committed
62
63
    print('Sub Dimension is ', sub_dim)
    for param in params:
mgassner's avatar
mgassner committed
64
        if param < -1.0 or model_name == 'tfim':
mgassner's avatar
mgassner committed
65
            hamming_weight = None
mgassner's avatar
mgassner committed
66
67
68
        else:
            hamming_weight = number_spins // 2
        model = spin_model(model_name=model_name, number_spins=number_spins, periodic=periodic, 
mgassner's avatar
mgassner committed
69
70
71
72
                            param=param, hamming_weight=hamming_weight ,spin_inversion=spin_inversion)
        model.compute_ew_and_ev()
        basis_states = model.basis.states
        gs = model.eigenstates[:,0]
mgassner's avatar
mgassner committed
73
        MI = compute_MI(sub_dim, number_spins, hamming_weight, gs, basis_states, spin_inversion=None, first_trace_spin=first_trace_spin)
mgassner's avatar
mgassner committed
74
        MIs.append(MI)
mgassner's avatar
mgassner committed
75
        rho_half = reduced_dm(number_spins // 2, number_spins, hamming_weight, gs, basis_states)
mgassner's avatar
mgassner committed
76
        Entropies.append(compute_entropy(rho_half))
mgassner's avatar
mgassner committed
77
        del model
mgassner's avatar
mgassner committed
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

    data = {
        'Model': model_name,
        'Number_spins': number_spins,
        'Periodic': periodic,
        'Hamming_weight': hamming_weight,
        'Sub_dim': sub_dim,
        'Delta or h over J': params,
        'Entropies': Entropies,
        'Mutual Informations': MIs
    }

    timestr = time.strftime("%Y%m%d-%H%M%S")
    filename = 'MI_fixed_dim' + timestr
    with open('output/' + filename + '.yaml', 'w') as outfile:
        yaml.dump(data, outfile, default_flow_style=False)
    print(filename)

    plt.figure(figsize=(12,12))
    plt.title('MI: number spins = ' + str(number_spins) + 'periodic = ' + str(periodic))
mgassner's avatar
mgassner committed
98
99
100
101
    plt.plot(params, Entropies, label='Half-chain Entropy')
    plt.plot(params, MIs, label='Mutual Information')
    plt.grid(True)
    plt.legend()
mgassner's avatar
mgassner committed
102
103
104
105
106
107
108
    plt.savefig('output/' + filename + '.png')

if __name__ == "__main__":
    model_name = args.model_name
    param_range = args.param_range
    number_spins = args.number_spins
    MI_vs_Entropy(model_name, number_spins, param_range)