Skip to content

Commit

Permalink
Add: Macro rules for qargs and other sequences.
Browse files Browse the repository at this point in the history
- Create `QargSet` and `PropsMap` using the new macros.
- Return a `TargetOpNames` ordered set to python in `operation_names`.
- Remove the Python side `operation_names.`
- Fix faulty docstring in `target.py`.
- Other tweaks and fixes.
  • Loading branch information
raynelfss committed May 14, 2024
1 parent 2575431 commit 37070b2
Show file tree
Hide file tree
Showing 6 changed files with 470 additions and 297 deletions.
149 changes: 10 additions & 139 deletions crates/accelerate/src/target_transpiler/gate_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
// copyright notice, and modified files need to carry a notice indicating
// that they have been altered from the originals.

use super::property_map::PropsMap;
use super::{macro_rules::key_like_set_iterator, property_map::PropsMap};
use hashbrown::HashSet;
use indexmap::{set::IntoIter, IndexMap, IndexSet};
use itertools::Itertools;
Expand All @@ -24,144 +24,15 @@ use pyo3::{
type GateMapType = IndexMap<String, Py<PropsMap>>;
type GateMapIterType = IntoIter<String>;

#[pyclass(sequence)]
#[derive(Debug, Clone)]
pub struct GateMapKeys {
keys: IndexSet<String>,
}

#[pymethods]
impl GateMapKeys {
fn __iter__(slf: PyRef<Self>) -> PyResult<Py<GateMapIter>> {
let iter = GateMapIter {
iter: slf.keys.clone().into_iter(),
};
Py::new(slf.py(), iter)
}

fn __eq__(slf: PyRef<Self>, other: Bound<PySet>) -> PyResult<bool> {
for item in other.iter() {
let key = item.extract::<String>()?;
if !(slf.keys.contains(&key)) {
return Ok(false);
}
}
Ok(true)
}

fn __len__(slf: PyRef<Self>) -> usize {
slf.keys.len()
}

fn __sub__(&self, other: HashSet<String>) -> GateMapKeys {
GateMapKeys {
keys: self
.keys
.iter()
.cloned()
.collect::<HashSet<String>>()
.difference(&other)
.cloned()
.collect::<IndexSet<String>>(),
}
}

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))
}

fn __repr__(slf: PyRef<Self>) -> String {
let mut output = "gate_map_keys[".to_owned();
output.push_str(slf.keys.iter().join(", ").as_str());
output.push(']');
output
}
}

key_like_set_iterator!(
GateMapKeys,
GateMapKeysIter,
keys,
String,
IntoIter<String>,
"",
"gate_map_keys"
);
#[pyclass]
pub struct GateMapIter {
iter: GateMapIterType,
Expand Down
Loading

0 comments on commit 37070b2

Please sign in to comment.