Skip to content

Get nodes/variables from driver by name #1138

@Riezebos

Description

@Riezebos

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])

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions