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
4.85k stars 2.29k forks source link

Optimization level 3 gives more complex circuits #5491

Open wjy99-c opened 3 years ago

wjy99-c commented 3 years ago

Informations

Steps to reproduce the problem

When I follow the tutorial on https://qiskit.org/documentation/tutorials/circuits_advanced/04_transpiler_passes_and_passmanager.html, I try my circuit with the coupling map and basis_gate. I find Optimization_level=3 gives a more complex circuit than Optimization_level=1. But Optimization_level=3 does a heavy-weight optimization, right?

Here is my code:

def make_circuit(n:int) -> QuantumCircuit:
    # circuit begin
    prog = QuantumCircuit(n)
    prog.x(0)
    prog.cx(0,1)
    prog.x(0)
    prog.cx(0,1)
    prog.cx(0, 1)
    prog.h(0)
    prog.h(0)
    prog.h(0)
    # circuit end

    prog.measure_all()

    return prog

if __name__ == '__main__':

    prog = make_circuit(2)
    backend = BasicAer.get_backend('qasm_simulator')

    coupling_map = [[0,1]]
    basis_gate = ['cx', 'u3','u2']
    info = transpile(prog, backend=backend, coupling_map=coupling_map, basis_gates=basis_gate, optimization_level=3)
    print(info)

Opt_level=1 gives 3 gates but Opt_level=3 gives 5. Opt_level=3:

         ┌───────────────────────┐          ┌───────────┐       ░ ┌─┐   
q_0 -> 0 ┤ U3(pi,1.8925,-4.3906) ├──■───────┤ U2(-pi,0) ├───────░─┤M├───
         └────┬──────────────┬───┘┌─┴─┐┌────┴───────────┴─────┐ ░ └╥┘┌─┐
q_1 -> 1 ─────┤ U3(0,0,-2pi) ├────┤ X ├┤ U3(0,2.3982,-2.3982) ├─░──╫─┤M├
              └──────────────┘    └───┘└──────────────────────┘ ░  ║ └╥┘
 meas_0: ══════════════════════════════════════════════════════════╩══╬═
                                                                      ║ 
 meas_1: ═════════════════════════════════════════════════════════════╩═

Opt_level=1:

q_0 -> 0 ┤ U3(pi,0,pi) ├──■──┤ U2(pi,pi) ├─░─┤M├───
         └─────────────┘┌─┴─┐└───────────┘ ░ └╥┘┌─┐
q_1 -> 1 ───────────────┤ X ├──────────────░──╫─┤M├
                        └───┘              ░  ║ └╥┘
 meas_0: ═════════════════════════════════════╩══╬═
                                                 ║ 
 meas_1: ════════════════════════════════════════╩═

What is the expected behavior?

Optimization_level=3 should give at least same complex circuit as Optimization_level=1

Suggested solutions

jwoehr commented 2 years ago

Still valid in current release of Qiskit.