Code Monkey home page Code Monkey logo

Comments (3)

mrossinek avatar mrossinek commented on August 16, 2024

rustworkx graph cans actually store arbitrary data as payloads of their nodes and edges.
You cannot possibly cover all possible such payloads and expect the code to extract a weight from it.

That said, the payload which is supported (i.e. just a plain weight) should probably be documented properly. Right now, the only mention of this fact is here:

ValueError: If the graph edges are non-numeric.

Therefore, this is either a documentation issue or a feature request to support more types although I would not be sure how this would work in a general case because the Hamiltonian generators which use the lattice would need to be adapted to interpret non-numeric payloads, too.

from qiskit-nature.

MarcoBarroca avatar MarcoBarroca commented on August 16, 2024

This is relevant because networkx graphs only store weights as values in the dictionary and if Lattice() is given a NetworkX graphs it does the following:


if not isinstance(graph, PyGraph):
            _optionals.HAS_NETWORKX.require_now("Lattice construction from networkx.Graph")
            graph = networkx_converter(graph)
            

The network_converter() will keep the format networkx uses into the rustworkx graph.

I stumbled into this issue trying to take a weighted graph that was built elsewhere with NetworkX into a Lattice(). I had to manually convert it to rustworkx to make sure it wasn’t using a dictionary for the weights before building the lattice.

from qiskit-nature.

MarcoBarroca avatar MarcoBarroca commented on August 16, 2024

Here is an example.

G = nx.Graph()
edges=[[1,2],[0,3],[2,3],[0,1]]
for edge in edges:
    edge_1 = edge[0]
    edge_2 = edge[1]
    weight = 1.0
    G.add_edge(edge_1, edge_2,weight=weight)

general_lattice = Lattice(G)

Gives ValueError: Unsupported weight {'weight': 1.0} on edge with index 0.

To fix it we have to manually convert it to an adequately formatted rustworkx object. This works:

G = nx.Graph()
edges=[[1,2],[0,3],[2,3],[0,1]]
for edge in edges:
    edge_1 = edge[0]
    edge_2 = edge[1]
    weight = 1.0
    G.add_edge(edge_1, edge_2,weight=weight)

rG = rx.PyGraph(multigraph=G.is_multigraph())
nodes = list(G.nodes)
node_indices = dict(zip(nodes, rG.add_nodes_from(nodes)))
rG.add_edges_from(
        [(node_indices[x[0]], node_indices[x[1]], x[2]['weight']) for x in G.edges(data=True)]
    ) 


general_lattice = Lattice(rG)

from qiskit-nature.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.