-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Fix GateDirection pass #10825
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
Fix GateDirection pass #10825
Conversation
One or more of the the following people are requested to review this:
|
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.
This LGTM, the check there was unnecessary. TBH, this is straightforward enough that it didn't need a release note or a unittest so having them is just icing on the cake.
I expect pylint might fail complaining about the TranspilerError
in the docstring. But if it does pass I'll enqueue this for merging, otherwise it'll be easy to fix if that fails.
I don't think this is a change to be made this lightly; (Fwiw, I don't like that method of defining physical qubits, it's just what we've got at the moment.) |
I'm not actually convinced that the use of single register is a contract on the physical qubit allocation. We do that in |
Also, even if it's our API contract we don't need to eagerly raise an exception like this, it's not the job of every pass to do input validation. |
This isn't fully true in any of the routing passes; they typically do canonical_register = dag.qregs["q"]
initial_layout = Layout.generate_trivial_layout(canonical_register)
# and sometimes
layout_mapping = {bit: index for index, bit in enumerate(canonical_register)} Sometimes they also look at qiskit/qiskit/transpiler/passes/routing/utils.py Lines 22 to 23 in 2ab1e54
I agree that it's not necessarily the contract of In a more strongly typed language, this wouldn't be allowed; the caller would need to produce some sort of |
More to the point: is there any reason why it would be especially onerous for the calling in the linked issue just to use the "correct" form? All that's needed is to manually make the circuit physical by changing q1 = QuantumRegister(1)
q2 = QuantumRegister(1)
qc = QuantumCircuit(q1, q2)
qc.cx(1, 0)
qc.cx(0, 1) into q1 = QuantumRegister(1)
q2 = QuantumRegister(1)
qc = QuantumCircuit(QuantumRegister(name="q", bits=q1[:] + q2[:]))
qc.cx(1, 0)
qc.cx(0, 1) (if the |
TBH, I don't think the enforcement is enforcing anything of value, and this PR is of such little impact it's wasted effort to continue arguing over it. The thing to remember is that there is nothing stopping someone from running gate direction standalone. You could easily do: q1 = QuantumRegister(1)
q2 = QuantumRegister(1)
qc = QuantumCircuit(q1, q2)
qc.cx(1, 0)
qc.cx(0, 1)
direction_qc = GateDirection(CouplingMap.from_line(2, bidirectional=False))(qc) and that is a completely valid use case from my PoV. The pass shouldn't be enforcing that it is running as part of full pipeline especially as it has a small well defined job (which maybe could be documented better). I agree the model around physical vs virtual circuits is far from ideal, and things can get mixed frequently. But that doesn't change because of this PR, nor does this PR make the situation any appreciably different, except from a philosophical standpoint. I think short of defining physical qubits and physical circuits explicitly and then making a hard contract to make the distinction clear in which domain a pass (or anything) is working in this ambiguity is something we have to live with (and have been). All this really does is increase the general utility of the pass because it makes it easier to use in custom workflows (not just the above example), without introducing any potential regression. The only thing I would potentially ask for here to address your concerns is we can document the implicit assumption that qubit indices are the physical qubit index in either the target or coupling map. This is the implicit assumption the pass is actually working off of, so making it explicit in the documentation seems like a good step. |
I don't agree that a check that the input is physical is worthless, nor that this PR is innocent in slackening what little definition of physical circuits we have, but I won't prevent it from merging (why I didn't put a "request changes" review in the first place). I totally agree that somebody can call or use |
(and yeah, I'd appreciate it if it is going to merge that we do at least document the required contract that the bit indices represent the physical qubits in the coupling map) |
I guess I just don't agree that our meaning of physical in the transpiler is a flat register |
Sure, that works well for me - I would for sure be keen to formalise the concept of physical qubits. |
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.
I just added a note to the docstring about the assumption on qubit indices the pass makes. This LGTM (assuming I didn't break lint or the docs build).
* Fix GateDirection * Fix black * Add note about qubit index assumption to class docstring --------- Co-authored-by: Matthew Treinish <mtreinish@kortar.org> (cherry picked from commit 3a49759)
Summary
Fixes #10824
Details and comments