qml.decomposition.DecompGraphSolution

class DecompGraphSolution(visitor, all_op_indices, op_to_op_nodes)[source]

Bases: object

A solution to a decomposition graph.

An instance of this class is returned from DecompositionGraph.solve()

Example

from pennylane.decomposition import DecompositionGraph

op = qml.CRX(0.5, wires=[0, 1])
graph = DecompositionGraph(
    operations=[op],
    gate_set={"RZ", "RX", "CNOT", "GlobalPhase"},
)
solution = graph.solve()
>>> with qml.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={RZ: 6, CNOT: 2, RX: 2}, weighted_cost=10.0>

decomposition(op[, num_work_wires])

Returns the optimal decomposition rule for a given operator.

is_solved_for(op[, num_work_wires])

Tests whether the decomposition graph is solved for a given operator.

resource_estimate(op[, num_work_wires])

Returns the resource estimate for a given operator.

decomposition(op, num_work_wires=0)[source]

Returns the optimal decomposition rule for a given operator.

Parameters:
  • op (Operator) – The operator for which to return the optimal decomposition.

  • num_work_wires (int) – The number of work wires available to decompose this operator.

Returns:

The optimal decomposition.

Return type:

DecompositionRule

Example

The decomposition rule is a quantum function that takes (*op.parameters, wires=op.wires, **op.hyperparameters) as arguments.

op = qml.CRY(0.2, wires=[0, 2])
graph = DecompositionGraph(
    operations=[op],
    gate_set={"RZ", "RX", "CNOT", "GlobalPhase"},
)
solution = graph.solve()
rule = solution.decomposition(op)
>>> with qml.queuing.AnnotatedQueue() as q:
...     rule(*op.parameters, wires=op.wires, **op.hyperparameters)
>>> q.queue
[RY(0.1, wires=[2]),
 CNOT(wires=[0, 2]),
 RY(-0.1, wires=[2]),
 CNOT(wires=[0, 2])]
is_solved_for(op, num_work_wires=0)[source]

Tests whether the decomposition graph is solved for a given operator.

Parameters:
  • op (Operator) – The operator to check.

  • num_work_wires (int) – The number of available work wires to decompose this operator.

resource_estimate(op, num_work_wires=0)[source]

Returns the resource estimate for a given operator.

Parameters:
  • op (Operator) – The operator for which to return the resource estimates.

  • num_work_wires (int) – The number of work wires available to decompose this operator.

Returns:

The resource estimate.

Return type:

Resources

Example

The resource estimate is a gate count in terms of the target gate set, not the immediate set of gates that the operator decomposes to.

op = qml.CRX(0.5, wires=[0, 1])
graph = DecompositionGraph(
    operations=[op],
    gate_set={"RZ", "RX", "CNOT", "GlobalPhase"},
)
solution = graph.solve()
>>> with qml.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])]
>>> graph.resource_estimate(op)
<num_gates=10, gate_counts={RZ: 6, CNOT: 2, RX: 2}, weighted_cost=10.0>