qp.register_resources

register_resources(ops, qfunc=None, *, name=None, work_wires=None, exact=True)[source]

Binds a quantum function to its required resources.

Parameters:
  • ops (dict or Callable) – a dictionary mapping unique operators within the given qfunc to their number of occurrences therein. If a function is provided instead of a static dictionary, a dictionary must be returned from the function. For more information, consult the “Quantum Functions as Decomposition Rules” section below.

  • qfunc (Callable) – the quantum function that implements the decomposition. If None, returns a decorator for acting on a function.

Keyword Arguments:
  • name (Optional(str)) – Optional public rule name, if None, use the function name with leading underscores removed. Names must be non-blank and must not start with an underscore.

  • work_wires (dict or Callable) – a dictionary declaring the number of work wires of each type required to perform this decomposition. Accepted work wire types include "zeroed", "borrowed", "burnable", and "garbage". For more information, consult the “Dynamic Allocation of Work Wires” section below.

  • exact (bool) – whether the resources are computed exactly (True, default) or estimated heuristically (False). This information is only relevant for testing and validation purposes.

Returns:

a data structure that represents a decomposition rule, which contains a PennyLane quantum function representing the decomposition, and its resource function.

Return type:

DecompositionRule

Raises:
  • TypeError – If an explicit name is not a string.

  • ValueError – If the name is blank or starts with an underscore, or a registered rule is renamed.

Example

This function can be used as a decorator to bind a quantum function to its required resources so that it can be used as a decomposition rule within the new graph-based decomposition system.

import pennylane as qp

@qp.register_resources({qp.H: 2, qp.CZ: 1}, name="my_cnot")
def my_cnot(wires, **_):
    qp.H(wires=wires[1])
    qp.CZ(wires=wires)
    qp.H(wires=wires[1])

@qp.transforms._tape_decompose(gate_set={qp.CZ, qp.H}, fixed_decomps={qp.CNOT: my_cnot})
@qp.qnode(qp.device("default.qubit"))
def circuit():
    qp.CNOT(wires=[0, 1])
    return qp.state()
>>> print(qp.draw(circuit, level="device")())
0: ────╭●────┤ ╭State
1: ──H─╰Z──H─┤ ╰State

Alternatively, the decomposition rule can be created in-line:

>>> my_cnot = qp.register_resources({qp.H: 2, qp.CZ: 1}, my_cnot, name="my_cnot")

The public name is set by register_resources(), independently of the function name.

>>> my_cnot.name
'my_cnot'

For example, use a short strategy name:

@qp.register_resources({qp.H: 2, qp.CZ: 1}, name="to_cz")
def my_cnot(wires, **_):
    qp.H(wires=wires[1])
    qp.CZ(wires=wires)
    qp.H(wires=wires[1])
>>> my_cnot.name
'to_cz'

Quantum functions representing decomposition rules within the new decomposition system are expected to take (*op.parameters, op.wires, **op.hyperparameters) as arguments, where op is an instance of the operator type that the decomposition is for.

In many cases, the resource requirement of an operator’s decomposition is not static; some operators have properties that directly affect the resource estimate of its decompositions, i.e., the types of gates that exist in the decomposition and their number of occurrences.

For each operator class, the set of parameters that affects the type of gates and their number of occurrences in its decompositions is given by the resource_keys attribute.

See also

resource_rep()

Some decomposition rules make use of work wires, which can be dynamically requested within the quantum function using allocate(). Such decomposition rules should register the number of work wires they require so that the decomposition algorithm is able to budget the use of work wires across decomposition rules.

There are four types of work wires:

  • “zeroed” wires are guaranteed to be in the \(|0\rangle\) state initially, and they must be restored to the \(|0\rangle\) state before deallocation.

  • “borrowed” wires are allocated in an arbitrary state, but they must be restored to the same initial state before deallocation.

  • “burnable” wires are guaranteed to be in the \(|0\rangle\) state initially, but they can be deallocated in any arbitrary state.

  • “garbage” wires can be allocated in any state, and can be deallocated in any state.

Here’s a decomposition for a multi-controlled Rot that uses a zeroed work wire:

import pennylane as qp
from pennylane.allocation import allocate
from pennylane.typing import Wire

def _condition_fn(control_wires, **_):
    return len(control_wires) > 1

def _ops_fn(control_wires, **_):
    return {
        qp.MultiControlledX(Wire[len(control_wires) + 1]): 2,
        qp.CRot: 1
    }

@qp.register_condition(_condition_fn)
@qp.register_resources(ops=_ops_fn, work_wires={"zeroed": 1}, name="ancilla_controlled_rot")
def _controlled_rot_standard(base, control_wires, **_):
    wires = control_wires + base.wires
    with allocate(1, state="zero", restored=True) as work_wires:
        qp.MultiControlledX([*wires[:-1], work_wires[0]])
        qp.CRot(**base.dynamic_args, wires=[work_wires[0], wires[-1]])
        qp.MultiControlledX([*wires[:-1], work_wires[0]])

decomps = {"C(Rot)": _controlled_rot_standard}

@qp.transforms._tape_decompose(
    gate_set={qp.MultiControlledX, qp.CRot},
    fixed_decomps=decomps,
    num_work_wires=1,
)
@qp.qnode(qp.device("default.qubit"))
def circuit():
    qp.ctrl(qp.Rot(0.1, 0.2, 0.3, wires=3), control=[0, 1, 2])
    return qp.probs(wires=[0, 1, 2, 3])
>>> print(qp.draw(circuit)())
 0: ───────╭●──────────────────────╭●────┤ ╭Probs
 1: ───────├●──────────────────────├●────┤ ├Probs
 2: ───────├●──────────────────────├●────┤ ├Probs
 3: ───────│──╭Rot(0.10,0.20,0.30)─│─────┤ ╰Probs
      |0>├─╰X─╰●───────────────────╰X──┤

Contents

Using PennyLane

Release news

Development

API

Internals