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 basis_count checking in ConsolidateBlocks #13463

Merged
merged 2 commits into from
Nov 20, 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
5 changes: 3 additions & 2 deletions crates/accelerate/src/consolidate_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,12 @@ const MAX_2Q_DEPTH: usize = 20;

#[allow(clippy::too_many_arguments)]
#[pyfunction]
#[pyo3(signature = (dag, decomposer, force_consolidate, target=None, basis_gates=None, blocks=None, runs=None))]
#[pyo3(signature = (dag, decomposer, basis_gate_name, force_consolidate, target=None, basis_gates=None, blocks=None, runs=None))]
pub(crate) fn consolidate_blocks(
py: Python,
dag: &mut DAGCircuit,
decomposer: &TwoQubitBasisDecomposer,
basis_gate_name: &str,
force_consolidate: bool,
target: Option<&Target>,
basis_gates: Option<HashSet<String>>,
Expand Down Expand Up @@ -125,7 +126,7 @@ pub(crate) fn consolidate_blocks(
let inst = dag.dag()[*node].unwrap_operation();
block_qargs.extend(dag.get_qargs(inst.qubits));
all_block_gates.insert(*node);
if inst.op.name() == decomposer.gate_name() {
if inst.op.name() == basis_gate_name {
basis_count += 1;
}
if !is_supported(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ def run(self, dag):
consolidate_blocks(
dag,
self.decomposer._inner_decomposer,
self.decomposer.gate.name,
self.force_consolidate,
target=self.target,
basis_gates=self.basis_gates,
Expand Down
80 changes: 78 additions & 2 deletions test/python/transpiler/test_consolidate_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,17 @@
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.circuit import QuantumCircuit, QuantumRegister, IfElseOp, Gate, Parameter
from qiskit.circuit.library import (
U2Gate,
SwapGate,
CXGate,
CZGate,
UnitaryGate,
SXGate,
XGate,
RZGate,
)
from qiskit.converters import circuit_to_dag
from qiskit.quantum_info.operators import Operator
from qiskit.quantum_info.operators.measures import process_fidelity
Expand Down Expand Up @@ -590,6 +599,73 @@ def test_no_kak_gates_in_preset_pm(self, opt_level):
tqc = pm.run(qc)
self.assertEqual(ref_tqc, tqc)

def test_non_cx_basis_gate(self):
"""Test a non-cx kak gate is consolidated correctly."""
qc = QuantumCircuit(2)
qc.cz(0, 1)
qc.x(0)
qc.h(1)
qc.z(1)
qc.t(1)
qc.h(0)
qc.t(0)
qc.cz(1, 0)
qc.sx(0)
qc.sx(1)
qc.cz(0, 1)
qc.sx(0)
qc.sx(1)
qc.cz(1, 0)
qc.x(0)
qc.h(1)
qc.z(1)
qc.t(1)
qc.h(0)
qc.t(0)
qc.cz(0, 1)

consolidate_pass = ConsolidateBlocks(basis_gates=["sx", "x", "rz", "cz"])
res = consolidate_pass(qc)
self.assertEqual({"unitary": 1}, res.count_ops())
self.assertEqual(Operator.from_circuit(qc), Operator(res.data[0].operation.params[0]))

def test_non_cx_target(self):
"""Test a non-cx kak gate is consolidated correctly."""
qc = QuantumCircuit(2)
qc.cz(0, 1)
qc.x(0)
qc.h(1)
qc.z(1)
qc.t(1)
qc.h(0)
qc.t(0)
qc.cz(1, 0)
qc.sx(0)
qc.sx(1)
qc.cz(0, 1)
qc.sx(0)
qc.sx(1)
qc.cz(1, 0)
qc.x(0)
qc.h(1)
qc.z(1)
qc.t(1)
qc.h(0)
qc.t(0)
qc.cz(0, 1)

phi = Parameter("phi")
target = Target(num_qubits=2)
target.add_instruction(SXGate(), {(0,): None, (1,): None})
target.add_instruction(XGate(), {(0,): None, (1,): None})
target.add_instruction(RZGate(phi), {(0,): None, (1,): None})
target.add_instruction(CZGate(), {(0, 1): None, (1, 0): None})

consolidate_pass = ConsolidateBlocks(target=target)
res = consolidate_pass(qc)
self.assertEqual({"unitary": 1}, res.count_ops())
self.assertEqual(Operator.from_circuit(qc), Operator(res.data[0].operation.params[0]))


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