Skip to content

Commit a00279b

Browse files
1ucian0ElePT
andauthored
Circuits with control flow now raise a proper error when circuit_to_instruction is called (Qiskit#13921)
* Circuits with control flow now raise a proper error when circuit_to_instruction is called * test * cyclic import * Update qiskit/converters/circuit_to_instruction.py Co-authored-by: Elena Peña Tapia <[email protected]> --------- Co-authored-by: Elena Peña Tapia <[email protected]>
1 parent b89dd7f commit a00279b

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

qiskit/converters/circuit_to_instruction.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# that they have been altered from the originals.
1212

1313
"""Helper function for converting a circuit to an instruction."""
14+
from qiskit.circuit.controlflow.control_flow import ControlFlowOp
1415
from qiskit.exceptions import QiskitError
1516
from qiskit.circuit.instruction import Instruction
1617
from qiskit.circuit.quantumregister import QuantumRegister
@@ -84,6 +85,13 @@ def circuit_to_instruction(circuit, parameter_map=None, equivalence_library=None
8485
" You may be able to use `QuantumCircuit.compose` to inline this circuit into another."
8586
)
8687

88+
for inst in circuit.data:
89+
if isinstance(inst.operation, ControlFlowOp):
90+
raise QiskitError(
91+
f"Circuits with control flow operations ({type(inst.operation)}) "
92+
"cannot be converted to an instruction."
93+
)
94+
8795
if parameter_map is None:
8896
parameter_dict = {p: p for p in circuit.parameters}
8997
else:
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
upgrade_circuits:
3+
- |
4+
Converting a quantum circuit to a gate with :func:`.converters.circuit_to_instruction` fails properly when given
5+
circuit contains control flow instructions.

test/python/converters/test_circuit_to_instruction.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,17 @@ def test_underspecified_parameter_map_raises(self):
143143
QiskitError, circuit_to_instruction, qc, {theta: gamma, phi: phi, delta: delta}
144144
)
145145

146+
def test_control_flow_raises(self):
147+
"""Raising if a circuit to convert has control flow.
148+
See https://github.com/Qiskit/qiskit/issues/11379"""
149+
qc = QuantumCircuit(2)
150+
qc.h(0)
151+
qc.cx(0, 1)
152+
qc.measure_all()
153+
with qc.if_test((qc.clbits[0], 0)):
154+
qc.x(0)
155+
self.assertRaises(QiskitError, circuit_to_instruction, qc)
156+
146157
def test_parameter_map(self):
147158
"""Verify alternate parameter specification"""
148159
qr = QuantumRegister(3, "qr")

0 commit comments

Comments
 (0)