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

Get nodes/variables from driver by name #1138

Open
Riezebos opened this issue Sep 12, 2024 · 2 comments
Open

Get nodes/variables from driver by name #1138

Riezebos opened this issue Sep 12, 2024 · 2 comments

Comments

@Riezebos
Copy link
Contributor

Is your feature request related to a problem? Please describe.
This is a minor inconvenience, but currently if I'd want to get a node from the driver by name, I'd have to do:

node = next(n for n in dr.list_available_variables() if node.name == "the_node_i_want")

Describe the solution you'd like
A simple way to get a node by name, for example:

node = dr.get_variable("the_node_i_want")

or

node = dr.variables["the_node_i_want"]

The bottom option seems slightly nicer as in an interactive session dr.variables could be called on its own first to have a look at all the names and then called with the correct name.

Additional context
I looked at the code and a potential solution for the above would be to add the following to the Driver class:

    @functools.cached_property
    def variables(self) -> dict[str, Variable]:
        """Returns a dictionary of all the variables (HamiltonNodes) in the graph."""
        return {k: Variable.from_node(v) for k, v in self.graph.nodes.items()}

or

    def get_variable(self, name: str) -> Variable:
        """Returns a variable (HamiltonNode) by name."""
        return Variable.from_node(self.graph.nodes[name])
@zilto
Copy link
Collaborator

zilto commented Sep 16, 2024

Linking this relevant issue on have a __getitem__ on the Driver or Driver.graph: #836

And this PR that added __getitem__ on the HamiltonGraph: #919

@elijahbenizzy
Copy link
Collaborator

Adding on this -- get the HamiltonGraph object from the driver as well

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

No branches or pull requests

3 participants