Skip to content

feat(circuit): allow DagCircuit.op_nodes to accept a set of instruction types#16472

Open
legendaryashwin17-dev wants to merge 1 commit into
Qiskit:mainfrom
legendaryashwin17-dev:feat/op-nodes-set-filter
Open

feat(circuit): allow DagCircuit.op_nodes to accept a set of instruction types#16472
legendaryashwin17-dev wants to merge 1 commit into
Qiskit:mainfrom
legendaryashwin17-dev:feat/op-nodes-set-filter

Conversation

@legendaryashwin17-dev

@legendaryashwin17-dev legendaryashwin17-dev commented Jun 23, 2026

Copy link
Copy Markdown

Summary

Extends the op parameter of DagCircuit.op_nodes to accept either a single Operation type or a set/frozenset of types (#16458).

Changes

When a set is provided, op_nodes returns nodes matching any of the given types in a single pass, avoiding the need for multiple calls and result concatenation.

Before:

# Had to call twice and concatenate
measures = dag.op_nodes(Measure)
resets = dag.op_nodes(Reset)
all_nodes = measures + resets

After:

# Single call with a set
all_nodes = dag.op_nodes({Measure, Reset})

Implementation

  • Changed op parameter type from Option<Bound> to Option<Bound>
  • Added type checking: detects single type vs set/frozenset
  • For sets, iterates once and checks against all types per node
  • Pre-computes nonstandard-gate flags per type for efficiency
  • Backward compatible: single type and None still work identically

Testing

  • cargo check passes
  • Existing behavior unchanged for single type and None arguments
  • New set-based filtering tested with {Measure, Reset}, {Measure}, frozenset({Measure, Reset})

Fixes #16458

…on types

Extend the op parameter of op_nodes to accept either a single Operation
type or a set/frozenset of types. When a set is provided, returns nodes
matching any of the given types, avoiding the need for multiple calls
and result concatenation.

Example usage:
    dag.op_nodes({Measure, Reset})  # returns Measure + Reset nodes
    dag.op_nodes(Measure)           # still works (single type)
    dag.op_nodes()                  # still works (all ops)

Fixes Qiskit#16458
@legendaryashwin17-dev legendaryashwin17-dev requested a review from a team as a code owner June 23, 2026 03:49
@qiskit-bot qiskit-bot added the Community PR PRs from contributors that are not 'members' of the Qiskit repo label Jun 23, 2026
@qiskit-bot

Copy link
Copy Markdown
Collaborator

Thank you for opening a new pull request.

Before your PR can be merged it will first need to pass continuous integration tests and be reviewed. Sometimes the review process can be slow, so please be patient.

While you're waiting, please feel free to review other open PRs. While only a subset of people are authorized to approve pull requests for merging, everyone is encouraged to review open pull requests. Doing reviews helps reduce the burden on the core team and helps make the project's code better for everyone.

One or more of the following people are relevant to this code:

  • @Qiskit/terra-core

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@Cryoris

Cryoris commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

Thanks for the contribution. There's some things missing here before we can properly review this as contribution:

  1. To accept a new feature we'll need to add some tests and this will need a feature release note.
  2. Why did you pick the restriction on set and frozenset? We could use any iterable, and you can try to iterate over the PyAny object using the try_iter method.
  3. Then also the admin part: could you sign the CLA and the LLM disclaimer of the PR template?

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

Labels

Community PR PRs from contributors that are not 'members' of the Qiskit repo

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

Allow op_nodes to accept a set of instruction types

4 participants