示例#1
0
def run_ro(ro_0, H, dt, nt, config, states=set(), fidelity_mode=False, thres=0.1):
    # --------------------------------------------------------
    U = Unitary(H, dt)

    if __debug__:
        U.to_csv(config.U_csv)

    U_data = U.data

    U_conj = U.conj()
    U_conj_data = U_conj.data
    # --------------------------------------------------------
    if fidelity_mode:
        ro_0_sqrt = lg.fractional_matrix_power(ro_0.data, 0.5)

        fidelity = []

    ro_t = ro_0.data

    # ----------------------------------------------------------------------------
    with open(config.z_csv, "w") as csv_file:
        writer = csv.writer(
            csv_file, quoting=csv.QUOTE_NONE, lineterminator="\n")

        for t in range(0, nt+1):
            diag_abs = np.abs(np.diag(ro_t))
            trace_abs = np.sum(diag_abs)

            print(diag_abs)

            Assert(abs(1 - trace_abs) <= 0.1, "ro is not normed", FILE(), LINE())

            writer.writerow(["{:.5f}".format(x) for x in diag_abs])
            # --------------------------------------------------------------------

            if fidelity_mode:
                fidelity_t = round(Fidelity(ro_0_sqrt, ro_t), 5)
                fidelity_t = "{:.5f}".format(fidelity_t)

                fidelity.append(fidelity_t)
            # --------------------------------------------------------------------
            ro_t = U_data.dot(ro_t).dot(U_conj_data)
    # ----------------------------------------------------------------------------
    st = dict()

    for k in range(len(H.states)):
        if k in states:
            st[k] = str(H.states[k][1])
        else:
            st[k] = ""

    write_xbp(H.states, config.x_csv, ind=st)
    write_t(T_str_v(config.T), config.nt, config.y_csv)
    # ----------------------------------------------------------
    if fidelity_mode:
        list_to_csv(config.fid_csv, fidelity, header=["fidelity"])
示例#2
0
def run_out_click(args):
    # ---------------------------------------------------------------------------------------------
    ro_0 = args['ro_0'] if 'ro_0' in args else None
    # print(ro_0.data.diagonal())
    Assert(ro_0 is not None, 'param[\'ro_0\'] is not set')
    # ---------------------------------------------------------------------------------------------
    H = args['H'] if 'H' in args else None

    Assert(H is not None, 'param[\'H\'] is not set')
    # ---------------------------------------------------------------------------------------------
    if 'T_list' in args:
        T_list = args['T_list']
        T_list.clear()

    # if 'sink_list' in args:
    #     sink_list = args['sink_list']
    #     sink_list.clear()
    # ---------------------------------------------------------------------------------------------
    dt = args['dt'] if 'dt' in args else None

    Assert(dt is not None, 'param[\'dt\'] is not set')
    # ---------------------------------------------------------------------------------------------
    # print("run starts ...")
    # ---------------------------------------------------------------------------------------------

    # ---------------------------------------------------------------------------------------------
    # Unitary
    U = Unitary(H, dt)

    # if 'U_csv' in args:
    #     U.to_csv(args['U_csv'])

    U_data = U.data

    U_conj = U.conj()
    # ---------------------------------------------------------------------------------------------
    ro_t = ro_0
    # ---------------------------------------------------------------------------------------------
    states_dim = []

    en = 0

    L_out = operator_L(ro_t, args['lindblad']['out'])

    diag_abs = np.abs(ro_t.data.diagonal(), dtype=np.longdouble)

    L_RO = []
    L_OUT = []

    for i in args['lindblad']['out']:
        L_ = operator_L(ro_t, i)
        L_RO.append(L_)
        L_OUT.append(L_)
    start_energy = ro_t.energy(H.capacity, H.cavity.n_atoms, H.states_bin,
                               diag_abs)

    start_energy['0_1'] = np.sum(start_energy['0_1'])
    start_energy['1_2'] = np.sum(start_energy['1_2'])

    sink = {}

    # start_energy = ro_t.energy(
    #     H.capacity, H.cavity.n_atoms, H.states_bin, diag_abs)
    # start_energy = np.sum(start_energy)

    t = 0

    L_op = L_out

    L_type = 'out'

    cnt = 0

    t_in = 0

    T_click = []

    nt = 0
    p_sink_prev = 0

    while True:
        # if t > 0:
        # print('\t'+time_unit_full(t))
        # -----------------------------------------------------------
        ro_t.data = ((U.data).dot(ro_t.data + dt * L_op(ro_t).data)).dot(
            U_conj.data)

        # ro_t.normalize()

        # -----------------------------------------------------------
        # print(L_type)
        nt += 1
        t += dt

        if t >= args['time_limit']:
            print('t >= time_limit')
            break

        diag_abs = np.abs(ro_t.data.diagonal(), dtype=np.longdouble)

        energy = ro_t.energy(H.capacity, H.cavity.n_atoms, H.states_bin,
                             diag_abs)
        if L_type == 'out':
            # p_sink = start_energy - np.sum(energy)
            sink['0_1'] = start_energy['0_1'] - \
                np.sum(energy['0_1'])
            sink['1_2'] = start_energy['1_2'] - \
                np.sum(energy['1_2'])

            # print(p_sink)
            if nt % args['dt_click'] == 0:
                # if nt % 50 == 0:
                p_coin = np.random.random_sample()

                if p_coin <= p_sink:
                    # print('p_sink: ', "{:.3f}".format(np.round(
                    #     p_sink, 3)), ', p_coin: ', "{:.3f}".format(np.round(p_coin, 3)), ' ', time_unit_full(t), sep='')
                    # print(p_sink, p_coin)
                    # print(nt, time_unit_full(t))
                    # exit(0)
                    return t

        # -----------------------------------------------------------

    return cnt
示例#3
0
def run(args):
    ro_0 = H = None

    T = nt = dt = l = None

    U_csv = x_csv = y_csv = z_csv = None

    thres = None

    T_list = sink_list = None

    sink_limit = None

    in_photons = out_photons = None

    lindblad = args['lindblad']

    if 'in_photons' in args:
        in_photons = args['in_photons']
    if 'out_photons' in args:
        out_photons = args['out_photons']
    # ---------------------------------------------------------------------------------------------
    if 'ro_0' in args:
        ro_0 = args['ro_0']

    Assert(ro_0 is not None, 'param[\'ro_0\'] is not set')
    # ---------------------------------------------------------------------------------------------
    if 'H' in args:
        H = args['H']

    Assert(H is not None, 'param[\'H\'] is not set')
    # ---------------------------------------------------------------------------------------------
    if 'l' in args:
        l = args['l']
    # ---------------------------------------------------------------------------------------------
    if 'T' in args:
        T = args['T']

    if 'dt' in args:
        dt = args['dt']

    if 'nt' in args:
        nt = args['nt']

    if 'sink_limit' in args:
        sink_limit = args['sink_limit']

    if 'precision' in args:
        precision = args['precision']
    else:
        precision = 1e-10
    # ---------------------------------------------------------------------------------------------
    if 'U_csv' in args:
        U_csv = args['U_csv']

    if 'x_csv' in args:
        x_csv = args['x_csv']

    if 'y_csv' in args:
        y_csv = args['y_csv']

    if 'z_csv' in args:
        z_csv = args['z_csv']
    # ---------------------------------------------------------------------------------------------
    if 'thres' in args:
        thres = args['thres']

    if 'T_list' in args:
        T_list = args['T_list']
        T_list.clear()

    if 'sink_list' in args:
        sink_list = args['sink_list']
    # ---------------------------------------------------------------------------------------------
    Assert(dt is not None, 'param[\'dt\'] is not set')
    # ---------------------------------------------------------------------------------------------
    print("run starts ...")
    # ---------------------------------------------------------------------------------------------
    # Unitary
    U = Unitary(H, dt)
    # print(type(U.data))

    if U_csv is not None:
        U.to_csv(args['U_csv'])

    U_data = U.data

    U_conj = U.conj()
    # print(type(U_conj))
    # ---------------------------------------------------------------------------------------------
    ro_t = ro_0
    # ---------------------------------------------------------------------------------------------
    if "z_csv" is not None:
        fz_csv = open("z_csv", "w")

    writer = csv.writer(fz_csv, quoting=csv.QUOTE_NONE, lineterminator="\n")

    states_dim = []

    en = 0

    L_RO = []
    L_OUT = []

    for i in args['lindblad']['out']:
        L_ = operator_L(ro_t, i)
        L_RO.append(L_)
        L_OUT.append(L_)

    diag_abs = np.abs(ro_t.data.diagonal(), dtype=np.longdouble)

    start_energy = ro_t.energy(H.capacity, H.cavity.n_atoms, H.states_bin,
                               diag_abs)

    start_energy['0_1'] = np.sum(start_energy['0_1'])
    start_energy['1_2'] = np.sum(start_energy['1_2'])

    sink = {}

    t = 0

    while True:
        diag_abs = np.abs(ro_t.data.diagonal(), dtype=np.longdouble)

        energy = ro_t.energy(H.capacity, H.cavity.n_atoms, H.states_bin,
                             diag_abs)
        # print(np.sum(energy))

        if sink_list is None:
            if np.sum(energy['0_1']) - start_energy['0_1'] > in_photons:
                # print("init_energy: ", start_energy, ", energy: ", np.sum(
                #     energy[1:]), ", ", np.round(energy, 3), sep="")
                # exit(0)
                return False
        # -----------------------------------------------------------
        # write
        # writer_all.writerow(["{:.5f}".format(x) for x in diag_abs])
        # writer.writerow(["{:.5f}".format(x) for x in v_bin])

        if "z_csv" is not None:
            writer.writerow(["{:.5f}".format(x) for x in diag_abs])
        # -----------------------------------------------------------
        # T_list
        if T_list is not None:
            T_list.append(t)
        # -----------------------------------------------------------

        if sink_list is not None:
            zero = diag_abs[0]

            # print(energy['0_1'])
            # exit(0)
            sink['0_1'] = start_energy['0_1'] - \
                np.sum(energy['0_1'])
            sink['1_2'] = start_energy['1_2'] - \
                np.sum(energy['1_2'])

            sink_list['0_1'].append(sink['0_1'])
            sink_list['1_2'].append(sink['1_2'])

            # print('dist:', abs(sink_limit - sink['0_1']), abs(sink_limit - sink['1_2']))
            if sink_limit is not None:
                if abs(sink_limit - sink['0_1']) < precision:
                    # or \
                    # abs(sink_limit - sink['1_2']) < precision:
                    print(np.round(diag_abs, 3))
                    return False

            print(sink['0_1'], sink['1_2'])
        # -----------------------------------------------------------
        for L_ in L_RO:
            ro_t.data += dt * L_(ro_t).data

        ro_t.data = ((U.data).dot(ro_t.data)).dot(U_conj.data)

        Assert(
            abs(1 - ro_t.abs_trace()) <= args['precision'],
            "ro is not normed: " + str(ro_t.abs_trace()))

        # ro_t.normalize()
        t += dt

    return True
