Commit 0b03cb3
* 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.
* Fix clippy comments
* Prefix types with GraphML
Suggested by @IvanIsCoding: Qiskit#1464 (comment)
* Black
* Add release notes
* Fix stubs error
* Add documentation
* Remove read_graphml_with_keys / write_graphml for single graph only
* Use `DictMap` everywhere
Suggested by @IvanIsCoding: Qiskit#1464 (comment)
* rustfmt and clippy
* Remove unused math module (ruff check)
* Use `from __future__ import annotations` for Python <3.10
* Add stub for `write_graphml`
* Remove `read_graphml_with_keys` from documentation
* Apply suggestions from code review
* Update rustworkx/__init__.py
* Apply suggestions from code review
---------
Co-authored-by: Ivan Carvalho <[email protected]>
1 parent 041cb69 commit 0b03cb3
File tree
8 files changed
+876
-74
lines changed- docs/source/api
- releasenotes/notes
- rustworkx
- src
- tests
8 files changed
+876
-74
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
11 | 12 | | |
12 | 13 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2279 | 2279 | | |
2280 | 2280 | | |
2281 | 2281 | | |
| 2282 | + | |
| 2283 | + | |
| 2284 | + | |
| 2285 | + | |
| 2286 | + | |
| 2287 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
163 | 163 | | |
164 | 164 | | |
165 | 165 | | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
166 | 169 | | |
167 | 170 | | |
168 | 171 | | |
| |||
662 | 665 | | |
663 | 666 | | |
664 | 667 | | |
| 668 | + | |
| 669 | + | |
| 670 | + | |
| 671 | + | |
| 672 | + | |
| 673 | + | |
| 674 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
68 | 68 | | |
69 | 69 | | |
70 | 70 | | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
71 | 95 | | |
72 | 96 | | |
73 | 97 | | |
| |||
685 | 709 | | |
686 | 710 | | |
687 | 711 | | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
| 715 | + | |
| 716 | + | |
| 717 | + | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
| 721 | + | |
| 722 | + | |
| 723 | + | |
| 724 | + | |
| 725 | + | |
688 | 726 | | |
689 | 727 | | |
690 | 728 | | |
| |||
0 commit comments