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

Fix QuantumCircuit.depth with zero-operands and Expr nodes #12429

Merged
merged 1 commit into from
Jun 7, 2024

Conversation

jakelishman
Copy link
Member

Summary

This causes QuantumCircuit.depth to correctly handle cases where a circuit instruction has zero operands (such as GlobalPhaseGate), and to treat classical bits and real-time variables used inside Expr conditions as part of the depth calculations. This is in line with DAGCircuit.

This commit still does not add the same recurse argument from DAGCircuit.depth, because the arguments for not adding it to QuantumCircuit.depth at the time still hold; there is no clear meaning to it for general control flow from a user's perspective, and it was only added to the DAGCircuit methods because there it is more of a proxy for optimising over all possible inner blocks.

Details and comments

Fix #12426.

This causes `QuantumCircuit.depth` to correctly handle cases where a
circuit instruction has zero operands (such as `GlobalPhaseGate`), and
to treat classical bits and real-time variables used inside `Expr`
conditions as part of the depth calculations.  This is in line with
`DAGCircuit`.

This commit still does not add the same `recurse` argument from
`DAGCircuit.depth`, because the arguments for not adding it to
`QuantumCircuit.depth` at the time still hold; there is no clear meaning
to it for general control flow from a user's perspective, and it was
only added to the `DAGCircuit` methods because there it is more of a
proxy for optimising over all possible inner blocks.
@jakelishman jakelishman added stable backport potential The bug might be minimal and/or import enough to be port to stable Changelog: Bugfix Include in the "Fixed" section of the changelog mod: circuit Related to the core of the `QuantumCircuit` class or the circuit library labels May 17, 2024
@jakelishman jakelishman added this to the 1.1.1 milestone May 17, 2024
@jakelishman jakelishman requested a review from a team as a code owner May 17, 2024 12:41
@qiskit-bot
Copy link
Collaborator

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

  • @Qiskit/terra-core

@coveralls
Copy link

Pull Request Test Coverage Report for Build 9128390726

Details

  • 24 of 24 (100.0%) changed or added relevant lines in 1 file are covered.
  • 10 unchanged lines in 2 files lost coverage.
  • Overall coverage decreased (-0.001%) to 89.605%

Files with Coverage Reduction New Missed Lines %
crates/qasm2/src/lex.rs 4 93.13%
crates/qasm2/src/parse.rs 6 97.15%
Totals Coverage Status
Change from base Build 9121595300: -0.001%
Covered Lines: 62294
Relevant Lines: 69521

💛 - Coveralls

Copy link
Contributor

@ElePT ElePT left a comment

Choose a reason for hiding this comment

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

LGTM! I only have one minor comment.

@@ -3307,6 +3307,9 @@ def depth(
) -> int:
"""Return circuit depth (i.e., length of critical path).

.. warning::
This operation is not well defined if the circuit contains control-flow operations.
Copy link
Contributor

Choose a reason for hiding this comment

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

would it make sense to add more info in this warning? would it be inaccurate to say, for example:

Suggested change
This operation is not well defined if the circuit contains control-flow operations.
This operation is not well defined if the circuit contains control-flow operations, but
their variables and clbits are counted in the depth calculation.

Copy link
Member Author

Choose a reason for hiding this comment

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

I was mostly deliberately trying to avoid saying anything about what happens in the return value of this if there's control-flow operations, since the whole result is ill defined. I didn't want to commit to anything one way or the other.

Copy link
Contributor

Choose a reason for hiding this comment

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

I suspected that was the case, and I think that makes sense too.

@@ -3307,6 +3307,9 @@ def depth(
) -> int:
"""Return circuit depth (i.e., length of critical path).

.. warning::
This operation is not well defined if the circuit contains control-flow operations.
Copy link
Contributor

Choose a reason for hiding this comment

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

I suspected that was the case, and I think that makes sense too.

@jakelishman jakelishman added this pull request to the merge queue Jun 7, 2024
Merged via the queue into Qiskit:main with commit d18a74c Jun 7, 2024
15 checks passed
mergify bot pushed a commit that referenced this pull request Jun 7, 2024
This causes `QuantumCircuit.depth` to correctly handle cases where a
circuit instruction has zero operands (such as `GlobalPhaseGate`), and
to treat classical bits and real-time variables used inside `Expr`
conditions as part of the depth calculations.  This is in line with
`DAGCircuit`.

This commit still does not add the same `recurse` argument from
`DAGCircuit.depth`, because the arguments for not adding it to
`QuantumCircuit.depth` at the time still hold; there is no clear meaning
to it for general control flow from a user's perspective, and it was
only added to the `DAGCircuit` methods because there it is more of a
proxy for optimising over all possible inner blocks.

(cherry picked from commit d18a74c)
@jakelishman jakelishman deleted the fix-depth-0q branch June 7, 2024 12:37
github-merge-queue bot pushed a commit that referenced this pull request Jun 7, 2024
… (#12528)

This causes `QuantumCircuit.depth` to correctly handle cases where a
circuit instruction has zero operands (such as `GlobalPhaseGate`), and
to treat classical bits and real-time variables used inside `Expr`
conditions as part of the depth calculations.  This is in line with
`DAGCircuit`.

This commit still does not add the same `recurse` argument from
`DAGCircuit.depth`, because the arguments for not adding it to
`QuantumCircuit.depth` at the time still hold; there is no clear meaning
to it for general control flow from a user's perspective, and it was
only added to the `DAGCircuit` methods because there it is more of a
proxy for optimising over all possible inner blocks.

(cherry picked from commit d18a74c)

Co-authored-by: Jake Lishman <jake.lishman@ibm.com>
Procatv pushed a commit to Procatv/qiskit-terra-catherines that referenced this pull request Aug 1, 2024
…t#12429)

This causes `QuantumCircuit.depth` to correctly handle cases where a
circuit instruction has zero operands (such as `GlobalPhaseGate`), and
to treat classical bits and real-time variables used inside `Expr`
conditions as part of the depth calculations.  This is in line with
`DAGCircuit`.

This commit still does not add the same `recurse` argument from
`DAGCircuit.depth`, because the arguments for not adding it to
`QuantumCircuit.depth` at the time still hold; there is no clear meaning
to it for general control flow from a user's perspective, and it was
only added to the `DAGCircuit` methods because there it is more of a
proxy for optimising over all possible inner blocks.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: Bugfix Include in the "Fixed" section of the changelog mod: circuit Related to the core of the `QuantumCircuit` class or the circuit library stable backport potential The bug might be minimal and/or import enough to be port to stable
Projects
None yet
Development

Successfully merging this pull request may close these issues.

GlobalPhaseGate causing circuit.depth() error
4 participants