示例#4
0
def run(args):
    # ---------------------------------------------------------------------------------------------
    ro_0 = H = None

    T = nt = dt = l = None

    U_csv = x_csv = y_csv = z_csv = None

    thres = None

    T_list = sink_list = None

    sink_limit = None

    in_photons = out_photons = None

    lindblad = args['lindblad']

    if 'in_photons' in args:
        in_photons = args['in_photons']
    if 'out_photons' in args:
        out_photons = args['out_photons']
    # ---------------------------------------------------------------------------------------------
    if 'ro_0' in args:
        ro_0 = args['ro_0']

    Assert(ro_0 is not None, 'param[\'ro_0\'] is not set')
    # ---------------------------------------------------------------------------------------------
    if 'H' in args:
        H = args['H']

    Assert(H is not None, 'param[\'H\'] is not set')
    # ---------------------------------------------------------------------------------------------
    if 'l' in args:
        l = args['l']
    # ---------------------------------------------------------------------------------------------
    if 'T' in args:
        T = args['T']

    if 'dt' in args:
        dt = args['dt']

    if 'nt' in args:
        nt = args['nt']

    if 'sink_limit' in args:
        sink_limit = args['sink_limit']

    if 'precision' in args:
        precision = args['precision']
    else:
        precision = 1e-10
    # ---------------------------------------------------------------------------------------------
    if 'U_csv' in args:
        U_csv = args['U_csv']

    if 'x_csv' in args:
        x_csv = args['x_csv']

    if 'y_csv' in args:
        y_csv = args['y_csv']

    if 'z_csv' in args:
        z_csv = args['z_csv']
    # ---------------------------------------------------------------------------------------------
    if 'thres' in args:
        thres = args['thres']

    if 'T_list' in args:
        T_list = args['T_list']
        T_list.clear()

    if 'sink_list' in args:
        sink_list = args['sink_list']
        sink_list.clear()
    # ---------------------------------------------------------------------------------------------
    Assert(dt is not None, 'param[\'dt\'] is not set')
    # ---------------------------------------------------------------------------------------------
    # print("run starts ...")
    # ---------------------------------------------------------------------------------------------
    # a = operator_a(H, H.capacity, H.cavity.n_atoms)
    # acrossa = operator_acrossa(H, H.capacity, H.cavity.n_atoms)

    # across = a.conj()

    # # a.print(precision=3)
    # # print()
    # # across.print(precision=3)
    # # exit(0)

    # if check:
    #     across_a = Matrix(H.size, H.size, dtype=np.longdouble)
    #     across_a.data = lil_matrix((across.data).dot(a.data), dtype=np.longdouble)
    #     # a_cross_a.print()

    #     # across_a__cross = Matrix(H.size, H.size, dtype=np.double)
    #     across_a__cross = across_a.conj()

    # aa_cross = Matrix(H.size, H.size, dtype=np.double)
    # aa_cross.data = (aa.data.dot(aa.data.transpose()))
    # ---------------------------------------------------------------------------------------------
    # Unitary
    U = Unitary(H, dt)
    # print(type(U.data))

    if U_csv is not None:
        U.to_csv(args['U_csv'])

    U_data = U.data

    U_conj = U.conj()
    # print(type(U_conj))
    # ---------------------------------------------------------------------------------------------
    ro_t = ro_0
    # ---------------------------------------------------------------------------------------------
    if "z_csv" is not None:
        fz_csv = open("z_csv", "w")

    writer = csv.writer(fz_csv, quoting=csv.QUOTE_NONE, lineterminator="\n")

    states_dim = []

    en = 0

    L_ro = L_out = operator_L(ro_t, args['lindblad']['out'])

    diag_abs = np.abs(ro_t.data.diagonal(), dtype=np.longdouble)

    start_energy = ro_t.energy(H.capacity, H.cavity.n_atoms, H.states_bin,
                               diag_abs)

    start_energy['0_1'] = np.sum(start_energy['0_1'])
    start_energy['1_2'] = np.sum(start_energy['1_2'])

    t = 0

    while True:
        diag_abs = np.abs(ro_t.data.diagonal(), dtype=np.longdouble)
        energy = ro_t.energy(H.capacity, H.cavity.n_atoms, H.states_bin,
                             diag_abs)
        # print(np.sum(energy))
        # exit(0)
        if sink_list is None:
            # print(np.round(diag_abs, 3))
            # energy = ro_t.energy(H.capacity, H.cavity.n_atoms, H.states_bin, diag_abs)
            # print("energy:", np.sum(energy))
            # print()
            # sleep(1)

            if np.sum(energy['0_1']) - start_energy['0_1'] > in_photons:
                # print("init_energy: ", start_energy, ", energy: ", np.sum(
                #     energy[1:]), ", ", np.round(energy, 3), sep="")
                # exit(0)
                return False
        # -----------------------------------------------------------
        # write
        # writer_all.writerow(["{:.5f}".format(x) for x in diag_abs])
        # writer.writerow(["{:.5f}".format(x) for x in v_bin])

        if "z_csv" is not None:
            writer.writerow(["{:.5f}".format(x) for x in diag_abs])
        # -----------------------------------------------------------
        # T_list
        if T_list is not None:
            # sleep(1)
            T_list.append(t)
        # -----------------------------------------------------------
        # print(states_bin)
        # exit(0)
        # sink
        sink = {}

        if sink_list is not None:
            # zero = abs(ro_t.data[0, 0])
            zero = diag_abs[0]

            # print('start_energy:', start_energy['0_1'])
            # exit(0)
            # print(energy['0_1'])
            # exit(0)
            sink['0_1'] = start_energy['0_1'] - \
                np.sum(energy['0_1'][1:H.capacity['0_1']+1])
            sink['1_2'] = start_energy['1_2'] - \
                np.sum(energy['1_2'][1:H.capacity['1_2']+1])
            # sink['1_2'] = start_energy['1_2'] - \
            # np.sum(energy['1_2'][1:H.capacity+1])
            # sink = start_energy - np.sum(energy[1:H.capacity+1])
            # st_energy = ((operator_acrossa(H, H.capacity, H.cavity.n_atoms)).data.dot(ro_0.data))
            # st_energy = np.sum(np.abs(start_energy))
            # sink = start_energy - st_energy

            # sink = args['en_'] - np.sum(energy[1:])
            # print("sink:", sink, ", energy:", np.sum(energy[1:H.capacity+1]))
            # sink = H.capacity - np.sum(energy[1:])
            # sink = args['H'].capacity * zero
            # print("sink =", H.capacity, "-", np.sum(energy[1:]))
            # sink = args['H'].capacity * zero
            # print("init_energy: ", start_energy, ", energy: ", np.sum(
            #     energy[1:]), ", ", np.round(energy, 3), ", sink: ", sink, sep="")

            # if len(sink_list) != 0 and (sink_list[-1] - sink) > precision:
            #     print("err:", sink, "<", sink_list[-1])
            #     exit(0)

            sink_list.append(sink['0_1'])

            if sink_limit is not None:
                if abs(sink_limit - sink['0_1']) < precision:
                    return False
                if abs(sink_limit - sink['1_2']) < precision:
                    return False

            print(sink['0_1'], sink['1_2'])
        # -----------------------------------------------------------
        # evolution
        # ro_t.data = ro_t.data + dt * L_ro(ro_t).data

        # LL = Matrix(m=ro_t.m, n=ro_t.n, dtype=np.complex128)
        # LL.data = L_ro(ro_t).data
        # print("L_ro:")
        # LL.print()

        # print("ro_t:")
        # ro_t.print()
        # exit(0)

        # ro_t.data = ((U.data).dot(ro_t.data)).dot(U_conj.data)

        ro_t.data = ((U.data).dot(ro_t.data + dt * L_ro(ro_t).data)).dot(
            U_conj.data)

        Assert(
            abs(1 - ro_t.abs_trace()) <= args['precision'],
            "ro is not normed: " + str(ro_t.abs_trace()))

        # ro_t.normalize()
        # -----------------------------------------------------------
        t += dt
        # across_a__cross.print()
        # H.print_states()
        # ro_0.print()
        # sleep(1)

    # ---------------------------------------------------------------------------------------------
    if x_csv is not None:
        write_x_not_ind(H.states, x_csv)
    # write_xbp(H.states, config.x_csv, ind=st)

    if y_csv is not None:
        write_t(T_str_v(T), nt, y_csv)
    # ---------------------------------------------------------------------------------------------
    if z_csv is not None:
        fz_csv.close()
    # ---------------------------------------------------------------------------------------------
    return True
