Skip to content

Commit

Permalink
Remove workaround for abi3 BigUint -> Python int conversio (#10683)
Browse files Browse the repository at this point in the history
In #10120 we moved to using the Python stable C API for the qiskit
binaries we build. In that PR we encountered a limitation with PyO3 at
the time when using abi3 it was unable to convert a BigUInt into a
Python int directly. To workaround this we side stepped the issue by
generating a string representation of the integer converting that to
python and then having python go from a string to a int. This has some
performance penalty and also prevented parallelism because a GIL handle
was needed to do the conversion. In PyO3 0.19.1 this limitation was
fixed and the library can handle the conversion directly now with abi3
and this commit restores the code that existed in the marginalization
module prior to #10120.
  • Loading branch information
mtreinish authored Aug 22, 2023
1 parent 0b51250 commit 4c88f07
Showing 1 changed file with 13 additions and 18 deletions.
31 changes: 13 additions & 18 deletions crates/accelerate/src/results/marginalization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,24 +152,19 @@ pub fn marginal_memory(
.collect()
};
if return_int {
// Replace with:
//
// .iter()
// .map(|x| BigUint::parse_bytes(x.as_bytes(), 2).unwrap())
// .collect::<Vec<BigUint>>()
// .to_object(py))
//
// (also this can be done in parallel, see
// https://github.com/Qiskit/qiskit-terra/pull/10120 for more
// details)
//
// After PyO3/pyo3#3198 is included in a pyo3 release.
let int_pyobject = py.import("builtins")?.getattr("int")?;
Ok(out_mem
.iter()
.map(|x| int_pyobject.call1((x, 2u8)).unwrap())
.collect::<Vec<_>>()
.to_object(py))
if out_mem.len() < parallel_threshold || !run_in_parallel {
Ok(out_mem
.iter()
.map(|x| BigUint::parse_bytes(x.as_bytes(), 2).unwrap())
.collect::<Vec<BigUint>>()
.to_object(py))
} else {
Ok(out_mem
.par_iter()
.map(|x| BigUint::parse_bytes(x.as_bytes(), 2).unwrap())
.collect::<Vec<BigUint>>()
.to_object(py))
}
} else {
Ok(out_mem.to_object(py))
}
Expand Down

0 comments on commit 4c88f07

Please sign in to comment.