Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve issue #1029 #1170

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Conversation

gluonhiggs
Copy link

@gluonhiggs gluonhiggs commented Apr 23, 2024

In fact, I do not want to merge my code straightforwardly, because I need some advice!

  1. I added the function minimum_cycle_basis and some tests. It works, but I believe it it need some modifications to be more well programmed, including the choice of having error handler or not.
  2. With the assumption that the Rust core of minimum_cycle_basis is working fine, we need to add a Python layer to wrap around it. And there are some errors in src/connectivity/mod.rs where we use #[pyfunction] annotation. I have no idea to fix this. Perhaps, to fix it, I need to modify even the minimum_cycle_basis.rs core in rustworkx-core/src/connectivity/.
  3. [for fun] By the way, I don't feel like this is a good first issue from 0 knowledge about Rust to 7 months developing this :D
    Link to the issue https://github.com/Qiskit/rustworkx/issues/1029

@CLAassistant
Copy link

CLAassistant commented Apr 23, 2024

CLA assistant check
All committers have signed the CLA.

Copy link
Collaborator

@IvanIsCoding IvanIsCoding left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will try to review this when I have time. I am sorry we mislabeled #1029, maybe we underestimated the effort to implement the algorithm?

Nevertheless, congrats on completing the work and submitting the PR! It is a big accomplishment

@gluonhiggs
Copy link
Author

gluonhiggs commented Apr 26, 2024

For convenience, I think I need to explain my idea why I perform the subgraph creation and the lifted graph construction as in the minimum_cycle_basis.rs so that you can find it easier to review.
Our ultimate goal is to return a (minimal) cycle basis which contains the information of the input graph nodes (i.e NodeIndex(n) for node n). Because in Rust, if we create a new graph which preserves some structure of another graph, we would have the NodeIndex() reset (this phenomenon doesn't happen in Python, and I can't think of any other way to overcome). Therefore, I have to use the name of the original nodes and pass them across the flow to track the node order. We manipulate the subgraphs, the lifted graphs and get the data, using the names to map afterwards to obtain the NodeIndex.

Copy link
Collaborator

@IvanIsCoding IvanIsCoding left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left some comments, but overall I think we will need to rewrite most of the hashmap usage because we should avoid the overhead of creating and hashing intermediate strings.

Also, you should replace A* with Dijkstra

IntoNodeIdentifiers, IntoNodeReferences, NodeIndexable, Visitable,
};
use petgraph::Undirected;
use std::fmt::Debug;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should remove std::fmt::Debug trait from here and the function signatures

Comment on lines 60 to 61
let source_name = format!("{}", graph.to_index(edge.source()));
let target_name = format!("{}", graph.to_index(edge.target()));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above about hashing

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering without the name, how can we know which node is which after putting these into calculations related to the subgraphs and the lifted graph. Imagine NodeIndex(1) in the original graph (user input), which might be NodeIndex(0) in a subgraph, and NodeIndex(2) in the lifted graph. I can only think of mapping these NodeIndex(i) from one graph with respect to another, and apparently we still need have to use HashMap. Does it sound good?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can create a custom struct to store data e.g. a pair of integers. And then implement the hash trait with derive https://doc.rust-lang.org/std/hash/trait.Hash.html#implementing-hash.

Strings contain at least 24 bits even if they are empty. So we should find better representations to store intermediate data in a more compact way. You don’t want string hashing to be the bottleneck of the algorithm

.map(|component| {
let mut subgraph = Graph::<String, i32>::new();
// Create map index to NodeIndex of the nodes in each component
let mut name_idx_map: HashMap<String, usize> = HashMap::new();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not necessary, you should be able to use the NodeId directly because it is hashable. Or use the usize as a hash

for &node_id in &component {
let node_index = graph.to_index(node_id);
// Create the name of the node in subgraph from the original graph
let node_name = format!("{}", node_index).trim_matches('"').to_string();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above about hashing

let mst = min_spanning_tree(&graph);
let mut mst_edges: Vec<(usize, usize)> = Vec::new();
for element in mst {
// println!("Element: {:?}", element);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the debug statements

graph: G,
orth: HashSet<(usize, usize)>,
mut weight_fn: F,
name_idx_map: &HashMap<String, usize>,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll need to propagate the hashing changes to _min_cycle as well

fn _min_cycle_basis<G, F, E>(
graph: G,
weight_fn: F,
name_idx_map: &HashMap<String, usize>,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll need to propagate the hashing changes to _min_cycle as well

for node in path {
let node_name = gi.node_weight(node).unwrap();
if node_name.contains("_lifted") {
let original_node_name = node_name.replace("_lifted", "");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you need to add additional information to the hash you can also hash tuples in Rust. Or you can come up with a custom struct and write a hasher for it

let nodeidx = NodeIndex::new(node);
let lifted_node = gi_name_to_node_index[&lifted_node_name];
let lifted_nodeidx = NodeIndex::new(lifted_node);
let result = astar(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably just be a call for dijkstra given the A* heuristic is a constant

@1ucian0 1ucian0 linked an issue May 14, 2024 that may be closed by this pull request
@gluonhiggs
Copy link
Author

I have added some modifications based on your advice. However, I don't think my approach using the new trait EdgeWeightToNumber is a good idea. I will try to generalize the edge weight type instead of forcefully using i32.

Comment on lines +921 to +950
#[pyfunction]
#[pyo3(text_signature = "(graph, /)")]
pub fn minimum_cycle_basis(py: Python, graph: &PyGraph) -> PyResult<Vec<Vec<usize>>> {
py.allow_threads(|| {
let result = connectivity::minimum_cycle_basis(&graph.graph);
match result {
Ok(min_cycle_basis) => {
// Convert Rust Vec<Vec<NodeIndex>> to Python Vec<Vec<usize>>
let py_min_cycle_basis = min_cycle_basis
.iter()
.map(|cycle| {
cycle
.iter()
.map(|&node_index| graph.graph.to_index(node_index))
.collect::<Vec<usize>>()
})
.collect::<Vec<Vec<usize>>>();
Ok(py_min_cycle_basis)
}
Err(e) => {
// Handle errors by converting them into Python exceptions
Err(PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!(
"An error occurred: {:?}",
e
)))
}
}
})
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My piece of advice is the rustworkx-core function looks good but we need to do some work on the Python bindings. I think we can just remove the Python bindings and add it later on

@IvanIsCoding
Copy link
Collaborator

I think the code is looking better now, the compiler is complaining about some unused variables + trait missing for Python but overall this was a big improvement! Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

minimal_cycle_basis
3 participants