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 12 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
12 changes: 12 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 @@ -995,6 +996,17 @@ impl CircuitData {
self.param_table.clear();
}

pub fn count_ops(&mut self) -> IndexMap<String, i32> {
melechlapson marked this conversation as resolved.
Show resolved Hide resolved
let mut ops_count: IndexMap<String, i32> = IndexMap::new();
melechlapson marked this conversation as resolved.
Show resolved Hide resolved
let circuit: &Vec<PackedInstruction> = &self.data;
for instruction in circuit {
melechlapson marked this conversation as resolved.
Show resolved Hide resolved
*ops_count
.entry(instruction.op.view().name().to_string())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't actually need to build the OperationRef view here, PackedOperation has a name method you can just call. This is actually what I think blocked you from returning a &str as the key before.

Suggested change
.entry(instruction.op.view().name().to_string())
.entry(instruction.op.name().to_string())

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah for some reason I didn't see that and that made things more complicated for me

.or_insert(0) += 1;
}
ops_count
melechlapson marked this conversation as resolved.
Show resolved Hide resolved
}

// 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