mimiqcircuits.operations.power

Power operation.

Functions

canonical_power(inner_gate_type, exponent)

Decorator that registers a named subclass of Power.

register_power_alias(exponent, gate_type, name)

Register a display name for a gate raised to a given exponent.

register_power_decomposition(exponent, gate_type)

Decorator that registers a custom decomposition for a gate power.

Classes

Power([operation, exponent])

Gate raised to a real power.

class Power(operation=None, exponent=None, *args, **kwargs)[source]

Bases: Gate

Gate raised to a real power.

Power(gate, p) is the unitary \(U^p\), where \(U\) is the matrix of gate. For non-integer p the principal matrix power is used.

The constructor takes the gate (or a gate class followed by its constructor arguments) and the exponent. The exponent must be a real number: a float that is a simple fraction (denominator up to 1000) is stored as a Fraction or int, so Power(g, 0.5) and Power(g, Fraction(1, 2)) are the same operation. A power of a Power is folded into one: Power(Power(g, a), b) is Power(g, a * b).

When a named subclass is registered for the combination (see canonical_power()), the constructor returns an instance of that subclass: Power(GateZ(), 0.5) is a GateS. A folded power stays a plain Power, even when a subclass is registered for the folded exponent.

The power method of a gate, or the ** operator, is the usual way to build one; it may simplify the result further.

Parameters:
  • operation (Type[Gate] | Gate) – The gate to raise, or a gate class.

  • exponent (Any) – The exponent, a real number.

  • *args – Constructor arguments when operation is a class.

  • **kwargs – Constructor keyword arguments when operation is a class.

Raises:
  • ValueError – If operation is not a gate or gate class.

  • TypeError – If exponent is not a real number, for example a symbol or a complex number.

Examples

>>> from mimiqcircuits import *
>>> # the generic form returns the named gate when one exists
>>> isinstance(Power(GateZ(), 0.5), GateS)
True
>>> c = Circuit()
>>> # the square root of X prints with its own name, SX
>>> c.push(Power(GateX(), 1/2), 1)
2-qubit circuit with 1 instruction:
└── SX @ q[1]

>>> c.push(Power(GateX(), 5), 1)
2-qubit circuit with 2 instructions:
├── SX @ q[1]
└── X**5 @ q[1]

>>> # an integer power decomposes into repeated applications
>>> Power(GateX(), 3).decompose()
1-qubit circuit with 3 instructions:
├── X @ q[0]
├── X @ q[0]
└── X @ q[0]
static __new__(cls, operation=None, exponent=None, *args, **kwargs)[source]

Create a Power instance, returning canonical subclass if registered.

If a canonical subclass is registered for (type(operation), exponent), an instance of that subclass is returned instead of a plain Power.

Parameters:
  • operation (Type[Gate] | Gate) – Gate operation or gate class to raise to a power

  • exponent (Any) – The exponent value

  • *args – Arguments to pass to operation constructor if a class is provided

  • **kwargs – Keyword arguments to pass to operation constructor

Returns:

Instance of Power or a registered canonical subclass

property op

The gate being raised to a power. Read-only.

property exponent

The exponent, normalized to an int or Fraction when possible. Read-only.

iswrapper()[source]

Return False: a plain gate does not wrap another operation.

get_operation()[source]

Return the operation that this one wraps, or the operation itself.

Wrappers such as Control or Power return the operation they wrap; other operations return themselves.

Returns:

Operation

power(*args)[source]

Raise to a further power, multiplying the exponents.

Parameters:

*args – The exponent. When omitted, a lazy expression is returned that takes the exponent later.

Returns:

op.power(exponent * p), or a lazy expression when called without arguments.

Return type:

Gate

Raises:

ValueError – If more than one argument is given.

Examples

>>> from mimiqcircuits import *
>>> # (X^(1/2))^2 is X
>>> Power(GateX(), 0.5).power(2)
X
inverse()[source]

Return the inverse, Inverse(self).

control(*args)[source]

Return the gate controlled by num_controls qubits.

Gates with a named controlled form return it directly (for example GateX().control(1) is GateCX()); otherwise the gate is wrapped in Control. The controls are the first qubits.

Parameters:

*args – The number of controls. Without it, return a LazyExpr that takes the number later.

Raises:

ValueError – If more than one argument is given.

parallel(*args)[source]

Return a Parallel applying the gate to separate qubit groups.

Parameters:

*args – The number of repetitions. Without it, return a LazyExpr that takes the number later.

Raises:

ValueError – If more than one argument is given.

getparams()[source]

Return the values of the operation’s parameters.

Returns:

One value per name in parnames, in the same order. Values may be numbers or symbolic expressions.

Return type:

list

Examples

>>> from mimiqcircuits import *
>>> # GateU has a fourth parameter, the global phase, defaulting to 0
>>> g = GateU(0.1, 0.2, 0.3)
>>> g.parnames
('theta', 'phi', 'lmbda', 'gamma')
>>> g.getparams()
[0.1, 0.2, 0.3, 0.0]
isopalias()[source]

Whether the operation is a named alias of a wrapper.

Some named gates are defined as a wrapper around another operation, for example GateS as GateZ to the power 1/2. Such gates return True. Not every such gate reports it: GateSDG and GateCX return False.

Returns:

True for a named alias of a wrapper that reports it, False otherwise.

Return type:

bool

evaluate(d)[source]

Substitute symbols in the gate parameters and return the result.

Parameters:

d (dict) – Map from symbols to the values to substitute.

Returns:

self if nothing changed, otherwise a new gate of the same type with the substituted parameters.

Return type:

Gate

gettypekey()[source]

Return the key used to look up registered decompositions: the gate class.

decompose()[source]

Decompose the operation one level into simpler operations.

The result is a new circuit acting on qubits 0 to num_qubits - 1 (and likewise for bits and z-variables). Operations without a decomposition return a circuit holding only themselves.

Returns:

The decomposed operation.

Return type:

Circuit

Examples

>>> from mimiqcircuits import *
>>> # GateSWAP decomposes into three CX gates
>>> GateSWAP().decompose()
2-qubit circuit with 3 instructions:
├── CX @ q[0], q[1]
├── CX @ q[1], q[0]
└── CX @ q[0], q[1]
canonical_power(inner_gate_type, exponent)[source]

Decorator that registers a named subclass of Power.

After registration, Power(g, exponent) with g an instance of inner_gate_type builds an instance of the decorated class instead of a plain Power. This is how named gates such as GateS (Power(GateZ(), 1/2)) are defined. The exponent is compared after normalization, so 0.5 and Fraction(1, 2) match the same entry.

Registering a class for a pair that already has one replaces it for the rest of the session.

Parameters:
  • inner_gate_type (Type) – Class of the wrapped gate.

  • exponent (Any) – The exponent, in normalized form (an int or a Fraction when the value is a simple fraction).

Returns:

The decorator. It registers the class and returns it unchanged.

Examples

>>> from mimiqcircuits import *
>>> # GateS is registered for GateZ to the power 1/2
>>> isinstance(Power(GateZ(), 0.5), GateS)
True