forked from Qiskit/qiskit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix reverse permutation for transpiled circuits in Operator.from_circ…
…uit (Qiskit#8802) * Fix reverse permutation for transpiled circuits in Operator.from_circuit 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 * Update bip mapper test for new _layout format Didn't see this locally because cplex is not installed. * Remove stray debug prints * Use a dataclass for _layout attribute 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. * Disable bogus runtime import cyclic-import pylint error Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
- Loading branch information
1 parent
875b646
commit b7e6329
Showing
17 changed files
with
246 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
releasenotes/notes/fix-Opertor.from_circuit-transpile-5c056968ee40025e.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
--- | ||
upgrade: | ||
- | | ||
The ``._layout`` attribute of the :class:`~.QuantumCircuit` object has | ||
been changed from storing a :class:`~.Layout` object to storing a | ||
data class with 2 attributes, ``initial_layout`` which contains a | ||
:class:`~.Layout` object for the initial layout set during compilation | ||
and ``input_qubit_mapping`` which contains a dictionary mapping qubits | ||
to position indices in the original circuit. This change was necessary to | ||
provide all the information for a post-transpiled circuit to be able to | ||
fully reverse the permutation caused by initial layout in all situations. While | ||
this attribute is private and shouldn't be used externally, it is | ||
the only way to track the initial layout through :func:`~.transpile` | ||
so the change is being documented in case you're relying on it. If | ||
you have a use case for the ``_layout`` attribute that is not being | ||
addressed by the Qiskit API please open an issue so we can address this | ||
feature gap. | ||
fixes: | ||
- | | ||
The :meth:`.Operator.from_circuit` constructor method has been updated | ||
so that it can handle the layout output from :func:`~.transpile` and | ||
correctly reverse the qubit permutation caused by layout in all cases. | ||
Previously, if your transpiled circuit used loose :class:`~.Qubit` objects, | ||
multiple :class:`~.QuantumRegister` objects, or a single | ||
:class:`~.QuantumRegister` with a name other than ``"q"`` the constructor | ||
would have failed to create an :class:`~.Operator` from the circuit. | ||
Fixed `#8800 <https://github.com/Qiskit/qiskit-terra/issues/8800>`__. |
Oops, something went wrong.