-
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
Operator.from_circuit
fails on transpiled circuits with user-defined registers
#8800
Comments
I tried to integrate a fix for this here: https://github.com/Qiskit/qiskit-terra/pull/8597/files#diff-f5222a986231cd8c6a8d2c71592e43aaf0ba310b123373fb47fe156d4dc34e72R244-R255 (if that PR doesn't make the release we can always extract it as a standalone bugfix). It is a tricky problem in general because as you say the |
I'm not opposed to us adding a public |
Well IIRC, when the |
Yeah, or perhaps a dataclass / some custom object that contains the original qubits and the layout? It's easier to extend an attribute-based object than a tuple if we need to add more later. |
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
I pushed up a fix PR in #8802 and left the property as private for now (and used a tuple) to make sure we could include it in 0.22.0 at this point in the cycle. I'll open a new issue for 0.23.0 to make a public layout attribute and/or a public method to reverse the layout somehow and we can make a container class for it as part of that |
…uit (#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 #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>
Environment
What is happening?
Operator.from_circuit
throws a "cannot find bit" error when called on a transpiled circuit that was originally constructed out of:q
)How can we reproduce the issue?
All of these examples print "failed":
The actual exception is along the lines of
What should happen?
The bits we're looking up should be the ones that are actually on the circuit.
Any suggestions?
The current form only works (as best as I can tell) with circuits constructed as
QuantumCircuit(n)
, wheren
is the number of bits in the backend used in the transpilation call. The fact that this works is a side-effect of how the legacyQubit
hash function works, and that the legacy behaviour ofQuantumCircuit(n)
is to create a register called "q", which is the same name as what the layout passes create by default.The
_layout
field in a circuit fromtranspile
has the same qubits as the input circuit in its "virtual bits" fields, but the returned circuit has a single register called "q" that fills everything. The lookup inOperator.find_circuit
tries to use the virtual bits from the original circuit, but these don't exist any more.I'm not actually sure if this is recoverable off the top of my head - I'm not certain
Layout
retains enough information to infer the order the virtual qubits of the previous circuit were in, especially if they are loose bits.The text was updated successfully, but these errors were encountered: