- QuantumCircuit.if_test(condition: Tuple[Union[ClassicalRegister, Clbit], int], true_body: None, qubits: None, clbits: None, *, label: Optional[str]) qiskit.circuit.controlflow.if_else.IfContext ¶
- QuantumCircuit.if_test(condition: Tuple[Union[ClassicalRegister, Clbit], int], true_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: Optional[str] = None) InstructionSet
ifstatement 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
IfElseOpwith the given
true_body, and there will be no branch for the
falsecondition (see also the
if_else()method). However, if
clbits) are not passed, then this acts as a context manager, which can be used to build
ifstatements. The return value of the
withstatement is a chainable context manager, which can be used to create subsequent
elseblocks. 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.
from qiskit.circuit import QuantumCircuit, Qubit, Clbit bits = [Qubit(), Qubit(), Qubit(), Clbit(), Clbit()] qc = QuantumCircuit(bits) qc.h(0) qc.cx(0, 1) qc.measure(0, 0) qc.h(0) qc.cx(0, 1) qc.measure(0, 1) with qc.if_test((bits, 0)) as else_: qc.x(2) with else_: qc.h(2) qc.z(2)
condition (Tuple[Union[ClassicalRegister, Clbit], int]) -- A condition to be evaluated at circuit runtime which, if true, will trigger the evaluation of
true_body. Can be specified as either a tuple of a
ClassicalRegisterto be tested for equality with a given
int, or as a tuple of a
Clbitto be compared to either a
true_body (Optional[QuantumCircuit]) -- The circuit body to be run if
qubits (Optional[Sequence[QubitSpecifier]]) -- The circuit qubits over which the if/else should be run.
clbits (Optional[Sequence[ClbitSpecifier]]) -- The circuit clbits over which the if/else should be run.
label (Optional[str]) -- The string label of the instruction in the circuit.
depending on the call signature, either a context manager for creating the
ifblock (it will automatically be added to the circuit at the end of the block), or an
InstructionSethandle to the appended conditional operation.
- Return type
InstructionSet or IfContext
CircuitError -- If the provided condition references Clbits outside the enclosing circuit.
CircuitError -- if an incorrect calling convention is used.
A handle to the instruction created.