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.28k stars 2.37k forks source link

Optimize consecutive RZZ gates into a single RZZ gate #13428

Open t-imamichi opened 5 days ago

t-imamichi commented 5 days ago

What should we add?

Since the fractional gates are available, RZZ can be used as a basis gate. But, optimization of consecutive RZZ gates does not seem to be enough as follows. It would be nice to merge them into a single RZZ.

from qiskit import generate_preset_pass_manager, QuantumCircuit

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

pm = generate_preset_pass_manager(optimization_level=3, basis_gates=["rzz"])
qc2 = pm.run(qc)
print(qc2)

output (both 1.2.4 and main branch)

q_0: ─■─────────■────────
      │ZZ(0.1)  │ZZ(0.2)
q_1: ─■─────────■────────
melechlapson commented 5 days ago

Is this issue limited to the rzz gates and not other gates like ryy rzx etc.?

t-imamichi commented 5 days ago

Not limited to RZZ. But I don't mind RYY and RZX because RZZ becomes a basis gate of devices that supports fractional gates. RYY and RZX are not basis gates of any device. https://docs.quantum.ibm.com/guides/fractional-gates

ShellyGarion commented 4 days ago

There is a synthesis algorithm TwoQubitControlledUDecomposer that takes a 2q-unitary and synthesizes it using the 2q basis gate RZZGate (with angles in the range [-pi/2, pi/2]. This algorithm has recently been ported to rust in #13139.

Here is an example of the output of this algorithm on the circuit suggested below:

        from qiskit.quantum_info import Operator
        from qiskit.synthesis.two_qubit.two_qubit_decompose import TwoQubitControlledUDecomposer
        unitary = Operator(qc)
        decomposer = TwoQubitControlledUDecomposer(RZZGate)
        qc3 = decomposer(unitary)
        print (qc3)

outputs:

global phase: -π
     ┌─────────┐┌──────────┐                       ┌─────────┐┌──────────┐»
q_0: ┤ Ry(π/2) ├┤ Ry(-π/2) ├─────────────■─────────┤ Ry(π/2) ├┤ Ry(-π/2) ├»
     └┬────────┤├──────────┤┌──────────┐ │ZZ(-0.3) ├─────────┤└┬────────┬┘»
q_1: ─┤ Rz(-π) ├┤ Ry(-π/2) ├┤ Ry(-π/2) ├─■─────────┤ Ry(π/2) ├─┤ Rz(-π) ├─»
      └────────┘└──────────┘└──────────┘           └─────────┘ └────────┘ »
«                 
«q_0: ────────────
«     ┌──────────┐
«q_1: ┤ Ry(-π/2) ├
«     └──────────┘

This algorithm should be added to the unitary synthesis tranpiler pass (https://github.com/Qiskit/qiskit/issues/13320). Then I think that this issues will be solved. Note that the redundant 1q gates should be eliminated by the 1q synthesis transpiler passes.

ShellyGarion commented 4 days ago

Is this issue limited to the rzz gates and not other gates like ryy rzx etc.?

@melechlapson - Note that the synthesis algorithm TwoQubitControlledUDecomposer suggested above works also for gates like RYY, RZX, RXX etc. that are equivalent to an RZZ up to 1q-unitary gates.

t-imamichi commented 4 days ago

Thank you for your information, @ShellyGarion. I would like to use the algorithm as a transpiler pass. So I wait for #13320.