Skip to content

UnrollIfs pass has issues with elif/else #286

Open
@david-pl

Description

@david-pl

Noticed while looking into #284.

elif gets removed:

@qasm2.extended
def main():
    q = qasm2.qreg(1)
    c = qasm2.creg(1)
    qasm2.h(q[0])
    qasm2.measure(q, c)

    parity = 0
    if c[0] == 1 and parity == 0:
        qasm2.x(q[0])
        parity = 0
    elif c[0] == 0:
        parity = 1
    elif c[0] == 2:
        parity = 2

    return parity

QASM2Fold(qasm2.extended, unroll_ifs=True, no_raise=False).fixpoint(main)

main.print()

The result is only a single if (the first one) remains:

func.func main() -> Literal(0,int) {
  ^0(%main_self):
  │      %0 = py.constant.constant 1 : !py.int%q = qasm2.core.qreg.new(n_qubits=%0) : !py.IList[!py.Qubit, !Any]
  │      %c = qasm2.core.creg.new(n_bits=%0) : !py.CReg%parity = py.constant.constant 0 : !py.int%1 = qasm2.core.qreg.get(reg=%q, idx=%parity) : !py.Qubitqasm2.uop.h(qarg=%1)
  │           qasm2.core.measure(qarg=%q, carg=%c)
  │      %2 = qasm2.core.creg.get(reg=%c, idx=%parity) : !py.Bit%3 = py.cmp.eq(lhs=%2, rhs=%0) : !py.bool%4 = py.constant.constant True : !py.bool%5 = py.boolop.and(%3, %4) : !py.boolscf.if %5 {
  │           │ ^1(%6):
  │           │ │ qasm2.uop.x(qarg=%1)
  │           │ │ scf.yield 
  │           } -> purity=Falsefunc.return %parity
} // func.func main

else errors, but only when combined with at least one elif

@qasm2.extended
def main():
    q = qasm2.qreg(1)
    c = qasm2.creg(1)
    qasm2.h(q[0])
    qasm2.measure(q, c)

    parity = 0
    if c[0] == 1 and parity == 0:
        qasm2.x(q[0])
        parity = 0
    elif c[0] == 0:
        parity = 1
    else:
        parity = 2

    return parity

QASM2Fold(qasm2.extended, unroll_ifs=True, no_raise=False).fixpoint(main)

main.print()

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions