Skip to content
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

Error when running final_index_layout(filter_ancillas=False) #13732

Closed
victor-villar opened this issue Jan 24, 2025 · 2 comments · Fixed by #13833
Closed

Error when running final_index_layout(filter_ancillas=False) #13732

victor-villar opened this issue Jan 24, 2025 · 2 comments · Fixed by #13833
Labels
bug Something isn't working
Milestone

Comments

@victor-villar
Copy link

Environment

  • Qiskit version: 1.3.2
  • Python version: 3.11
  • Operating system: macos/linux

What is happening?

On certain circuits transpiled with a backend and a coupling map, this call fails

transpiled_circuit.layout.final_index_layout(filter_ancillas=False)

raising KeyError: "The item Qubit(QuantumRegister(5, 'q'), 3) does not exist in the Layout".

This worked fine until qiskit 1.3.0

How can we reproduce the issue?

Running the following code:

from qiskit import QuantumCircuit, transpile, qasm2
from qiskit_ibm_runtime import QiskitRuntimeService

service = QiskitRuntimeService(channel="ibm_quantum")
backend = service.backend("ibmq_manila")

qc = qasm2.loads('OPENQASM 2.0;include "qelib1.inc";qreg q[3];rccx q[2],q[1],q[0];t q[2];u(0.28905709212845454,5.626877140821112,5.481851443455051) q[1];h q[0];cx q[1],q[2];s q[0];', custom_instructions=qasm2.LEGACY_CUSTOM_INSTRUCTIONS)

transpiled_circuit = transpile(qc,backend=backend,coupling_map=[[0,1],[1,2],[2,3]],optimization_level=2,seed_transpiler=42)
transpiled_circuit.layout.final_index_layout(filter_ancillas=False)

What should happen?

There should not be any error in the call. The output to that particular example in qiskit 1.2.4 is [3, 1, 2, 0]

Any suggestions?

No response

@victor-villar victor-villar added the bug Something isn't working label Jan 24, 2025
@mtreinish mtreinish added this to the 1.3.3 milestone Feb 10, 2025
@jakelishman
Copy link
Member

There definitely is a bug in Qiskit here somewhere, but it's not in TranspileLayout - it happens before that, somewhere in the Rust-only version of SabreLayout. I suspect that it's because the Rust version of Sabre only gets told about 4 qubits (because of the coupling map).

We can fix that, but to get you moving: I suspect your test is actually typoed, and you meant for the coupling map to have 5 qubits, like Manila does? If so, you might find:

from qiskit.transpiler import CouplingMap

coupling = CouplingMap.from_line(num_qubits)

less error prone.

@jakelishman
Copy link
Member

As it happens, this is a bug in SabreLayout's handling of disjoint coupling maps, and it's been around for just under two years. The fact that there's a disjoint coupling map in the test case is, I think, just a typo in the test case as mentioned above: you start with a 5q backend, then reduce its connectivity to 4q (using a mix of tight [Target] and loose [coupling_map] constraints that please bear in mind will be invalid in Qiskit 2), but it's still a 5q backend. So there's now an isolated qubit, so the coupling map is disjoint, and we get the bug.

The reason this seemed to appear in Qiskit 1.3 is because of a (correct) bug fix in how transpile and generate_preset_pass_manager handled the loose constraints used by this method. Before #13394, those methods incorrectly threw away the Target in the backend and used only the loose constraints to generate the pseudo-Target, which means that in Qiskit 1.2, your test case comes out compiled to a 4q backend which isn't actually compatible with the Manila-except-with-4q-linear-connectivity backend that your test case attempts to construct. So prior to Qiskit 1.3 your test also hit a Qiskit bug, just in a way that didn't affect your assertion. Qiskit 1.3 corrected that, which meant you hit another, which does affect you.

#13833 fixes this, and the output is now (as of this commit, might change in the future due to randomisation) [2, 0, 1, 3, 4] - correctly using 5q.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants