Skip to content

feature: Added counts based on operator name and qubits specified#1275

Open
ACE07-Sev wants to merge 3 commits into
amazon-braket:mainfrom
ACE07-Sev:count-ops
Open

feature: Added counts based on operator name and qubits specified#1275
ACE07-Sev wants to merge 3 commits into
amazon-braket:mainfrom
ACE07-Sev:count-ops

Conversation

@ACE07-Sev

Copy link
Copy Markdown

Issue #, if available: Closes #1235

Description of changes:
Added instruction counting to Circuit with additional filtering based on specific gate name and/or qubits applied to.

Testing done:
Added a unit tester to test_circuit.py which asserts the desired behavior, and ensures a ValueError is raised for out of bounds qubits specified.

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.

@ACE07-Sev ACE07-Sev requested a review from a team as a code owner June 4, 2026 03:48
@ACE07-Sev

Copy link
Copy Markdown
Author

I added the qubit specifier you requested, but not quite sure what you meant by density of operators. If you can kindly guide me on that, I can add that as well today.

@ACE07-Sev

Copy link
Copy Markdown
Author

@sesmart Can I please get a review?

@sesmart

sesmart commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

Hi @ACE07-Sev , @aniksd-braket will be reviewing this issue.

@codecov

codecov Bot commented Jun 8, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (a5ee47d) to head (5d8d274).

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #1275   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          169       169           
  Lines        10963     10975   +12     
  Branches      1412      1416    +4     
=========================================
+ Hits         10963     10975   +12     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jaimeajl

jaimeajl commented Jun 9, 2026

Copy link
Copy Markdown

This looks like a useful direction. I think the “operator density” stretch goal from the issue could be implemented as a small complementary layer on top of raw instruction counts.

My interpretation would be a deterministic circuit summary that includes both counts and normalized densities, for example:

  • total instruction count
  • count by operator name
  • count filtered by qubits
  • operator density: operator_count / total_instruction_count
  • per-qubit instruction count
  • per-qubit instruction density

This would make the feature useful not only as a convenience method, but also for benchmarking, sanity checks, and comparing generated or transformed circuits.

A possible output shape could be:

{
"total": 10,
"by_operator": {"h": 2, "cnot": 3, "rz": 5},
"operator_density": {"h": 0.2, "cnot": 0.3, "rz": 0.5},
"per_qubit": {0: 6, 1: 4},
"per_qubit_density": {0: 0.6, 1: 0.4}
}

If this interpretation aligns with the maintainers’ expectations, I can open a focused follow-up PR for the density/summary part, without duplicating the existing count implementation.

@ACE07-Sev

Copy link
Copy Markdown
Author

@jaimeajl may I ask if you're part of the maintainer team?

@ACE07-Sev

Copy link
Copy Markdown
Author

@aniksd-braket Can I please get a review?

@aniksd-braket aniksd-braket left a comment

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.

Would be great if you could also add the case of getting counts for of operations on all qubits specified, instead of just any of the qubits specified


for instruction in self.instructions:
if qubits and not set(instruction.target).intersection(qubits):
continue

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.

Would the output be an empty dictionary if a qubit exists in the circuit but has no operation? Could you add a test for this case?

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.

You mean if I have this

from braket.circuits import Circuit

circuit = Circuit().h(0).cnot(0, 2)
print(circuit.count(qubits={1}))

and wondering what will return for this?

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.

f"Invalid qubits: {qubits - self.qubits}"
)

for instruction in self.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.

Here do you consider all operators, including noise operations?

@ACE07-Sev ACE07-Sev Jun 12, 2026

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.

Any instruction applied to the circuit (if we go with just ones applied to qubits then gphase would need to be skipped I suppose). Are there exclusions you want me to skip?

@ACE07-Sev

Copy link
Copy Markdown
Author

Greetings @aniksd-braket

Thank you very much for the review. Sure thing, I'll get to it first thing in the morning.

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.

Easily count instructions in a circuit

4 participants