Skip to content

Conversation

raynelfss
Copy link
Contributor

Summary

This method was added to fix a performance regression introduced by #13921. This check should be quicker than the multiple calls to isinstance().

Details and comments

This new method for QuantumCircuit performs a fast path to determine if a circuit has a control flow operation present within. This is performed by using QuantumCircuit.count_ops() and checking if any of the names there belong to a ControlFlowOp.

Additionally, this method fixes an unintentional performance regression caused by #13921.

This method was added to fix a performance regression introduced by Qiskit#13921. This check should be quicker than the multiple calls to `isinstance`.
@raynelfss raynelfss added Changelog: None Do not include in changelog mod: circuit Related to the core of the `QuantumCircuit` class or the circuit library labels Feb 28, 2025
@raynelfss raynelfss added this to the 2.0.0 milestone Feb 28, 2025
@raynelfss raynelfss requested a review from a team as a code owner February 28, 2025 17:59
@qiskit-bot
Copy link
Collaborator

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

  • @Qiskit/terra-core

@mtreinish mtreinish added Changelog: New Feature Include in the "Added" section of the changelog and removed Changelog: None Do not include in changelog labels Feb 28, 2025
@jakelishman
Copy link
Member

I'm not sure we need to do this beyond the feature-freeze deadline. There's already no need to do all the isinstance checks - the original PR could have written

any(instruction.is_control_flow() for instruction in circuit.data)

@coveralls
Copy link

coveralls commented Feb 28, 2025

Pull Request Test Coverage Report for Build 15312306687

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 8 of 9 (88.89%) changed or added relevant lines in 3 files are covered.
  • 1209 unchanged lines in 50 files lost coverage.
  • Overall coverage decreased (-0.5%) to 87.861%

Changes Missing Coverage Covered Lines Changed/Added Lines %
crates/circuit/src/circuit_data.rs 5 6 83.33%
Files with Coverage Reduction New Missed Lines %
crates/quantum_info/src/convert_2q_block_matrix.rs 1 92.65%
crates/transpiler/src/passes/optimize_1q_gates_decomposition.rs 1 90.8%
qiskit/circuit/library/standard_gates/r.py 1 96.88%
qiskit/circuit/library/standard_gates/rxx.py 1 97.14%
qiskit/circuit/library/standard_gates/rzx.py 1 97.14%
qiskit/circuit/library/standard_gates/u.py 1 92.22%
qiskit/circuit/library/generalized_gates/gms.py 2 94.44%
qiskit/circuit/library/standard_gates/global_phase.py 2 91.3%
qiskit/circuit/library/standard_gates/ryy.py 2 94.29%
qiskit/circuit/library/standard_gates/rzz.py 2 93.94%
Totals Coverage Status
Change from base Build 15216054136: -0.5%
Covered Lines: 79558
Relevant Lines: 90550

💛 - Coveralls

Copy link
Member

@mtreinish mtreinish left a comment

Choose a reason for hiding this comment

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

Thanks for tackling this. I left a few comments inline. The biggest issue @jakelishman already pointed out that this shouldn't be a new public method for 2.0 so close to the release. There are paths to fixing the performance issue without adding a new method, we can add the public api in 2.1 though.

@mtreinish mtreinish modified the milestones: 2.0.0, 2.1.0 Mar 1, 2025
@mtreinish
Copy link
Member

In this interest of getting a fix for the regression included in 2.0 I pushed up: #13942 and moved this to 2.1. We should definitely do this after 2.0 is released.

Copy link
Member

@mtreinish mtreinish left a comment

Choose a reason for hiding this comment

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

Overall this LGTM now, but could you add some tests to test this method explicitly?

- One test case with a regular control flow operation and another with a custom one.
@@ -1550,6 +1551,82 @@ def instructions():
self.assertEqual(circuit, expected)
self.assertEqual(circuit.name, "test")

def test_circuit_has_control_flow_op(self):
Copy link
Member

Choose a reason for hiding this comment

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

I would expand this to cover a circuit with each control flow op type and make sure it's detected. The set of defined operations is here: https://github.com/Qiskit/qiskit/blob/main/qiskit/circuit/controlflow/__init__.py#L28

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed in 98c2e2c

@raynelfss raynelfss requested a review from mtreinish May 5, 2025 19:06
@mtreinish mtreinish enabled auto-merge May 28, 2025 23:01
@mtreinish mtreinish added this pull request to the merge queue May 28, 2025
Merged via the queue into Qiskit:main with commit 2ce6863 May 28, 2025
26 checks passed
rahaman-quantum pushed a commit to rahaman-quantum/qiskit that referenced this pull request Jun 20, 2025
* Add: Add method has_control_flow_op(), to `QuantumCircuit`

This method was added to fix a performance regression introduced by Qiskit#13921. This check should be quicker than the multiple calls to `isinstance`.

* Lint: Fix formatting

* Lint: Removed unused import and f-string

* Docs: Add release note
- Add `has_control_flow_op` to autogenerate docs.

* Fix: Addressing review comments

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>

* Docs: Fix formatting issue in `QuantumCircuit` docstring.

* Lint: Fix formatting bug in `QuantumCircuit`.

* Test: Add testcases
- One test case with a regular control flow operation and another with a custom one.

* Test: Remove unsupported test and extend original test.
- Implement a combinatorics based test for control flow operations.

* Fix: Import order in `test_circuit_operations`

* Update test/python/circuit/test_circuit_operations.py

---------

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: New Feature Include in the "Added" section of the changelog mod: circuit Related to the core of the `QuantumCircuit` class or the circuit library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants