Skip to content

Commit 70fa389

Browse files
Fix control flow panics in PyDAGCircuit
Replace .unwrap() calls in pack_into and unpack_into methods with proper error handling to prevent panics when converting circuits with control flow operations to DAGs. This fixes the TODO mentioned in PR Qiskit#15301 about test failures/panics with control flow (circuit to dag issues). Changes: - Replace .unwrap() on qubits.find() with ok_or_else() returning DAGCircuitError - Replace .unwrap() on clbits.find() with ok_or_else() returning DAGCircuitError - Replace .unwrap() on vars.find() with ok_or_else() returning DAGCircuitError - Replace .unwrap() on qubits.get() with ok_or_else() returning DAGCircuitError - Replace .unwrap() on clbits.get() with ok_or_else() returning DAGCircuitError - Replace .unwrap() on vars.get() with ok_or_else() returning DAGCircuitError All control flow tests now pass successfully.
1 parent 456d2d9 commit 70fa389

File tree

1 file changed

+102
-12
lines changed

1 file changed

+102
-12
lines changed

crates/circuit/src/dag_circuit.rs

Lines changed: 102 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -194,23 +194,83 @@ impl PyDAGCircuit {
194194
let in_node = in_node.borrow();
195195
let wire = in_node.wire.bind(py);
196196
if let Ok(qubit) = wire.extract::<ShareableQubit>() {
197-
NodeType::QubitIn(self.dag_circuit.qubits.find(&qubit).unwrap())
197+
NodeType::QubitIn(
198+
self.dag_circuit
199+
.qubits
200+
.find(&qubit)
201+
.ok_or_else(|| {
202+
DAGCircuitError::new_err(format!(
203+
"Qubit {:?} not found in DAGCircuit",
204+
qubit
205+
))
206+
})?,
207+
)
198208
} else if let Ok(clbit) = wire.extract::<ShareableClbit>() {
199-
NodeType::ClbitIn(self.dag_circuit.clbits.find(&clbit).unwrap())
209+
NodeType::ClbitIn(
210+
self.dag_circuit
211+
.clbits
212+
.find(&clbit)
213+
.ok_or_else(|| {
214+
DAGCircuitError::new_err(format!(
215+
"Clbit {:?} not found in DAGCircuit",
216+
clbit
217+
))
218+
})?,
219+
)
200220
} else {
201221
let var = wire.extract::<expr::Var>()?;
202-
NodeType::VarIn(self.dag_circuit.vars.find(&var).unwrap())
222+
NodeType::VarIn(
223+
self.dag_circuit
224+
.vars
225+
.find(&var)
226+
.ok_or_else(|| {
227+
DAGCircuitError::new_err(format!(
228+
"Variable {:?} not found in DAGCircuit",
229+
var
230+
))
231+
})?,
232+
)
203233
}
204234
} else if let Ok(out_node) = b.cast::<DAGOutNode>() {
205235
let out_node = out_node.borrow();
206236
let wire = out_node.wire.bind(py);
207237
if let Ok(qubit) = wire.extract::<ShareableQubit>() {
208-
NodeType::QubitOut(self.dag_circuit.qubits.find(&qubit).unwrap())
238+
NodeType::QubitOut(
239+
self.dag_circuit
240+
.qubits
241+
.find(&qubit)
242+
.ok_or_else(|| {
243+
DAGCircuitError::new_err(format!(
244+
"Qubit {:?} not found in DAGCircuit",
245+
qubit
246+
))
247+
})?,
248+
)
209249
} else if let Ok(clbit) = wire.extract::<ShareableClbit>() {
210-
NodeType::ClbitOut(self.dag_circuit.clbits.find(&clbit).unwrap())
250+
NodeType::ClbitOut(
251+
self.dag_circuit
252+
.clbits
253+
.find(&clbit)
254+
.ok_or_else(|| {
255+
DAGCircuitError::new_err(format!(
256+
"Clbit {:?} not found in DAGCircuit",
257+
clbit
258+
))
259+
})?,
260+
)
211261
} else {
212262
let var = wire.extract::<expr::Var>()?;
213-
NodeType::VarOut(self.dag_circuit.vars.find(&var).unwrap())
263+
NodeType::VarOut(
264+
self.dag_circuit
265+
.vars
266+
.find(&var)
267+
.ok_or_else(|| {
268+
DAGCircuitError::new_err(format!(
269+
"Variable {:?} not found in DAGCircuit",
270+
var
271+
))
272+
})?,
273+
)
214274
}
215275
} else if let Ok(op_node) = b.cast::<DAGOpNode>() {
216276
let op_node = op_node.borrow();
@@ -264,7 +324,12 @@ impl PyDAGCircuit {
264324
self.dag_circuit
265325
.qubits
266326
.get(*qubit)
267-
.unwrap()
327+
.ok_or_else(|| {
328+
DAGCircuitError::new_err(format!(
329+
"Qubit {:?} not found in DAGCircuit",
330+
qubit
331+
))
332+
})?
268333
.into_py_any(py)?,
269334
),
270335
)?
@@ -276,7 +341,12 @@ impl PyDAGCircuit {
276341
self.dag_circuit
277342
.qubits
278343
.get(*qubit)
279-
.unwrap()
344+
.ok_or_else(|| {
345+
DAGCircuitError::new_err(format!(
346+
"Qubit {:?} not found in DAGCircuit",
347+
qubit
348+
))
349+
})?
280350
.into_py_any(py)?,
281351
),
282352
)?
@@ -288,7 +358,12 @@ impl PyDAGCircuit {
288358
self.dag_circuit
289359
.clbits
290360
.get(*clbit)
291-
.unwrap()
361+
.ok_or_else(|| {
362+
DAGCircuitError::new_err(format!(
363+
"Clbit {:?} not found in DAGCircuit",
364+
clbit
365+
))
366+
})?
292367
.into_py_any(py)?,
293368
),
294369
)?
@@ -300,7 +375,12 @@ impl PyDAGCircuit {
300375
self.dag_circuit
301376
.clbits
302377
.get(*clbit)
303-
.unwrap()
378+
.ok_or_else(|| {
379+
DAGCircuitError::new_err(format!(
380+
"Clbit {:?} not found in DAGCircuit",
381+
clbit
382+
))
383+
})?
304384
.into_py_any(py)?,
305385
),
306386
)?
@@ -342,7 +422,12 @@ impl PyDAGCircuit {
342422
self.dag_circuit
343423
.vars
344424
.get(*var)
345-
.unwrap()
425+
.ok_or_else(|| {
426+
DAGCircuitError::new_err(format!(
427+
"Variable {:?} not found in DAGCircuit",
428+
var
429+
))
430+
})?
346431
.clone()
347432
.into_py_any(py)?,
348433
),
@@ -355,7 +440,12 @@ impl PyDAGCircuit {
355440
self.dag_circuit
356441
.vars
357442
.get(*var)
358-
.unwrap()
443+
.ok_or_else(|| {
444+
DAGCircuitError::new_err(format!(
445+
"Variable {:?} not found in DAGCircuit",
446+
var
447+
))
448+
})?
359449
.clone()
360450
.into_py_any(py)?,
361451
),

0 commit comments

Comments
 (0)