mimiqcircuits.operations.ifstatement

Conditional execution (IfStatement).

Classes

IfStatement(operation, bitstring)

Operation applied only when classical bits match a bitstring.

class IfStatement(operation, bitstring)[source]

Bases: Operation

Operation applied only when classical bits match a bitstring.

IfStatement(op, bs) applies op when the condition bits hold the values in bs, the first condition bit compared with the first character of bs, and so on. Otherwise it does nothing.

The instruction takes the targets of op followed by the condition bits: qubits of op, classical bits of op, len(bs) condition bits, then z-variables of op. The condition bits may overlap with the bits of op.

Parameters:
  • operation – The operation to apply, or an operation class built with no arguments.

  • bitstring (BitString) – The values the condition bits must hold.

Raises:

ValueError – If operation is not an operation or bitstring is not a BitString.

Examples

>>> from mimiqcircuits import *
>>> c = Circuit()
>>> # measure qubit 0 into bit 0
>>> c.push(Measure(), 0, 0)
1-qubit, 1-bit circuit with 1 instruction:
└── M @ q[0], c[0]

>>> # flip qubit 1 only if bit 0 is 1
>>> c.push(IfStatement(GateX(), BitString('1')), 1, 0)
2-qubit, 1-bit circuit with 2 instructions:
├── M @ q[0], c[0]
└── IF(c==1) X @ q[1], condition[0]

>>> # measure qubit 1 into bit 3 if bits 1 and 2 are 0 and 1:
>>> # the bit of the measurement comes before the condition bits
>>> c.push(IfStatement(Measure(), BitString('01')), 1, 3, 1, 2)
2-qubit, 4-bit circuit with 3 instructions:
├── M @ q[0], c[0]
├── IF(c==1) X @ q[1], condition[0]
└── IF(c==01) M @ q[1], c[3], condition[1:2]
property op

The conditioned operation. 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

supports_canonical_rewrite()[source]

Whether CanonicalRewrite may decompose this operation.

Operations opt in: only those the canonical rewrite knows how to handle return True.

Returns:

bool

getparams()[source]

Return the parameters of the conditioned operation.

inverse()[source]

Not supported for conditional operations.

Raises:

NotImplementedError – Always.

power(power)[source]

Not supported for conditional operations.

Raises:

NotImplementedError – Always.

control(num_controls)[source]

Not supported for conditional operations.

Raises:

NotImplementedError – Always.

format_with_targets(qubits, bits, zvars)[source]

Nested conditional formatting (explicit IF(c==..) chain).

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 conditioned operation, op.

get_bitstring()[source]

Return the condition, bitstring.