Qiskit / qiskit

Qiskit is an open-source SDK for working with quantum computers at the level of extended quantum circuits, operators, and primitives.
https://www.ibm.com/quantum/qiskit
Apache License 2.0
5.04k stars 2.32k forks source link

Instead of transpiling the circuit, transpiling with lookahead routing method sometimes throws exception 'Lookahead failed to find a swap which mapped gates or improved layout score.' #8133

Open Over-TheRainbow opened 2 years ago

Over-TheRainbow commented 2 years ago

Environment

What is happening?

Running qiskit.compiler.transpile with routing_method='lookahead' sometimes throws qiskit.transpiler.exceptions.TranspilerError: 'Lookahead failed to find a swap which mapped gates or improved layout score.'

I would expect that the transpiler gives me back the transpiled circuit. It should not terminate with the exception above, after all, basic routing doesn't fail.

How can we reproduce the issue?

Inside the .zip are 3 files, run_me.py, and two example circuits in qasm format. Examples are from https://www.cda.cit.tum.de/mqtbench/. Run run_me.py and the exception should occur. example.zip

Alternatively, download the examples directly by going to https://www.cda.cit.tum.de/mqtbench/, and selecting Variational Quantum Eigensolver (VQE) , 7 qubits, and Indep. The downloaded files that I used in the following code is vqe_indep_7.qasm.

# run_me.py
# Issue with transpilation using "lookahead" method

import qiskit

from qiskit.test import mock

# This throws an exception: qiskit.transpiler.exceptions.TranspilerError: 'Lookahead failed to find a swap which mapped gates or improved layout score.'
qiskit.compiler.transpile(
    qiskit.circuit.QuantumCircuit.from_qasm_file('./vqe_indep_5.qasm'), 
    coupling_map=mock.FakeCambridgeV2().coupling_map, 
    layout_method='trivial', 
    routing_method='lookahead', 
    optimization_level=1
)

What should happen?

When the transpiler runs with the circuit from vqe_indep_7.qasm and the coupling map from FakeCambridgeV2, it will result in the following exception: qiskit.transpiler.exceptions.TranspilerError: 'Lookahead failed to find a swap which mapped gates or improved layout score.'

This is unexpected since I would expect my circuit to be transpilable on a device with more than twice the number of qubits as the circuit.

Any suggestions?

The issue seems to be originating in a commit a few years ago: https://github.com/Qiskit/qiskit-terra/commit/7d0ba7cb8b82588168356480bdad23a83028b592 It seems that they add some convoluted control flow in order to prevent hangs, however, it now throws the exception, which has result of no transpiled circuit returned.

jakelishman commented 2 years ago

Thanks for the report - we've just been seeing similar failures in our randomised test suite, so it's helpful to have a report to help track things down a bit more.

I can't promise this will be a priority, because LookaheadSwap is pretty out-dated and hasn't been looked at in quite a while (it's slow too), but I do want to get to it, probably after the Terra 0.21 release window closes. Are you able to use routing="sabre" or one of the other routing methods as a workaround for now? (Sabre in particular should be fast and give good/better results than Lookahead.)

Over-TheRainbow commented 2 years ago

Yes, sabre does work for me. I can use that as a workaround for now, thanks for the advice and the update on the situation!