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

Fix post-oxidization change in ConsolidateBlocks behavior #13450

Merged
merged 4 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is the angle here fixed to pi/2 ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's because that's what UnitarySynthesis uses if it's a basis_gates only setup: https://github.com/Qiskit/qiskit/blob/main/qiskit/transpiler/passes/synthesis/unitary_synthesis.py#L98 the idea is that it's trying to match the decomposer used for estimating to what the synthesis pass will use later.

The whole logic between the passes is more than a bit hacky. ConsolidateBlocks tries to guess how many 2q gates will be used to decide whether to consolidate the block into a unitary or not. This is strongly coupled to the behavior of UnitarySynthesis because that's what actually does the synthesis later. But none of this actually takes into account multiple possible decomposers/synthesis methods being supported by a target, or the presence of a non-default plugin being used.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, I did some testing locally with this and in some cases using TwoQubitBasisDecomposer(RXXGate(pi/2)) under estimates what the XXDecomposer estimates, 3 vs 4 gates. This might result in some blocks not getting consolidated which were before, but in practice I find it probably pretty unlikely since it only counts rzz gates when evaluating if the block's gates are more than the expected synthesis output.

}


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()