-
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
Extracting permutation of classical registers from Quantum Circuit #5350
Comments
The |
Hi @yourball, cregs will not be modified by the routing or layout process. Can you provide an example of the behavior you're seeing and the behavior you'd expect? |
The |
This is a topic for the hackathon next week: qiskit-community/qiskit-hackathon-korea-21#2 |
@ajavadia Thanks, that is the exactly what I mean! It is useful to access the information about virtual permutation of qubits at the end of the circuit even when there are no measurement gates. For example this is important for equivalence checking of circuits before/after optimization. |
It is not strictly needed in that case, although it makes it much easier. Instead of checking equiv to identity with possible phase, you look for equiv up to a permutation matrix. |
I recently wrote a function for my own usage, but something like this could be helpful. def unpermute(circ_mapped):
"""
Apply left and right permutations to remove effect of layout and measurement retargeting.
"""
empty_circ = QuantumCircuit(circ_mapped.num_qubits)
lo = circ_mapped._layout
tokenswapper = LayoutTransformation(coupling_map=CouplingMap.from_line(8),
to_layout=lo,
from_layout=Layout.generate_trivial_layout(lo.get_registers().pop()))
pm = PassManager(tokenswapper)
initial_permutation = pm.run(empty_circ)
initial_permutation.barrier()
final_perm = [0] * 8
for op, qubits, clbits in circ_mapped.data:
if op.name == 'measure':
final_perm[qubits[0].index] = clbits[0].index
tokenswapper = LayoutTransformation(coupling_map=CouplingMap.from_line(8),
to_layout=Layout({v: p for v, p in zip(circ_mapped.qubits, final_perm)}),
from_layout=Layout.generate_trivial_layout(lo.get_registers().pop()))
pm = PassManager(tokenswapper)
empty_circ.barrier()
final_permutation = pm.run(empty_circ)
circ_left_permuted = initial_permutation.compose(circ_mapped.remove_final_measurements(inplace=False))
circ_left_right_permuted = circ_left_permuted.compose(final_permutation)
return circ_left_right_permuted Calling I used the |
Could PR #5280 help here?
|
This is done for the StochasticSwap in #6827. Others can be modified in a similar manner. |
This commit expands the transpiler to return the final layout permutation caused by the swap mapping in the routing stage. The routing passes already return the final layout permutation caused by the swap mapping to the property set (although for some passes it was only setting it sometime, which this fixes). This commit just sets the _final_layout attribute of QuantumCircuit objects to store this Layout object. This lets user track the qubit permutation caused by transpilation if they need. Fixes Qiskit#5350
* Add final layout to output quantum circuit from transpiler This commit expands the transpiler to return the final layout permutation caused by the swap mapping in the routing stage. The routing passes already return the final layout permutation caused by the swap mapping to the property set (although for some passes it was only setting it sometime, which this fixes). This commit just sets the _final_layout attribute of QuantumCircuit objects to store this Layout object. This lets user track the qubit permutation caused by transpilation if they need. Fixes #5350 * Fix qarg list construction * Use permutation to get a hacky solution * Fix lint * Add release note * Add test case for manually setting final layout * Handle transpile changing registers internally * Fix tests * Remove stray debug print * Remove out of date raises documentation Co-authored-by: Alexander Ivrii <alexi@il.ibm.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Hi,
during routing/mapping passes input quantum registers could be permuted, the permutation logical -> physical qregs is saved in
quantum_circuit._layout
property and could be easily accessed by a user.Similarly, classical cregs also become permuted after routing/mapping: physical cregs->logical cregs. This permutation can be extracted when applying measure gates
quantum_circuit.measure_all()
. E.g. when two cregs (0 and 1) are permuted thanthe measure gates will look like:
measure qreg[0] creg[1];
measure qreg[1] creg[0]
.However, when measure gates are not applied, there is still could be implicit permutation of cregs after mapping/routing pass. It seems that there is no simple way to access information about cregs permutation, which could be inconvenient in some cases. For example this information is necessary to check the equivalence of original and transpiled circuits.
Is it possible to add this information as a property of QuantumCircuit object? Or there is some other way to access this information?
Thanks a lot!
The text was updated successfully, but these errors were encountered: