Skip to content

Commit

Permalink
Fix post-oxidization change in ConsolidateBlocks behavior (#13450) (#…
Browse files Browse the repository at this point in the history
…13453)

* Set decomposer to None if kak_gates not found during ConsolidateBlocks initialization.

* Add test that reproduces issue

* Re-add rzz to list of kak gates

* Add path for rzx gate.

(cherry picked from commit 94cea41)

Co-authored-by: Elena Peña Tapia <[email protected]>
  • Loading branch information
mergify[bot] and ElePT authored Nov 19, 2024
1 parent 6a10a59 commit 3c20e1a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
12 changes: 9 additions & 3 deletions qiskit/transpiler/passes/optimization/consolidate_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@

"""Replace each block of consecutive gates by a single Unitary node."""
from __future__ import annotations
from math import pi

from qiskit.synthesis.two_qubit import TwoQubitBasisDecomposer
from qiskit.circuit.library.standard_gates import CXGate, CZGate, iSwapGate, ECRGate
from qiskit.circuit.library.standard_gates import CXGate, CZGate, iSwapGate, ECRGate, RXXGate

from qiskit.transpiler.basepasses import TransformationPass
from qiskit.transpiler.passmanager import PassManager
from qiskit._accelerate.consolidate_blocks import consolidate_blocks
Expand All @@ -27,6 +29,7 @@
"cz": CZGate(),
"iswap": iSwapGate(),
"ecr": ECRGate(),
"rxx": RXXGate(pi / 2),
}


Expand Down Expand Up @@ -70,7 +73,6 @@ def __init__(
if basis_gates is not None:
self.basis_gates = set(basis_gates)
self.force_consolidate = force_consolidate

if kak_basis_gate is not None:
self.decomposer = TwoQubitBasisDecomposer(kak_basis_gate)
elif basis_gates is not None:
Expand All @@ -79,8 +81,12 @@ def __init__(
self.decomposer = TwoQubitBasisDecomposer(
KAK_GATE_NAMES[kak_gates.pop()], basis_fidelity=approximation_degree or 1.0
)
elif "rzx" in basis_gates:
self.decomposer = TwoQubitBasisDecomposer(
CXGate(), basis_fidelity=approximation_degree or 1.0
)
else:
self.decomposer = TwoQubitBasisDecomposer(CXGate())
self.decomposer = None
else:
self.decomposer = TwoQubitBasisDecomposer(CXGate())

Expand Down
29 changes: 24 additions & 5 deletions test/python/transpiler/test_consolidate_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,19 @@

import unittest
import numpy as np
from ddt import ddt, data

from qiskit.circuit import QuantumCircuit, QuantumRegister, IfElseOp, Gate
from qiskit.circuit.library import U2Gate, SwapGate, CXGate, CZGate, UnitaryGate
from qiskit.converters import circuit_to_dag
from qiskit.transpiler.passes import ConsolidateBlocks
from qiskit.quantum_info.operators import Operator
from qiskit.quantum_info.operators.measures import process_fidelity
from qiskit.transpiler import PassManager
from qiskit.transpiler import Target
from qiskit.transpiler.passes import Collect1qRuns
from qiskit.transpiler.passes import Collect2qBlocks
from qiskit.transpiler import PassManager, Target, generate_preset_pass_manager
from qiskit.transpiler.passes import ConsolidateBlocks, Collect1qRuns, Collect2qBlocks
from test import QiskitTestCase # pylint: disable=wrong-import-order


@ddt
class TestConsolidateBlocks(QiskitTestCase):
"""
Tests to verify that consolidating blocks of gates into unitaries
Expand Down Expand Up @@ -571,6 +570,26 @@ def __init__(self):

self.assertEqual(res, qc)

@data(2, 3)
def test_no_kak_gates_in_preset_pm(self, opt_level):
"""Test correct initialization of ConsolidateBlocks pass when kak_gates aren't found.
Reproduces https://github.com/Qiskit/qiskit/issues/13438."""

qc = QuantumCircuit(2)
qc.cz(0, 1)
qc.sx([0, 1])
qc.cz(0, 1)

ref_pm = generate_preset_pass_manager(
optimization_level=1, basis_gates=["rz", "rzz", "sx", "x", "rx"]
)
ref_tqc = ref_pm.run(qc)
pm = generate_preset_pass_manager(
optimization_level=opt_level, basis_gates=["rz", "rzz", "sx", "x", "rx"]
)
tqc = pm.run(qc)
self.assertEqual(ref_tqc, tqc)


if __name__ == "__main__":
unittest.main()

0 comments on commit 3c20e1a

Please sign in to comment.