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

Calling stochasticswap pass directly fails #7724

Closed
nonhermitian opened this issue Mar 3, 2022 · 8 comments
Closed

Calling stochasticswap pass directly fails #7724

nonhermitian opened this issue Mar 3, 2022 · 8 comments
Labels
bug Something isn't working

Comments

@nonhermitian
Copy link
Contributor

Environment

  • Qiskit Terra version: {'qiskit-terra': '0.19.2', 'qiskit-aer': '0.10.3', 'qiskit-ignis': '0.7.0', 'qiskit-ibmq-provider': '0.18.3', 'qiskit-aqua': None, 'qiskit': '0.34.2', 'qiskit-nature': None, 'qiskit-finance': None, 'qiskit-optimization': None, 'qiskit-machine-learning': None}
  • Python version:
  • Operating system:

What is happening?

A simple modification to a doc example from https://qiskit.org/documentation/tutorials/circuits_advanced/04_transpiler_passes_and_passmanager.html#Different-Variants-of-the-Same-Pass

from qiskit import QuantumCircuit
from qiskit.transpiler import CouplingMap
from qiskit.transpiler.passes import StochasticSwap
from qiskit.test.mock import FakeBoeblingen
backend = FakeBoeblingen()


coupling = backend.configuration().coupling_map

circuit = QuantumCircuit(7)
circuit.h(3)
circuit.cx(0, 6)
circuit.cx(6, 0)
circuit.cx(0, 1)
circuit.cx(3, 1)
circuit.cx(3, 0)

coupling_map = CouplingMap(couplinglist=coupling)

ss = StochasticSwap(coupling_map=coupling_map)
pass_manager = PassManager(ss)
stochastic_circ = pass_manager.run(circuit)

yields

File "/opt/miniconda3/envs/qiskit/lib/python3.10/site-packages/qiskit/transpiler/passes/routing/stochastic_swap.py", line 337, in _mapper
    raise TranspilerError(
TranspilerError: 'swap mapper failed: layer 0, sublayer 1'

How can we reproduce the issue?

Run above

What should happen?

It should work. It works if calling from transpile with routing_method='stochastic'

Any suggestions?

No response

@nonhermitian nonhermitian added the bug Something isn't working label Mar 3, 2022
@jakelishman
Copy link
Member

This appears to have been fixed on main already for me, probably as a result of #7658 - could you check locally to see if the problem persists for you on main?

@nonhermitian
Copy link
Contributor Author

Yes. This now works on main. Although still curious why it worked via transpile but not directly.

@mtreinish
Copy link
Member

My guess is that it worked with transpile() because transpile() will run a layout pass (which by default will be DenseLayout) prior to running stochastic swap. With the layout applied the input dag to stochastic swap will have ancilla qubits for all the idle physical qubits and also a different order based on the layout. I expect that all makes it easier for it to find a valid mapping. As to why it's fixed on main I'm actually just assuming it's random chance because we're using a different rng with the rust re-implementation in #7658

@nonhermitian
Copy link
Contributor Author

So the backend in question has bi-direction coupling for qubits 0->4. But then this circuit fails:

ghz = QuantumCircuit(5)
ghz.h(0)
ghz.cx(0,range(1,5))

If I pass a restricted coupling map:

coupling = [[0,1], [1,0], [1,2], [2,1], [2,3], [3,2], [3,4], [4,3]]

which is contained inside of the original then it works. So it appears that the swap mapper requires the circuit to be of the same width as the device given by the coupling map. Indeed the exact same circuit but over 20q works. We should probably throw a better error if passing a circuit to a swap mapper where that is not the case.

@nonhermitian
Copy link
Contributor Author

I am not sure how rust changes things here.

@mtreinish
Copy link
Member

Functionally there is no difference between the rust and cython implementations. The rust just runs the trials in parallel instead of serially and is using a different rng (each thread has a thread local PCG64MCG rng and the seeding is different)

@nonhermitian
Copy link
Contributor Author

Yeah but the above fails no matter which seed you use, which is why I am confused.

@jakelishman
Copy link
Member

Looks like StochasticSwap originally used to reject all DAGs/layouts that had a mismatch in the number of qubits, but #2598 relaxed the constraint, and I don't see a particular reason in that PR why. Perhaps smaller DAGs worked at that point, and our tests never caught the point at which it stopped working.

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

No branches or pull requests

3 participants