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 function to swap connected nodes in DAGCircuit #9160

Merged
merged 7 commits into from
Apr 17, 2023

Conversation

ewinston
Copy link
Contributor

@ewinston ewinston commented Nov 17, 2022

Summary

Sometimes when doing circuit optimization it is useful to be able to swap two connected gates for instance when they commute. If the two gates are on the same qubits this can be done with the existing method substitute_node, however when the qubits are disjoint this can't be used. This pr adds a method swap_nodes which allows this operation.

Details and comments

@ewinston ewinston requested a review from a team as a code owner November 17, 2022 18:54
@qiskit-bot
Copy link
Collaborator

Thank you for opening a new pull request.

Before your PR can be merged it will first need to pass continuous integration tests and be reviewed. Sometimes the review process can be slow, so please be patient.

While you're waiting, please feel free to review other open PRs. While only a subset of people are authorized to approve pull requests for merging, everyone is encouraged to review open pull requests. Doing reviews helps reduce the burden on the core team and helps make the project's code better for everyone.

One or more of the the following people are requested to review this:

  • @Qiskit/terra-core

Copy link
Member

@jakelishman jakelishman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have a particular case / transpiler pass in mind for when you want this? There's a general hope (cc: @alexanderivrii) that we might be able to use DAGDependency as our principle driver type for dealing with commutation relations, though that's likely to be a long way off. If this is useful in the near-term, it generally seems like a useful helper.

Don't forget the release note.

qiskit/dagcircuit/dagcircuit.py Outdated Show resolved Hide resolved
qiskit/dagcircuit/dagcircuit.py Outdated Show resolved Hide resolved
qiskit/dagcircuit/dagcircuit.py Outdated Show resolved Hide resolved
edge_parent = self._multi_graph.find_predecessors_by_edge(node1_id, edge_find)[0]
self._multi_graph.remove_edge(edge_parent._node_id, node1_id)
self._multi_graph.add_edge(edge_parent._node_id, node2_id, edge)
edge_child = self._multi_graph.find_successors_by_edge(node2_id, lambda x: x == edge)[0]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if there's an equivalent for predecessors, but this method could be find_adjacent_node_by_edge to avoid calculating the whole list.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you make find_adjacent_node_by_edge filter on predecessor or successor?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a little buried in the documentation, but it only finds successors - that's why I wasn't sure if there's an equivalent for the predecessors.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Predecessors doesn't exist but there is a PR to add it,
Qiskit/rustworkx#756

qiskit/extensions/quantum_initializer/uc.py Outdated Show resolved Hide resolved
test/python/dagcircuit/test_dagcircuit.py Outdated Show resolved Hide resolved
test/python/dagcircuit/test_dagcircuit.py Outdated Show resolved Hide resolved
test/python/dagcircuit/test_dagcircuit.py Outdated Show resolved Hide resolved
@coveralls
Copy link

coveralls commented Nov 29, 2022

Pull Request Test Coverage Report for Build 3950153453

  • 18 of 19 (94.74%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.002%) to 84.876%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit/dagcircuit/dagcircuit.py 18 19 94.74%
Totals Coverage Status
Change from base Build 3950148285: 0.002%
Covered Lines: 66103
Relevant Lines: 77882

💛 - Coveralls

Copy link
Member

@jakelishman jakelishman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the new tests. Aside from my wanting to shift the unrelated visualisation-related changes to a different PR, I think this is fine to merge.

Comment on lines 1831 to 1860
def draw(self, scale=0.7, filename=None, style="color"):
def draw(self, scale=0.7, filename=None, style="color", **kwargs):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we shift this to a different PR? It seems unrelated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed in ff987f1

qiskit/visualization/dag_visualization.py Outdated Show resolved Hide resolved
@ewinston ewinston modified the milestones: 0.24.0, 0.23.2 Feb 8, 2023
@mtreinish mtreinish modified the milestones: 0.23.2, 0.24.0 Feb 14, 2023
@mtreinish mtreinish self-assigned this Apr 11, 2023
@kdk kdk added Changelog: New Feature Include in the "Added" section of the changelog mod: transpiler Issues and PRs related to Transpiler labels Apr 17, 2023
@kdk kdk added this pull request to the merge queue Apr 17, 2023
Merged via the queue into Qiskit:main with commit 212b1b5 Apr 17, 2023
king-p3nguin pushed a commit to king-p3nguin/qiskit-terra that referenced this pull request May 22, 2023
* add function to swap connected nodes in DAGCircuit

* linting. remove multiplexor test.

The multiplexor partial swap wasn't passing. Not sure this is rerlated
to this pr.

* Update qiskit/dagcircuit/dagcircuit.py

Co-authored-by: Jake Lishman <jake@binhbar.com>

* build dagcircuit directly in tests

* add spectator ops to swap tests

* add release notes

* remove dag visualization changes from this pr

---------

Co-authored-by: Jake Lishman <jake@binhbar.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: New Feature Include in the "Added" section of the changelog mod: transpiler Issues and PRs related to Transpiler
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants