-
Notifications
You must be signed in to change notification settings - Fork 197
Add Driver helpers for variable lookup #1605
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ | |
| # under the License. | ||
|
|
||
| import abc | ||
| import functools | ||
| import importlib | ||
| import importlib.util | ||
| import json | ||
|
|
@@ -792,6 +793,26 @@ def list_available_variables( | |
| results = [Variable.from_node(n) for n in all_nodes] | ||
| return results | ||
|
|
||
| @functools.cached_property | ||
| def variables(self) -> dict[str, Variable]: | ||
| """Returns all variables in the graph keyed by name.""" | ||
| return { | ||
| node_name: Variable.from_node(node_) for node_name, node_ in self.graph.nodes.items() | ||
| } | ||
|
Comment on lines
+797
to
+801
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On second thought, if we are exposing |
||
|
|
||
| def get_variable(self, name: str) -> Variable: | ||
| """Returns a variable by name. | ||
|
|
||
| :param name: Name of the variable to return. | ||
| :return: Matching HamiltonNode. | ||
| :raises KeyError: If the variable does not exist in this Driver's graph. | ||
| """ | ||
| return self.variables[name] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could do build this straight from |
||
|
|
||
| def get_graph(self) -> graph_types.HamiltonGraph: | ||
| """Returns the public HamiltonGraph representation for this Driver.""" | ||
| return graph_types.HamiltonGraph.from_graph(self.graph) | ||
|
|
||
| @capture_function_usage | ||
| def display_all_functions( | ||
| self, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any particular reason for this to be cached? IMO, if the cache is required, would be better to have this as an internal variable explicitly instead of introducing hidden state