Skip to content

Final layout is not invoked in the transpiler unless the swap mapper is called. #10818

@jaygambetta

Description

@jaygambetta

Environment

  • Qiskit Terra version:
  • Python version:
  • Operating system:

What is happening?

In the following code

theta = Parameter('theta')
qc_example = QuantumCircuit(3)
qc_example.h(0) # generate superposition
qc_example.p(theta, 0) # add quantum phase
qc_example.cx(0, 1) # condition 1st qubit on 0th qubit
qc_example.cx(0, 2) # condition 2nd qubit on 0th qubit

qc_ibm = transpile(qc_example, basis_gates = ['cz', 'sx', 'rz'],  coupling_map =[[0, 1], [1, 2]], optimization_level=3)
print(qc_ibm)

The transpiler performs a mapping but only initial_layout is updated not the final_laytout is set to none

qc_ibm.layout

gives

TranspileLayout(initial_layout=Layout({
1: Qubit(QuantumRegister(3, 'q'), 0),
2: Qubit(QuantumRegister(3, 'q'), 1),
0: Qubit(QuantumRegister(3, 'q'), 2)
}), input_qubit_mapping={Qubit(QuantumRegister(3, 'q'), 0): 0, Qubit(QuantumRegister(3, 'q'), 1): 1, Qubit(QuantumRegister(3, 'q'), 2): 2}, final_layout=None)

This should give

final_layout=Layout({
1: Qubit(QuantumRegister(3, 'q'), 0),
2: Qubit(QuantumRegister(3, 'q'), 1),
0: Qubit(QuantumRegister(3, 'q'), 2)

How can we reproduce the issue?

see code above

What should happen?

This should give

final_layout=Layout({
1: Qubit(QuantumRegister(3, 'q'), 0),
2: Qubit(QuantumRegister(3, 'q'), 1),
0: Qubit(QuantumRegister(3, 'q'), 2)

Any suggestions?

In the pass_manager after choosing the initial_layout to the swap mapper set the final_layout and only update it after the final layout.

Personally I find the notation very confusing. It took me a while (actually I never did @nonhermitian explained it to me) to realize initial_layout is not the initial_layout but the initial_layout to the swap mapper and the final_layout is the layout after the swap mapper (also no idea what 'input_qubit_mapping' is). Personally I think layout should just be the final one but if the user wants the history of how the layout changed then the should have meta data that correlates passes used with layout changes. But I think this would be a longer issue. My suggestion is to perform a quick update to set final_layout for this example and then in the future create an issue for what should be in the layout after the transpiler and how a user can get meta data that includes how the layout changes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions