-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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 reverse permutation for transpiled circuits in Operator.from_circuit #8802
Conversation
This commit fixes a bug in the Operator.from_circuit() constructor method for Layout's generated by transpile(). The transpiler would set the _layout property for an output circuit to be the mapping from the input circuit's virtual qubits to the physical qubits (also the output circuit's qubit index). This is useful for visualization and visually tracking the permutation assuming you have the original circuit. However for the `Operator.from_circuit()` constructor method which will reverse the permutation caused by layout in the generated matrix this is not sufficient information since we need to order of the original circuits qubits. To fix this issue this commit changes the `_layout` attribute of the QuantumCircuit class to store both the initial layout as before and also a mapping of qubit objects to indices from the original circuit. Using this extra information we can reliably handle the qubit permutation in the constructor. Fixes Qiskit#8800
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:
|
Pull Request Test Coverage Report for Build 3189095581
💛 - Coveralls |
Didn't see this locally because cplex is not installed.
@mtreinish, one quick question, to see if I understand things correctly: with this PR we would be able to reliably check equivalence of the original and the transpiled circuits by checking |
@alexanderivrii Yeah, that's the basic idea of this (except it's |
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.
A minor comment about potentially changing the type of the _layout
attribute, but mostly this looks fine to me.
This commit updates the _layout QuantumCircuit attribute to store a new dataclass, TranspileLayout, which currently stores the initial_layout used by the transpiler and the initial qubit mapping which maps the qubit object to it's position index in the circuit. For 0.23.0 we plan to make the circuit layout attribute public and having a proper container class will make it easier to do this in a way where we can evolve the interface over time if needed (likely adding a final_layout attribute in the near future). Right now the class isn't advertised or re-exported as the interface is still private/internal but we can make it a public interface at the same time we add a public getter for the circuit layout.
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.
Looks good to me, and good to get the bug fixed!
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.
Sorry for the delayed reply -- it took me some time to understand the intricacies of this issue and why the original layout alone is not sufficient to fully reconstruct the operator from the transpiled circuit. And, indeed, Layout
stores a mapping from indices of physical qubits to things like Qubit(QuantumRegister(3, 'q2'), 2)
(corresponding to virtual qubits in the original circuit), but does not store the indices of these Qubit(QuantumRegister(3, 'q2'), 2)
virtual qubits in the original circuit. The current PR addresses this by defining TranspilerLayout
that contains both the layout and these indices (called input_qubit_mapping
). The Operator.from_circuit
has been modified to factor this input_qubit_mapping
into construction. Overall, this LGTM and constitutes an important step to enable checking the equivalence of the original and the transpiled circuits.
However, things are still a bit trickier than described, and more work is needed to fully check the equivalence of the original and the transpiled circuits. For one, the layout also contains things like Qubit(QuantumRegister(1, 'ancilla'), 0)
(corresponding to auxiliary qubits not present in the original circuit). On the positive side, input_qubit_mapping
has these as well (mapping these to indices outside of the range of the original qubits). Not all of these ancilla qubits are in general idle, so possibly the equivalence check has to be done for the [original circuit extended with the appropriate number of ancilla qubits] vs. the [transpiled circuit]. But possibly completely idle qubits can be removed from the analysis. For two, as far as I can tell, we might still be missing the final permutation, for instance RemoveSwapsBeforeMeasure
can remove the trailing swaps in the circuit and this information is not reflected in TranspilerLayout
.
Right, this alone is not sufficient to fully reverse the permutation caused by the transpiler in |
As part of this update the final_layout is integrated into the TranspileLayout class which was added in Qiskit#8802 instead of a standalone attribute on the quantum circuit.
This PR makes QCEC compatible with the newest `qiskit-terra` release, which changed some internals of the `QuantumCircuit` class that broke the circuit import for compiled circuits in QCEC. The cause for this issue was the change in Qiskit/qiskit#8802. The (backwards-compatible) solution has been implemented in cda-tum/mqt-core#207. This PR also drops the upper-cap on the `qiskit-terra` version easing the integration with future versions of qiskit. Signed-off-by: burgholzer <burgholzer@me.com>
Summary
This commit fixes a bug in the Operator.from_circuit() constructor method for Layout's generated by transpile(). The transpiler would set the _layout property for an output circuit to be the mapping from the input circuit's virtual qubits to the physical qubits (also the output circuit's qubit index). This is useful for visualization and visually tracking the permutation assuming you have the original circuit. However for the
Operator.from_circuit()
constructor method which will reverse the permutation caused by layout in the generated matrix this is not sufficient information since we need to order of the original circuits qubits. To fix this issue this commit changes the_layout
attribute of the QuantumCircuit class to store both the initial layout as before and also a mapping of qubit objects to indices from the original circuit. Using this extra information we can reliably handle the qubit permutation in the constructor.Details and comments
Fixes #8800