Skip to content

Commit

Permalink
Fix: Make Target inherit from Rust
Browse files Browse the repository at this point in the history
- Make Target inherit from the rust-side Target by using subclass attribute, then extending its functionality using python.
- Switch from __init__ to __new__ to adapt to the Target rust class.
- Modify every trait that worked with `target._Target` to use `super()` or `self` instead.
- Fix repr in InstructionProperties to not show `Some()` when a value exists.
- Fix `__str__` method in `Target` to not display "None" if no description is given.
- Assume `num_qubits` is the first argument when an integer is provided as a first argument and nothing else is provided for second (Target initializer).
- Return a set in operation_names instead of a Vec.
- Other tweaks and fixes.
  • Loading branch information
raynelfss committed Apr 19, 2024
1 parent d97c13b commit 8e141d9
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 134 deletions.
46 changes: 31 additions & 15 deletions crates/accelerate/src/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,29 +174,45 @@ impl InstructionProperties {
}

fn __repr__(&self, py: Python<'_>) -> PyResult<String> {
let mut output = "InstructionProperties(".to_owned();
if let Some(duration) = self.duration {
output.push_str("duration=");
output.push_str(duration.to_string().as_str());
output.push(' ');
} else {
output.push_str("duration=None ");
}

if let Some(error) = self.error {
output.push_str("error=");
output.push_str(error.to_string().as_str());
output.push(' ');
} else {
output.push_str("error=None ");
}

if let Some(calibration) = self.get_calibration(py) {
Ok(format!(
"InstructionProperties(duration={:?}, error={:?}, calibration={:?})",
self.duration,
self.error,
calibration
.call_method0(py, "__repr__")?
.extract::<String>(py)?
))
output.push_str(
format!(
"calibration={:?})",
calibration
.call_method0(py, "__str__")?
.extract::<String>(py)?
)
.as_str(),
);
} else {
Ok(format!(
"InstructionProperties(duration={:?}, error={:?}, calibration=None)",
self.duration, self.error
))
output.push_str("calibration=None)");
}
Ok(output)
}
}

type GateMapType =
IndexMap<String, Option<IndexMap<Option<HashableVec<u32>>, Option<InstructionProperties>>>>;
type TargetValue = Option<IndexMap<Option<HashableVec<u32>>, Option<InstructionProperties>>>;

#[pyclass(mapping, module = "qiskit._accelerate.target.Target")]
#[pyclass(mapping, subclass, module = "qiskit._accelerate.target.Target")]
#[derive(Clone, Debug)]
pub struct Target {
#[pyo3(get, set)]
Expand Down Expand Up @@ -927,9 +943,9 @@ impl Target {
}

#[getter]
fn operation_names(&self) -> Vec<String> {
fn operation_names(&self) -> HashSet<String> {
// Get the operation names in the target.
return Vec::from_iter(self.gate_map.keys().cloned());
return HashSet::from_iter(self.gate_map.keys().cloned());
}

#[getter]
Expand Down
Loading

0 comments on commit 8e141d9

Please sign in to comment.