From a17a92b14a04164fb4316bea3c24cf855013e705 Mon Sep 17 00:00:00 2001 From: Yael Ben-Haim Date: Wed, 11 Sep 2024 14:09:32 +0300 Subject: [PATCH 1/3] a test to cover a bug in isa check --- test/unit/test_sampler.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/test/unit/test_sampler.py b/test/unit/test_sampler.py index 181142e4b..9ca284cbd 100644 --- a/test/unit/test_sampler.py +++ b/test/unit/test_sampler.py @@ -21,7 +21,7 @@ from qiskit.primitives.containers.sampler_pub import SamplerPub from qiskit.circuit.library import RealAmplitudes from qiskit_ibm_runtime import Session, SamplerV2, SamplerOptions, IBMInputValueError -from qiskit_ibm_runtime.fake_provider import FakeFractionalBackend, FakeSherbrooke +from qiskit_ibm_runtime.fake_provider import FakeFractionalBackend, FakeSherbrooke, FakeCusco from ..ibm_test_case import IBMTestCase from ..utils import MockSession, dict_paritally_equal, get_mocked_backend, transpile_pubs @@ -241,3 +241,21 @@ def test_gate_not_in_target(self): with self.assertRaisesRegex(IBMInputValueError, " h "): sampler.run(pubs=[(circ)]) + + @data(FakeSherbrooke(), FakeCusco()) + def test_isa_inside_condition_block(self, backend): + """Test no exception for 2q gates involving qubits that are not connected in + the coupling map, inside control operation blocks; and yes exception for + qubit pairs that are not connected""" + + circ = QuantumCircuit(5, 1) + circ.x(0) + circ.measure(0, 0) + with circ.if_test((0, 1)): + circ.ecr(1, 2) + + if backend.name == "fake_sherbrooke": + SamplerV2(backend).run(pubs=[(circ)]) + else: + with self.assertRaises(IBMInputValueError): + SamplerV2(backend).run(pubs=[(circ)]) From 6d26058cf55a7e52101308997abe30303d1d8034 Mon Sep 17 00:00:00 2001 From: Yael Ben-Haim Date: Wed, 11 Sep 2024 14:37:27 +0300 Subject: [PATCH 2/3] fixed isa bug about qubit indices inside condition blocks --- qiskit_ibm_runtime/utils/utils.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/qiskit_ibm_runtime/utils/utils.py b/qiskit_ibm_runtime/utils/utils.py index 4fb688b62..c80da156d 100644 --- a/qiskit_ibm_runtime/utils/utils.py +++ b/qiskit_ibm_runtime/utils/utils.py @@ -47,7 +47,7 @@ def is_simulator(backend: BackendV1 | BackendV2) -> bool: return getattr(backend, "simulator", False) -def _is_isa_circuit_helper(circuit: QuantumCircuit, target: Target) -> str: +def _is_isa_circuit_helper(circuit: QuantumCircuit, target: Target, qubit_map: Dict) -> str: """ A section of is_isa_circuit, separated to allow recursive calls within blocks of conditional operations. @@ -56,7 +56,7 @@ def _is_isa_circuit_helper(circuit: QuantumCircuit, target: Target) -> str: operation = instruction.operation name = operation.name - qargs = tuple(circuit.find_bit(x).index for x in instruction.qubits) + qargs = tuple(qubit_map[bit] for bit in instruction.qubits) if ( not target.instruction_supported(name, qargs) and name != "barrier" @@ -68,7 +68,7 @@ def _is_isa_circuit_helper(circuit: QuantumCircuit, target: Target) -> str: if isinstance(operation, ControlFlowOp): for sub_circ in operation.blocks: - sub_string = _is_isa_circuit_helper(sub_circ, target) + sub_string = _is_isa_circuit_helper(sub_circ, target, qubit_map) if sub_string: return sub_string @@ -92,7 +92,8 @@ def is_isa_circuit(circuit: QuantumCircuit, target: Target) -> str: f"but the target system requires {target.num_qubits} qubits." ) - return _is_isa_circuit_helper(circuit, target) + qubit_map = {qubit: index for index, qubit in enumerate(circuit.qubits)} + return _is_isa_circuit_helper(circuit, target, qubit_map) def are_circuits_dynamic(circuits: List[QuantumCircuit], qasm_default: bool = True) -> bool: From 32a80c6a76a49b024a611afb6ac3ec9deec60a74 Mon Sep 17 00:00:00 2001 From: Yael Ben-Haim Date: Wed, 11 Sep 2024 19:58:48 +0300 Subject: [PATCH 3/3] lint --- test/unit/test_sampler.py | 1 + 1 file changed, 1 insertion(+) diff --git a/test/unit/test_sampler.py b/test/unit/test_sampler.py index 9ca284cbd..4c2054a93 100644 --- a/test/unit/test_sampler.py +++ b/test/unit/test_sampler.py @@ -247,6 +247,7 @@ def test_isa_inside_condition_block(self, backend): """Test no exception for 2q gates involving qubits that are not connected in the coupling map, inside control operation blocks; and yes exception for qubit pairs that are not connected""" + # pylint: disable=invalid-name,not-context-manager circ = QuantumCircuit(5, 1) circ.x(0)