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

Suboptimal optimization result of consecutive RZZ gates with optimization level 2 and 3 #13431

Open
t-imamichi opened this issue Nov 13, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@t-imamichi
Copy link
Member

Environment

  • Qiskit version: 1.2.4 and main branch
  • Python version: 3.12.7
  • Operating system: 15.1

What is happening?

Transpilation with optimization level 2 and 3 result in a deeper circuit than that of optimization level 1 for a case with consecutive RZZ gates.

How can we reproduce the issue?

from qiskit import generate_preset_pass_manager, QuantumCircuit

qc = QuantumCircuit(2)
qc.rzz(0.1, 0, 1)
qc.rzz(0.2, 0, 1)

for level in [0, 1, 2, 3]:
    pm = generate_preset_pass_manager(optimization_level=level, basis_gates=["rz", "cx", "sx","x"])
    qc2 = pm.run(qc)
    print('optimization level', level)
    print(qc2)
    print()

output

# 1.2.4
optimization level 0

q_0: ──■───────────────■────■───────────────■──
     ┌─┴─┐┌─────────┐┌─┴─┐┌─┴─┐┌─────────┐┌─┴─┐
q_1: ┤ X ├┤ Rz(0.1) ├┤ X ├┤ X ├┤ Rz(0.2) ├┤ X ├
     └───┘└─────────┘└───┘└───┘└─────────┘└───┘

optimization level 1

q_0: ──■───────────────■──
     ┌─┴─┐┌─────────┐┌─┴─┐
q_1: ┤ X ├┤ Rz(0.3) ├┤ X ├
     └───┘└─────────┘└───┘

optimization level 2
global phase: 0
     ┌─────────────┐        ┌───┐        ┌───┐┌────────────┐
q_0: ┤ Rz(-3.1374) ├──■─────┤ X ├─────■──┤ X ├┤ Rz(3.1374) ├
     └─┬──────────┬┘┌─┴─┐┌──┴───┴──┐┌─┴─┐├───┤└┬─────────┬─┘
q_1: ──┤ Rz(-π/2) ├─┤ X ├┤ Rz(0.3) ├┤ X ├┤ X ├─┤ Rz(π/2) ├──
       └──────────┘ └───┘└─────────┘└───┘└───┘ └─────────┘

optimization level 3
global phase: 0
     ┌─────────────┐        ┌───┐        ┌───┐┌────────────┐
q_0: ┤ Rz(-3.1374) ├──■─────┤ X ├─────■──┤ X ├┤ Rz(3.1374) ├
     └─┬──────────┬┘┌─┴─┐┌──┴───┴──┐┌─┴─┐├───┤└┬─────────┬─┘
q_1: ──┤ Rz(-π/2) ├─┤ X ├┤ Rz(0.3) ├┤ X ├┤ X ├─┤ Rz(π/2) ├──
       └──────────┘ └───┘└─────────┘└───┘└───┘ └─────────┘
# main branch
optimization level 0

q_0: ──■───────────────■────■───────────────■──
     ┌─┴─┐┌─────────┐┌─┴─┐┌─┴─┐┌─────────┐┌─┴─┐
q_1: ┤ X ├┤ Rz(0.1) ├┤ X ├┤ X ├┤ Rz(0.2) ├┤ X ├
     └───┘└─────────┘└───┘└───┘└─────────┘└───┘

optimization level 1
global phase: 2π

q_0: ──■───────────────■──
     ┌─┴─┐┌─────────┐┌─┴─┐
q_1: ┤ X ├┤ Rz(0.3) ├┤ X ├
     └───┘└─────────┘└───┘

optimization level 2
     ┌─────────────┐        ┌───┐        ┌───┐┌────────────┐
q_0: ┤ Rz(-3.1374) ├──■─────┤ X ├─────■──┤ X ├┤ Rz(3.1374) ├
     └─┬──────────┬┘┌─┴─┐┌──┴───┴──┐┌─┴─┐├───┤└┬─────────┬─┘
q_1: ──┤ Rz(-π/2) ├─┤ X ├┤ Rz(0.3) ├┤ X ├┤ X ├─┤ Rz(π/2) ├──
       └──────────┘ └───┘└─────────┘└───┘└───┘ └─────────┘

optimization level 3
     ┌─────────────┐        ┌───┐        ┌───┐┌────────────┐
q_0: ┤ Rz(-3.1374) ├──■─────┤ X ├─────■──┤ X ├┤ Rz(3.1374) ├
     └─┬──────────┬┘┌─┴─┐┌──┴───┴──┐┌─┴─┐├───┤└┬─────────┬─┘
q_1: ──┤ Rz(-π/2) ├─┤ X ├┤ Rz(0.3) ├┤ X ├┤ X ├─┤ Rz(π/2) ├──
       └──────────┘ └───┘└─────────┘└───┘└───┘ └─────────┘

What should happen?

It would be nice to generate the circuit of optimization level 1

Any suggestions?

No response

@t-imamichi t-imamichi added the bug Something isn't working label Nov 13, 2024
@alexanderivrii
Copy link
Contributor

This is interesting. I don't think we claim anywhere that transpiler with optimization_level=2 should always provide a better result than with optimization_level=1, and Qiskit's behavior did not change between the two Qiskit versions, so technically this is not a bug.

Still, in this specific case under the hood the level-2 transpiler first brings it to the form of level-1, and then applies the two-qubit resynthesis (Collect2qBlocks + ConsolidateBlocks + UnitarySynthesis), which seemingly makes the circuit more complex. I also remember wishing for something similar in the past when seeing two-qubit resynthesis messing up Clifford gates even though all of them were in the basis gates. Though note that the number of 2-qubit gates is the same for levels 1 and 2, and in this sense the two-qubit resynthesis is "optimal".

I am wondering if we could attach the "oroginal definition" to UnitaryGates and return that instead of the resynthesized definition when all of its gates are in basis_gates and it's better (same number of 2-qubit gates but fewer 1-qubit gates).

@mtreinish
Copy link
Member

I'm wondering if the new pass I'm working on in: #13419 will help with this. If we replace the use of the 3 passes for 2 qubit peephole optimization with that new pass it'll hopefully see the synthesis output is less efficient than the original block and not substitute it.

@t-imamichi
Copy link
Member Author

Although it's not a bug technically, this case is trivial as seen as the result of optimization level 1. So, I expect Qiskit can generate the optimal circuit for optimization level 2 and 3. But, of course, it's not urgent.

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