qiskit.circuit.QuantumCircuit.if_test¶
- QuantumCircuit.if_test(condition: tuple[ClassicalRegister | Clbit, int], true_body: None, qubits: None, clbits: None, *, label: str | None) qiskit.circuit.controlflow.if_else.IfContext [fuente]¶
- QuantumCircuit.if_test(condition: tuple[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: str | None = None) InstructionSet
Create an
if
statement 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 aIfElseOp
with the giventrue_body
, and there will be no branch for thefalse
condition (see also theif_else()
method). However, iftrue_body
(andqubits
andclbits
) are not passed, then this acts as a context manager, which can be used to buildif
statements. The return value of thewith
statement is a chainable context manager, which can be used to create subsequentelse
blocks. 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.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[3], 0)) as else_: qc.x(2) with else_: qc.h(2) qc.z(2)
- Parámetros
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 aClassicalRegister
to be tested for equality with a givenint
, or as a tuple of aClbit
to be compared to either abool
or anint
.true_body (Optional[QuantumCircuit]) – The circuit body to be run if
condition
is true.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.
- Devuelve
depending on the call signature, either a context manager for creating the
if
block (it will automatically be added to the circuit at the end of the block), or anInstructionSet
handle to the appended conditional operation.- Tipo del valor devuelto
InstructionSet or IfContext
- Muestra
CircuitError – If the provided condition references Clbits outside the enclosing circuit.
CircuitError – if an incorrect calling convention is used.
- Devuelve
A handle to the instruction created.