mimiqcircuits.lazy

Lazy evaluation and composition functions.

Functions

control(*args)

Return a controlled operation, or a lazy expression for one.

inverse(op)

Return the inverse of an operation; same as op.inverse().

parallel(*args)

Return an operation repeated in parallel on separate qubits, or a lazy expression for one.

power(*args)

Return a power of an operation, or a lazy expression for one.

Classes

LazyArg()

Placeholder for an argument of a LazyExpr that is not known yet.

LazyExpr(obj, *args)

Operation whose construction waits for some of its arguments.

class LazyArg[source]

Bases: object

Placeholder for an argument of a LazyExpr that is not known yet. It prints as ?.

class LazyExpr(obj, *args)[source]

Bases: object

Operation whose construction waits for some of its arguments.

Calling control, parallel, power or repeat on a gate without the count, or a size-dependent gate such as QFT() without its size, returns a LazyExpr. A LazyExpr can be wrapped further with control(), parallel(), power() and inverse(). Calling it fills the LazyArg placeholders, in the order they appear in the printed expression (left to right), and builds the operation. Passing more arguments than placeholders raises TypeError.

Parameters:
  • obj – Callable that builds the operation.

  • *args – Its arguments; LazyArg() marks the ones still missing.

Examples

>>> from mimiqcircuits import *
>>> # leave the number of controls open
>>> lazy = control(GateX())
>>> lazy
lazy control(?, X)
>>> lazy(2)
C₂X
>>> # nest lazy operations: the placeholders are filled left to right
>>> lazy = GateX().power().control()
>>> lazy
lazy control(?, lazy power(X, ?))
>>> lazy(2, 0.5)
C₂SX
inverse()[source]

Return a lazy expression for the inverse of this expression.

parallel(*args)[source]

Return a lazy expression for the parallel repetition of this expression.

Parameters:

*args – Optional number of repetitions. Without it, the number becomes a new placeholder.

Raises:

TypeError – If more than one argument is given.

control(*args)[source]

Return a lazy expression for the controlled version of this expression.

Parameters:

*args – Optional number of controls. Without it, the number becomes a new placeholder.

Raises:

TypeError – If more than one argument is given.

power(*args)[source]

Return a lazy expression for a power of this expression.

Parameters:

*args – Optional exponent. Without it, the exponent becomes a new placeholder.

control(*args)[source]

Return a controlled operation, or a lazy expression for one.

Parameters:

*args –

One of the following.

  • (num_controls, op): return op.control(num_controls).

  • (op,): return the lazy expression control(?, op).

  • (num_controls,) with an int: return the lazy expression control(num_controls, ?).

Returns:

The controlled operation or a lazy expression.

Return type:

Operation or LazyExpr

Raises:

TypeError – If a single argument is neither an Operation nor an int, or the number of arguments is not 1 or 2.

Examples

>>> from mimiqcircuits import *
>>> control(2, GateX())
C₂X
>>> # fix the number of controls, give the operation later
>>> lazy_ctrl = control(2)
>>> lazy_ctrl(GateX())
C₂X
parallel(*args)[source]

Return an operation repeated in parallel on separate qubits, or a lazy expression for one.

Parameters:

*args –

One of the following.

  • (num_repeats, op): return op.parallel(num_repeats).

  • (op,): return the lazy expression parallel(?, op).

  • (num_repeats,) with an int: return the lazy expression parallel(num_repeats, ?).

Returns:

The parallel operation or a lazy expression.

Return type:

Operation or LazyExpr

Raises:

TypeError – If a single argument is neither an Operation nor an int, or the number of arguments is not 1 or 2.

Examples

>>> from mimiqcircuits import *
>>> parallel(3, GateH())
⨷ ³ H
>>> # fix the number of repetitions, give the operation later
>>> parallel(3)(GateH())
⨷ ³ H
inverse(op)[source]

Return the inverse of an operation; same as op.inverse().

Parameters:

op (Operation or LazyExpr) – The operation to invert.

Returns:

The inverse.

Return type:

Operation or LazyExpr

Examples

>>> from mimiqcircuits import *
>>> op = inverse(GateS())
>>> op
S†
power(*args)[source]

Return a power of an operation, or a lazy expression for one.

Parameters:

*args –

One of the following.

  • (op, exponent): return op.power(exponent).

  • (op,): return the lazy expression power(op, ?).

  • (exponent,) with an int: return the lazy expression power(?, exponent). A non-integer exponent cannot be given alone.

Returns:

The powered operation or a lazy expression.

Return type:

Operation or LazyExpr

Raises:

TypeError – If a single argument is neither an Operation nor an int, or the number of arguments is not 1 or 2.

Examples

>>> from mimiqcircuits import *
>>> power(GateX(), 0.5)
SX
>>> # fix the exponent, give the operation later
>>> power(2)(GateS())
Z