QuantumCircuit.for_loop(indexset: Iterable[int], loop_parameter: Parameter | None, body: None, qubits: None, clbits: None, *, label: str | None) qiskit.circuit.controlflow.for_loop.ForLoopContext[source]
QuantumCircuit.for_loop(indexset: Iterable[int], loop_parameter: Optional[Parameter], body: QuantumCircuit, qubits: Sequence[Union[Qubit, QuantumRegister, int, slice, Sequence[Union[Qubit, int]]]], clbits: Sequence[Union[Clbit, ClassicalRegister, int, slice, Sequence[Union[Clbit, int]]]], *, label: str | None) InstructionSet

Create a for loop on this circuit.

There are two forms for calling this function. If called with all its arguments (with the possible exception of label), it will create a ForLoopOp with the given body. If body (and qubits and clbits) are not passed, then this acts as a context manager, which, when entered, provides a loop variable (unless one is given, in which case it will be reused) and will automatically build a ForLoopOp when the scope finishes. In this form, you do not need to keep track of the qubits or clbits you are using, because the scope will handle it for you.

For example:

from qiskit import QuantumCircuit
qc = QuantumCircuit(2, 1)

with qc.for_loop(range(5)) as i:
    qc.h(0), 1)
    qc.measure(0, 0)
    qc.break_loop().c_if(0, True)
  • indexset (Iterable[int]) -- A collection of integers to loop over. Always necessary.

  • loop_parameter (Optional[Parameter]) --

    The parameter used within body to which the values from indexset will be assigned. In the context-manager form, if this argument is not supplied, then a loop parameter will be allocated for you and returned as the value of the with statement. This will only be bound into the circuit if it is used within the body.

    If this argument is None in the manual form of this method, body will be repeated once for each of the items in indexset but their values will be ignored.

  • body (Optional[QuantumCircuit]) -- The loop body to be repeatedly executed. Omit this to use the context-manager mode.

  • qubits (Optional[Sequence[QubitSpecifier]]) -- The circuit qubits over which the loop body should be run. Omit this to use the context-manager mode.

  • clbits (Optional[Sequence[ClbitSpecifier]]) -- The circuit clbits over which the loop body should be run. Omit this to use the context-manager mode.

  • label (Optional[str]) -- The string label of the instruction in the circuit.


depending on the call signature, either a context manager for creating the for loop (it will automatically be added to the circuit at the end of the block), or an InstructionSet handle to the appended loop operation.

রিটার্ন টাইপ

InstructionSet or ForLoopContext


CircuitError -- if an incorrect calling convention is used.