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 a method to make a graph from a complex matrix #411

Closed
k-tamuraphys opened this issue Aug 16, 2021 · 2 comments · Fixed by #412
Closed

Add a method to make a graph from a complex matrix #411

k-tamuraphys opened this issue Aug 16, 2021 · 2 comments · Fixed by #412
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@k-tamuraphys
Copy link

What is the expected enhancement?

Add a method like from_complex_adjacency_matrix , for makiing a graph from an adjacency matrix whose elements can be complex values.
I expect this method to work as follows:

adjacency_matrix = np.array([
    [1.0, 1.0+1.0j, -2.0j], 
    [0.0, 2.0, 1.0+1.0j], 
    [0.0, 0.0, 3.0]
])

graph = retworkx.PyGraph.from_complex_adjacency_matrix(adjacency_matrix)
list(graph.weighted_edge_list())

output:

[(0, 0, 1.0),
 (0, 1, (1+1j)),
 (0, 2, (-0-2j)),
 (1, 1, 2.0),
 (1, 2, (1+1j)),
 (2, 2, 3.0)]

When the input matrix is not symmetric, it may be convenient to make a graph from the elements of the upper triangular part of it, similar to from_adjacency_matrix.

@mtreinish mtreinish added enhancement New feature or request good first issue Good for newcomers labels Aug 16, 2021
@mtreinish mtreinish modified the milestone: 0.10.0 Aug 16, 2021
@mtreinish
Copy link
Member

So I have a draft locally implementing this but the only thing in your example which I will not able to do is reproduce the types in the output edge list. In your example for the real numbers they're floats in the output weight list, but when retworkx looks at the underlying numpy array it will see a 3x3 array complex128 and in the retworkx from_complex_adjacency_matrix() that complex number will be added as a complex so it will be (1.0+0j) not 1.0.

Other than that the PR is ready to go however unfortunately it's blocked on PyO3/pyo3#1798 and won't work yet. I will push it up as a WIP for the time being (and it'll fail on the msrv tests). So unfortunately I don't think this will make 0.10.0 as it's blocked on a pyo3 release.

mtreinish added a commit to mtreinish/retworkx that referenced this issue Aug 16, 2021
This commit adds a new constructor method from_complex_adjacency_matrix
that will build a new graph from an adjacency matrix with complex
elements.

Fixes Qiskit#411
mtreinish added a commit to mtreinish/retworkx that referenced this issue Aug 16, 2021
This commit adds a new constructor method from_complex_adjacency_matrix
that will build a new graph from an adjacency matrix with complex
elements.

Fixes Qiskit#411
@k-tamuraphys
Copy link
Author

Thank you for the quick response. Having the output type (1.0+0j) would not matter to me.

@mtreinish mtreinish self-assigned this Aug 17, 2021
mtreinish added a commit to mtreinish/retworkx that referenced this issue Sep 3, 2021
This commit adds a new constructor method from_complex_adjacency_matrix
that will build a new graph from an adjacency matrix with complex
elements.

Fixes Qiskit#411
georgios-ts added a commit that referenced this issue Sep 10, 2021
* Add from complex matrix function

This commit adds a new constructor method from_complex_adjacency_matrix
that will build a new graph from an adjacency matrix with complex
elements.

Fixes #411

* Make from_adjacency_matrix generic and add PyGraph version

This commit updates the implementation of the from_adjacency_matrix()
functions to have a shared generic private function to be shared across
the per type variants. To accomplish this the IsNan trait from rust
nightly is copied into retworkx (so we can build with stable rust). If
in the future if an int type variant is added we'll need to implement
this trait for the int type (and just return false).

Co-authored-by: georgios-ts <[email protected]>

* Add tests

* Add release notes

* Fix copy paste issues in do strange

Co-authored-by: georgios-ts <[email protected]>

* Remove unused Zero trait bound on generic function

* Iterate over enumerate elements instead of index range

Co-authored-by: georgios-ts <[email protected]>

Co-authored-by: georgios-ts <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants