Ejemplo n.º 1
0
    def apply_swap(self, wire_index1, wire_index2):
        '''
        Method applying the swap gate to the quantum state \n
        @param wire_index1: Integer indicating location of gate \n
        @param wire_index2: Integer indicating location of gate \n
        @return: State changed via going through gate \n
        '''
        assert wire_index1 < self.qn or wire_index2 < self.qn, (
            'Input argument should be between wire 0 to ' + str(self.qn - 1))

        if self.qn == 2:
            self.state = SparseMatrix.dot(QG.SWAP, self.state)
        else:
            if wire_index1 < wire_index2:
                a = wire_index1
            else:
                a = wire_index2
            gate_list = []
            for i in range(self.qn - 1):
                if i == a:
                    gate_list.append(QG.SWAP)
                else:
                    gate_list.append(QG.eye)
            gate_M = gate_list[0]
            for i in range(1, self.qn - 1):
                gate_M = SparseMatrix.tensordot(gate_M, gate_list[i])
            self.state = SparseMatrix.dot(gate_M, self.state)
Ejemplo n.º 2
0
 def __init__(self):
     self.sparseMatrix = SparseMatrix()
     self.filename_entry = None
     self.matrix_length_entry = None
     self.matrix_density_entry = None
     self.filename = None
     self.res = None
Ejemplo n.º 3
0
 def store(self, distance_hash, n):
     self.order = n
     self._dbl_lvl_dict = _get_dbl_level_dict(distance_hash)
     sp_dijkstra = Dijkstra(self._dbl_lvl_dict, self.order)
     for i in range(self.order):
         self.ub_matrix.append([1] * self.order)
     for i in range(self.order):
         nodes = list(range(self.order))
         sp = sp_dijkstra.shortest_path(nodes, i)
         for index in range(self.order):
             # distance, node, parent
             self.ub_matrix[i][index] = sp[index][0]
             self.ub_matrix[index][i] = sp[index][0]
     self.search_started = [False] * n
     for i in range(n):
         self.lb_matrix.append([0] * n)
         if n - i - 1 != 0:
             self.uncalculated[i] = set(range(i + 1, n))
     for k in distance_hash.keys():
         x, y = k
         self.lb_matrix[x][y] = distance_hash[k]
         self.lb_matrix[y][x] = distance_hash[k]
         self.uncalculated[min(x, y)].remove(max(x, y))
         if len(self.uncalculated[min(x, y)]) == 0:
             del self.uncalculated[min(x, y)]
     self.sparse_matrix = SparseMatrix(distance_hash, n)
     if self.max_path_length == 2:
         for i in range(n):
             for j in range(i + 1, n):
                 self._update(i, j)
     else:
         for i in range(n):
             self._dfs(i)
Ejemplo n.º 4
0
 def __init__(self, file, detail):
     self.nodes = []
     self.elements = []
     self.temps = []
     self.K = SparseMatrix(None, True)
     self.F = []
     self.detail = detail
     self.read_mesh(file)
Ejemplo n.º 5
0
    def test_append(self):
        upper_half = SparseMatrix.build(
            np.matrix('10 0 0 0 -2 0; 3 9 0 0 0 3; 0 7 8 7 0 0'))
        lower_half = SparseMatrix.build(
            np.matrix('3 0 8 7 5 0; 0 8 0 9 9 13; 0 4 0 0 2 -1'))
        full_matrix = SparseMatrix.build(
            np.matrix(
                '10 0 0 0 -2 0; 3 9 0 0 0 3; 0 7 8 7 0 0; 3 0 8 7 5 0; 0 8 0 9 9 13; 0 4 0 0 2 -1'
            ))

        self.assertEqual(full_matrix,
                         SparseMatrix.concatenate(upper_half, lower_half))
Ejemplo n.º 6
0
    def test_wrap(self):
        val = [10, -2, 3, 9, 3, 7, 8, 7, 3, 8, 7, 5, 8, 9, 9, 13, 4, 2, -1]
        col_ind = [0, 4, 0, 1, 5, 1, 2, 3, 0, 2, 3, 4, 1, 3, 4, 5, 1, 4, 5]
        row_ptr = [0, 2, 5, 8, 12, 16, 19]
        matrix = np.matrix(
            '10 0 0 0 -2 0; 3 9 0 0 0 3; 0 7 8 7 0 0; 3 0 8 7 5 0; 0 8 0 9 9 13; 0 4 0 0 2 -1'
        )
        sparse = SparseMatrix.build(matrix)
        wrapped = SparseMatrix.wrap(val, col_ind, row_ptr)

        self.assertTrue(np.array_equal(sparse.val, wrapped.val))
        self.assertTrue(np.array_equal(sparse.col_ind, wrapped.col_ind))
        self.assertTrue(np.array_equal(sparse.row_ptr, wrapped.row_ptr))
