Ejemplo n.º 1
0
    def __init__(self, states, init_state, amplitude=1):
        Assert(isinstance(states, dict), "states is not dict", cf())
        Assert(isinstance(init_state, list), "init_state is not list", cf())

        Assert(len(states) > 1, "w_0 is not set", cf())

        self.states = states

        k_found = None

        # print(states)
        # print(init_state)
        for k, v in states.items():
            if init_state == v:
                k_found = k

                break

        Assert(k_found is not None, "w_0 is not set", cf())

        super(WaveFunction, self).__init__(m=len(states),
                                           n=1,
                                           dtype=np.complex128)

        self.data[k_found] = amplitude
Ejemplo n.º 2
0
    def __init__(self, m, n, dtype):
        Assert(m > 0, "m <= 0", cf())
        Assert(n > 0, "n <= 0", cf())

        self.m = m
        self.n = n

        self.data = np.matrix(np.zeros([m, n]), dtype=dtype)
Ejemplo n.º 3
0
def sigma(i, j, n_levels):
    Assert(i >= 0, "i < 0", cf())
    Assert(j >= 0, "j < 0", cf())
    Assert(n_levels > 0, "n_levels <= 0", cf())

    sigma = SparseMatrix(m=n_levels, n=n_levels, orient='row')

    sigma.add((j, i), 1)

    return sigma
Ejemplo n.º 4
0
def add_row(self, i, k):
    Assert(i >= 0, 'i < 0', cf())
    Assert(i < self.m, 'i >= m', cf())

    if i not in self.row.keys():
        return

    if k == 0:
        return

    for j_pos, j in enumerate(self.row[i]['ind']):
        self.add_item(i, j_pos, k)
Ejemplo n.º 5
0
def print_row(self, i, sep='\t', precision=0, file=sys.stdout):
    Assert(i >= 0, "i < 0", cf())

    if i in self.row:
        row = []

        for j in range(self.n):
            found = False

            for k, ind_j in enumerate(self.row[i]['ind']):
                # print(self.row[i]['ind'], 'ind_j=', ind_j, 'j=', j)
                if ind_j == j:
                    row += [self.row[i]['items'][k]]
                    found = True
                    break

            if not found:
                row += [0]
                # re = format(value.real, "." + str(precision) + "f")
        # v =
        row_str = sep.join(
            [str(format(i, "." + str(precision) + "f")) for i in row])
    else:
        row_str = sep.join(['0' for i in range(self.n)])

    print(row_str, end='', file=file)
Ejemplo n.º 6
0
def identity(n):
    Assert(n > 0, "n <= 0", cf())

    I = SparseMatrix(orient='row')

    for i in range(n):
        I.add((i, i), 1)

    return I
Ejemplo n.º 7
0
def remove_from_heap(self, i, j):
    found = False

    for v in self.heap:
        if v[0] == (i, j):
            self.heap.remove(v)
            found = True
            break

    Assert(found, 'not found', cf())
Ejemplo n.º 8
0
def divide_row(self, i, k):
    Assert(k != 0, 'k == 0', cf())

    Assert(i >= 0, 'i < 0', cf())
    Assert(i < self.m, 'i >= m', cf())

    if i not in self.row.keys():
        return

    if k == 1:
        return

    if k == -1:
        for j_pos, j in enumerate(self.row[i]['ind']):
            self.row[i]['items'][j_pos] = -self.row[i]['items'][j_pos]
    else:
        for j_pos, j in enumerate(self.row[i]['ind']):
            self.div_item(i, j_pos, k)

    return
Ejemplo n.º 9
0
    def check_unitarity(self):
        data = self.data
        data_H = self.data.getH()

        I = np.eye(len(data))

        Assert(
            np.all(abs(data.dot(data_H) - I) < eps)
            and np.all(abs(data_H.dot(data) - I) < eps),
            "matrix is not unitary", cf())

        return
Ejemplo n.º 10
0
    def set_ampl(self, state, amplitude=1):
        k_found = None

        for k, v in self.states.items():
            print(state, v)
            if state == v:
                k_found = k

                break

        Assert(k_found is not None, "w_0 is not set", cf())
        self.data[k_found] = amplitude
Ejemplo n.º 11
0
 def check_hermiticity(ro):
     Assert(self.is_hermitian(), "not hermitian", cf())
Ejemplo n.º 12
0
 def check_hermiticity(self):
     Assert(np.all(abs(self.data - self.data.getH()) < eps),
            "matrix is not hermitian", cf())
Ejemplo n.º 13
0
    def __init__(self, n, wc, wa, g):
        Assert(isinstance(n, int), "n is not integer", cf())
        Assert(isinstance(wc, (int, float)), "wc is not numeric", cf())
        Assert(isinstance(wa, list), "wa is not list", cf())
        Assert(isinstance(g, list), "g is not list", cf())

        Assert(n > 0, "n <= 0", cf())
        Assert(wc > 0, "wc <= 0", cf())

        Assert(len(wa) == n, "len(wa) != n", cf())

        for i in range(len(wa)):
            Assert(isinstance(wa[i], (int, float)),
                   "wa[" + str(i) + "] is not numeric", cf())
            Assert(wa[i] > 0, "wa[" + str(i) + "] <= 0", cf())

        Assert(len(g) == n, "len(g) != n", cf())

        for i in range(len(g)):
            Assert(isinstance(g[i], (int, float)),
                   "g[" + str(i) + "] is not numeric", cf())
            Assert(g[i] > 0, "g[" + str(i) + "] <= 0", cf())

        self.n = n

        self.wc = wc
        self.wa = wa

        self.g = g
Ejemplo n.º 14
0
    def __init__(self, capacity, cavity):
        Assert(isinstance(cavity, Cavity), "cavity is not Cavity", cf())
        Assert(isinstance(capacity, int), "capacity is not int", cf())
        Assert(capacity > 0, "capacity <=0", cf())

        self.cavity = cavity

        self.D = {}

        # ------------
        n = cavity.n

        wc = cavity.wc
        wa = cavity.wa
        g = cavity.g
        # ------------

        _min = min(capacity, n)

        self.states = {}

        # ---------------------------------------
        self.init_states(capacity, n)
        # ---------------------------------------

        self.size = len(self.states)
        self.matrix = Matrix(self.size, self.size, dtype=np.complex128)

        i = 1

        for i1 in range(0, _min + 1):
            for i2 in range(0, min(n, capacity - i1) + 1):
                j = 1

                for j1 in range(0, _min + 1):
                    for j2 in range(0, min(n, capacity - j1) + 1):
                        if i1 != j1:
                            p = [i1, j1]
                        elif i2 != j2:
                            p = [i2, j2]
                        else:
                            p = [1, 2]

                        mi = min(p[0], p[1])

                        kappa = sqrt((n - mi) * (mi + 1))

                        if abs(i1 - j1) + abs(i2 - j2) == 1:
                            _max = max(capacity - i1 - i2, capacity - j1 - j2)
                            self.matrix.data[i - 1, j - 1] = g * \
                                sqrt(max(capacity - i1 - i2,
                                         capacity - j1 - j2)) * kappa
                        elif abs(i1 - j1) + abs(i2 - j2) == 0:
                            self.matrix.data[
                                i - 1,
                                j - 1] = (capacity -
                                          (i1 + i2)) * wc + (i1 + i2) * wa
                        else:
                            self.matrix.data[i - 1, j - 1] = 0

                        j += 1

                i += 1