Skip to content

Commit

Permalink
Improve fix panicking on incorrect values of longest path
Browse files Browse the repository at this point in the history
The unchecked subtraction was a runtime error. It should only
happen for one value of longest path: zero. It's better
to panic on longest path less than zero.
  • Loading branch information
jlapeyre committed Jul 18, 2024
1 parent 003d10e commit 643fcdd
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions crates/circuit/src/dag_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1979,8 +1979,14 @@ def _format(operand):
None => return Err(DAGCircuitError::new_err("not a DAG")),
}
};
let _depth = depth_plus_one.checked_sub(1);
Ok(_depth.unwrap_or(0))
if depth_plus_one == 0 {
Ok(0)
} else if depth_plus_one > 0 {
Ok(depth_plus_one - 1)
} else {
// Correctly computed longest path is non-negative
unreachable!()
}
}

/// Return the total number of qubits + clbits used by the circuit.
Expand Down

0 comments on commit 643fcdd

Please sign in to comment.