Ejemplo n.º 7
0
 def apply_grover_oracle(self, marks):
     '''
     Method to apply grover oracle \n
     @param marks: Integer or list of location for oracle \n
     @return: Changed qubit state with grover oracle \n
     '''
     eye = np.eye(2**self.qn)
     oracle = eye
     if isinstance(marks, int):
         oracle[marks][marks] = -1
     else:
         for mark in marks:
             oracle[mark][mark] = -1
     oracle = SparseMatrix.sparsify(oracle)
     self.state = SparseMatrix.dot(oracle, self.state)
Ejemplo n.º 8
0
    def test_eq(self):
        m1 = SparseMatrix.build(
            np.matrix(
                '10 0 0 0 -2 0; 3 9 0 0 0 3; 0 7 8 7 0 0; 3 0 8 7 5 0; 0 8 0 9 9 13; 0 4 0 0 2 -1'
            ))
        m2 = SparseMatrix.build(
            np.matrix(
                '10 0 0 0 -2 0; 3 9 0 0 0 3; 0 7 8 7 0 0; 3 0 8 7 5 0; 0 8 0 9 9 13; 0 4 0 0 2 -1'
            ))
        m3 = SparseMatrix.build(
            np.matrix(
                '3 9 0 0 0 3; 0 7 8 7 0 0; 3 0 8 7 5 0; 0 8 0 9 9 13; 0 4 0 0 2 -1; 10 0 0 0 -2 0'
            ))

        self.assertEqual(m1, m2)
        self.assertNotEqual(m1, m3)
Ejemplo n.º 9
0
 def get_initial_state(self):
     '''
     Initialize the qubit state given by the number of qubits \n
     @return: Quantum state as a sparse matrix \n
     '''
     state = np.zeros(2**self.qn)
     state[0] = 1
     return SparseMatrix.sparsify(state.reshape(len(state), 1))
Ejemplo n.º 10
0
 def test_get(self):
     matrix = np.matrix(
         '10 0 0 0 -2 0; 3 9 0 0 0 3; 0 7 8 7 0 0; 3 0 8 7 5 0; 0 8 0 9 9 13; 0 4 0 0 2 -1'
     )
     sparse = SparseMatrix.build(matrix)
     rows, cols = matrix.shape
     for i in range(rows):
         for j in range(cols):
             self.assertEqual(sparse.get(i, j), matrix[i, j])
Ejemplo n.º 11
0
    def apply_pauliZ(self, wire_index):
        '''
        Method applying the Pauli Z gate to the quantum state \n
        @param wire_index: Integer indicating location of gate \n
        @return: State changed via going through gate \n
        '''
        assert -1 < wire_index < self.qn, (
            'Input argument should be between wire 0 to ' + str(self.qn - 1))

        if self.qn == 1:
            self.state = SparseMatrix.dot(QG.PZ, self.state)
        else:
            gate_list = []
            for i in range(self.qn):
                if i == wire_index:
                    gate_list.append(QG.PZ)
                else:
                    gate_list.append(QG.eye)
            gate_M = gate_list[0]
            for i in range(1, self.qn):
                gate_M = SparseMatrix.tensordot(gate_M, gate_list[i])
            self.state = SparseMatrix.dot(gate_M, self.state)
Ejemplo n.º 12
0
def split_tree(path_name):
    with open(os.path.join(path_name, "graph.json")) as fp:
        graph = json.load(fp)
    with open(os.path.join(path_name, "idx_map.json")) as fp:
        idx = json.load(fp)
    graph = {int(k): set(v) for k, v in graph.items()}
    if idx is not None:
        idx = {int(k): int(v) for k, v in idx.items()}
    sparse_matrix = SparseMatrix(graph, idx)
    left_child, right_child = sparse_matrix.split()
    print "graph split", path_name

    left_child.save(path_name + ".L")
    influencer(left_child, path_name + ".L")
    right_child.save(path_name + ".R")
    influencer(right_child, path_name + ".R")

    children_paths = []
    if left_child.dim > 200:
        children_paths.append(path_name + ".L")
    if right_child.dim > 200:
        children_paths.append(path_name + ".R")
    return children_paths
Ejemplo n.º 13
0
    def multiply(self, widget, data=None):
        if self.filename is None:
            dialog = Gtk.MessageDialog(
                None, 0, Gtk.MessageType.INFO, Gtk.ButtonsType.OK,
                "Please generate a sparse matrix first")
            dialog.run()
            dialog.destroy()
        else:
            vector_chooser = Gtk.FileChooserDialog(
                "Select vector file", None, Gtk.FileChooserAction.OPEN,
                (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_OPEN,
                 Gtk.ResponseType.OK))
            response = vector_chooser.run()
            if response == Gtk.ResponseType.OK:
                filename = vector_chooser.get_filename()

                with open(filename) as vector_file:
                    reader = csv.reader(vector_file, delimiter=' ')
                    vector = list(reader)
                    vector = np.array(vector).astype("float64")
                sparse_matrix = SparseMatrix()
                self.res = sparse_matrix.multiply(self.filename + "_CSR",
                                                  vector)
            vector_chooser.destroy()
Ejemplo n.º 14
0
    def apply_amplification(self):
        '''
        Method applying amplitude amplification of marked item \n
        @return: Changed qubit state via amplification of marked item \n
        '''
        s = np.zeros(2**self.qn)
        s[0] = 1
        s = SparseMatrix.sparsify(s.reshape(len(s), 1))
        gate_list = []
        for i in range(self.qn):
            gate_list.append(QG.H)

        H = gate_list[0]
        for i in range(1, self.qn):
            H = SparseMatrix.tensordot(H, gate_list[i])
        s = SparseMatrix.dot(H, s)
        s_T = SparseMatrix.transpose(s)
        s_s = SparseMatrix.tensordot(s, s_T)
        s_s_2 = SparseMatrix.multiply(s_s, 2)
        eye = SparseMatrix.sparsify(np.eye(2**self.qn))
        diffuser = SparseMatrix.minus(s_s_2, eye)
        self.state = SparseMatrix.dot(diffuser, self.state)
Ejemplo n.º 15
0
    def test_build(self):
        matrix = np.matrix(
            '10 0 0 0 -2 0; 3 9 0 0 0 3; 0 7 8 7 0 0; 3 0 8 7 5 0; 0 8 0 9 9 13; 0 4 0 0 2 -1'
        )
        val = [10, -2, 3, 9, 3, 7, 8, 7, 3, 8, 7, 5, 8, 9, 9, 13, 4, 2, -1]
        col_ind = [0, 4, 0, 1, 5, 1, 2, 3, 0, 2, 3, 4, 1, 3, 4, 5, 1, 4, 5]
        row_ptr = [0, 2, 5, 8, 12, 16, 19]
        sparse = SparseMatrix.build(matrix)

        self.assertEqual(len(sparse.row_ptr), len(row_ptr))
        for i in range(len(sparse.row_ptr)):
            self.assertEqual(sparse.row_ptr[i], row_ptr[i])

        self.assertEqual(len(sparse.col_ind), len(col_ind))
        for i in range(len(sparse.col_ind)):
            self.assertEqual(sparse.col_ind[i], col_ind[i], "%d" % i)

        self.assertEqual(len(sparse.val), len(val))
        for i in range(len(sparse.val)):
            self.assertEqual(sparse.val[i], val[i])
Ejemplo n.º 16
0
    def plot_pr(self):
        '''
        Plotting probabilities of qubit states
        @result: Bar graph of probabilities
        '''
        temp_x = range(1, 2**self.qn + 1)
        x = []
        for elem in temp_x:
            x.append(str(elem - 1))
        y = []

        ss = SparseMatrix.numpy(self.state)
        for i in range(ss.shape[0]):
            y.append((ss[i][0])**2)
        plt.style.use('seaborn')
        plt.bar(x, y, width=0.5)
        plt.tick_params(axis='both', labelsize=15)
        if self.qn > 4:
            plt.tick_params(axis='x', labelsize=10)
        plt.ylim(0, 1)
        plt.ylabel('Probability', fontsize=15)
        plt.xlabel('State', fontsize=15)
        plt.show()
Ejemplo n.º 17
0
def pipeline(args):
    '''
    Runs the model loop.

    If you wish to edit any of the parameters for the models, please edit the
    model_loop.py file directly.
    '''
    train_data = SparseMatrix()
    train_data.load_csv(args.train_filename)
    y_train = train_data.get(args.label).todense()
    X_train = train_data.get_all_except(args.label)
    y_train[y_train>1] = 1 # Remove multiclass
    y_train = np.array(np.reshape(y_train, y_train.shape[0]))[0] # Correct shape

    test_data = SparseMatrix()
    test_data.load_csv(args.test_filename)
    y_test = test_data.get(args.label).todense()
    X_test = test_data.get_all_except(args.label)
    y_test[y_test>1] = 1 # Remove multiclass
    y_test = np.array(np.reshape(y_test, y_test.shape[0]))[0] # Correct shape

    loop = ModelLoop(X_train, X_test, y_train, y_test, args.models,
                     args.iterations, args.run_name,
                     args.thresholds, args.label, float(args.comparison),
                     args.project_folder)
    loop.run()
Ejemplo n.º 18
0
 def show_state(self):
     '''
     Method to show current state in terminal \n
     '''
     print(SparseMatrix.numpy(self.state))
Ejemplo n.º 19
0
class FEM:
    def __init__(self, file, detail):
        self.nodes = []
        self.elements = []
        self.temps = []
        self.K = SparseMatrix(None, True)
        self.F = []
        self.detail = detail
        self.read_mesh(file)

    def read_mesh(self, file):
        with open(file) as fobj:
            xml = fobj.read().encode()
        objectify.fromstring(xml)
        dolfin = etree.fromstring(xml)
        mesh = dolfin.getchildren()[0]
        vertices = mesh.getchildren()[0]
        cells = mesh.getchildren()[1]
        for vertex in vertices.getchildren():
            self.nodes.append(
                Node(int(vertex.get('index')), float(vertex.get('x')),
                     float(vertex.get('y'))))
        for cell in cells.getchildren():
            self.elements.append(
                Element(int(cell.get('index')),
                        self.nodes[int(cell.get('v0'))],
                        self.nodes[int(cell.get('v1'))],
                        self.nodes[int(cell.get('v2'))]))

    def set_type_border(self, elem):
        """
        Description
        -----------
        If the element is on border defines its type: convective_heat_transfer, defined_T or heat_flow
        """
        is_border = False
        for i in range(len(self.detail.borders)):
            if i in [0, 1, 2, 3, 5]:
                type = 'convective_heat_transfer'
                if i in [3, 5]:
                    val = ALPHA1
                else:
                    val = ALPHA2
            elif i == 4:
                type = 'defined_T'
                val = T_DEF
            else:
                type = 'heat_flow'
                val = Q_DEF

            if elem.s1.is_in_line(
                    self.detail.borders[i]) and elem.s2.is_in_line(
                        self.detail.borders[i]):
                is_border = True
                border = '12'
                if type == 'defined_T':
                    self.nodes[elem.s1.index].t = val
                    self.nodes[elem.s2.index].t = val
            if elem.s2.is_in_line(
                    self.detail.borders[i]) and elem.s3.is_in_line(
                        self.detail.borders[i]):
                is_border = True
                border = '23'
                if type == 'defined_T':
                    self.nodes[elem.s2.index].t = val
                    self.nodes[elem.s3.index].t = val
            if elem.s3.is_in_line(
                    self.detail.borders[i]) and elem.s1.is_in_line(
                        self.detail.borders[i]):
                is_border = True
                border = '31'
                if type == 'defined_T':
                    self.nodes[elem.s3.index].t = val
                    self.nodes[elem.s1.index].t = val
            if is_border:
                elem.borders[border]['type'] = type
                elem.borders[border]['val'] = val
            is_border = False

    def define_border_conditions(self):
        for elem in self.elements:
            self.set_type_border(elem)

    def build_system(self):
        """
        Description
        -----------
        Forms system of equations to solve
        """
        self.K.shape = (len(self.nodes), len(self.nodes))
        self.F = np.zeros(len(self.nodes))
        for i, elem in enumerate(self.elements):
            k = elem.form_elem_matrix(Kxx, Kyy)
            for j in range(3):
                for r in range(3):
                    self.K.add(index=(elem.s[j].index, elem.s[r].index),
                               val=k[j][r])
            f = elem.form_vector_of_external_influences(
                self.detail.source_points)

            for j in range(3):
                self.F[elem.s[j].index] += f[j]

        for node in self.nodes:
            if node.t is not None:
                self.K.set(index=(node.index, node.index), val=1)
                self.F[node.index] = node.t
                for node_k in self.nodes:
                    if node_k.index != node.index:
                        self.K.set(index=(node.index, node_k.index), val=0)
                        self.F[node_k.index] -= self.K.get(
                            index=(node_k.index, node.index)) * node.t
                        self.K.set(index=(node_k.index, node.index), val=0)

    def solve_system(self):
        self.temps = solve(self.K, self.F)

    def get_info(self):
        print('mesh: {} nodes, {} elements'.format(len(self.nodes),
                                                   len(self.elements)))
        print('max temperature is {}'.format(np.max(self.temps)))
        print('min temperature is {}'.format(np.min(self.temps)))
        print('mean temperature is {}'.format(np.mean(self.temps)))

    def build_gradients(self):
        """
        Description
        -----------
        Builds gradients fields and view it
        """
        for elem in self.elements:
            for p in self.detail.source_points:
                if abs(p.x - elem.s1.x) < 0.0001 and abs(p.y -
                                                         elem.s1.y) < 0.0001:
                    elem.ps = True
                    break
                else:
                    elem.ps = False
            elem.grad = 1 / (2 * elem.A) * np.dot(
                np.array([[elem.b[0], elem.b[1], elem.b[2]],
                          [elem.c[0], elem.c[1], elem.c[2]]]),
                np.array([[self.temps[elem.s1.index]],
                          [self.temps[elem.s2.index]],
                          [self.temps[elem.s3.index]]]))
        w = 3
        X = np.array([(elem.s1.x + elem.s2.x + elem.s3.x) / 2
                      for elem in self.elements])
        Y = np.array([(elem.s1.y + elem.s2.y + elem.s3.y) / 2
                      for elem in self.elements])
        U = np.array([elem.grad[0][0] for elem in self.elements])
        V = np.array([elem.grad[1][0] for elem in self.elements])

        fig3, ax3 = plt.subplots()
        speed = np.sqrt(U**2 + V**2)
        Q = ax3.quiver(X, Y, U, V, speed, width=0.0008)
        ax3.quiverkey(Q,
                      0.9,
                      0.9,
                      2,
                      r'$2 \frac{m}{s}$',
                      labelpos='E',
                      coordinates='figure')
        ax3.scatter(X, Y, color='0.5', s=1.1)
        plt.gca().set_aspect('equal', adjustable='box')
        plt.show()

    def create_vtu(self, file):
        """
        Description
        -----------
        Forms vtu file for paraview vizualization
        """
        output = '<?xml version="1.0"?>\n<VTKFile type="UnstructuredGrid" version="0.1" >\n\t<UnstructuredGrid>'
        output += '\n\t\t<Piece NumberOfPoints="{}" NumberOfCells="{}">'.format(
            len(self.nodes), len(self.elements))
        components = ''
        for node in self.nodes:
            components += '{} {} 0 '.format(node.x, node.y)
        output += '\n\t\t<Points>\n\t\t\t<DataArray type="Float64" ' \
                  'NumberOfComponents="3" format="ascii">{}</DataArray>\n\t\t</Points>'.format(components)
        output += '\n\t\t<Cells>'
        connectivity = ''
        offsets = ''
        types = ''
        temps = ''

        for elem in self.elements:
            connectivity += '{} {} {} '.format(elem.s1.index, elem.s2.index,
                                               elem.s3.index)
        for i in range(len(self.elements)):
            offsets += '{} '.format((i + 1) * 3)
            types += '{} '.format(5)
        for t in self.temps:
            temps += '{} '.format(t)

        output += '\n\t\t\t<DataArray type="UInt32" Name="connectivity" format="ascii">{}</DataArray>'.format(
            connectivity)
        output += '\n\t\t\t<DataArray type="UInt32" Name="offsets" format="ascii">{}</DataArray>'.format(
            offsets)
        output += '\n\t\t\t<DataArray type="UInt8" Name="types" format="ascii">{}</DataArray>'.format(
            types)
        output += '\n\t\t</Cells>'
        output += '\n\t\t<PointData Scalars="T">\n\t\t\t<DataArray type="Float64" Name="T"' \
                  ' format="ascii">{}</DataArray>\n\t\t</PointData>'.format(temps)
        output += '\n\t\t</Piece>\n\t</UnstructuredGrid>\n</VTKFile>'

        f = open(file, "w+")
        f.write(output)
        f.close()
