Skip to content

Commit

Permalink
Don't change interface for DAGCircuit
Browse files Browse the repository at this point in the history
Previously this PR was making the op_names field of the DAGCircuit
struct public so it was accessible to the new transpiler pass code.
However, this opened up the possibility of mutating the field by
mistake, instead this makes the field private again and adds a no-copy
method to get an immutable reference to the field. Additionally, there
were some interface changes made to one method in DAGCircuit that were
not needed anymore, which this reverts to minimize the diff.
  • Loading branch information
mtreinish committed Sep 10, 2024
1 parent 2d8d9ed commit cc94600
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
2 changes: 1 addition & 1 deletion crates/accelerate/src/commutation_cancellation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub(crate) fn cancel_commutations(
HashSet::new()
};
let z_var_gate = dag
.op_names
.get_op_counts()
.keys()
.find_map(|g| {
VAR_Z_MAP
Expand Down
15 changes: 12 additions & 3 deletions crates/circuit/src/dag_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ pub struct DAGCircuit {
var_output_map: _VarIndexMap,

/// Operation kind to count
pub op_names: IndexMap<String, usize, RandomState>,
op_names: IndexMap<String, usize, RandomState>,

// Python modules we need to frequently access (for now).
control_flow_module: PyControlFlowModule,
Expand Down Expand Up @@ -6228,7 +6228,7 @@ impl DAGCircuit {
&mut self,
new_gate: (StandardGate, &[f64]),
old_index: NodeIndex,
) -> NodeIndex {
) {
self.increment_op(new_gate.0.name());
let old_node = &self.dag[old_index];
let inst = if let NodeType::Operation(old_node) = old_node {
Expand All @@ -6255,7 +6255,6 @@ impl DAGCircuit {
self.dag.add_edge(parent_index, new_index, weight.clone());
self.dag.add_edge(new_index, old_index, weight);
self.dag.remove_edge(edge_index);
new_index
}

/// Remove a sequence of 1 qubit nodes from the dag
Expand Down Expand Up @@ -6455,6 +6454,16 @@ impl DAGCircuit {
}
}

/// Get an immutable reference to the op counts for this DAGCircuit
///
/// This differs from count_ops() in that it doesn't handle control flow recursion at all
/// and it returns a reference instead of an owned copy. If you don't need to work with
/// control flow or ownership of the counts this is a more efficient alternative to
/// `DAGCircuit::count_ops(py, false)`
pub fn get_op_counts(&self) -> &IndexMap<String, usize, RandomState> {
&self.op_names
}

/// Extends the DAG with valid instances of [PackedInstruction]
pub fn extend<I>(&mut self, py: Python, iter: I) -> PyResult<Vec<NodeIndex>>
where
Expand Down

0 comments on commit cc94600

Please sign in to comment.