Skip to content

Commit

Permalink
Port the count_ops method in QuantumCircuit to Rust (#13050)
Browse files Browse the repository at this point in the history
* added get_ops method to CircuitData Rust class

* Updated count_ops method in QuantumCircuit

* remove extra indexmap dependency

* fixed linting issues

* moved import

* removed return statement

* updated method names

* added release note

* add blank line

* revert changes to Cargo.lock

* removed unnecessary features

* Apply suggestions from Matthew's review

Co-authored-by: Matthew Treinish <[email protected]>

* added docstring to count_ops method

* Update crates/circuit/src/circuit_data.rs

Co-authored-by: Matthew Treinish <[email protected]>

---------

Co-authored-by: Matthew Treinish <[email protected]>
  • Loading branch information
melechlapson and mtreinish authored Aug 30, 2024
1 parent 623415c commit 5fc1635
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
17 changes: 17 additions & 0 deletions crates/circuit/src/circuit_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use pyo3::types::{IntoPyDict, PyDict, PyList, PySet, PyTuple, PyType};
use pyo3::{import_exception, intern, PyTraverseError, PyVisit};

use hashbrown::{HashMap, HashSet};
use indexmap::IndexMap;
use smallvec::SmallVec;

import_exception!(qiskit.circuit.exceptions, CircuitError);
Expand Down Expand Up @@ -983,6 +984,22 @@ impl CircuitData {
self.param_table.clear();
}

/// Counts the number of times each operation is used in the circuit.
///
/// # Parameters
/// - `self` - A mutable reference to the CircuitData struct.
///
/// # Returns
/// An IndexMap containing the operation names as keys and their respective counts as values.
pub fn count_ops(&self) -> IndexMap<&str, usize, ::ahash::RandomState> {
let mut ops_count: IndexMap<&str, usize, ::ahash::RandomState> = IndexMap::default();
for instruction in &self.data {
*ops_count.entry(instruction.op.name()).or_insert(0) += 1;
}
ops_count.par_sort_by(|_k1, v1, _k2, v2| v2.cmp(v1));
ops_count
}

// Marks this pyclass as NOT hashable.
#[classattr]
const __hash__: Option<Py<PyAny>> = None;
Expand Down
6 changes: 2 additions & 4 deletions qiskit/circuit/quantumcircuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -3516,10 +3516,8 @@ def count_ops(self) -> "OrderedDict[Instruction, int]":
Returns:
OrderedDict: a breakdown of how many operations of each kind, sorted by amount.
"""
count_ops: dict[Instruction, int] = {}
for instruction in self._data:
count_ops[instruction.operation.name] = count_ops.get(instruction.operation.name, 0) + 1
return OrderedDict(sorted(count_ops.items(), key=lambda kv: kv[1], reverse=True))
ops_dict = self._data.count_ops()
return OrderedDict(ops_dict)

def num_nonlocal_gates(self) -> int:
"""Return number of non-local gates (i.e. involving 2+ qubits).
Expand Down
5 changes: 5 additions & 0 deletions releasenotes/notes/port-countops-method-3ad362c20b13182c.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
features_circuits:
- |
The :meth:`~.QuantumCircuit.count_ops` method in :class:`.QuantumCircuit`
has been re-written in Rust. It now runs between 3 and 9 times faster.

0 comments on commit 5fc1635

Please sign in to comment.