示例#5
0
def run_wf(w_0, H, dt, nt, config, states=set(), fidelity_mode=False, thres=0.1):
    # --------------------------------------------------------
    U = Unitary(H, dt)

    if __debug__:
        U.to_csv(config.U_csv)

    U_data = U.data
    # --------------------------------------------------------
    if fidelity_mode:
        fidelity = []

    w_t = w_0.data
    # ----------------------------------------------------------------------------
    with open(config.z_csv, "w") as csv_file:
        writer = csv.writer(
            csv_file, quoting=csv.QUOTE_NONE, lineterminator="\n")

        for t in range(0, nt+1):
            diag_abs = np.abs(w_t.toarray()[:, 0])**2
            trace_abs = np.sum(diag_abs)

            # print(diag_abs)
            # print(trace_abs)

            for i, j in enumerate(diag_abs):
                if abs(j) > thres:
                    states.add(i)

            Assert(abs(1 - trace_abs) <= 0.1, "ro is not normed", FILE(), LINE())
            # --------------------------------------------------------------------
            # if fidelity_mode:
            #     w_t = np.matrix(w_t)

            #     D = w_0.getH().dot(w_t).reshape(-1)[0, 0]

            #     fidelity_t = round(abs(D), 3)

            #     fidelity_t = "{:>.5f}".format(fidelity_t)

            #     fidelity.append(fidelity_t)

            # print(fidelity)

            writer.writerow(["{:.5f}".format(x)
                             for x in diag_abs])
            # --------------------------------------------------------------------
            w_t = U_data.dot(w_t)
    # ----------------------------------------------------------------------------
    st = dict()

    print(states)
    print(H.states)

    for k in range(len(H.states)):
        if k in states:
            st[k] = str(H.states[k][1])
        else:
            st[k] = ""

    write_xbp(H.states, config.x_csv, ind=st)
    write_t(T_str_v(config.T), config.nt, config.y_csv)
示例#6
0
def run_click(args):
    # ---------------------------------------------------------------------------------------------
    ro_0 = H = None

    T = nt = dt = l = None

    U_csv = x_csv = y_csv = z_csv = None

    thres = None

    T_list = sink_list = None

    sink_limit = None

    in_photons = out_photons = None

    lindblad = args['lindblad']

    if 'in_photons' in args:
        in_photons = args['in_photons']
    if 'out_photons' in args:
        out_photons = args['out_photons']
    # ---------------------------------------------------------------------------------------------
    if 'ro_0' in args:
        ro_0 = args['ro_0']

    Assert(ro_0 is not None, 'param[\'ro_0\'] is not set')
    # ---------------------------------------------------------------------------------------------
    if 'H' in args:
        H = args['H']

    Assert(H is not None, 'param[\'H\'] is not set')
    # ---------------------------------------------------------------------------------------------
    if 'l' in args:
        l = args['l']
    # ---------------------------------------------------------------------------------------------
    if 'T' in args:
        T = args['T']

    if 'dt' in args:
        dt = args['dt']

    if 'nt' in args:
        nt = args['nt']

    if 'sink_limit' in args:
        sink_limit = args['sink_limit']

    if 'precision' in args:
        precision = args['precision']
    else:
        precision = 1e-10
    # ---------------------------------------------------------------------------------------------
    if 'U_csv' in args:
        U_csv = args['U_csv']

    if 'x_csv' in args:
        x_csv = args['x_csv']

    if 'y_csv' in args:
        y_csv = args['y_csv']

    if 'z_csv' in args:
        z_csv = args['z_csv']
    # ---------------------------------------------------------------------------------------------
    if 'thres' in args:
        thres = args['thres']

    if 'T_list' in args:
        T_list = args['T_list']
        T_list.clear()

    if 'sink_list' in args:
        sink_list = args['sink_list']
        sink_list.clear()
    # ---------------------------------------------------------------------------------------------
    Assert(dt is not None, 'param[\'dt\'] is not set')
    # ---------------------------------------------------------------------------------------------
    # print("run starts ...")
    # ---------------------------------------------------------------------------------------------
    # Unitary
    U = Unitary(H, dt)

    # if U_csv is not None:
    #     U.to_csv(args['U_csv'])

    U_data = U.data

    U_conj = U.conj()
    # ---------------------------------------------------------------------------------------------
    ro_t = ro_0
    # ---------------------------------------------------------------------------------------------
    if "z_csv" is not None:
        fz_csv = open("z_csv", "w")

    writer = csv.writer(fz_csv, quoting=csv.QUOTE_NONE, lineterminator="\n")

    states_dim = []

    en = 0

    L_ro = L_out = operator_L(ro_t, args['lindblad']['out'])

    diag_abs = np.abs(ro_t.data.diagonal(), dtype=np.longdouble)

    start_energy = ro_t.energy(H.capacity, H.cavity.n_atoms, H.states_bin,
                               diag_abs)
    start_energy = np.sum(start_energy)

    t = 0

    ph_out = False

    T_click = []

    while True:
        # for t in range(0, nt+1):
        # -----------------------------------------------------------
        # print("\t", t)
        # print(t, "/", nt)

        diag_abs = np.abs(ro_t.data.diagonal(), dtype=np.longdouble)
        # print(diag_abs)
        energy = ro_t.energy(H.capacity, H.cavity.n_atoms, H.states_bin,
                             diag_abs)
        # print(np.sum(energy))

        p_sink = 1 - np.sum(energy)

        p_coin = np.random.random_sample()
        print('p_sink: ', p_sink, ', p_coin: ', p_coin, sep='')

        if p_coin <= p_sink:
            T_click.append(t)
            # ph_out = True
            break

        if sink_list is None:
            if np.sum(energy) - start_energy > in_photons:
                return False
        # -----------------------------------------------------------
        if "z_csv" is not None:
            writer.writerow(["{:.5f}".format(x) for x in diag_abs])
        # -----------------------------------------------------------
        # T_list
        if T_list is not None:
            T_list.append(t)
        # -----------------------------------------------------------
        if sink_list is not None:
            zero = diag_abs[0]
            sink = start_energy - np.sum(energy[1:H.capacity + 1])

            if len(sink_list) != 0 and (sink_list[-1] - sink) > precision:
                print("err:", sink, "<", sink_list[-1])
                exit(0)

            sink_list.append(sink)

            if sink_limit is not None:
                if abs(sink_limit - sink) < precision:
                    return False
        # -----------------------------------------------------------
        ro_t.data = ((U.data).dot(ro_t.data + dt * L_ro(ro_t).data)).dot(
            U_conj.data)

        Assert(
            abs(1 - ro_t.abs_trace()) <= args['precision'],
            "ro is not normed: " + str(ro_t.abs_trace()))

        # ro_t.normalize()
        # -----------------------------------------------------------
        t += dt

    if ph_out:
        return True
    # ---------------------------------------------------------------------------------------------
    if x_csv is not None:
        write_x_not_ind(H.states, x_csv)
    # write_xbp(H.states, config.x_csv, ind=st)

    if y_csv is not None:
        write_t(T_str_v(T), nt, y_csv)
    # ---------------------------------------------------------------------------------------------
    if z_csv is not None:
        fz_csv.close()
    # ---------------------------------------------------------------------------------------------
    return True
