diff --git a/qiskit/dagcircuit/dagcircuit.py b/qiskit/dagcircuit/dagcircuit.py index d53b2481a05f..998266c7d0b1 100644 --- a/qiskit/dagcircuit/dagcircuit.py +++ b/qiskit/dagcircuit/dagcircuit.py @@ -1441,10 +1441,11 @@ def _collect_1q_runs(self): while len(s) == 1 and \ s[0].type == "op" and \ len(s[0].qargs) == 1 and \ - len(s[0].cargs) == 0 and \ + not s[0].cargs and \ s[0].condition is None and \ + s[0] not in nodes_seen and \ not s[0].op.is_parameterized() and \ - isinstance(node.op, Gate): + isinstance(s[0].op, Gate): group.append(s[0]) nodes_seen.add(s[0]) s = self._multi_graph.successors(s[0]._node_id) diff --git a/releasenotes/notes/fix-invalid-ops-collect-runs-b9fd2f2e85136834.yaml b/releasenotes/notes/fix-invalid-ops-collect-runs-b9fd2f2e85136834.yaml new file mode 100644 index 000000000000..f06b75f39a74 --- /dev/null +++ b/releasenotes/notes/fix-invalid-ops-collect-runs-b9fd2f2e85136834.yaml @@ -0,0 +1,8 @@ +--- +fixes: + - | + Fixed an issue introduced in 0.16.2 that would cause errors when running + :func:`~qiskit.compiler.transpile` on a circuit with a series of 1 qubit + gates and a non-gate instruction that only operates on a qubit (e.g. + :class:`~qiskit.circuit.Reset`). Fixes + `#5736 `__ diff --git a/test/python/dagcircuit/test_dagcircuit.py b/test/python/dagcircuit/test_dagcircuit.py index 5b1fea83bda1..4ffce14bfe34 100644 --- a/test/python/dagcircuit/test_dagcircuit.py +++ b/test/python/dagcircuit/test_dagcircuit.py @@ -734,6 +734,21 @@ def test_dag_collect_1q_runs(self): else: self.fail('Unknown run encountered') + def test_dag_no_reset_in_collect_1q_runs(self): + """Test a run does not include a reset instruction.""" + dag = DAGCircuit() + qreg = QuantumRegister(1, 'qr') + creg = ClassicalRegister(1, 'cr') + dag.add_qreg(qreg) + dag.add_creg(creg) + dag.apply_operation_back(U1Gate(3.14), [qreg[0]]) + dag.apply_operation_back(HGate(), [qreg[0]]) + dag.apply_operation_back(Reset(), [qreg[0]]) + dag.apply_operation_back(Measure(), [qreg[0]], [creg[0]]) + runs = dag._collect_1q_runs() + self.assertEqual([[x.name for x in run] for run in runs], + [['u1', 'h']]) + def test_dag_collect_1q_runs_start_with_conditional(self): """Test collect 1q runs with a conditional at the start of the run.""" h_gate = HGate()