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

improving quantum_causal_cone method in python #12668

Merged
merged 2 commits into from
Jul 1, 2024

Conversation

alexanderivrii
Copy link
Contributor

@alexanderivrii alexanderivrii commented Jun 26, 2024

Summary

When trying to port the method DAGCircuit.quantum_causal_cone (introduced in #10325) to Rust, I have noticed that the existing algorithm examines each node multiple times (when reached from different paths), making the algorithm worst-case exponential.

For a specific example, consider the circuit

q0_0: ──■─────────■─────────■─────────■─────────■─────────■───────
      ┌─┴─┐     ┌─┴─┐     ┌─┴─┐     ┌─┴─┐     ┌─┴─┐     ┌─┴─┐     
q0_1: ┤ X ├──■──┤ X ├──■──┤ X ├──■──┤ X ├──■──┤ X ├──■──┤ X ├──■──
      └───┘┌─┴─┐└───┘┌─┴─┐└───┘┌─┴─┐└───┘┌─┴─┐└───┘┌─┴─┐└───┘┌─┴─┐
q0_2: ──■──┤ X ├──■──┤ X ├──■──┤ X ├──■──┤ X ├──■──┤ X ├──■──┤ X ├      ...
      ┌─┴─┐└───┘┌─┴─┐└───┘┌─┴─┐└───┘┌─┴─┐└───┘┌─┴─┐└───┘┌─┴─┐└───┘
q0_3: ┤ X ├─────┤ X ├─────┤ X ├─────┤ X ├─────┤ X ├─────┤ X ├─────
      └───┘     └───┘     └───┘     └───┘     └───┘     └───┘     
q0_4: ────────────────────────────────────────────────────────────

In this PR we hash the set of processed non-directive nodes.

Let me also explain the point about not hashing "directive" nodes such as barriers (or measures). A barrier may involve all the qubits in the circuit, however not all of these should be considered to be a part of the causal cone. So when processing a barrier node the original algorithm only adds the quantum predecessors of that barrier node that intersect the current set of qubits in the quantum cone. However, this does potentially require to examine each barrier multiple times. (Aside: I am still not fully certain that this logic is 100% correct, however I was not able to find an example where this does not do what's intended). See also #10325.

This PR does not change this logic, however for clarity the two cases (directives and not-directives) are treated separately (and there are some other minor renaming and other minor code simplifications). It also adds more Python tests that explore the corner-cases.

It would be nice to have this PR merged before adding the Rust version of this to the new DAGCircuit, as it can serve as a reference for that implementation and also adds more python tests.

@alexanderivrii alexanderivrii requested a review from a team as a code owner June 26, 2024 10:35
@qiskit-bot
Copy link
Collaborator

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

  • @Qiskit/terra-core

@alexanderivrii alexanderivrii added this to the 1.2.0 milestone Jun 26, 2024
@coveralls
Copy link

coveralls commented Jun 26, 2024

Pull Request Test Coverage Report for Build 9678104465

Details

  • 15 of 15 (100.0%) changed or added relevant lines in 1 file are covered.
  • 5 unchanged lines in 1 file lost coverage.
  • Overall coverage increased (+0.02%) to 89.776%

Files with Coverage Reduction New Missed Lines %
crates/qasm2/src/lex.rs 5 92.88%
Totals Coverage Status
Change from base Build 9676629284: 0.02%
Covered Lines: 63802
Relevant Lines: 71068

💛 - Coveralls

raynelfss
raynelfss previously approved these changes Jun 26, 2024
Copy link
Contributor

@raynelfss raynelfss left a comment

Choose a reason for hiding this comment

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

This LGTM, thank you for improving this function!

@raynelfss raynelfss added this pull request to the merge queue Jun 26, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Jun 26, 2024
Comment on lines 2 to 5
upgrade_circuits:
- |
Improved the method :meth:`.DAGCircuit.quantum_causal_cone` to avoid examining the same
non-directive node multiple times when reached from different paths.
Copy link
Member

Choose a reason for hiding this comment

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

This isn't really an upgrade note, an upgrade note is reserved for something that we want to document might require user input when upgrading from the previous release to the new one. Examples would be a breaking api change, minimum dependency version bump, etc. I would expect this to be more of a feature note (to indicate improved performance) or a bug fix release note if we want to document this in the release.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, this is not the first time I am making this mistake, but I will eventually remember what "upgrade" stands for.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done in 23877fe

@coveralls
Copy link

coveralls commented Jun 27, 2024

Pull Request Test Coverage Report for Build 9692828532

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

  • 15 of 15 (100.0%) changed or added relevant lines in 1 file are covered.
  • 93 unchanged lines in 3 files lost coverage.
  • Overall coverage increased (+0.02%) to 89.78%

Files with Coverage Reduction New Missed Lines %
crates/qasm2/src/lex.rs 5 92.88%
qiskit/circuit/quantumcircuit.py 16 95.69%
crates/circuit/src/operations.rs 72 72.35%
Totals Coverage Status
Change from base Build 9676629284: 0.02%
Covered Lines: 63818
Relevant Lines: 71083

💛 - Coveralls

Copy link
Contributor

@raynelfss raynelfss left a comment

Choose a reason for hiding this comment

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

Thank you for fixing the release note, this should be good to merge.

@raynelfss raynelfss added this pull request to the merge queue Jul 1, 2024
Merged via the queue into Qiskit:main with commit 5deed7a Jul 1, 2024
15 checks passed
@ElePT ElePT added the Changelog: New Feature Include in the "Added" section of the changelog label Jul 30, 2024
Procatv pushed a commit to Procatv/qiskit-terra-catherines that referenced this pull request Aug 1, 2024
* improving quantum_causal_cone

* fixing release note
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants