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.11k stars 2.34k forks source link

H gate does not get decomposed correctly #6411

Closed nonhermitian closed 3 years ago

nonhermitian commented 3 years ago

Information

What is the current behavior?

An H gate does not get decomposed correctly. It is easy to see this at optimization level 0:

image

becomes

image

when it should involve no more than one sx gate and two rz gates.

Steps to reproduce the problem

What is the expected behavior?

An H gate gets decomposed into the correct minimal set of basis gates.

Suggested solutions

It looks like the decomposition for s and sdg does not know they are simple z-axis rotations.

Avhijit-Nair commented 3 years ago

Hi @nonhermitian !

This is an interesting issue. I would love to fix this, but this is my first time contributing to Qiskit, so a lot of guidance will be helpful.

iamsantanubanerjee commented 3 years ago

@nonhermitian Can you share the steps to reproduce this problem?

nonhermitian commented 3 years ago

Simply transpile the H gate using optimization level 0

Avhijit-Nair commented 3 years ago

@nonhermitian I transpiled it with optimization level 0, but all I get is the H gate back

qc = QuantumCircuit(1)
qc.h(0)
a = transpile(qc,optimization_level = 0)
a.draw(output='mpl')

image

nonhermitian commented 3 years ago

Yeah sorry, you need to target an IBM backend or set the basis gates to be the same.

Avhijit-Nair commented 3 years ago

By backend, do you mean the actual hardware or the simulator? qasm_simulator gave me the same output as above, and so did basis_gates=['h']

nonhermitian commented 3 years ago

A real backend

Avhijit-Nair commented 3 years ago

I used ibmq_belem as the backend, and I got this error. What am I doing wrong? (the code is same as above)

Cannot unroll the circuit to the given basis, ['id', 'rz', 'sx', 'x', 'cx', 'reset']. No rule to expand instruction h.
nonhermitian commented 3 years ago

This works on the latest release:


qc = QuantumCircuit(1)
qc.h(0)

backend = provider.backend.ibmq_belem
trans_qc = transpile(qc, backend, optimization_level=0)
trans_qc.draw()

If it is not working on master than it is a bug.

Avhijit-Nair commented 3 years ago

Ah..I see. My bad, I was checking on PyPi version

nonhermitian commented 3 years ago

The latest pypi version should work.

Avhijit-Nair commented 3 years ago

The latest is version number 0.26.2, and I checked my code on this version. But I wasn't able to reproduce the problem.

nonhermitian commented 3 years ago

The IBM Quantum website is at Qiskit 0.26.2 and give me:

image

I am not sure what you are seeing or not

Avhijit-Nair commented 3 years ago

I see. Wonder why I'm not getting it. Btw, I tried it on a Jupyter Notebook. This is the full code

from qiskit import QuantumCircuit, Aer, IBMQ
from qiskit.compiler import transpile
from qiskit.providers.ibmq import least_busy

qc = QuantumCircuit(1)
qc.h(0)

provider = IBMQ.get_provider(hub='ibm-q')
small_devices = provider.backends(filters=lambda x: x.configuration().n_qubits == 5
                                   and not x.configuration().simulator)
backend = least_busy(small_devices)

a = transpile(qc,optimization_level = 0,backend = backend) # gives error
Avhijit-Nair commented 3 years ago

@nonhermitian Can I take up this issue?

nonhermitian commented 3 years ago

Sure thing

Avhijit-Nair commented 3 years ago

Thanks!

kdk commented 3 years ago

Thanks for your interest @Avhijit-codeboy ! Closing as this was already resolved as part of #4837 :

>>> qc = qk.QuantumCircuit(1)
>>> qc.h(0)
>>> qk.transpile(qc, basis_gates=['rz', 'sx'], optimization_level=0).draw()
global phase: π/4
     ┌─────────┐┌────┐┌─────────┐
q_0: ┤ RZ(π/2) ├┤ √X ├┤ RZ(π/2) ├
     └─────────┘└────┘└─────────┘

Please feel free to re-open if you're still seeing this behavior.