Ejemplo n.º 20
0
class SparseMatrixTab():
    def __init__(self):
        self.sparseMatrix = SparseMatrix()
        self.filename_entry = None
        self.matrix_length_entry = None
        self.matrix_density_entry = None
        self.filename = None
        self.res = None

    def get_sparse_tab(self):
        sparse_matrix_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)

        matrix_length_lbl = Gtk.Label("Matrix length - N")
        sparse_matrix_box.pack_start(matrix_length_lbl, True, True, 10)
        self.matrix_length_entry = Gtk.Entry()
        sparse_matrix_box.pack_start(self.matrix_length_entry, True, True, 10)

        matrix_density_lbl = Gtk.Label("Matrix density (0 - 1)")
        sparse_matrix_box.pack_start(matrix_density_lbl, True, True, 10)
        self.matrix_density_entry = Gtk.Entry()
        sparse_matrix_box.pack_start(self.matrix_density_entry, True, True, 10)

        image = Gtk.Image(stock=Gtk.STOCK_SAVE_AS)
        button1 = Gtk.Button(" Save matrix as", image=image)
        button1.connect("clicked", self.create_sparse_matrix)
        sparse_matrix_box.pack_start(button1, True, True, 10)

        operations_lbl = Gtk.Label("Operations: ")
        sparse_matrix_box.pack_start(operations_lbl, True, True, 10)

        button2 = Gtk.Button("Multiply")
        button2.connect("clicked", self.multiply)
        sparse_matrix_box.pack_start(button2, True, True, 10)

        button3 = Gtk.Button("Save result as")
        button3.connect("clicked", self.save_result)
        sparse_matrix_box.pack_start(button3, True, True, 10)

        return sparse_matrix_box

    def create_sparse_matrix(self, widget, data=None):
        matrix_length = self.matrix_length_entry.get_text()
        density = self.matrix_density_entry.get_text()

        if matrix_length == "" or density == "":
            dialog = Gtk.MessageDialog(
                None, 0, Gtk.MessageType.INFO, Gtk.ButtonsType.OK,
                "Please enter a length and density first")
            dialog.run()
            dialog.destroy()
        else:
            dialog = Gtk.FileChooserDialog(
                "Please choose a file", None, Gtk.FileChooserAction.SAVE,
                (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_SAVE,
                 Gtk.ResponseType.OK))

            Gtk.FileChooser.set_current_name(dialog, "matrix.txt")
            response = dialog.run()
            if response == Gtk.ResponseType.OK:
                self.filename = Gtk.FileChooser.get_filename(dialog)
                matrix_length = int(matrix_length)
                density = float(density)
                matrix_A, CSR_A, vector_b, vector_res = self.sparseMatrix.create_sparse_matrix(
                    self.filename, matrix_length, density)
                np.savetxt(self.filename + "_A",
                           matrix_A,
                           fmt="%1.9f",
                           delimiter=" ")
                np.savetxt(self.filename + "_b",
                           vector_b,
                           fmt="%1.9f",
                           delimiter=" ")
                np.savetxt(self.filename + "_res",
                           vector_res,
                           fmt="%1.9f",
                           delimiter=" ")
                file = open(self.filename + "_CSR", 'w')
                file.write(CSR_A)
                file.close()
            dialog.destroy()

    def multiply(self, widget, data=None):
        if self.filename is None:
            dialog = Gtk.MessageDialog(
                None, 0, Gtk.MessageType.INFO, Gtk.ButtonsType.OK,
                "Please generate a sparse matrix first")
            dialog.run()
            dialog.destroy()
        else:
            vector_chooser = Gtk.FileChooserDialog(
                "Select vector file", None, Gtk.FileChooserAction.OPEN,
                (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_OPEN,
                 Gtk.ResponseType.OK))
            response = vector_chooser.run()
            if response == Gtk.ResponseType.OK:
                filename = vector_chooser.get_filename()

                with open(filename) as vector_file:
                    reader = csv.reader(vector_file, delimiter=' ')
                    vector = list(reader)
                    vector = np.array(vector).astype("float64")
                sparse_matrix = SparseMatrix()
                self.res = sparse_matrix.multiply(self.filename + "_CSR",
                                                  vector)
            vector_chooser.destroy()

    def save_result(self, widget, data=None):
        if self.res is None:
            dialog = Gtk.MessageDialog(None, 0, Gtk.MessageType.INFO,
                                       Gtk.ButtonsType.OK,
                                       "Please multiply first")
            dialog.run()
            dialog.destroy()
        else:
            dialog = Gtk.FileChooserDialog(
                "Please choose a file", None, Gtk.FileChooserAction.SAVE,
                (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_SAVE,
                 Gtk.ResponseType.OK))

            Gtk.FileChooser.set_current_name(dialog, "result_CSR.txt")
            response = dialog.run()
            if response == Gtk.ResponseType.OK:
                vector_filename = dialog.get_filename()
                np.savetxt(vector_filename,
                           self.res,
                           fmt="%1.9f",
                           delimiter=" ")
            dialog.destroy()
Ejemplo n.º 21
0
"""
This script contains quantum gates in sparse matrix format
"""

import numpy as np
from sparse_matrix import SparseMatrix

eye = SparseMatrix.sparsify(np.eye(2))

PX = SparseMatrix.sparsify(np.array([[0, 1], [1, 0]]))

PZ = SparseMatrix.sparsify(np.array([[1, 0], [0, -1]]))

H = SparseMatrix.sparsify(
    np.array([[1 / np.sqrt(2), 1 / np.sqrt(2)],
              [1 / np.sqrt(2), -1 / np.sqrt(2)]]))
H_Shors = np.array([[1 / np.sqrt(2), 1 / np.sqrt(2)],
                    [1 / np.sqrt(2), -1 / np.sqrt(2)]])

SWAP = SparseMatrix.sparsify(
    np.array([[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1]]))
Ejemplo n.º 22
0
class ParamTriSearch:
    def __init__(self, max_path_length, sw_ub):
        self.max_path_length = max_path_length
        self.sw_ub = sw_ub
        self.sparse_matrix = None
        self.lb_matrix = []
        self.ub_matrix = []
        self.search_started = []
        self.uncalculated = {}
        assert self.max_path_length >= 2
        self.update_time = 0

    def lookup(self, x, y, fake=False):
        if fake:
            return [self.lb_matrix[x][y], self.ub_matrix[x][y]]
        if not (self.search_started[x] or self.search_started[y]):
            #self._bfs(x)
            self._update(x, y)
            self.search_started[x] = True
        return [self.lb_matrix[x][y], self.ub_matrix[x][y]]

    def store(self, distance_hash, n):
        self.order = n
        self._dbl_lvl_dict = _get_dbl_level_dict(distance_hash)
        sp_dijkstra = Dijkstra(self._dbl_lvl_dict, self.order)
        for i in range(self.order):
            self.ub_matrix.append([1] * self.order)
        for i in range(self.order):
            nodes = list(range(self.order))
            sp = sp_dijkstra.shortest_path(nodes, i)
            for index in range(self.order):
                # distance, node, parent
                self.ub_matrix[i][index] = sp[index][0]
                self.ub_matrix[index][i] = sp[index][0]
        self.search_started = [False] * n
        for i in range(n):
            self.lb_matrix.append([0] * n)
            if n - i - 1 != 0:
                self.uncalculated[i] = set(range(i + 1, n))
        for k in distance_hash.keys():
            x, y = k
            self.lb_matrix[x][y] = distance_hash[k]
            self.lb_matrix[y][x] = distance_hash[k]
            self.uncalculated[min(x, y)].remove(max(x, y))
            if len(self.uncalculated[min(x, y)]) == 0:
                del self.uncalculated[min(x, y)]
        self.sparse_matrix = SparseMatrix(distance_hash, n)
        if self.max_path_length == 2:
            for i in range(n):
                for j in range(i + 1, n):
                    self._update(i, j)
        else:
            for i in range(n):
                self._dfs(i)

    def update(self, edge, val):
        start = time.time()
        x, y = edge
        # try:
        #     assert not np.any(np.array(self.sw_ub) - np.array(self.ub_matrix) > 0.000001)
        # except AssertionError:
        #     print('Something hit me ')
        self.lb_matrix[x][y] = self.lb_matrix[y][x] = val
        self.ub_matrix[x][y] = self.ub_matrix[y][x] = val
        self.search_started = [False] * len(self.lb_matrix)
        self.uncalculated[min(x, y)].remove(max(x, y))
        if len(self.uncalculated[min(x, y)]) == 0:
            del self.uncalculated[min(x, y)]
        # try:
        #     assert not np.any(np.array(self.sw_ub) - np.array(self.ub_matrix) > 0.000001)
        # except:
        #     print('a')
        self._sw_lb_update(edge, val)
        # try:
        #     assert not np.any(np.array(self.sw_ub) - np.array(self.ub_matrix) > 0.000001)
        # except AssertionError:
        #     print('Something hit me ')
        end = time.time()
        self.update_time += end - start

    def _bfs(self, node):
        queue = []
        # (node_index, max, path_length)
        last = (node, 0, 0)
        queue.append(last)
        cur_path_length = 0
        while len(queue) > 0:
            head = queue.pop(0)
            distance, neighbours = self.sparse_matrix.get_row_data(head[0])
            for i in range(len(neighbours)):
                cur_node = neighbours[i]
                max_val = max(head[1], distance[i])
                path_val = head[2] + distance[i]
                self.lb_matrix[node][cur_node] = self.lb_matrix[cur_node][
                    node] = max(self.lb_matrix[cur_node][node],
                                2 * max_val - path_val)
                queue.append((cur_node, max_val, path_val))
            if head == last:
                if cur_path_length == self.max_path_length or len(queue) == 0:
                    return
                else:
                    last = queue[-1]
                    cur_path_length += 1

    def _update(self, x, y):
        if self.max_path_length > 2:
            self._dfs(x)
        else:
            self._calculate(x, y)

    def _calculate(self, x, y):
        _, n1 = self.sparse_matrix.get_row_data(x)
        _, n2 = self.sparse_matrix.get_row_data(y)
        common = set(n1).intersection(set(n2))
        for c in common:
            d1 = self.sparse_matrix.get_element(x, c)
            d2 = self.sparse_matrix.get_element(y, c)
            if d1 > d2:
                _max = d1
                _min = d2
            else:
                _max = d2
                _min = d1
            self.lb_matrix[x][y] = self.lb_matrix[x][y] = max(
                self.lb_matrix[x][y], _max - _min)

    def _dfs(self, node):
        self._dfs_recursive(node, node, 0, 0, {node}, 0)

    def _dfs_recursive(self, start_node, node, max_edge, path_length, visited,
                       depth):
        distance, neighbours = self.sparse_matrix.get_row_data(node)
        for i in range(len(neighbours)):
            cur_node = neighbours[i]
            if cur_node in visited:
                continue
            max_val = max(max_edge, distance[i])
            path_val = path_length + distance[i]
            self.lb_matrix[start_node][cur_node] = self.lb_matrix[cur_node][
                start_node] = max(self.lb_matrix[start_node][cur_node],
                                  2 * max_val - path_val)
            new_visited = visited.union({cur_node})
            if depth < self.max_path_length:
                self._dfs_recursive(start_node, cur_node, max_val, path_val,
                                    new_visited, depth + 1)

    def is_uncalculated(self, x, y):
        return min(x, y) in self.uncalculated and max(
            x, y) in self.uncalculated[min(x, y)]

    def _sw_lb_update(self, edge, d):
        x, y = edge
        for i, inds in self.uncalculated.items():
            for j in inds:
                self.ub_matrix[i][j] = self.ub_matrix[j][i] = min(
                    self.ub_matrix[j][i],
                    self.ub_matrix[i][x] + d + self.ub_matrix[y][j],
                    self.ub_matrix[i][y] + d + self.ub_matrix[x][j])
Ejemplo n.º 23
0
        stemmed_sub = stem(line_split[2])
        if stemmed_sub not in entity2idx:
            continue
        sub_idx = entity2idx[stemmed_sub]
        stemmed_obj = stem(line_split[4])
        if stemmed_obj not in entity2idx:
            continue
        obj_idx = entity2idx[stemmed_obj]
        graph[sub_idx].add(obj_idx)
        graph[obj_idx].add(sub_idx)
        counter += 1
        if counter % 10000 == 0:
            print counter, "edges added"
print "graph pre-loaded"

root = SparseMatrix(graph, {i: i for i in idx2entity})
root.save("test/root")
influencer(root, "test/root")
print "graph constructed"


def split_tree(path_name):
    with open(os.path.join(path_name, "graph.json")) as fp:
        graph = json.load(fp)
    with open(os.path.join(path_name, "idx_map.json")) as fp:
        idx = json.load(fp)
    graph = {int(k): set(v) for k, v in graph.items()}
    if idx is not None:
        idx = {int(k): int(v) for k, v in idx.items()}
    sparse_matrix = SparseMatrix(graph, idx)
    left_child, right_child = sparse_matrix.split()