mimiqcircuits.operations.whilestatement

Conditional loop (WhileStatement).

Classes

WhileStatement(operation, bitstring)

Operation applied repeatedly while classical bits match a bitstring.

class WhileStatement(operation, bitstring)[source]

Bases: Operation

Operation applied repeatedly while classical bits match a bitstring.

WhileStatement(op, bs) is the loop counterpart of IfStatement. Before each iteration the condition bits are compared with bs; while they match, op is applied again.

The instruction takes the same targets as an IfStatement: qubits of op, classical bits of op, len(bs) condition bits, then z-variables of op. For the loop to end, op must change at least one condition bit, so the bits of op may 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 operation is not an operation or bitstring is not a BitString.

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.

True for composite operations such as Control, Power, Inverse, Parallel or IfStatement, False otherwise.

Returns:

bool

getparams()[source]

Return the parameters of the loop body.

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.

evaluate(d)[source]

Substitute values for the symbols in the operation’s parameters.

Operations without symbolic parameters may return themselves.

Parameters:

d (dict) – Map from symbols (or their names) to values.

Returns:

The operation with the symbols substituted.

Return type:

Operation

get_operation()[source]

Return the loop body, op.

get_bitstring()[source]

Return the condition, bitstring.