mimiqcircuits.operations.repeat

Repeat operation.

Functions

repeat(*args)

Build a Repeat operation, or a lazy one that waits for its count.

Classes

Repeat(repeats, op)

Operation applied several times in a row on the same targets.

class Repeat(repeats, op)[source]

Bases: Operation

Operation applied several times in a row on the same targets.

Repeat(n, op) applies op n times on the same qubits, bits and z-variables. Unlike Power, it works for any operation, not only gates, and keeps the repetitions explicit: decomposing it gives n copies of op.

The repeat method of a gate and the repeat() function are shorthands for it.

Parameters:
  • repeats (int) – Number of repetitions, zero or more.

  • op (Operation) – The operation to repeat.

Raises:
  • ValueError – If repeats is not a non-negative int.

  • TypeError – If op is not an operation.

Examples

>>> from mimiqcircuits import *
>>> from symengine import *
>>> # five X gates in a row
>>> Repeat(5, GateX())
∏⁵ X
>>> Repeat(3, GateRX(Symbol("x")))
∏³ RX(x)
>>> # nested repeats are kept as they are
>>> Repeat(2, Repeat(3, GateX()))
∏² ∏³ X
>>> # without a count, repeat() returns a lazy expression
>>> Parallel(2, GateH()).repeat()
lazy repeat(?, ⨷ ² H)
>>> Parallel(2, GateH()).repeat()(10)
∏¹⁰ ⨷ ² H
>>> c = Circuit().push(Repeat(2, GateX()), 0)
>>> c
1-qubit circuit with 1 instruction:
└── ∏² X @ q[0]
>>> # decomposing gives the individual repetitions
>>> Repeat(2, GateX()).decompose()
1-qubit circuit with 2 instructions:
├── X @ q[0]
└── X @ q[0]
>>> Repeat(3, GateSWAP()).decompose()
2-qubit circuit with 3 instructions:
├── SWAP @ q[0:1]
├── SWAP @ q[0:1]
└── SWAP @ q[0:1]
get_operation()[source]

Return the repeated operation, op.

getparam(name)[source]

Return the parameter name of the repeated operation.

getparams()[source]

Return the parameters of the repeated operation.

power(pow)[source]

Return the repetitions of op raised to pow.

Parameters:

pow – The exponent.

Returns:

Repeat(repeats, op.power(pow)).

Return type:

Repeat

parallel(repeat)[source]

Return the repetitions of repeat parallel copies of op.

Parameters:

repeat – Number of parallel copies.

Returns:

Repeat(repeats, op.parallel(repeat)).

Return type:

Repeat

control(repeat)[source]

Return the repetitions of op controlled by repeat qubits.

Parameters:

repeat – Number of control qubits.

Returns:

Repeat(repeats, op.control(repeat)).

Return type:

Repeat

inverse()[source]

Return the repetitions of the inverse of op.

Returns:

Repeat(repeats, op.inverse()). Fails when op has no inverse.

Return type:

Repeat

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

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

copy()[source]

Return a shallow copy of the operation.

The copy shares its attributes (for example a wrapped operation) with the original. Use deepcopy() for an independent copy.

Returns:

A new operation of the same type.

Return type:

Operation

deepcopy()[source]

Return a deep copy of the operation and all its attributes.

Returns:

A new operation equal to the original that shares no mutable state with it.

Return type:

Operation

repeat(*args)[source]

Build a Repeat operation, or a lazy one that waits for its count.

repeat(n, op) is Repeat(n, op). repeat(op) returns a lazy expression; calling it with n builds Repeat(n, op).

Parameters:

*args – (n, op) or (op,).

Returns:

The operation, or the lazy expression.

Return type:

Repeat or LazyExpr

Raises:

TypeError – If called with no arguments or with more than two.

Examples

>>> from mimiqcircuits import *
>>> repeat(3, GateX())
∏³ X
>>> repeat(GateX())(3)
∏³ X