qp.decomposition

This module implements the infrastructure for PennyLane’s graph-based decomposition system.

Warning

This module is experimental and is subject to change in the future.

Defining Decomposition Rules

register_resources(ops[, qfunc, name, ...])

Binds a quantum function to its required resources.

register_condition(condition[, qfunc])

Binds a condition to a decomposition rule for when it is applicable.

resource_rep(op_type, **params)

Binds an operator type with additional resource parameters.

controlled_resource_rep(base_class, ...[, ...])

Creates a CompressedResourceOp representation of a controlled operator.

adjoint_resource_rep(base_class[, base_params])

Creates a CompressedResourceOp representation of the adjoint of an operator.

pow_resource_rep(base_class, base_params, z)

Creates a CompressedResourceOp representation of the power of an operator.

change_op_basis_resource_rep(compute_op, ...)

Creates a CompressedResourceOp representation of the compute-uncompute pattern ChangeOpBasis of operators.

DecompositionRule(func[, resources, ...])

Represents a decomposition rule for an operator.

Resources([gate_counts, weighted_cost])

Stores resource estimates.

CompressedResourceOp(op_type[, params])

A lightweight representation of an operator to be decomposed.

null_decomp

A decomposition rule that can be assigned to an operator so that the operator decomposes to nothing.

A decomposition rule must be defined as a quantum function that accepts (*op.parameters, op.wires, **op.hyperparameters) as arguments, where op is an instance of the operator type that the decomposition is for. Additionally, a decomposition rule must declare its resource requirements using the register_resources decorator:

@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])

Inspecting and Managing Decomposition Rules

add_decomps(op_type, *decomps)

Globally registers new decomposition rules with an operator class.

list_decomps(op)

Lists decomposition rules for an operator.

exclude_decomps(op, *names)

Excludes decomposition rules of an operator by name.

has_decomp(op)

Checks whether an operator has decomposition rules defined.

inspect_decomps(op, *rules[, ...])

Inspect the decomposition rules of an operator.

local_decomps()

Start a new context in which additions to decomposition rules are localized.

register_signature(op, **kwargs)

Register a possible signature for an operator.

signature_registry()

Return a read-only mapping of the registered operator signatures.

DecompCollection([decomps])

An ordered, name-addressable collection of DecompositionRule objects.

all_decomps(op[, skip_ops])

Collect all decomposition rules downstream of an Operator.

PennyLane maintains a global dictionary of decomposition rules. New decomposition rules can be registered under an operator using add_decomps, and list_decomps can be called to inspect a list of known decomposition rules for a given operator. An operator can be associated with multiple decomposition rules, and the one that leads to the most resource-efficient decomposition towards a target gate set is chosen.

Integration with the Decompose Transform

The _tape_decompose() transform uses the graph-based decomposition algorithm to find resource-efficient decompositions towards a target gate set.

from pprint import pprint

with qp.queuing.AnnotatedQueue() as q:
    qp.CRX(0.5, wires=[0, 1])

tape = qp.tape.QuantumScript.from_queue(q)
[new_tape], _ = qp.transforms._tape_decompose([tape], gate_set={"RX", "RY", "RZ", "CZ", "GlobalPhase"})
>>> pprint(new_tape.operations)
[RX(0.25, wires=[1]), CZ(wires=[0, 1]), RX(-0.25, wires=[1]), CZ(wires=[0, 1])]

Customizing Decompositions

The _tape_decompose() transform accepts custom decomposition rules through the keyword arguments fixed_decomps and alt_decomps. The user can define custom decomposition rules as explained in the Defining Decomposition Rules section, and provide them to the transform via these arguments.

fixed_decomps forces the transform to use the specified decomposition rules for certain operators, whereas alt_decomps is used to provide alternative decomposition rules for operators that may be chosen if they lead to a more resource-efficient decomposition.

In the following example, isingxx_decomp will always be used to decompose qp.IsingXX gates; when it comes to qp.CNOT, the system will choose the most efficient decomposition rule among my_cnot1, my_cnot2, and all existing decomposition rules defined for qp.CNOT.

@qp.register_resources({qp.CNOT: 2, qp.RX: 1}, name="isingxx_decomp")
def isingxx_decomp(phi, wires, **__):
    qp.CNOT(wires=wires)
    qp.RX(phi, wires=[wires[0]])
    qp.CNOT(wires=wires)

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

@qp.register_resources({qp.RY: 2, qp.CZ: 1, qp.Z: 2}, name="my_cnot2")
def my_cnot2(wires, **__):
    qp.RY(np.pi/2, wires[1])
    qp.Z(wires[1])
    qp.CZ(wires=wires)
    qp.RY(np.pi/2, wires[1])
    qp.Z(wires[1])

@qp.transforms._tape_decompose(
    gate_set={"RX", "RZ", "CZ", "GlobalPhase"},
    alt_decomps={qp.CNOT: [my_cnot1, my_cnot2]},
    fixed_decomps={qp.IsingXX: isingxx_decomp},
)
@qp.qnode(qp.device("lightning.qubit", wires=2))
def circuit():
    qp.CNOT(wires=[0, 1])
    qp.IsingXX(0.5, wires=[0, 1])
    return qp.state()
>>> qp.specs(qp.qjit(circuit, capture=False))()["resources"].quantum_operations
{'GlobalPhase': 6, 'CZ': 3, 'RX': 7, 'RZ': 12}

To register alternative decomposition rules under an operator to be used globally, use add_decomps(). See Inspecting and Managing Decomposition Rules for details.

Graph-based Decomposition Solver

DecompositionGraph(operations, gate_set[, ...])

A graph that models a decomposition problem.

DecompGraphSolution(visitor, all_op_indices, ...)

A solution to a decomposition graph.

The decomposition graph is a directed graph of operators and decomposition rules. Dijkstra’s algorithm is used to explore the graph and find the most efficient decomposition of a given operator towards a target gate set.

op = qp.CRX(0.5, wires=[0, 1])
graph = DecompositionGraph(
    operations=[op],
    gate_set={"RZ", "RX", "CNOT", "GlobalPhase"},
)
solution = graph.solve()
>>> with qp.queuing.AnnotatedQueue() as q:
...     solution.decomposition(op)(0.5, wires=[0, 1])
>>> q.queue
[RZ(1.5707963267948966, wires=[1]),
    RY(0.25, wires=[1]),
    CNOT(wires=[0, 1]),
    RY(-0.25, wires=[1]),
    CNOT(wires=[0, 1]),
    RZ(-1.5707963267948966, wires=[1])]
>>> solution.resource_estimate(op)
<num_gates=10, gate_counts={CNOT: 2, RX: 2, RZ: 6}, weighted_cost=10.0>

Utility Classes

DecompositionError

Base class for decomposition errors.

GateSet(gate_set[, name])

Stores the target gate set of a decomposition pass.

Contents

Using PennyLane

Release news

Development

API

Internals