mimiqcircuits.operations.inverse

Inverse operation.

Functions

canonical_inverse(inner_gate_type)

Decorator that registers a named subclass of Inverse.

register_inverse_alias(gate_type, name)

Register a display name for the inverse of a gate.

register_inverse_decomposition(gate_type)

Decorator that registers a custom decomposition for the inverse of a gate.

Classes

Inverse([operation])

Inverse of a gate.

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

Bases: Gate

Inverse of a gate.

Inverse(gate) is the unitary \(U^\dagger\), where \(U\) is the matrix of gate. The inverse is kept as a wrapper: it is not rewritten into another gate when built.

The constructor takes the gate, or a gate class followed by its constructor arguments.

When a named subclass is registered for the gate type (see canonical_inverse()), the constructor returns an instance of that subclass: Inverse(GateS()) is a GateSDG.

Parameters:
  • operation – The gate to invert, or a gate class.

  • *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.

Warning

Prefer the inverse method of a gate to building Inverse directly. The method simplifies where it can, for example GateH().inverse() is GateH() and op.inverse().inverse() is op.

Examples

>>> from mimiqcircuits import *
>>> # the generic form returns the named gate when one exists
>>> isinstance(Inverse(GateS()), GateSDG)
True
>>> # the matrix is the conjugate transpose of the gate's matrix
>>> Inverse(GateP(1)).matrix()
[1.0, 0]
[0, 0.54030230586814 - 0.841470984807897*I]

>>> c = Circuit()
>>> c.push(Inverse(GateP(1)), 1)
2-qubit circuit with 1 instruction:
└── P(1)† @ q[1]
static __new__(cls, operation=None, *args, **kwargs)[source]

Create an Inverse instance, returning canonical subclass if registered.

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

Parameters:
  • operation – Gate operation or gate class to invert

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

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

Returns:

Instance of Inverse or a registered canonical subclass

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

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

inverse()[source]

Return the wrapped gate, undoing the inversion.

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]
power(*args)[source]

Return the gate raised to a power.

Gates with a known power return it directly (for example GateZ().power(0.5) is GateS(), and GateRX(a).power(p) is GateRX(p * a)); otherwise the gate is wrapped in Power.

Parameters:

*args – The exponent. Without it, return a LazyExpr that takes the exponent later.

Raises:
  • ValueError – If more than one argument is given.

  • TypeError – If the exponent is not a real number.

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.

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.

canonical_inverse(inner_gate_type)[source]

Decorator that registers a named subclass of Inverse.

After registration, Inverse(g) with g an instance of inner_gate_type builds an instance of the decorated class instead of a plain Inverse. This is how named gates such as GateSDG (Inverse(GateS())) are defined.

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

Parameters:

inner_gate_type (Type) – Class of the wrapped gate.

Returns:

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

Examples

>>> from mimiqcircuits import *
>>> # GateSDG is registered as the inverse of GateS
>>> isinstance(Inverse(GateS()), GateSDG)
True