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

Default optimization level does not perform self-inverse gate cancellations #7016

Closed
nonhermitian opened this issue Sep 14, 2021 · 3 comments · Fixed by #11210
Closed

Default optimization level does not perform self-inverse gate cancellations #7016

nonhermitian opened this issue Sep 14, 2021 · 3 comments · Fixed by #11210
Labels
type: enhancement It's working, but needs polishing

Comments

@nonhermitian
Copy link
Contributor

Information

  • Qiskit Terra version: latest
  • Python version:
  • Operating system:

What is the current behavior?

qc= QuantumCircuit(2)
qc.h(range(2))
qc.cz(0,1)
qc.draw()
     ┌───┐   
q_0: ┤ H ├─■─
     ├───┤ │ 
q_1: ┤ H ├─■─
     └───┘   
transpile(qc, basis_gates=['h', 'cx']).draw()
     ┌───┐               
q_0: ┤ H ├───────■───────
     ├───┤┌───┐┌─┴─┐┌───┐
q_1: ┤ H ├┤ H ├┤ X ├┤ H ├
     └───┘└───┘└───┘└───┘

Steps to reproduce the problem

What is the expected behavior?

I would expect the default level of optimization to remove trivial things like repeated H gates.

Suggested solutions

@nonhermitian nonhermitian added the bug Something isn't working label Sep 14, 2021
@kdk kdk changed the title Default optimization level does not perform trivial gate cancellations Default optimization level does not perform self-inverse gate cancellations Sep 14, 2021
@kdk
Copy link
Member

kdk commented Sep 14, 2021

Work was recently started in this direction by @vadebayo49 (see #6855 and #6576 ) but there is still some work to be done before this can be utilized by the default optimization levels, namely a way to identify (either through gate annotations or a library) which gates and gate pairs are inverse.

@kdk kdk added type: enhancement It's working, but needs polishing and removed bug Something isn't working labels Sep 14, 2021
@mtreinish
Copy link
Member

mtreinish commented Sep 14, 2021

This is caused by the OneQubitEulerDecomposer not knowing how to simplify things in terms of h (you can see a list of the basis it can work in here: https://qiskit.org/documentation/stubs/qiskit.quantum_info.OneQubitEulerDecomposer.html#qiskit.quantum_info.OneQubitEulerDecomposer). The level 1 optimization level doesn't contain any passes that know how to cancel two consecutive h gates. I think higher optimizations definitely will though.

I think we need to re-evaluate which passes we include in the different optimization levels because I think we can get away with more optimization in level 1 than we have right now.

@nonhermitian
Copy link
Contributor Author

It does work at level 2 and higher. However, given that the vast majority of users just do execute at the default level, perhaps level 1 should be beefed up some as you suggest.

mtreinish added a commit to mtreinish/qiskit-core that referenced this issue Nov 7, 2023
This commit updates the default optimization stage plugin to use the
InverseCancellation pass instead of CXCancellation for optimization
level 1. The CXCancellation pass was hard coded to only cancel runs of
CX gates on the same qubits. This was fine when CX is the target, but
for other targets which aren't using CX the pass had no value. An
alternative, more general, inverse cancellation pass was added in Qiskit#6855
that enables defining arbitrary inverse cancellation rules and
simplifying a dag based on it. This commit updates the default
optimization plugin at optimization level 1 to use this with some common
inverse rules for 2q gates from the standard gate library.

Closes: Qiskit#6576
Closes: Qiskit#7016
Related-to: Qiskit#7112
mtreinish added a commit to mtreinish/qiskit-core that referenced this issue Nov 7, 2023
This commit updates the default optimization stage plugin to use the
InverseCancellation pass instead of CXCancellation for optimization
level 1. The CXCancellation pass was hard coded to only cancel runs of
CX gates on the same qubits. This was fine when CX is the target, but
for other targets which aren't using CX the pass had no value. An
alternative, more general, inverse cancellation pass was added in Qiskit#6855
that enables defining arbitrary inverse cancellation rules and
simplifying a dag based on it. This commit updates the default
optimization plugin at optimization level 1 to use this with some common
inverse rules for 2q gates from the standard gate library.

Closes: Qiskit#6576
Closes: Qiskit#7016
Related-to: Qiskit#7112
github-merge-queue bot pushed a commit that referenced this issue Nov 8, 2023
)

* Use InverseCancellation in opt level 1 instead of CXCancellation

This commit updates the default optimization stage plugin to use the
InverseCancellation pass instead of CXCancellation for optimization
level 1. The CXCancellation pass was hard coded to only cancel runs of
CX gates on the same qubits. This was fine when CX is the target, but
for other targets which aren't using CX the pass had no value. An
alternative, more general, inverse cancellation pass was added in #6855
that enables defining arbitrary inverse cancellation rules and
simplifying a dag based on it. This commit updates the default
optimization plugin at optimization level 1 to use this with some common
inverse rules for 2q gates from the standard gate library.

Closes: #6576
Closes: #7016
Related-to: #7112

* Add sx sx dagger inverse pair to pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement It's working, but needs polishing
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants