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

Extend CommutativeInverseCancellation to cancel pairs of gates up-to-phase #11248

Merged
merged 11 commits into from
Jan 25, 2024

Conversation

alexanderivrii
Copy link
Contributor

Summary

Extending CommutativeInverseCancellation transpiler pass to cancel pairs of gates that are inverse up to a phase. Updates the global phase of the circuit accordingly.

Here is a small example:

import numpy as np
from qiskit.circuit import QuantumCircuit
from qiskit.transpiler import PassManager
from qiskit.transpiler.passes import CommutativeInverseCancellation

circuit = QuantumCircuit(1)
circuit.rz(np.pi / 4, 0)
circuit.p(-np.pi / 4, 0)

passmanager = PassManager(CommutativeInverseCancellation(upto_phase_optimization=True))
new_circuit = passmanager.run(circuit)

The pass is able to cancel the RZ and P gates, adjusting the global phase of the circuit to 15 \pi / 8.

Details:

The pass has two new arguments max_qubits and upto_phase_optimization. The new optimization is enabled by setting upto_phase_optimization = True. Generally this leads to more reductions at the expense of increased runtime. Internally, the check is based on constructing and comparing unitary matrices corresponding to various gates in the circuit. The argument max_qubits limits the number of qubits in matrix-based commutativity and inverse checks.

@alexanderivrii alexanderivrii requested review from ikkoham and a team as code owners November 15, 2023 14:35
@qiskit-bot
Copy link
Collaborator

One or more of the the following people are requested to review this:

  • @Qiskit/terra-core
  • @ikkoham

@alexanderivrii alexanderivrii added the mod: transpiler Issues and PRs related to Transpiler label Nov 15, 2023
@alexanderivrii alexanderivrii added this to the 0.46.0 milestone Nov 15, 2023
@coveralls
Copy link

coveralls commented Nov 15, 2023

Pull Request Test Coverage Report for Build 7656730918

Warning: This coverage report may be inaccurate.

We've detected an issue with your CI configuration that might affect the accuracy of this pull request's coverage report.
To ensure accuracy in future PRs, please see these guidelines.
A quick fix for this PR: rebase it; your next report should be accurate.

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.02%) to 89.514%

Totals Coverage Status
Change from base Build 7645403491: -0.02%
Covered Lines: 59559
Relevant Lines: 66536

💛 - Coveralls

@alexanderivrii alexanderivrii changed the title pass, tests, reno Extend CommutativeInverseCancellation to cancel pairs of gates up-to-phase Nov 15, 2023
@jakelishman jakelishman modified the milestones: 0.46.0, 1.0.0 Nov 15, 2023
Copy link
Contributor

@Cryoris Cryoris left a comment

Choose a reason for hiding this comment

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

It seems like this is a much stronger comparison than just up to global phase: any two adjacent gates that implement the same operation can now be cancelled, whereas before we only cancelled them if they were the same type. It might be worth for the input argument to reflect that.

@Cryoris
Copy link
Contributor

Cryoris commented Jan 23, 2024

LGTM except I still think this does more than just ignoring global phase. If I define two custom adjacent gates who will cancel each other, this new PR will be able to cancel them -- no matter how these gates are called. This wasn't possible before, was it? If this is correct, how about renaming upto_phase_optimization to something like matrix_based?

@alexanderivrii
Copy link
Contributor Author

Thanks @Cryoris, I did the renaming as per review comments and offline conversation.

Copy link
Contributor

@Cryoris Cryoris left a comment

Choose a reason for hiding this comment

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

Small comments, pretty much looks good to go!

Copy link
Contributor

@Cryoris Cryoris left a comment

Choose a reason for hiding this comment

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

LGTM thanks for the adjustments!

@Cryoris Cryoris added the Changelog: New Feature Include in the "Added" section of the changelog label Jan 25, 2024
@Cryoris Cryoris added this pull request to the merge queue Jan 25, 2024
Merged via the queue into Qiskit:main with commit 8c29cee Jan 25, 2024
12 checks passed
@alexanderivrii alexanderivrii deleted the cic-uptophase branch January 25, 2024 18:52
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: transpiler Issues and PRs related to Transpiler
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants