qp.subcircuit

subcircuit(qfunc=None, dynamic_argnames=(), wire_argnames=('wires',), compilable_argnames=(), hybrid_argnames=(), static_argnames=(), **cls_attrs)[source]

A decorator for turning a quantum function with registered resources into an Operator2 subclass.

The quantum function decorated with subcircuit can be any valid quantum function, provided that nothing is returned from it (e.g., terminal measurements are not allowed) and that it has resources registered to it via register_resources().

The decorated quantum function will automatically be registered as an Operator2 subclass with a single decomposition rule that corresponds to the quantum function body.

Parameters:

qfunc (DecompositionRule) – a quantum function whose resources have been registered with register_resources()

Keyword Arguments:
  • dynamic_argnames (Sequence[str]) – names of the arguments that are dynamic data of the operator. For more details, see dynamic_argnames

  • wire_argnames (Sequence[str]) – names of the arguments that are wires. Defaults to ("wires",). For more details, see wire_argnames

  • compilable_argnames (Sequence[str]) – names of arguments that are static and compilable data of the operator. For more details, see compilable_argnames

  • hybrid_argnames (Sequence[str]) – names of arguments that are dynamic data wrapped in static data structures (pytrees). For more details, see hybrid_argnames

  • static_argnames (Sequence[str]) – names of arguments that are static but not compilable. For more details, see static_argnames

  • **cls_attrs – additional class attributes to set on the created operator class

Returns:

a new operator class named after the quantum function

Return type:

type[Operator2]

Note

Every parameter of the quantum function must be classified into one of dynamic_argnames, wire_argnames, compilable_argnames, hybrid_argnames or static_argnames. See Operator2 for details on how arguments are classified.

Example

An operator can be created by decorating a quantum function that has registered resources. Its parameters are classified using the *_argnames keyword arguments; here phi is dynamic data and wires are (by default) the wires:

@qp.subcircuit(dynamic_argnames=("phi",))
@qp.register_resources({qp.H: 1, qp.RZ: 1}, name="MyOp")
def MyOp(phi, wires):
    qp.H(wires)
    qp.RZ(phi, wires)

The returned object is an Operator2 subclass named after the quantum function, and is instantiated and used like any other operator:

>>> op = MyOp(0.5, wires=0)
>>> op
MyOp(0.5, wires=[0])
>>> isinstance(op, qp.core.Operator2)
True

The body of the quantum function is registered as its decomposition rule:

>>> qp.inspect_decomps(op)
Decomposition 0 (name: MyOp_decomp)
0: ──H──RZ(0.50)─┤
Gate Count: {Hadamard: 1, RZ: 1}

Contents

Using PennyLane

Release news

Development

API

Internals