示例#7
0
def run_wf(w_0, H, dt, nt, config, states=set(), fidelity_mode=False, thres=0.1):
    # --------------------------------------------------------
    U = Unitary(H, dt)

    if __debug__:
        U.to_csv(config.U_csv)

    U_data = U.data
    # --------------------------------------------------------
    if fidelity_mode:
        fidelity = []

    w_0 = np.matrix(w_0.data)
    w_t = np.array(w_0.data)
    # ----------------------------------------------------------------------------
    with open(config.z_csv, "w") as csv_file:
        writer = csv.writer(
            csv_file, quoting=csv.QUOTE_NONE, lineterminator="\n")

        for t in range(0, nt+1):
            w_t_arr = w_t.reshape(1, -1)[0]

            diag_abs = np.abs(w_t_arr)**2
            trace_abs = np.sum(diag_abs)

            # peaks = peakutils.indexes(diag_abs, thres=thres)
            # print(diag_abs)
            for i, j in enumerate(diag_abs):
                if abs(j) > thres:
                    states.add(i)
            # for i in peaks:
            #     print(i)
            #     exit(0)
            #     states.add(i)
            # exit(0)
            # print(diag_abs)

            Assert(abs(1 - trace_abs) <= 0.1, "ro is not normed", cf())
            # --------------------------------------------------------------------
            if fidelity_mode:
                w_t = np.matrix(w_t)

                D = w_0.getH().dot(w_t).reshape(-1)[0, 0]

                fidelity_t = round(abs(D), 3)

                fidelity_t = "{:>.5f}".format(fidelity_t)

                fidelity.append(fidelity_t)
                # print(fidelity)
            # print(diag_abs)

            writer.writerow(["{:.5f}".format(x)
                             for x in diag_abs])
            # --------------------------------------------------------------------
            w_t = np.array(U_data.dot(w_t))
    # ----------------------------------------------------------------------------
    st = dict()
    print(states)
    print(H.states)
    # exit(0)
    for k in range(len(H.states)):
        if k in states:
            st[k] = str(H.states[k][1])
        else:
            st[k] = ""

    write_xbp(H.states, config.x_csv, ind=st)
    write_t(T_str_v(config.T), config.nt, config.y_csv)
    # ----------------------------------------------------------
    if fidelity_mode:
        list_to_csv(config.fid_csv, fidelity, header=["fidelity"])
示例#8
0
def run_w(w_0, H, dt, nt, config, fidelity_mode=False):
    # --------------------------------------------------------
    U = Unitary(H, dt)

    if __debug__:
        U.write_to_file(config.U_csv)

    U_conj = U.conj()
    # --------------------------------------------------------
    if fidelity_mode:
        fidelity = []

    w_0 = np.matrix(w_0.data)

    w_t = np.array(w_0.data)
    # ----------------------------------------------------------------------------
    dt_ = nt / (config.T/config.mks) / 20000 * 1000
    nt_ = int(nt/dt_)

    z_0 = []
    z_1 = []
    z_max = []

    # ind_0 = None
    # ind_1 = None

    # for k, v in H.states.items():
    #     if v == [0, H.n]:
    #         ind_0 = k
    #     elif v == [H.n, 0]:
    #         ind_1 = k

    with open(config.z_csv, "w") as csv_file:
        writer = csv.writer(
            csv_file, quoting=csv.QUOTE_NONE, lineterminator="\n")

        for t in range(0, nt+1):
            w_t_arr = w_t.reshape(1, -1)[0]

            diag_abs = np.abs(w_t_arr)**2
            trace_abs = np.sum(diag_abs)

            Assert(abs(1 - trace_abs) <= 0.1, "ro is not normed", cf())
            # --------------------------------------------------------------------
            if fidelity_mode:
                w_t = np.matrix(w_t)

                D = w_0.getH().dot(w_t).reshape(-1)[0, 0]

                fidelity_t = round(abs(D), 3)

                fidelity_t = "{:>.5f}".format(fidelity_t)

                fidelity.append(fidelity_t)

            # z_0.append("{:.5f}".format(diag_abs[ind_0]))
            # z_1.append("{:.5f}".format(diag_abs[ind_1]))

            # zmax = 0

            # for i_ in range(0, len(diag_abs)):
            #     if i_ != ind_0 and i_ != ind_1 and diag_abs[i_] > zmax:
            #         # exit(1)
            #         zmax = diag_abs[i_]

            # z_max.append(zmax)

            # z_1.append(round(diag_abs[ind_1], config.precision))
            # z_1.append(float(diag_abs[ind_1]))

            # if t % nt_ == 0:
            writer.writerow(["{:.5f}".format(x)
                             for x in diag_abs])
            # --------------------------------------------------------------------
            w_t = np.array(U.data.dot(w_t))
    # ----------------------------------------------------------------------------
    states = H.states

    write_x(states, config.x_csv, ind=[[1, [1, 1, 0]], [1, [0, 1, 1]], [1, [1, 0, 1]], [2, [0, 1, 0]], [
            2, [0, 0, 1]], [2, [1, 0, 0]], [3, [0, 0, 0]]])
    # write_x(states, config.x_csv, ind=[[0, H.n], [H.n, 0]])
    write_t(T_str_v(config.T), config.nt, config.y_csv)
示例#9
0
def run_RWA(w0, H, t0, t1, initstate, certain_state, nt=200, ymin=0, ymax=1, RWA=True, title='title', color='blue'):
    # ------------------------------------------------------------------------------------------------------------------
    if not(nt in range(0, 501)):
        return -1

    Assert(t0 >= 0, "t0 < 0", cf())
    Assert(t1 > 0, "t1 <= 0", cf())
    Assert(t0 < t1, "t0 >= t1", cf())

    if len(np.shape(H.matrix.data)) != 2:
        return -1
    # if np.shape(H)[0] != np.shape(H)[1]:
        # return -1
    # if np.shape(H)[0] != len(w0):
        # return -1
    # ------------------------------------------------------------------------------------------------------------------
    nt = t1 * 100

    t = np.linspace(t0, t1, nt+1)

    dt = t[1] - t[0]

    U = Unitary(H, dt)

    # ------------------------------------------------------------------------------------------------------------------
    state = []

    at_count = len(initstate[1])

    for i in range(0, len(w0.data)):
        ph_count = int(i / pow(2, at_count))
        st_number = i % pow(2, at_count)

        at_binary = bin(st_number)[2:].zfill(at_count)

        state.append('[' + str(ph_count) + '|' + at_binary + ']')
    # ------------------------------------------------------------------------------------------------------------------
    w = []

    wt = w0

    for i in range(0, nt+1):
        wt = get_wdt(wt.data, U.data)

        Assert(np.max(wt) <= 1, "np.max(wt) > 1", cf())

        w.append(np.abs(wt))

    w = np.array(w)
    w = w[:, :, 0]
    # ------------------------------------------------------------------------------------------------------------------
    st = certain_state[0]*(pow(2, at_count))
    at = 0

    for i in range(0, at_count):
        at += pow(2, i) * certain_state[1][at_count-i-1]
    # ------------------------------------------------------------------------------------------------------------------

    animator.make_plot(t, t0, t1, ymin, ymax,
                       w[:, st+at], color, title=title, X=r'$t,\ мкс$', Y=r'$Probability$   ')

    return
