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.31k stars 2.38k forks source link

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

Open t-imamichi opened 2 weeks ago

t-imamichi commented 2 weeks ago

Environment

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

alexanderivrii commented 2 weeks ago

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 commented 2 weeks ago

I'm wondering if the new pass I'm working on in: https://github.com/Qiskit/qiskit/pull/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 commented 2 weeks ago

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.