Skip to content
Merged
Changes from 1 commit
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
26 changes: 26 additions & 0 deletions crates/accelerate/src/equivalence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,8 @@ impl ToPyObject for CircuitFromPython {
type GraphType = StableDiGraph<NodeData, Option<EdgeData>>;
type KTIType = IndexMap<Key, NodeIndex, RandomState>;

/// A library providing a one-way mapping of Gates to their equivalent
Comment thread
mtreinish marked this conversation as resolved.
Outdated
/// implementations as QuantumCircuits.
#[pyclass(
subclass,
name = "BaseEquivalenceLibrary",
Expand Down Expand Up @@ -469,6 +471,16 @@ impl EquivalenceLibrary {
}

// TODO: Remove once BasisTranslator is in Rust.
/// Return graph representing the equivalence library data.
///
/// This property should be treated as read-only as it provides
/// a reference to the internal state of the :class:`~.EquivalenceLibrary` object.
/// If the graph returned by this property is mutated it could corrupt the
/// the contents of the object. If you need to modify the output ``PyDiGraph``
/// be sure to make a copy prior to any modification.
///
/// Returns:
/// PyDiGraph: A graph object with equivalence data in each node.
#[getter]
fn get_graph(&mut self, py: Python) -> PyResult<PyObject> {
if let Some(graph) = &self._graph {
Expand All @@ -492,6 +504,10 @@ impl EquivalenceLibrary {
}
}

/// Return list of keys to key to node index map.
///
/// Returns:
/// List: Keys to the key to node index map.
#[pyo3(name = "keys")]
fn py_keys(slf: PyRef<Self>) -> PyResult<PyObject> {
let py_dict = PyDict::new_bound(slf.py());
Expand All @@ -501,6 +517,13 @@ impl EquivalenceLibrary {
Ok(py_dict.as_any().call_method0("keys")?.into())
}

/// Return node index for a given key.
///
/// Args:
/// key (Key): Key to an equivalence.
///
/// Returns:
/// Int: Index to the node in the graph for the given key.
#[pyo3(name = "node_index")]
fn py_node_index(&self, key: &Key) -> usize {
self.node_index(key).index()
Expand Down Expand Up @@ -664,6 +687,7 @@ impl EquivalenceLibrary {
self.key_to_node_index.contains_key(&key)
}

/// Returns an iterator with all the Keys in the `EquivalenceLibrary`.
Comment thread
raynelfss marked this conversation as resolved.
Outdated
pub fn keys(&self) -> impl Iterator<Item = &Key> {
self.key_to_node_index.keys()
}
Expand Down Expand Up @@ -783,6 +807,8 @@ impl Display for EquivalenceError {
}
}

// Conversion helpers

fn to_pygraph<N, E>(py: Python, pet_graph: &StableDiGraph<N, E>) -> PyResult<PyObject>
where
N: IntoPy<PyObject> + Clone,
Expand Down