mimiqcircuits.operations.whilestatement¶
Conditional loop (WhileStatement).
Classes
|
Operation applied repeatedly while classical bits match a bitstring. |
- class WhileStatement(operation, bitstring)[source]¶
Bases:
OperationOperation applied repeatedly while classical bits match a bitstring.
WhileStatement(op, bs)is the loop counterpart ofIfStatement. Before each iteration the condition bits are compared withbs; while they match,opis applied again.The instruction takes the same targets as an
IfStatement: qubits ofop, classical bits ofop,len(bs)condition bits, then z-variables ofop. For the loop to end,opmust change at least one condition bit, so the bits ofopmay overlap with the condition bits. There is no limit on the number of iterations: a loop whose condition never changes does not terminate.- Parameters:
operation – The loop body, or an operation class built with no arguments.
bitstring (BitString) – The values the condition bits must hold for the loop to continue.
- Raises:
ValueError – If
operationis not an operation orbitstringis not aBitString.
Examples
>>> from mimiqcircuits import * >>> c = Circuit() >>> # measure and reset qubit 0 as long as bit 0 holds 1: the >>> # measurement writes bit 0, which is also the condition bit, and >>> # after the reset it reads 0, which ends the loop >>> c.push(WhileStatement(MeasureReset(), BitString('1')), 0, 0, 0) 1-qubit, 1-bit circuit with 1 instruction: └── WHILE(c==1) MR @ q[0], c[0], condition[0] >>> # a classical body: flipping bit 1 while it is 1 runs at most once >>> c.push(WhileStatement(Not(), BitString('1')), 1, 1) 1-qubit, 2-bit circuit with 2 instructions: ├── WHILE(c==1) MR @ q[0], c[0], condition[0] └── WHILE(c==1) ! @ c[1], condition[1]
See also
IfStatement- property op¶
The loop body. Read-only.
- property bitstring¶
The values the condition bits must hold, as a
BitString.
- iswrapper()[source]¶
Whether the operation wraps another operation.
Truefor composite operations such asControl,Power,Inverse,ParallelorIfStatement,Falseotherwise.- Returns:
bool
- inverse()[source]¶
Not supported for loops.
- Raises:
NotImplementedError – Always.
- power(power)[source]¶
Not supported for loops.
- Raises:
NotImplementedError – Always.
- control(num_controls)[source]¶
Not supported for loops.
- Raises:
NotImplementedError – Always.