Qiskit / qiskit

Qiskit is an open-source SDK for working with quantum computers at the level of extended quantum circuits, operators, and primitives.
https://www.ibm.com/quantum/qiskit
Apache License 2.0
5.31k stars 2.38k forks source link

small rotation angles (parametrised) not removed by transpiler passes #6468

Open howie-kuo opened 3 years ago

howie-kuo commented 3 years ago

Information

What is the current behavior?

small rotations in generated ansatz are not removed by the transpiler.

image

Steps to reproduce the problem

to generate the circuit:

from qiskit import QuantumCircuit, Aer
from qiskit.circuit.parameter import Parameter
from qiskit.compiler import transpile
from numpy import pi
x = Parameter('x')
backend = Aer.get_backend('statevector_simulator')
qc = QuantumCircuit(2)
qc.h(0)
qc.cx(0,1)
qc.ry(-1.38777878078145e-17*x, 0)
qc.cx(0,1)
qc.h(0)
qc.draw()

image

and

qc = transpile(qc, backend=backend, seed_transpiler=0, optimization_level=3)
qc.draw()

image

What is the expected behavior?

the rotation with a small angle may be removed.

Suggested solutions

add a traspile pass that check for this condition.

peendebak commented 3 years ago

@howie-kuo Can you have a look at https://github.com/Qiskit/qiskit-terra/pull/6472? It adds a pass to remove small rotations. (have not tested it a lot yet)

kdk commented 3 years ago

This is an interesting one. Since the gates are parameterized (and we don't presently have bounds for circuit parameters), there is the possibility in general that the parameter value could be sufficiently large to make the rotation substantial, as you mentioned in the Qiskit slack. Once the parameter values are known, and the circuit is re-transpiled prior to device execution, these small rotations will be removed.

There may however be a case in the algorithms, where parameter bounds are known, and removing these rotations can be beneficial (maybe in removing a variable for the classical optimizer), but I'm not an expert there. @Cryoris might have some thoughts.

howie-kuo commented 3 years ago

I have been exploring and come up with a alternative and possibly more informative case. image

in this case, we have two stages, the first sandwidges some operation between a rotate forward, and rotate back. Very common when implementing $HxH^\dagger$ type of operations and the small angle was first discovered when I was playing with ANSATZ in the 2021 challenge which is a natural place for this to occure.

In the above example, two of this stages are put together one after another, and the inner pair of rotate back and rotate forward can be cancelled.

there are two thing that need to happen here. Firstly, it will be good if we can merge the two rotate that shares a common factor (parameter) and reduce it to (pi/2-pi/2)*x, and then later recongnise when the rounding of pi/2 and -pi/2 in double precision produce a episilon residule, to remove it altogether.

I think the first pass of factoring out the common parameter and collapsed the coefficients would be a useful addition. The second of rounding this coefficient to 0 will need more thought.