from numpy import pi
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister

qr = QuantumRegister(4, "q")
cr = ClassicalRegister(2, "cr")
qc = QuantumCircuit(qr, cr)

with qc.if_test((cr[1], 1)):
    qc.h(0)
    qc.cx(0, 1)
qc.draw()

qr = QuantumRegister(4, "q")
cr = ClassicalRegister(2, "cr")
qc = QuantumCircuit(qr, cr)

with qc.if_test((cr[1], 1)) as _else:
    qc.h(0)
    qc.cx(0, 1)
with _else:
    qc.cx(0, 1)
qc.draw()

qr = QuantumRegister(4, "q")
cr = ClassicalRegister(2, "cr")
qc = QuantumCircuit(qr, cr)

with qc.if_test((cr[1], 1)) as _else:
    qc.h(0)
    qc.cx(0, 1)
    with qc.if_test((cr[0], 0)):
        qc.x(0)
with _else:
    qc.cx(0, 1)
qc.draw()

qr = QuantumRegister(4, "q")
cr = ClassicalRegister(3, "cr")
qc = QuantumCircuit(qr, cr)
qc.h(0)

with qc.if_test((cr[1], 1)) as _else:
    qc.x(0, label="X c_if").c_if(cr, 4)
    with qc.if_test((cr[2], 1)):
        qc.z(0)
        qc.y(1)
        with qc.if_test((cr[1], 1)):
            qc.y(1)
            qc.z(2)
            with qc.if_test((cr[2], 1)):
                qc.cx(0, 1)
                with qc.if_test((cr[1], 1)):
                    qc.h(0)
                    qc.x(1)
with _else:
    qc.y(1)
    with qc.if_test((cr[2], 1)):
        qc.x(0)
        qc.x(1)
    qr1 = QuantumRegister(2, "qr1")
    cr1 = ClassicalRegister(2, "cr1")
    cr2 = ClassicalRegister(2, "cr2")
    circuit = QuantumCircuit(qr1, cr1, cr2)
    inst = QuantumCircuit(2, 2, name="Inst").to_instruction()
    qc.append(inst, [qr[0], qr[1]], [cr[0], cr[1]])
qc.x(0)
qc.draw(fold=30, style={"name": "default", "showindex": True})#,wire_order=(2, 0, 3, 1, 4, 5, 6))

qr = QuantumRegister(4, "q")
cr = ClassicalRegister(3, "cr")
qc = QuantumCircuit(qr, cr)
qc.h(0)

with qc.if_test((cr[1], 1)) as _else:
    qc.x(0, label="X c_if").c_if(cr, 4)
    with qc.if_test((cr[2], 1)):
        qc.z(0)
        qc.y(1)
        with qc.if_test((cr[1], 1)):
            qc.y(1)
            qc.z(2)
            with qc.if_test((cr[2], 1)):
                qc.cx(0, 1)
                with qc.if_test((cr[1], 1)):
                    qc.h(0)
                    qc.x(1)
with _else:
    qc.y(1)
    with qc.if_test((cr[2], 1)):
        qc.x(0)
        qc.x(1)
    qr1 = QuantumRegister(2, "qr1")
    cr1 = ClassicalRegister(2, "cr1")
    cr2 = ClassicalRegister(2, "cr2")
    circuit = QuantumCircuit(qr1, cr1, cr2)
    inst = QuantumCircuit(2, 2, name="Inst").to_instruction()
    qc.append(inst, [qr[0], qr[1]], [cr[0], cr[1]])
qc.x(0)
qc.draw(fold=7, style={"name": "default", "showindex": True}, scale=0.8)

qr = QuantumRegister(4, "q")
cr = ClassicalRegister(3, "cr")
qc = QuantumCircuit(qr, cr)
qc.h(0)

with qc.if_test((cr[1], 1)) as _else:
    qc.x(0, label="X c_if").c_if(cr, 4)
    with qc.if_test((cr[2], 1)):
        qc.z(0)
        qc.y(1)
        with qc.if_test((cr[1], 1)):
            qc.y(1)
            qc.z(2)
            with qc.if_test((cr[2], 1)):
                qc.cx(0, 1)
                with qc.if_test((cr[1], 1)):
                    qc.h(0)
                    qc.x(1)
with _else:
    qc.y(1)
    with qc.if_test((cr[2], 1)):
        qc.x(0)
        qc.x(1)
    qr1 = QuantumRegister(2, "qr1")
    cr1 = ClassicalRegister(2, "cr1")
    cr2 = ClassicalRegister(2, "cr2")
    circuit = QuantumCircuit(qr1, cr1, cr2)
    inst = QuantumCircuit(2, 2, name="Inst").to_instruction()
    qc.append(inst, [qr[0], qr[1]], [cr[0], cr[1]])
qc.x(0)
qc.draw(fold=30, style={"name": "textbook", "showindex": True})

qr = QuantumRegister(4, "q")
cr = ClassicalRegister(3, "cr")
qc = QuantumCircuit(qr, cr)
qc.h(0)
qc.measure(0, 2)
with qc.while_loop((cr[0], 0)):
    qc.h(0)
    qc.cx(0, 1)
    qc.measure(0, 0)
    with qc.if_test((cr[2], 1)):
        qc.x(0)
qc.draw(style={"showindex": True})

qr = QuantumRegister(4, "q")
cr = ClassicalRegister(3, "cr")
qc = QuantumCircuit(qr, cr)
qc.h(0)
qc.measure(0, 2)
with qc.for_loop((2, 4, 8, 16)) as i:
    qc.h(0)
    qc.cx(0, 1)
    qc.rx(pi/i, 1)
    qc.measure(0, 0)
    with qc.if_test((cr[2], 1)):
        qc.z(0)
qc.draw(style={"showindex": True})

from qiskit.circuit import QuantumCircuit, ClassicalRegister, QuantumRegister
qreg = QuantumRegister(3)
creg = ClassicalRegister(3)
qc = QuantumCircuit(qreg, creg)
qc.h([0, 1, 2])
qc.measure([0, 1, 2], [0, 1, 2])

with qc.switch(creg) as case:
    with case(0, 1, 2):
        qc.x(0)
    with case(3, 4, 5):
        qc.y(1)
        qc.y(0)
        qc.y(0)
    with case(case.DEFAULT):
        qc.cx(0, 1)
qc.h(0)
qc.draw(style={"showindex": True})


