diff --git a/qiskit_ibm_transpiler/wrappers/transpile.py b/qiskit_ibm_transpiler/wrappers/transpile.py index 4fc7e5d..a12d68d 100644 --- a/qiskit_ibm_transpiler/wrappers/transpile.py +++ b/qiskit_ibm_transpiler/wrappers/transpile.py @@ -203,16 +203,19 @@ def _create_transpile_layout(initial, final, circuit, orig_circuit): class FixECR(TransformationPass): def run(self, dag): - for node in dag.named_nodes("ecr"): - dag.substitute_node(node, library.ECRGate()) + for node in dag.op_nodes(): + if node.name.startswith("ecr"): + dag.substitute_node(node, library.ECRGate()) return dag def _get_circuit_from_qasm(qasm_string: str): try: - return qasm2.loads( - qasm_string, - custom_instructions=_get_circuit_from_qasm.QISKIT_INSTRUCTIONS, + return _get_circuit_from_qasm.fix_ecr( + qasm2.loads( + qasm_string, + custom_instructions=_get_circuit_from_qasm.QISKIT_INSTRUCTIONS, + ) ) except QASM2ParseError: return _get_circuit_from_qasm.fix_ecr(qasm3.loads(qasm_string)) diff --git a/release-notes/unreleased/74.bug.rst b/release-notes/unreleased/74.bug.rst new file mode 100644 index 0000000..4fbdc7f --- /dev/null +++ b/release-notes/unreleased/74.bug.rst @@ -0,0 +1 @@ +Fixes a bug with ECR gates coming from Qiskit when parsing using OpenQASM2 diff --git a/tests/test_transpiler_service.py b/tests/test_transpiler_service.py index 65f2756..dc35d3d 100644 --- a/tests/test_transpiler_service.py +++ b/tests/test_transpiler_service.py @@ -400,3 +400,18 @@ def test_fix_ecr_qasm3(): circuit_from_qasm = _get_circuit_from_qasm(qasm3.dumps(qc)) assert isinstance(list(circuit_from_qasm)[0].operation, ECRGate) + + +def test_fix_ecr_ibm_strasbourg(): + num_qubits = 16 + circuit = QuantumCircuit(num_qubits) + for i in range(num_qubits - 1): + circuit.ecr(i, i + 1) + + cloud_transpiler_service = TranspilerService( + backend_name="ibm_strasbourg", + ai="false", + optimization_level=3, + ) + transpiled_circuit = cloud_transpiler_service.run(circuit) + assert any(isinstance(gate.operation, ECRGate) for gate in list(transpiled_circuit))