Skip to content

Commit 39e867a

Browse files
Fix Qiskit#840: Add GraphML serializer
This commit adds a GraphML serializer: ```python def write_graphml( graphs: list[PyGraph | PyDiGraph], keys: list[tuple[str, Domain, str, Type, Any]], path: str, /, compression: str | None = ..., ) -> None: ... ``` `keys` is a list of tuples: id, domain, name of the key, type, and default value. This commit also introduces the `read_graphml_with_keys` function, which returns the key definitions in the same format, along with the list of parsed graphs. The implementation preserves the ids of graphs, nodes, and edges when possible. If some ids conflict, fresh ids are generated in the written GraphML file. The `read_graphml` function has also been updated to store the graph id in the graph attributes, just like node and edge ids are stored in the corresponding attributes. The `write_graphml` function supports gzip compression, as does `read_graphml`. Note that the JSON node-link serializer (the other part of Qiskit#840) was already implemented in Qiskit#1091. Compared to Qiskit#1462: - Keys are passed explicitly instead of being inferred (which allows to use the types `float` and `int`, and to use default values); - Attributes for graphs, nodes, and edges are taken from the weight of elements, instead of relying on callbacks. This allows write_graphml to act as a proper reciprocal of read_graphml. Round-trip tests have been added. - IDs are taken from attributes when possible, instead of being generated from indices. - Multiple graphs can be written to the same file. - Gzip compression is supported. - Tests have been added. Regarding @IvanIsCoding's comment (Qiskit#1462 (comment)), about using https://github.com/jonasbb/petgraph-graphml: - Rustworkx's `graphml.rs` introduces an internal `Graph` data structure, which is used for `read_graphml`. It is natural to have `write_graphml` rely on the same data structure. - `petgraph-graphml` generates ids from indices, which prevents us from preserving ids accross the `read_graphml`/`write_graphml` round trip.
1 parent 408397f commit 39e867a

File tree

4 files changed

+741
-57
lines changed

4 files changed

+741
-57
lines changed

rustworkx/rustworkx.pyi

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,20 @@ class ColoringStrategy:
6868
Saturation: Any
6969
IndependentSet: Any
7070

71+
@final
72+
class Domain:
73+
Node: Domain
74+
Edge: Domain
75+
Graph: Domain
76+
All: Domain
77+
78+
@final
79+
class Type:
80+
Boolean: Type
81+
Int: Type
82+
Float: Type
83+
Long: Type
84+
7185
# Cartesian product
7286

7387
def digraph_cartesian_product(
@@ -680,11 +694,23 @@ def directed_random_bipartite_graph(
680694

681695
# Read Write
682696

697+
def read_graphml_with_keys(
698+
path: str,
699+
/,
700+
compression: str | None = ...,
701+
) -> tuple[list[tuple[str, Domain, str, Type, Any]], list[PyGraph | PyDiGraph]]: ...
683702
def read_graphml(
684703
path: str,
685704
/,
686705
compression: str | None = ...,
687706
) -> list[PyGraph | PyDiGraph]: ...
707+
def write_graphml(
708+
graphs: list[PyGraph | PyDiGraph],
709+
keys: list[tuple[str, Domain, str, Type, Any]],
710+
path: str,
711+
/,
712+
compression: str | None = ...,
713+
) -> None: ...
688714
def digraph_node_link_json(
689715
graph: PyDiGraph[_S, _T],
690716
/,

0 commit comments

Comments
 (0)