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

Transpiler updates for optional registers. #5515

Merged

Conversation

kdk
Copy link
Member

@kdk kdk commented Dec 11, 2020

Summary

Following #5504 updates transpiler and in-tree passes to support optional registers, and to prefer inspecting bits over registers where possible.

Details and comments

on-hold until #5504 .

Commits in this PR (without #5504, #5498 and #5486): https://github.com/Qiskit/qiskit-terra/pull/5515/files/51db5270a21a98ca408ad9eca470b715b92639d0..cdfd7ddbbe7542d949426b7c90a7ac723e202569

WIP:

  • Skips test.python.compiler.test_transpiler.TestTranspile.test_wrong_initial_layout which raises a KeyError from Layout instead of the expected DAGCircuitError (Resolved by Better initial_layout validation #5526 .)
  • Some cases where we build qubit_indices might be better as Layout.generate_trivial_layout

@kdk kdk added on hold Can not fix yet optional-registers labels Dec 11, 2020
@kdk kdk added this to the 0.17 milestone Dec 11, 2020
@kdk kdk added this to To do in Circuits via automation Dec 11, 2020
@kdk kdk force-pushed the quantumcircuit-optional-registers-transpiler branch from cdfd7dd to 46e8fa7 Compare January 15, 2021 23:47
@kdk kdk force-pushed the quantumcircuit-optional-registers-transpiler branch from 46e8fa7 to c7ef6fc Compare January 20, 2021 22:48
@kdk kdk force-pushed the quantumcircuit-optional-registers-transpiler branch from c7ef6fc to 68afca2 Compare March 1, 2021 20:21
@kdk kdk removed the on hold Can not fix yet label Mar 2, 2021
Copy link
Member

@mtreinish mtreinish left a comment

Choose a reason for hiding this comment

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

This LGTM a couple small comments and questions inline. The big takeaway for me though is just like in the previous PR we basically are reconstructing a qubit indices dict for anywhere there was index based access before. I know we're already tracking a follow up where we move that to the QuantumCircuit, but it might make sense to copy that to the dag metadata as well in that follow up so we don't need to reconstruct it in all the passes.

@@ -31,7 +31,8 @@ cdef class NLayout:


cpdef NLayout nlayout_from_layout(object layout,
object dag,
object dag,
Copy link
Member

Choose a reason for hiding this comment

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

Is there a reason this has a different name than the implementation in utils.pyx? I probably would make this also typed as a dict. So something like:

Suggested change
object dag,
dict qubit_indices,

Copy link
Member Author

Choose a reason for hiding this comment

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

Agree, added in 23a6597 .

@@ -162,32 +162,27 @@ cdef class NLayout:
return out


cpdef NLayout nlayout_from_layout(object layout, object qregs,
cpdef NLayout nlayout_from_layout(object layout,
object qubit_indices,
Copy link
Member

Choose a reason for hiding this comment

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

I would make this a dict (assuming the signature declaration in the pxd is updated to match:

Suggested change
object qubit_indices,
dict qubit_indices,

I don't think it matters too much, but feels like explicitly typing the argument to always be a dict makes the most sense instead of taking any object. (I expect it was object before because an OrderedDict was used for python 3.5 compat)

Copy link
Member Author

Choose a reason for hiding this comment

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

Agree, added in 23a6597 .

int_layout = nlayout_from_layout(layout, qregs, coupling.size())
int_qubit_subset = np.fromiter((self._qubit_indices[bit]
for bit in qubit_subset),
dtype=np.int32,
Copy link
Member

Choose a reason for hiding this comment

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

Looking at these I do wonder we don't make these unsigned since we can't have negative qubit indices. But that would require more cython changes because this array gets passed as an arg to the swap_trial cython function.

Copy link
Member Author

Choose a reason for hiding this comment

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

That's a good idea, maybe an issue as a future improvement (or as a target of opportunity next time we need to dive into the cython code)?

Copy link
Member

Choose a reason for hiding this comment

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

yeah I can push a follow up to do this, it's not crtiical

@@ -47,9 +48,11 @@ def run(self, dag):
self.property_set['is_direction_mapped'] = True
edges = self.coupling_map.get_edges()

trivial_layout = Layout.generate_trivial_layout(*dag.qregs.values())
Copy link
Member

Choose a reason for hiding this comment

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

I saw the WIP todo in the PR summary about this, but what is the functional difference between using a layout here versus building a qubit indices dict?

Copy link
Member Author

Choose a reason for hiding this comment

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

Functionally, only that Layout is a bidirectional (instead of unidirectional) mapping from Qubits to ints. Since I wrote that comment though, I've moved away from the idea of using Layouts as a generic mapping (outside the contexts of layout and routing) because the resulting code ends up for me harder to read.

""" After each gate generate a new unique variable name for each of the
qubits, using scheme: 'q[id]_[gatenum]', e.g. q1_0 -> q1_1 -> q1_2,
q2_0 -> q2_1
Args:
qb_id (int): index of qubit to generate new variable for
qbt (Qubit): qubit to generate new variable for
Copy link
Member

Choose a reason for hiding this comment

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

Any reason to not use qubit?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch, added in 49865fc .

@kdk kdk force-pushed the quantumcircuit-optional-registers-transpiler branch from 68afca2 to 13a63e6 Compare March 3, 2021 16:54
Copy link
Member

@mtreinish mtreinish 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 update I think it LGTM. The one thing I think would be good to have before we merge is some testing of the passes without registers, especially for ones not in the default pipeline (like the hoare optimization pass).

@kdk kdk force-pushed the quantumcircuit-optional-registers-transpiler branch from 13a63e6 to ffe7ac0 Compare March 10, 2021 22:49
@kdk
Copy link
Member Author

kdk commented Mar 11, 2021

Thanks for the update I think it LGTM. The one thing I think would be good to have before we merge is some testing of the passes without registers, especially for ones not in the default pipeline (like the hoare optimization pass).

Good idea, added in 3d745bb .

Copy link
Member

@mtreinish mtreinish left a comment

Choose a reason for hiding this comment

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

LGTM, good to see the extra test coverage and that it caught a bunch of bugs. I think this LGTM now. I still think there are some coverage gaps on some of the non default passes, but we can always fix those in follow ups too.

@mergify mergify bot merged commit 4d64125 into Qiskit:master Mar 11, 2021
Circuits automation moved this from To do to Done Mar 11, 2021
@kdk kdk added the Changelog: None Do not include in changelog label Mar 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: None Do not include in changelog optional-registers
Projects
Circuits
  
Done
Development

Successfully merging this pull request may close these issues.

None yet

2 participants