Skip to content

Commit

Permalink
Fix: More failing tests
Browse files Browse the repository at this point in the history
- Fix repeated erroneous calls to `add_instruction` in `update_from_instruction_schedule_map`
- Add missing condition in `instruction_supported`
- Use `IndexSet` instead of `HashSet` for `QargsSet`.
- Other small tweaks and fixes.
  • Loading branch information
raynelfss committed May 13, 2024
1 parent 9df0586 commit 2575431
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 52 deletions.
89 changes: 45 additions & 44 deletions crates/accelerate/src/target_transpiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -689,45 +689,45 @@ impl Target {
let params = params.bind(py);
param_names.add(tupleize(params)?)?;
}
if qlen.len() > 1 || param_names.len() > 1 {
return Err(QiskitError::new_err(format!(
"Schedules for {:?} are defined non-uniformly for
multiple qubit lengths {:?},
or different parameter names {:?}.
Provide these schedules with inst_name_map or define them with
different names for different gate parameters.",
&inst_name,
qlen.iter().collect::<Vec<&usize>>(),
param_names,
)));
}
let gate_class = py.import_bound("qiskit.circuit.gate")?.getattr("Gate")?;
let kwargs = [
("name", inst_name.as_str().into_py(py)),
("num_qubits", qlen.iter().next().to_object(py)),
("params", Vec::<PyObject>::new().to_object(py)),
]
.into_py_dict_bound(py);
let mut inst_obj = gate_class.call((), Some(&kwargs))?;
if let Some(param) = param_names.iter().next() {
if param.is_truthy()? {
let parameter_class = py
.import_bound("qiskit.circuit.parameter")?
.getattr("Parameter")?;
let params = param
.iter()?
.flat_map(|x| -> PyResult<Bound<PyAny>> {
parameter_class.call1((x?,))
})
.collect_vec();
let kwargs = [
("name", inst_name.as_str().into_py(py)),
("num_qubits", qlen.iter().next().to_object(py)),
("params", params.to_object(py)),
]
.into_py_dict_bound(py);
inst_obj = gate_class.call((), Some(&kwargs))?;
}
}
if qlen.len() > 1 || param_names.len() > 1 {
return Err(QiskitError::new_err(format!(
"Schedules for {:?} are defined non-uniformly for
multiple qubit lengths {:?},
or different parameter names {:?}.
Provide these schedules with inst_name_map or define them with
different names for different gate parameters.",
&inst_name,
qlen.iter().collect::<Vec<&usize>>(),
param_names,
)));
}
let gate_class = py.import_bound("qiskit.circuit.gate")?.getattr("Gate")?;
let kwargs = [
("name", inst_name.as_str().into_py(py)),
("num_qubits", qlen.iter().next().to_object(py)),
("params", Vec::<PyObject>::new().to_object(py)),
]
.into_py_dict_bound(py);
let mut inst_obj = gate_class.call((), Some(&kwargs))?;
if let Some(param) = param_names.iter().next() {
if param.is_truthy()? {
let parameter_class = py
.import_bound("qiskit.circuit.parameter")?
.getattr("Parameter")?;
let params = param
.iter()?
.flat_map(|x| -> PyResult<Bound<PyAny>> {
parameter_class.call1((x?,))
})
.collect_vec();
let kwargs = [
("name", inst_name.as_str().into_py(py)),
("num_qubits", qlen.iter().next().to_object(py)),
("params", params.to_object(py)),
]
.into_py_dict_bound(py);
inst_obj = gate_class.call((), Some(&kwargs))?;
}
self.add_instruction(
py,
Expand Down Expand Up @@ -1109,7 +1109,7 @@ impl Target {
{
return Ok(false);
}
if param.eq(&param_at_index)? && !param_at_index.is_instance(parameter_class)? {
if !param.eq(&param_at_index)? && !param_at_index.is_instance(parameter_class)? {
return Ok(false);
}
}
Expand Down Expand Up @@ -1238,9 +1238,8 @@ impl Target {
}
if let Some(_qargs) = qargs.as_ref() {
let qarg_set: HashSet<PhysicalQubit> = _qargs.vec.iter().cloned().collect();
if self.gate_map.map.contains_key(operation_names) {
let gate_map_name =
&self.gate_map.map[operation_names].extract::<PropsMap>(py)?;
if let Some(gate_prop_name) = self.gate_map.map.get(operation_names) {
let gate_map_name = gate_prop_name.extract::<PropsMap>(py)?;
if gate_map_name.map.contains_key(&qargs) {
return Ok(true);
}
Expand Down Expand Up @@ -1290,6 +1289,8 @@ impl Target {
}));
}
}
} else {
return Ok(true);
}
}
}
Expand Down Expand Up @@ -1533,7 +1534,7 @@ impl Target {
/// The set of qargs in the target.
#[getter]
fn qargs(&self) -> PyResult<Option<QargsSet>> {
let qargs: HashSet<Option<Qargs>> = self.qarg_gate_map.keys().cloned().collect();
let qargs: IndexSet<Option<Qargs>> = self.qarg_gate_map.keys().cloned().collect();
// Modify logic to account for the case of {None}
let next_entry = qargs.iter().flatten().next();
if qargs.len() == 1 && (qargs.iter().next().is_none() || next_entry.is_none()) {
Expand Down
10 changes: 2 additions & 8 deletions crates/accelerate/src/target_transpiler/qargs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ use std::{
hash::{Hash, Hasher},
};

use hashbrown::{hash_set::IntoIter, HashSet};

use indexmap::{set::IntoIter, IndexSet};
use itertools::Itertools;
use pyo3::{
exceptions::{PyKeyError, PyTypeError},
Expand Down Expand Up @@ -85,16 +84,11 @@ impl QargsIter {
#[pyclass(sequence)]
#[derive(Debug, Clone)]
pub struct QargsSet {
pub set: HashSet<Option<Qargs>>,
pub set: IndexSet<Option<Qargs>>,
}

#[pymethods]
impl QargsSet {
#[new]
pub fn new(set: HashSet<Option<Qargs>>) -> Self {
Self { set }
}

fn __eq__(slf: PyRef<Self>, other: Bound<PySet>) -> PyResult<bool> {
for item in other.iter() {
let qargs = if item.is_none() {
Expand Down

0 comments on commit 2575431

Please sign in to comment.