示例#10
0
def run(args):
    # ---------------------------------------------------------------------------------------------
    ro_0 = H = None

    T = nt = dt = l = None

    U_csv = x_csv = y_csv = z_csv = None

    thres = None

    T_list = sink_list = None

    sink_limit = None

    in_photons = out_photons = None

    lindblad = args['lindblad']

    if 'in_photons' in args:
        in_photons = args['in_photons']
    if 'out_photons' in args:
        out_photons = args['out_photons']
    # ---------------------------------------------------------------------------------------------
    if 'ro_0' in args:
        ro_0 = args['ro_0']

    Assert(ro_0 is not None, 'param[\'ro_0\'] is not set')
    # ---------------------------------------------------------------------------------------------
    if 'H' in args:
        H = args['H']

    Assert(H is not None, 'param[\'H\'] is not set')
    # ---------------------------------------------------------------------------------------------
    if 'l' in args:
        l = args['l']
    # ---------------------------------------------------------------------------------------------
    if 'T' in args:
        T = args['T']

    if 'dt' in args:
        dt = args['dt']

    if 'nt' in args:
        nt = args['nt']

    if 'sink_limit' in args:
        sink_limit = args['sink_limit']

    if 'precision' in args:
        precision = args['precision']
    else:
        precision = 1e-10
    # ---------------------------------------------------------------------------------------------
    if 'U_csv' in args:
        U_csv = args['U_csv']

    if 'x_csv' in args:
        x_csv = args['x_csv']

    if 'y_csv' in args:
        y_csv = args['y_csv']

    if 'z_csv' in args:
        z_csv = args['z_csv']
    # ---------------------------------------------------------------------------------------------
    if 'thres' in args:
        thres = args['thres']

    if 'T_list' in args:
        T_list = args['T_list']
        T_list.clear()

    if 'sink_list' in args:
        sink_list = args['sink_list']
        sink_list.clear()
    # ---------------------------------------------------------------------------------------------
    Assert(dt is not None, 'param[\'dt\'] is not set')
    # ---------------------------------------------------------------------------------------------
    # print("run starts ...")
    # ---------------------------------------------------------------------------------------------
    # a = operator_a(H, H.capacity, H.cavity.n_atoms)
    # acrossa = operator_acrossa(H, H.capacity, H.cavity.n_atoms)

    # across = a.conj()

    # # a.print(precision=3)
    # # print()
    # # across.print(precision=3)
    # # exit(0)

    # if check:
    #     across_a = Matrix(H.size, H.size, dtype=np.longdouble)
    #     across_a.data = lil_matrix((across.data).dot(a.data), dtype=np.longdouble)
    #     # a_cross_a.print()

    #     # across_a__cross = Matrix(H.size, H.size, dtype=np.double)
    #     across_a__cross = across_a.conj()

    # aa_cross = Matrix(H.size, H.size, dtype=np.double)
    # aa_cross.data = (aa.data.dot(aa.data.transpose()))
    # ---------------------------------------------------------------------------------------------
    # Unitary
    U = Unitary(H, dt)
    # print(type(U.data))

    if U_csv is not None:
        U.to_csv(args['U_csv'])

    U_data = U.data

    U_conj = U.conj()
    # print(type(U_conj))
    # ---------------------------------------------------------------------------------------------
    ro_t = ro_0
    # ---------------------------------------------------------------------------------------------
    if "z_csv" is not None:
        fz_csv = open("z_csv", "w")

    writer = csv.writer(fz_csv, quoting=csv.QUOTE_NONE, lineterminator="\n")

    states_dim = []

    en = 0

    # states_bin = {}

    # for k, v in enumerate(H.states):
    #     en = v[0] + np.sum(v[1])

    #     if en not in states_bin:
    #         states_bin[en] = []

    #     states_bin[en].append(k)

    # if v[0] + np.sum(v[1]) > en:
    #     en += 1
    #     states_dim.append(k-1)

    # print(states_dim)

    # exit(0)
    # ll = across_a.data+across.data
    # ll_cross = ll.getH()

    L_ro = L_out = operator_L(ro_t, args['lindblad']['out'])

    diag_abs = np.abs(ro_t.data.diagonal(), dtype=np.longdouble)

    start_energy = ro_t.energy(
        H.capacity, H.cavity.n_atoms, H.states_bin, diag_abs)
    start_energy = np.sum(start_energy)

    t = 0

    while True:
        # for t in range(0, nt+1):
        # -----------------------------------------------------------
        # print("\t", t)
        # print(t, "/", nt)

        diag_abs = np.abs(ro_t.data.diagonal(), dtype=np.longdouble)
        # print(diag_abs)
        energy = ro_t.energy(H.capacity, H.cavity.n_atoms,
                             H.states_bin, diag_abs)
        print(np.sum(energy))
        if sink_list is None:
            # print(np.round(diag_abs, 3))
            # energy = ro_t.energy(H.capacity, H.cavity.n_atoms, H.states_bin, diag_abs)
            # print("energy:", np.sum(energy))
            # print()
            # sleep(1)

            if np.sum(energy) - start_energy > in_photons:
                # print("init_energy: ", start_energy, ", energy: ", np.sum(
                #     energy[1:]), ", ", np.round(energy, 3), sep="")
                # exit(0)
                return False
        # -----------------------------------------------------------
        # write
        # writer_all.writerow(["{:.5f}".format(x) for x in diag_abs])
        # writer.writerow(["{:.5f}".format(x) for x in v_bin])

        if "z_csv" is not None:
            writer.writerow(["{:.5f}".format(x) for x in diag_abs])
        # -----------------------------------------------------------
        # T_list
        if T_list is not None:
            # sleep(1)
            T_list.append(t)
        # -----------------------------------------------------------
        # print(states_bin)
        # exit(0)
        # sink

        if sink_list is not None:
            # zero = abs(ro_t.data[0, 0])
            zero = diag_abs[0]

            # states_dim = [1, 4, 4, 3]
            # print(np.abs(ro_t.diagonal()))
            # capacity = 1
            # start = states_dim[0]+1

            # energy = [0] * (H.capacity + H.cavity.n_atoms+1)

            # # print(energy)

            # for i in range(1, len(states_bin)):
            #     # print(i)

            #     for j in states_bin[i]:
            #         # print(states_bin[i])
            #         energy[i] += diag_abs[j]
            #     # energy[i] += np.sum(diag_abs[states_bin[i]])
            # energy = ro_t.energy(H.capacity, H.cavity.n_atoms, H.states_bin, diag_abs)
            # for i in range(1, len(states_bin)):
            #     energy[i] *= i
            # for c in states_dim[1:]:
            #     # energy[capacity] = capacity
            #     fin = c+1
            #     # fin = start+c
            #     print(capacity, c, '[', start, ':', fin, ']')
            #     energy[capacity] = np.sum(diag_abs[start:fin]) * (capacity)
            #     # energy[capacity] = np.sum(diag_abs[start:fin]) * (H.capacity-capacity)
            #     print("energy[", capacity, "] = ", np.sum(diag_abs[start:fin]), " * ", (capacity))

            #     # print("cnt =", cnt, energy[cnt], np.sum(energy[cnt]))
            #     capacity += 1
            #     start = fin

            # exit(0)
            # print(np.round(energy, 3))
            # print("energy:", energy)
            # sink = np.sum(energy) - np.sum(energy[1:])
            # sink = np.sum(energy) - np.sum(energy[1:])
            sink = start_energy - np.sum(energy[1:H.capacity+1])
            # st_energy = ((operator_acrossa(H, H.capacity, H.cavity.n_atoms)).data.dot(ro_0.data))
            # st_energy = np.sum(np.abs(start_energy))
            # sink = start_energy - st_energy

            # sink = args['en_'] - np.sum(energy[1:])
            # print("sink:", sink, ", energy:", np.sum(energy[1:H.capacity+1]))
            # sink = H.capacity - np.sum(energy[1:])
            # sink = args['H'].capacity * zero
            # print("sink =", H.capacity, "-", np.sum(energy[1:]))
            # sink = args['H'].capacity * zero
            # print("init_energy: ", start_energy, ", energy: ", np.sum(
            #     energy[1:]), ", ", np.round(energy, 3), ", sink: ", sink, sep="")

            if len(sink_list) != 0 and (sink_list[-1] - sink) > precision:
                print("err:", sink, "<", sink_list[-1])
                exit(0)

            sink_list.append(sink)

            if sink_limit is not None:
                if abs(sink_limit - sink) < precision:
                    return False
        # -----------------------------------------------------------
        # evolution
        # ro_t.data = ro_t.data + dt * L_ro(ro_t).data

        # LL = Matrix(m=ro_t.m, n=ro_t.n, dtype=np.complex128)
        # LL.data = L_ro(ro_t).data
        # print("L_ro:")
        # LL.print()

        # print("ro_t:")
        # ro_t.print()
        # exit(0)

        # ro_t.data = ((U.data).dot(ro_t.data)).dot(U_conj.data)

        ro_t.data = ((U.data).dot(ro_t.data + dt *
                                  L_ro(ro_t).data)).dot(U_conj.data)

        Assert(abs(1 - ro_t.abs_trace()) <=
               args['precision'], "ro is not normed: " + str(ro_t.abs_trace()))

        # ro_t.normalize()
        # -----------------------------------------------------------
        t += dt
        # across_a__cross.print()
        # H.print_states()
        # ro_0.print()
        # sleep(1)

    # ---------------------------------------------------------------------------------------------
    if x_csv is not None:
        write_x_not_ind(H.states, x_csv)
    # write_xbp(H.states, config.x_csv, ind=st)

    if y_csv is not None:
        write_t(T_str_v(T), nt, y_csv)
    # ---------------------------------------------------------------------------------------------
    if z_csv is not None:
        fz_csv.close()
    # ---------------------------------------------------------------------------------------------
    return True