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

Add binomial tree graph generator #299

Merged
merged 40 commits into from
May 21, 2021
Merged

Conversation

nahumsa
Copy link
Contributor

@nahumsa nahumsa commented Apr 1, 2021

Related to #150

Add binomial tree graph for a given order.

@coveralls
Copy link

coveralls commented Apr 1, 2021

Pull Request Test Coverage Report for Build 863756254

  • 135 of 135 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.07%) to 96.849%

Totals Coverage Status
Change from base Build 858086935: 0.07%
Covered Lines: 7591
Relevant Lines: 7838

💛 - Coveralls

Comment on lines 1036 to 1038
if order.is_none() {
return Err(PyIndexError::new_err("order should be specified"));
}
Copy link
Member

Choose a reason for hiding this comment

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

Is there a reason to make this optional if it's always required? Couldn't we change the order argument to be: order: usize, above and just remove this?

Copy link
Contributor Author

@nahumsa nahumsa Apr 1, 2021

Choose a reason for hiding this comment

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

Sure. I was thinking to use weights to initialize as well, but was having some issues with rust's Option.
However, not making order optional would work.

@nahumsa
Copy link
Contributor Author

nahumsa commented Apr 7, 2021

I ran some benchmarks comparing to networkx:

import time
import retworkx as rx
import networkx as nx 


libraries = ["retworkx", "networkx"] # change for each branch
sizes = [5, 10, 20]

start = 0
stop = 0

for lib in libraries:
    
    for N in sizes:
        if lib == "retworkx":
            start = time.time()
            graph = rx.generators.binomial_tree_graph(N)
            stop = time.time()
        
        if lib == "networkx":
            start = time.time()
            graph = nx.generators.binomial_tree(N)
            stop = time.time()

        print(f"N = {N} {lib}: {str(stop - start)}")
Library N=5 N=10 N=20
retworkx 1.5e-05s 0.000129s 0.076s
networkx 0.0198s 0.0075s 5.8s

src/generators.rs Outdated Show resolved Hide resolved
src/generators.rs Outdated Show resolved Hide resolved
src/generators.rs Show resolved Hide resolved
tests/generators/test_binomial_tree.py Show resolved Hide resolved
for n in range(10):
graph = retworkx.generators.binomial_tree_graph(n)
self.assertEqual(len(graph), 2**n)
self.assertEqual(len(graph.edges()), 2**n - 1)
Copy link
Member

Choose a reason for hiding this comment

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

Can we add some assertions here on the edge list. It would be good to assert that the output graph is actually a binomial tree instead of just a graph with 2**n nodes and 2**n - 1 edges

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added assertions to check if the edges match what is expected for a binomial tree graph

tests/generators/test_binomial_tree.py Outdated Show resolved Hide resolved
tests/generators/test_binomial_tree.py Outdated Show resolved Hide resolved
nahumsa and others added 10 commits May 4, 2021 10:15
Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
`find_edge`makes sure that you don't add repeated edges on the graph
@mtreinish mtreinish added this to the 0.9.0 milestone May 18, 2021
Copy link
Member

@mtreinish mtreinish left a comment

Choose a reason for hiding this comment

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

Looks good, just a couple of quick inline comments. I think this is almost ready.

src/generators.rs Outdated Show resolved Hide resolved
src/generators.rs Outdated Show resolved Hide resolved
src/generators.rs Outdated Show resolved Hide resolved
order: u32,
weights: Option<Vec<PyObject>>,
bidirectional: bool,
) -> PyResult<digraph::PyDiGraph> {
Copy link
Member

Choose a reason for hiding this comment

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

Is there a reason to not do a multigraph flag here too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not really, I just did this because all other directed generators have this pattern. Should we add multigraph on all of them?

Copy link
Member

Choose a reason for hiding this comment

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

Ah, I didn't realize it was missing as an option from all the digraph generators. I think then it's fine to exclude it here for now, but we should push a follow up PR and add a multigraph option to all the digraph generators.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, I'll raise an issue and submit a PR for that

nahumsa and others added 3 commits May 21, 2021 08:52
Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
Copy link
Member

@mtreinish mtreinish left a comment

Choose a reason for hiding this comment

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

LGTM, I found a couple more places to use is_none() but other than that I think this is ready.

src/generators.rs Outdated Show resolved Hide resolved
src/generators.rs Outdated Show resolved Hide resolved
src/generators.rs Outdated Show resolved Hide resolved
@mtreinish mtreinish merged commit 8b86b73 into Qiskit:main May 21, 2021
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.

3 participants