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 controlflow handling to CheckMap transpiler pass. #8793

Merged
merged 9 commits into from
Sep 30, 2022

Conversation

ewinston
Copy link
Contributor

Summary

Adds control flow handling to the CheckMap transpiler pass.

Details and comments

@ewinston ewinston requested a review from a team as a code owner September 26, 2022 16:31
@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

@coveralls
Copy link

coveralls commented Sep 26, 2022

Pull Request Test Coverage Report for Build 3155923356

  • 11 of 11 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.09%) to 84.647%

Totals Coverage Status
Change from base Build 3155916177: 0.09%
Covered Lines: 61404
Relevant Lines: 72541

💛 - 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 splitting this off - it's easier to review, and hopefully to get this in first. I have a couple of concerns about using control-flow constructs with inner bits not in the outer circuits. It would also be good to test that nested control-flow constructs are also properly checked (both for valid and invalid routes).

test/python/transpiler/test_check_map.py Outdated Show resolved Hide resolved
Comment on lines 72 to 76
layout = Layout(dict(enumerate(cf_instr.qargs)))
for block in cf_instr.op.blocks:
dag_block = circuit_to_dag(block)
mapped_dag = dag_block.copy_empty_like()
order = layout.reorder_bits(mapped_dag.qubits)
Copy link
Member

Choose a reason for hiding this comment

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

The two uses of Layout here I think assume that the inner control-flow block and the outer circuit contain the same Qubit instances. The control-flow builders will do this, but it's not a requirement of the classes. It's possible that it'll be neater/easier to refactor this pass a little so that the current run method becomes an inner _run(self, dag, wire_map) method, which is what we use for the recursive calls, where wire_map maps inner-block virtual qubits up to the outer physical bits they are stored on.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just made the blocks map reference the root register so there was no need to divert run. It seems to accommodate the cases you mentioned in this review.

Comment on lines 137 to 150
def test_swap_mapped_cf_layout_change_true(self):
"""Check control flow blocks with layout change are mapped."""
num_qubits = 3
coupling = CouplingMap([(i, i + 1) for i in range(num_qubits - 1)])
qr = QuantumRegister(3)
cr = ClassicalRegister(3)
circuit = QuantumCircuit(qr, cr)
true_body = QuantumCircuit(qr)
true_body.cx(0, 2)
circuit.if_else((cr[0], 0), true_body, None, qr[[1, 0, 2]], cr)
dag = circuit_to_dag(circuit)
pass_ = CheckMap(coupling)
pass_.run(dag)
self.assertTrue(pass_.property_set["is_swap_mapped"])
Copy link
Member

Choose a reason for hiding this comment

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

From the above comment: this test will fail if we instead did true_body = QuantumCircuit(3) (so used different Qubit instances), but it still should pass in that case. It would be good to have an explicit test that that works. Since we're looking to make this pass run after the layout analysis passes but before the "apply layout" stage, we can't guarantee that we'll be running this pass on a full physical circuit any more, so the control-flow blocks may not yet be normalised.

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.

This definitely looks better, thanks. One minor comment about a quadratic scaling, and I think there's a lint failure, but after that let's get it in.

qiskit/transpiler/passes/utils/check_map.py Outdated Show resolved Hide resolved
jakelishman
jakelishman previously approved these changes Sep 29, 2022
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.

I've fixed the lint, renamed a duplicate test and fixed the quadratic dependency issue. Looks fine to me now!

@jakelishman jakelishman added Changelog: New Feature Include in the "Added" section of the changelog automerge labels Sep 29, 2022
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.

Now I fixed the other part of lint that the CI had masked from the black failure. Should be good to merge now.

@mergify mergify bot merged commit 1ee6c6a into Qiskit:main Sep 30, 2022
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants