- QuantumCircuit.switch(target: Union[Clbit, ClassicalRegister, int, slice, Sequence[Union[Clbit, int]]], cases: None, qubits: None, clbits: None, *, label: Optional[str]) qiskit.circuit.controlflow.switch_case.SwitchContext ¶
- QuantumCircuit.switch(target: Union[Clbit, ClassicalRegister, int, slice, Sequence[Union[Clbit, int]]], cases: Iterable[Tuple[Any, 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]) InstructionSet
casestructure 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
SwitchCaseOpwith the given case structure. If
clbits) are not passed, then this acts as a context manager, which will automatically build a
SwitchCaseOpwhen 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.
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): qc.x(0) with case(1, 2): qc.z(1) with case(case.DEFAULT): qc.cx(0, 1)
cases (Iterable[Tuple[Any, QuantumCircuit]]) – A sequence of case specifiers. Each tuple defines one case body (the second item). The first item of the tuple can be either a single integer value, the special value
CASE_DEFAULT, or a tuple of several integer values. Each of the integer values will be tried in turn; control will then pass to the body corresponding to the first match.
CASE_DEFAULTmatches all possible values. Omit in context-manager form.
qubits (Sequence[Qubit]) – The circuit qubits over which all case bodies execute. Omit in context-manager form.
clbits (Sequence[Clbit]) – The circuit clbits over which all case bodies execute. Omit in context-manager form.
label (Optional[str]) – The string label of the instruction in the circuit.
If used in context-manager mode, then this should be used as a
withresource, which will return an object that can be repeatedly entered to produce cases for the switch statement. If the full form is used, then this returns a handle to the instructions created.
- 반환 형식
InstructionSet or SwitchCaseContext
- 예외 발생
CircuitError – if an incorrect calling convention is used.