def test_cycle_basis_multiple_roots_same_cycles(self): res = sorted(sorted(x) for x in retworkx.cycle_basis(self.graph, 0)) self.assertEqual(res, [[0, 1, 2, 3], [0, 1, 6, 7, 8], [0, 3, 4, 5]]) res = sorted(sorted(x) for x in retworkx.cycle_basis(self.graph, 1)) self.assertEqual(res, [[0, 1, 2, 3], [0, 1, 6, 7, 8], [0, 3, 4, 5]]) res = sorted(sorted(x) for x in retworkx.cycle_basis(self.graph, 9)) self.assertEqual(res, [[0, 1, 2, 3], [0, 1, 6, 7, 8], [0, 3, 4, 5]])
def vacuum_operator(fer_op): """Use the stabilizers to find the vacuum state in bravyi_kitaev_fast. Args: fer_op (FermionicOperator): the fermionic operator in the second quantized form Returns: WeightedPauliOperator: the qubit operator """ edge_list = bravyi_kitaev_fast_edge_list(fer_op) num_qubits = edge_list.shape[1] vac_operator = WeightedPauliOperator(paulis=[[1.0, Pauli.from_label('I' * num_qubits)]]) graph = retworkx.PyGraph() graph.extend_from_edge_list(list(map(tuple, edge_list.transpose()))) stabs = np.asarray(retworkx.cycle_basis(graph)) for stab in stabs: a_op = WeightedPauliOperator(paulis=[[1.0, Pauli.from_label('I' * num_qubits)]]) stab = np.asarray(stab) for i in range(np.size(stab)): a_op = a_op * edge_operator_aij(edge_list, stab[i], stab[(i + 1) % np.size(stab)]) * 1j # a_op.scaling_coeff(1j) a_op += WeightedPauliOperator(paulis=[[1.0, Pauli.from_label('I' * num_qubits)]]) vac_operator = vac_operator * a_op * np.sqrt(2) # vac_operator.scaling_coeff() return vac_operator
def test_cycle_basis_disconnected_graphs(self): self.graph.add_nodes_from(["A", "B", "C"]) self.graph.add_edges_from_no_data([(10, 11), (10, 12), (11, 12)]) cycles = retworkx.cycle_basis(self.graph, 9) res = sorted(sorted(x) for x in cycles[:-1]) + [sorted(cycles[-1])] self.assertEqual( res, [[0, 1, 2, 3], [0, 1, 6, 7, 8], [0, 3, 4, 5], [10, 11, 12]])
def test_cycle_basis(self): graph = retworkx.PyGraph() graph.add_nodes_from(list(range(6))) graph.add_edges_from_no_data([(0, 1), (0, 3), (0, 5), (1, 2), (2, 3), (3, 4), (4, 5)]) res = sorted(sorted(c) for c in retworkx.cycle_basis(graph, 0)) self.assertEqual([[0, 1, 2, 3], [0, 3, 4, 5]], res)
def test_equal_distance_graph(self): n = 3 graph = retworkx.PyGraph() graph.add_nodes_from(range(n + 5)) graph.add_edges_from([ (n, n + 1, 0.5), (n, n + 2, 0.5), (n + 1, n + 2, 0.5), (n, n + 3, 0.5), (n + 1, n + 4, 0.5), ]) graph.add_edges_from([(i, n + 2, 2) for i in range(n)]) terminals = list(range(5)) + [n + 3, n + 4] tree = retworkx.steiner_tree(graph, terminals, weight_fn=float) # Assert no cycle self.assertEqual(retworkx.cycle_basis(tree), []) expected_edges = [ (3, 4, 0.5), (4, 5, 0.5), (3, 6, 0.5), (4, 7, 0.5), (0, 5, 2), (1, 5, 2), (2, 5, 2), ] self.assertEqual(tree.weighted_edge_list(), expected_edges)
def stabilizers(fer_op): """ stabilizers """ edge_list = bravyi_kitaev_fast_edge_list(fer_op) num_qubits = edge_list.shape[1] graph = retworkx.PyGraph() graph.extend_from_edge_list(list(map(tuple, edge_list.transpose()))) stabs = np.asarray(retworkx.cycle_basis(graph)) stabilizer_ops = [] for stab in stabs: a_op = WeightedPauliOperator(paulis=[[1.0, Pauli.from_label('I' * num_qubits)]]) stab = np.asarray(stab) for i in range(np.size(stab)): a_op = a_op * edge_operator_aij(edge_list, stab[i], stab[(i + 1) % np.size(stab)]) * 1j stabilizer_ops.append(a_op) return stabilizer_ops
def test_self_loop(self): self.graph.add_edge(1, 1, None) res = sorted(sorted(c) for c in retworkx.cycle_basis(self.graph, 0)) self.assertEqual([[0, 1, 2, 3], [0, 1, 6, 7, 8], [0, 3, 4, 5], [1]], res)
def test_invalid_types(self): digraph = retworkx.PyDiGraph() with self.assertRaises(TypeError): retworkx.cycle_basis(digraph)