Skip to content

feature: count circuit operation on circuit or given qubits#1278

Open
skushagra wants to merge 1 commit into
amazon-braket:mainfrom
skushagra:feat/circuits-instruction-counter
Open

feature: count circuit operation on circuit or given qubits#1278
skushagra wants to merge 1 commit into
amazon-braket:mainfrom
skushagra:feat/circuits-instruction-counter

Conversation

@skushagra

@skushagra skushagra commented Jun 4, 2026

Copy link
Copy Markdown

*Issue #1235

Description of changes:
I have added a count_instruction functions that gives you count of an instrcution on a circuit. I have added test cases for the required flow.

Features

  • Count instructions by operator type.
  • Optional filtering by:
    • Operator name, operator class, or operator instance.
    • Qubits involved in an instruction.
    • Moment types (GATE, GATE_NOISE, MEASURE etc.).
  • Supports multi-qubit filtering via:
    • QubitMatch.ANY — instruction on any specified qubit.
    • QubitMatch.ALL — instruction on all specified qubits.
  • When both operator and qubit filters are provided, instructions must satisfy both filters (AND).

Merge Checklist

Put an x in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your pull request.

General

Tests

  • I have added tests that prove my fix is effective or that my feature works (if appropriate)
  • I have checked that my tests are not configured for a specific region or account (if appropriate)

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@skushagra skushagra requested a review from a team as a code owner June 4, 2026 11:22
@skushagra skushagra changed the title feature:count circuit operation on circuit or given qubits feature: count circuit operation on circuit or given qubits Jun 4, 2026
@skushagra

Copy link
Copy Markdown
Author

I did not understand much around what can we do for calculating density of operations. I would need more clarification on how should we exactly define this density, and since there can be multiple ways with respect to which density can be calculated like, operations per layer, per qubit etc.

Hence I extracted this into a seprate helper file to prevent bloating the Circuit class. Perhaps more clarification on this would help me to add such a features.

return [_normalize_operator_name(op) for op in operators]


def count_instructions(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

why not make this a private function instead, or I do not mind moving it all to circuit.py

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

we can move it all to circuit.py but this keeping(or renaming circuit_analysis.py) seems to be a very nice option that will keep the circuit class clean. But if it goes against the normal practives that we follow, I'll move it back.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yes, I would prefer this to be in the circuit.py

include_types_set = set(include_types)
operator_names_set = set(_to_operator_names(operators))
_qs = QubitSet(qubits) if qubits is not None else None
filter_qubits = _qs if _qs else None # empty QubitSet treated as no filter

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

check for case when qubit specified is not part of the circuit

>>> circ.count_instructions(qubits=0)
Counter({'H': 1, 'CNot': 1, 'Rx': 1})
"""
from braket.circuits.circuit_analysis import count_instructions as _count_instructions

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

move all imports to the top

"""
return self._parameters

def count_instructions(self, operators=None, **kwargs) -> Counter[str]:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The function name should ideally be count()

@aniksd-braket

Copy link
Copy Markdown
Contributor

@skushagra could you also make sure to resolve all comments for the next updated CR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants