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.11k stars 2.34k forks source link

ConsolidateBlocks drops classical conditional #3215

Closed kdk closed 4 years ago

kdk commented 4 years ago

From https://travis-ci.com/Qiskit/qiskit-terra/jobs/242965523#L6482 :

Information

Attaching a callback to print the pass name and output dag, it looks like the conditional is lost in ConsolidateBlocks.

RemoveResetInZeroState
            ┌───────┐┌───┐┌───────┐┌───┐┌─────────┐┌─┐
q6476_0: |0>┤ U1(0) ├┤ X ├┤ U1(0) ├┤ X ├┤ U2(0,0) ├┤M├
            └───────┘└─┬─┘└───────┘└─┬─┘└────┬────┘└╥┘
q6476_1: |0>───────────┼─────────────┼───────┼──────╫─
                       │             │       │      ║
q6476_2: |0>───────────┼─────────────┼───────┼──────╫─
                       │             │       │      ║
q6476_3: |0>───────────┼─────────────┼───────┼──────╫─
                       │             │       │      ║
q6476_4: |0>───────────┼─────────────┼───────┼──────╫─
                       │             │       │      ║
q6476_5: |0>───────────┼─────────────┼───────┼──────╫─
                       │             │       │      ║
q6476_6: |0>───────────┼─────────────┼───────┼──────╫─
                       │             │       │      ║
q6476_7: |0>───────────■─────────────■───────┼──────╫─
                                             │      ║
q6477_0: |0>─────────────────────────────────┼──────╫─
                                             │      ║
q6477_1: |0>─────────────────────────────────┼──────╫─
                                          ┌──┴──┐   ║
 c1144_0: 0 ══════════════════════════════╡ = 1 ╞═══╩═
                                          └─────┘
ConsolidateBlocks
            ┌──────────┐┌─┐
q6476_0: |0>┤0         ├┤M├
            │          │└╥┘
q6476_1: |0>┤          ├─╫─
            │          │ ║
q6476_2: |0>┤          ├─╫─
            │          │ ║
q6476_3: |0>┤          ├─╫─
            │  unitary │ ║
q6476_4: |0>┤          ├─╫─
            │          │ ║
q6476_5: |0>┤          ├─╫─
            │          │ ║
q6476_6: |0>┤          ├─╫─
            │          │ ║
q6476_7: |0>┤1         ├─╫─
            └──────────┘ ║
q6477_0: |0>─────────────╫─
                         ║
q6477_1: |0>─────────────╫─
                         ║
 c1144_0: 0 ═════════════╩═
maddy-tod commented 4 years ago

Collect2qBlocks includes multiple gates with conditionals into the same block. For example, the circuit

            ┌─────────┐┌───────┐┌───┐┌───────┐┌───┐┌─────────┐┌─┐
q6476_0: |0>┤ U2(0,0) ├┤ U1(0) ├┤ X ├┤ U1(0) ├┤ X ├┤ U2(0,0) ├┤M├
            └────┬────┘└───────┘└─┬─┘└───────┘└─┬─┘└────┬────┘└╥┘
q6476_1: |0>─────┼────────────────┼─────────────┼───────┼──────╫─
                 │                │             │       │      ║ 
q6476_2: |0>─────┼────────────────┼─────────────┼───────┼──────╫─
                 │                │             │       │      ║ 
q6476_3: |0>─────┼────────────────┼─────────────┼───────┼──────╫─
                 │                │             │       │      ║ 
q6476_4: |0>─────┼────────────────┼─────────────┼───────┼──────╫─
                 │                │             │       │      ║ 
q6476_5: |0>─────┼────────────────┼─────────────┼───────┼──────╫─
                 │                │             │       │      ║ 
q6476_6: |0>─────┼────────────────┼─────────────┼───────┼──────╫─
                 │                │             │       │      ║ 
q6476_7: |0>─────┼────────────────■─────────────■───────┼──────╫─
                 │                                      │      ║ 
q6477_0: |0>─────┼──────────────────────────────────────┼──────╫─
                 │                                      │      ║ 
q6477_1: |0>─────┼──────────────────────────────────────┼──────╫─
              ┌──┴──┐                                ┌──┴──┐   ║ 
 c1144_0: 0 ══╡ = 0 ╞════════════════════════════════╡ = 1 ╞═══╩═
              └─────┘                                └─────┘     

is collected into the blocks[['u2', 'u1', 'cx', 'u1', 'cx', 'u2'], ['measure']] which ConsolidateBlocks then joins together.

I don't think that this grouping is correct as I cannot see a logical value that the condition on the unitary could hold in this instance. Therefore, I think that the update that is needed is to refine how Collect2QBlocks deals with gates which contain conditions.