Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump PyO3 and rust-numpy to 0.23.x #13577

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 24 additions & 18 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ hashbrown.version = "0.14.5"
num-bigint = "0.4"
num-complex = "0.4"
ndarray = "0.15"
numpy = "0.22.1"
numpy = "0.23"
smallvec = "1.13"
thiserror = "1.0"
rustworkx-core = "0.15"
Expand All @@ -34,7 +34,7 @@ rayon = "1.10"
# distributions). We only activate that feature when building the C extension module; we still need
# it disabled for Rust-only tests to avoid linker errors with it not being loaded. See
# https://pyo3.rs/main/features#extension-module for more.
pyo3 = { version = "0.22.6", features = ["abi3-py39"] }
pyo3 = { version = "0.23", features = ["abi3-py39"] }

# These are our own crates.
qiskit-accelerate = { path = "crates/accelerate" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ pub(super) fn compose_transforms<'a>(
for (node, params) in nodes_to_replace {
let param_mapping: HashMap<ParameterUuid, Param> = equiv_params
.iter()
.map(|x| ParameterUuid::from_parameter(x.to_object(py).bind(py)))
.map(|x| ParameterUuid::from_parameter(&x.into_pyobject(py).unwrap()))
.zip(params)
.map(|(uuid, param)| -> PyResult<(ParameterUuid, Param)> {
Ok((uuid?, param.clone_ref(py)))
Expand Down
18 changes: 9 additions & 9 deletions crates/accelerate/src/basis/basis_translator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ fn extract_basis(
unreachable!("Control flow operation is not an instance of PyInstruction.")
};
let inst_bound = inst.instruction.bind(py);
for block in inst_bound.getattr("blocks")?.iter()? {
for block in inst_bound.getattr("blocks")?.try_iter()? {
recurse_circuit(py, block?, basis, min_qubits)?;
}
}
Expand Down Expand Up @@ -255,7 +255,7 @@ fn extract_basis(
if inst.op.control_flow() {
let operation_ob = instruction_object.getattr(intern!(py, "operation"))?;
let blocks = operation_ob.getattr("blocks")?;
for block in blocks.iter()? {
for block in blocks.try_iter()? {
recurse_circuit(py, block?, basis, min_qubits)?;
}
}
Expand Down Expand Up @@ -326,7 +326,7 @@ fn extract_basis_target(
let bound_inst = op.instruction.bind(py);
// Use python side extraction instead of the Rust method `op.blocks` due to
// required usage of a python-space method `QuantumCircuit.has_calibration_for`.
let blocks = bound_inst.getattr("blocks")?.iter()?;
let blocks = bound_inst.getattr("blocks")?.try_iter()?;
for block in blocks {
extract_basis_target_circ(
&block?,
Expand Down Expand Up @@ -403,7 +403,7 @@ fn extract_basis_target_circ(
unreachable!("Control flow op is not a control flow op. But control_flow is `true`")
};
let bound_inst = op.instruction.bind(py);
let blocks = bound_inst.getattr("blocks")?.iter()?;
let blocks = bound_inst.getattr("blocks")?.try_iter()?;
for block in blocks {
extract_basis_target_circ(
&block?,
Expand Down Expand Up @@ -443,7 +443,7 @@ fn apply_translation(
let mut flow_blocks = vec![];
let bound_obj = control_op.instruction.bind(py);
let blocks = bound_obj.getattr("blocks")?;
for block in blocks.iter()? {
for block in blocks.try_iter()? {
let block = block?;
let dag_block: DAGCircuit =
circuit_to_dag(py, block.extract()?, true, None, None)?;
Expand Down Expand Up @@ -667,7 +667,7 @@ fn replace_node(
let parameter_map = target_params
.iter()
.zip(node.params_view())
.into_py_dict_bound(py);
.into_py_dict(py)?;
for inner_index in target_dag.topological_op_nodes()? {
let inner_node = &target_dag.dag()[inner_index].unwrap_operation();
let old_qargs = dag.get_qargs(node.qubits);
Expand Down Expand Up @@ -702,7 +702,7 @@ fn replace_node(
if let Param::ParameterExpression(param_obj) = param {
let bound_param = param_obj.bind(py);
let exp_params = param.iter_parameters(py)?;
let bind_dict = PyDict::new_bound(py);
let bind_dict = PyDict::new(py);
for key in exp_params {
let key = key?;
bind_dict.set_item(&key, parameter_map.get_item(&key)?)?;
Expand Down Expand Up @@ -769,7 +769,7 @@ fn replace_node(

if let Param::ParameterExpression(old_phase) = target_dag.global_phase() {
let bound_old_phase = old_phase.bind(py);
let bind_dict = PyDict::new_bound(py);
let bind_dict = PyDict::new(py);
for key in target_dag.global_phase().iter_parameters(py)? {
let key = key?;
bind_dict.set_item(&key, parameter_map.get_item(&key)?)?;
Expand All @@ -790,7 +790,7 @@ fn replace_node(
}
if !new_phase.getattr(intern!(py, "parameters"))?.is_truthy()? {
new_phase = new_phase.call_method0(intern!(py, "numeric"))?;
if new_phase.is_instance(&PyComplex::type_object_bound(py))? {
if new_phase.is_instance(&PyComplex::type_object(py))? {
return Err(TranspilerError::new_err(format!(
"Global phase must be real, but got {}",
new_phase.repr()?
Expand Down
2 changes: 1 addition & 1 deletion crates/accelerate/src/check_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fn recurse<'py>(
if let OperationRef::Instruction(py_inst) = inst.op.view() {
let raw_blocks = py_inst.instruction.getattr(py, "blocks")?;
let circuit_to_dag = CIRCUIT_TO_DAG.get_bound(py);
for raw_block in raw_blocks.bind(py).iter().unwrap() {
for raw_block in raw_blocks.bind(py).try_iter().unwrap() {
let block_obj = raw_block?;
let block = block_obj
.getattr(intern!(py, "_data"))?
Expand Down
8 changes: 3 additions & 5 deletions crates/accelerate/src/circuit_library/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,15 @@ impl BlockOperation {
)),
Self::PyCustom { builder } => {
// the builder returns a Python operation plus the bound parameters
let py_params =
PyList::new_bound(py, params.iter().map(|&p| p.clone().into_py(py))).into_any();
let py_params = PyList::new(py, params.iter().map(|&p| p.clone()))?.into_any();

let job = builder.call1(py, (py_params,))?;
let result = job.downcast_bound::<PyTuple>(py)?;

let operation: OperationFromPython = result.get_item(0)?.extract()?;
let bound_params = result
.get_item(1)?
.iter()?
.try_iter()?
.map(|ob| Param::extract_no_coerce(&ob?))
.collect::<PyResult<SmallVec<[Param; 3]>>>()?;

Expand Down Expand Up @@ -84,7 +83,6 @@ impl Block {
#[staticmethod]
#[pyo3(signature = (num_qubits, num_parameters, builder,))]
pub fn from_callable(
py: Python,
num_qubits: i64,
num_parameters: i64,
builder: &Bound<PyAny>,
Expand All @@ -96,7 +94,7 @@ impl Block {
}
let block = Block {
operation: BlockOperation::PyCustom {
builder: builder.to_object(py),
builder: builder.clone().unbind(),
},
num_qubits: num_qubits as u32,
num_parameters: num_parameters as usize,
Expand Down
2 changes: 1 addition & 1 deletion crates/accelerate/src/circuit_library/entanglement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ pub fn get_entangler_map<'py>(
Ok(entanglement) => entanglement
.into_iter()
.map(|vec| match vec {
Ok(vec) => Ok(PyTuple::new_bound(py, vec)),
Ok(vec) => PyTuple::new(py, vec),
Err(e) => Err(e),
})
.collect::<Result<Vec<_>, _>>(),
Expand Down
4 changes: 2 additions & 2 deletions crates/accelerate/src/circuit_library/parameter_ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub(super) type LayerParameters<'a> = Vec<BlockParameters<'a>>; // parameter in
/// Internally, the parameters are stored in a 1-D vector and the ledger keeps track of
/// which indices belong to which layer. For example, a 2-qubit circuit where both the
/// rotation and entanglement layer have 1 block with 2 parameters each, we would store
///
///
/// [x0 x1 x2 x3 x4 x5 x6 x7 ....]
/// ----- ----- ----- -----
/// rep0 rep0 rep1 rep2
Expand Down Expand Up @@ -105,7 +105,7 @@ impl ParameterLedger {
let parameter_vector: Vec<Param> = imports::PARAMETER_VECTOR
.get_bound(py)
.call1((parameter_prefix, num_parameters))? // get the Python ParameterVector
.iter()? // iterate over the elements and cast them to Rust Params
.try_iter()? // iterate over the elements and cast them to Rust Params
.map(|ob| Param::extract_no_coerce(&ob?))
.collect::<PyResult<_>>()?;

Expand Down
4 changes: 2 additions & 2 deletions crates/accelerate/src/circuit_library/pauli_feature_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ pub fn pauli_feature_map(
let pauli_strings = _get_paulis(feature_dimension, paulis)?;

// set the default value for entanglement
let default = PyString::new_bound(py, "full");
let default = PyString::new(py, "full");
let entanglement = entanglement.unwrap_or(&default);

// extract the parameters from the input variable ``parameters``
let parameter_vector = parameters
.iter()?
.try_iter()?
.map(|el| Param::extract_no_coerce(&el?))
.collect::<PyResult<Vec<Param>>>()?;

Expand Down
6 changes: 3 additions & 3 deletions crates/accelerate/src/circuit_library/quantum_volume.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ pub fn quantum_volume(
let num_unitaries = width * depth;
let mut permutation: Vec<Qubit> = (0..num_qubits).map(Qubit).collect();

let kwargs = PyDict::new_bound(py);
let kwargs = PyDict::new(py);
kwargs.set_item(intern!(py, "num_qubits"), 2)?;
let mut build_instruction = |(unitary_index, unitary_array): (usize, Array2<Complex64>),
rng: &mut Pcg64Mcg|
Expand All @@ -122,7 +122,7 @@ pub fn quantum_volume(
if layer_index == 0 {
permutation.shuffle(rng);
}
let unitary = unitary_array.into_pyarray_bound(py);
let unitary = unitary_array.into_pyarray(py);

let unitary_gate = UNITARY_GATE
.get_bound(py)
Expand All @@ -137,7 +137,7 @@ pub fn quantum_volume(
let qubit = layer_index * 2;
Ok((
PackedOperation::from_gate(Box::new(instruction)),
smallvec![Param::Obj(unitary.unbind().into())],
smallvec![Param::Obj(unitary.into_any().unbind())],
vec![permutation[qubit], permutation[qubit + 1]],
vec![],
))
Expand Down
Loading
Loading