Skip to content

Commit

Permalink
Fix: Incorrect creation of parameters in `update_from_instruction_sch…
Browse files Browse the repository at this point in the history
…edule_map`

- Add `tupelize` function to create tuples from non-downcastable items.
- Fix creation of Parameters by iterating through members of tuple object and mapping them to parameters in `update_from_instruction_schedule_map`.
- Add missing logic for creating a Target with/without `qubit_properties`.
- Add tuple conversion of `Qargs` to store items in a dict in `BasisTranslator` and `UnitarySynthesis` passes.
- Cast `PropsMap` object to dict when comparing in `test_fake_backends.py`.
- Modify logic of helper functions that receive a bound object reference, a second `py` not required as an argument.
- Add set operation methods to `GateMapKeys`.
- Other tweaks and fixes.
  • Loading branch information
raynelfss committed May 13, 2024
1 parent 97d9341 commit 9df0586
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 71 deletions.
85 changes: 85 additions & 0 deletions crates/accelerate/src/target_transpiler/gate_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type GateMapType = IndexMap<String, Py<PropsMap>>;
type GateMapIterType = IntoIter<String>;

#[pyclass(sequence)]
#[derive(Debug, Clone)]
pub struct GateMapKeys {
keys: IndexSet<String>,
}
Expand Down Expand Up @@ -65,6 +66,90 @@ impl GateMapKeys {
}
}

fn union(&self, other: &Bound<PyAny>) -> PyResult<Self> {
if let Ok(set) = other.extract::<Self>() {
Ok(Self {
keys: self.keys.union(&set.keys).cloned().collect(),
})
} else if let Ok(set) = other.extract::<HashSet<String>>() {
Ok(Self {
keys: self
.keys
.iter()
.cloned()
.collect::<HashSet<String>>()
.union(&set)
.cloned()
.collect(),
})
} else {
Err(PyKeyError::new_err(
"Could not perform union, Wrong Key Types",
))
}
}

fn intersection(&self, other: &Bound<PyAny>) -> PyResult<Self> {
if let Ok(set) = other.extract::<Self>() {
Ok(Self {
keys: self.keys.intersection(&set.keys).cloned().collect(),
})
} else if let Ok(set) = other.extract::<HashSet<String>>() {
Ok(Self {
keys: self
.keys
.iter()
.cloned()
.collect::<HashSet<String>>()
.intersection(&set)
.cloned()
.collect(),
})
} else {
Err(PyKeyError::new_err(
"Could not perform intersection, Wrong Key Types",
))
}
}

fn difference(&self, other: &Bound<PyAny>) -> PyResult<Self> {
if let Ok(set) = other.extract::<Self>() {
Ok(Self {
keys: self.keys.difference(&set.keys).cloned().collect(),
})
} else if let Ok(set) = other.extract::<HashSet<String>>() {
Ok(Self {
keys: self
.keys
.iter()
.cloned()
.collect::<HashSet<String>>()
.difference(&set)
.cloned()
.collect(),
})
} else {
Err(PyKeyError::new_err(
"Could not perform difference, Wrong Key Types",
))
}
}

fn __ior__(&mut self, other: &Bound<PyAny>) -> PyResult<()> {
self.keys = self.union(other)?.keys;
Ok(())
}

fn __iand__(&mut self, other: &Bound<PyAny>) -> PyResult<()> {
self.keys = self.intersection(other)?.keys;
Ok(())
}

fn __isub__(&mut self, other: &Bound<PyAny>) -> PyResult<()> {
self.keys = self.difference(other)?.keys;
Ok(())
}

fn __contains__(slf: PyRef<Self>, obj: String) -> PyResult<bool> {
Ok(slf.keys.contains(&obj))
}
Expand Down
Loading

0 comments on commit 9df0586

Please sign in to comment.