Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port the count_ops method in QuantumCircuit to Rust #13050

Merged
merged 16 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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.
Loading