diff --git a/qiskit/transpiler/passes/optimization/consolidate_blocks.py b/qiskit/transpiler/passes/optimization/consolidate_blocks.py index 63dca11f6d2..ce64233699b 100644 --- a/qiskit/transpiler/passes/optimization/consolidate_blocks.py +++ b/qiskit/transpiler/passes/optimization/consolidate_blocks.py @@ -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 @@ -27,6 +29,7 @@ "cz": CZGate(), "iswap": iSwapGate(), "ecr": ECRGate(), + "rxx": RXXGate(pi / 2), } @@ -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: @@ -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()) diff --git a/test/python/transpiler/test_consolidate_blocks.py b/test/python/transpiler/test_consolidate_blocks.py index 3b3aff1db22..fbe11ff77db 100644 --- a/test/python/transpiler/test_consolidate_blocks.py +++ b/test/python/transpiler/test_consolidate_blocks.py @@ -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 @@ -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()