import qiskit

qiskit.__version__

from qiskit.circuit import QuantumCircuit, QuantumRegister, ClassicalRegister
from qiskit.compiler import transpile

qreg = QuantumRegister(2)
creg = ClassicalRegister(2)
qc = QuantumCircuit(qreg, creg)

qc.h([0, 1])
qc.h([0, 1])
qc.h([0, 1])
qc.measure([0, 1], [0, 1])
with qc.switch(creg) as case:
  with case(0):  # if the register is '00'
    qc.z(0)
  with case(1, 2):  # if the register is '01' or '10'
    qc.cx(0, 1)
  with case(case.DEFAULT):  # the default case
    qc.h(0)

qc.draw("mpl")      

from qiskit.compiler import transpile
from qiskit.providers.fake_provider import FakeBelemV2
from qiskit.circuit import SwitchCaseOp, IfElseOp, WhileLoopOp

backend = FakeBelemV2()
backend.target.add_instruction(SwitchCaseOp, name="switch_case")
backend.target.add_instruction(IfElseOp, name="if_else")
backend.target.add_instruction(WhileLoopOp, name="while_loop")

transpile(qc, backend, optimization_level=2, seed_transpiler=671_44).draw('mpl')

transpile(qc, backend, optimization_level=3, seed_transpiler=671_44).draw('mpl')

from qiskit.circuit.classical import expr

qr = QuantumRegister(4)
cr1 = ClassicalRegister(2)
cr2 = ClassicalRegister(2)
qc = QuantumCircuit(qr, cr1, cr2)
qc.h(0)
qc.cx(0, 1)
qc.h(2)
qc.cx(2, 3)
qc.measure([0, 1, 2, 3], [0, 1, 2, 3])

# If the two registers are equal to each other.
with qc.if_test(expr.equal(cr1, cr2)):
  qc.x(0)

# While either of two bits are set.
with qc.while_loop(expr.logic_or(cr1[0], cr1[1])):
  qc.reset(0)
  qc.reset(1)
  qc.measure([0, 1], cr1)

qc.draw('mpl')

from qiskit.qasm3 import dumps

print(dumps(transpile(qc, backend, optimization_level=3)))

from qiskit.algorithms import VQE

import itertools
from math import pi

from qiskit.circuit.library import EfficientSU2
from qiskit.providers.fake_provider import FakeSherbrooke

backend = FakeSherbrooke()

circuit = EfficientSU2(127, entanglement='linear', reps=50)

print(f"Number of circuit parameters: {len(circuit.parameters)}")

value_cycle = itertools.cycle([0, pi / 4, pi / 2, 3*pi / 4, pi, 2* pi])
%timeit -n 1 circuit.assign_parameters([x[1] for x in zip(range(len(circuit.parameters)), value_cycle)], inplace=False)