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

optimization_level=1 always uses Trivial layout #2845

Closed
nonhermitian opened this issue Jul 22, 2019 · 4 comments · Fixed by #2971 or #3657
Closed

optimization_level=1 always uses Trivial layout #2845

nonhermitian opened this issue Jul 22, 2019 · 4 comments · Fixed by #2971 or #3657
Assignees
Labels
bug Something isn't working on hold Can not fix yet priority: high status: pending PR It has one or more PRs pending to solve this issue
Milestone

Comments

@nonhermitian
Copy link
Contributor

Information

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

What is the current behavior?

               ┌───┐                ░ ┌─┐            
q37_0: |0>─────┤ X ├────────────────░─┤M├────────────
          ┌───┐└─┬─┘┌───┐           ░ └╥┘┌─┐         
q37_1: |0>┤ H ├──■──┤ X ├───────────░──╫─┤M├─────────
          ├───┤     └─┬─┘┌───┐      ░  ║ └╥┘┌─┐      
q37_2: |0>┤ H ├───────■──┤ X ├──────░──╫──╫─┤M├──────
          ├───┤          └─┬─┘┌───┐ ░  ║  ║ └╥┘┌─┐   
q37_3: |0>┤ H ├────────────■──┤ X ├─░──╫──╫──╫─┤M├───
          ├───┤               └─┬─┘ ░  ║  ║  ║ └╥┘┌─┐
q37_4: |0>┤ H ├─────────────────■───░──╫──╫──╫──╫─┤M├
          └───┘                     ░  ║  ║  ║  ║ └╥┘
  c5_0: 0 ═════════════════════════════╩══╬══╬══╬══╬═
                                          ║  ║  ║  ║ 
  c5_1: 0 ════════════════════════════════╩══╬══╬══╬═
                                             ║  ║  ║ 
  c5_2: 0 ═══════════════════════════════════╩══╬══╬═
                                                ║  ║ 
  c5_3: 0 ══════════════════════════════════════╩══╬═
                                                   ║ 
  c5_4: 0 ═════════════════════════════════════════╩═
backend = provider.get_backend('ibmq_20_tokyo')
new_qc = transpile(qc, backend, optimization_level=1)

shows initial layout as the trivial layout, which is disconnected:

Screen Shot 2019-07-22 at 4 14 54 PM

However part #2 in the source says _layout_check does better layout
if the circuit needs swaps,

# 2. Use a better layout on densely connected qubits, if circuit needs swaps

which is obviously the case here, but no other layouts
are actually selected (or imported).

Steps to reproduce the problem

What is the expected behavior?

Expected that the DenseLayout is used if there are swaps needed.

Suggested solutions

@nonhermitian nonhermitian added the bug Something isn't working label Jul 22, 2019
@kdk kdk added this to the 0.9 milestone Jul 23, 2019
@ajavadia ajavadia self-assigned this Jul 23, 2019
@ajavadia ajavadia modified the milestones: 0.9, 0.10 Jul 25, 2019
@1ucian0
Copy link
Member

1ucian0 commented Jul 26, 2019

Confirmed.

qr = QuantumRegister(5)
cr = ClassicalRegister(5)
qc = QuantumCircuit(qr, cr)

qc.h(qr[1])
qc.h(qr[2])
qc.h(qr[3])
qc.h(qr[4])
qc.cx(qr[0], qr[1])
qc.cx(qr[1], qr[2])
qc.cx(qr[2], qr[3])
qc.cx(qr[3], qr[4])
qc.barrier(qr)
qc.measure(qr, cr)

backend = IBMQ.get_backend('ibmq_20_tokyo')
new_qc = transpile(qc, backend, optimization_level=1)
print('\n'.join([str((i, j)) for i, j in new_qc.layout._p2v.values() if i.name != 'ancilla']))
(QuantumRegister(5, 'q0'), 0)
(QuantumRegister(5, 'q0'), 1)
(QuantumRegister(5, 'q0'), 2)
(QuantumRegister(5, 'q0'), 3)
(QuantumRegister(5, 'q0'), 4)

Is also optimization_level=2 having a similar issue?

new_qc = transpile(qc, backend, optimization_level=2)
print('\n'.join([str((i, j)) for i, j in new_qc.layout._p2v.values() if i.name != 'ancilla']))
(QuantumRegister(5, 'q0'), 1)
(QuantumRegister(5, 'q0'), 4)
(QuantumRegister(5, 'q0'), 2)
(QuantumRegister(5, 'q0'), 3)
(QuantumRegister(5, 'q0'), 0)

@kdk
Copy link
Member

kdk commented Aug 13, 2019

Re-opened by #2975, will revisit post-0.9.

@ajavadia ajavadia removed this from the 0.9 milestone Aug 13, 2019
@1ucian0 1ucian0 added this to the 0.10 milestone Aug 20, 2019
@1ucian0
Copy link
Member

1ucian0 commented Aug 21, 2019

This can be fixed once "best of" approach is implemented (see #2969). On hold until then.

@1ucian0
Copy link
Member

1ucian0 commented Dec 30, 2019

Under the assumption that DenseLayout is always better than TrivialLayout (is that true?), the PR #3657 is a simpler approach (if TriviaLayout does not return a perfect scoring, then it uses DenseLayout).

@kdk kdk closed this as completed in #3657 Jan 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment