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

Simple Z-axis rotations not decomposed properly #6412

Closed nonhermitian closed 3 years ago

nonhermitian commented 3 years ago

Information

What is the current behavior?

As a follow on to #6411, no simple Z-rotations get properly decomposed into rz gates.

image

becomes

image

Steps to reproduce the problem

qc = QuantumCircuit(1)
qc.z(0)
qc.barrier()
qc.t(0)
qc.barrier()
qc.tdg(0)
qc.barrier()
qc.s(0)
qc.barrier()
qc.sdg(0)

out = transpile(qc, basis_gates=['rz'], optimization_level=0)
out.draw(output='mpl')

What is the expected behavior?

image

Suggested solutions

singhmeet11 commented 3 years ago

I am super interested in taking this issue as I have been studying about decomposition of gates for sometime now. But, it would be really great if you could tell me one thing, how do we know that two quantum circuits are same. At first I was checking if the net unitary is same but as global phase has effect on the unitary, this test fails. The only way I can think of is going through each possible state. How is this done in practice @nonhermitian

kdk commented 3 years ago

Thanks for your interest @singhmeet11 ! (and sorry I didn't see your comment earlier.) It looks like this bug was already resolved as part of #4837 :

>>> qc = qk.QuantumCircuit(1)
>>> qc.z(0)
>>> qc.barrier()
>>> qc.t(0)
>>> qc.barrier()
>>> qc.tdg(0)
>>> qc.barrier()
>>> qc.s(0)
>>> qc.barrier()
>>> qc.sdg(0)
>>> qk.transpile(qc, basis_gates=['rz'], optimization_level=0).draw()
global phase: π/2
     ┌───────┐ ░ ┌─────────┐ ░ ┌──────────┐ ░ ┌─────────┐ ░ ┌──────────┐
q_0: ┤ RZ(π) ├─░─┤ RZ(π/4) ├─░─┤ RZ(-π/4) ├─░─┤ RZ(π/2) ├─░─┤ RZ(-π/2) ├
     └───────┘ ░ └─────────┘ ░ └──────────┘ ░ └─────────┘ ░ └──────────┘

As to your question on how to assert the input and output circuits are the same, this is in general a difficult problem to solve (see e.g. #5692 for a proposed equivalence checker interface ) but for single qubit circuits, asserting the unitary is the same is sufficient (including global phase).

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

singhmeet11 commented 3 years ago

No problem