qp.decomp_inspector

decomp_inspector(tape, *, gate_set=None, num_work_wires=0, minimize_work_wires=False, fixed_decomps=None, alt_decomps=None)[source]

Returns a DecompGraphInspector for querying the decomposition decisions made for a given circuit and target gate set.

Parameters:
  • tape (QuantumScript or QNode or Callable) – a quantum circuit.

  • gate_set (Iterable[str or type], Dict[type or str, float]) – The target gate set specified as either (1) a sequence of operator types and/or names, (2) a dictionary mapping operator types and/or names to their respective costs, in which case the graph will try to minimize the total cost.

  • num_work_wires (int) – The maximum number of work wires that can be simultaneously allocated. If None, assume an infinite number of work wires. Defaults to 0.

  • minimize_work_wires (bool) – If True, minimize the number of work wires simultaneously allocated throughout the circuit. Defaults to False.

  • fixed_decomps (Dict[Type[Operator], DecompositionRule]) – a dictionary mapping operator types to custom decomposition rules. A decomposition rule is a quantum function decorated with register_resources(). The custom decomposition rules specified here will be used in place of the existing decomposition rules defined for this operator.

  • alt_decomps (Dict[Type[Operator], List[DecompositionRule]]) – a dictionary mapping operator types to lists of alternative custom decomposition rules. A decomposition rule is a quantum function decorated with register_resources(). The custom decomposition rules specified here will be considered as alternatives to the existing decomposition rules defined for this operator, and one of them may be chosen if they lead to a more resource-efficient decomposition.

Returns:

An interactive object that can be used to inspect the graph.

Return type:

DecompGraphInspector

Examples

Applying the decomp_inspector transform on a circuit will return a DecompGraphInspector object constructed from operators in the circuit. These examples weight RX, RY, and RZ at 50 and leave CNOT at 1, so a rotation costs as much as fifty Cliffords.

rotations = qp.gate_sets.ROTATIONS_PLUS_CNOT | {qp.RX: 50, qp.RY: 50, qp.RZ: 50}

@qp.decomp_inspector(gate_set=rotations, num_work_wires=2)
@qp.qnode(qp.device("default.qubit"))
def circuit():
    qp.ctrl(qp.MultiRZ(0.5, [0, 1]), control=[3, 4, 5])
    return qp.probs()

inspector = circuit()

The inspector object allows to query a given operator to identify which decomposition rule was “CHOSEN” among those that were considered.

>>> inspector.inspect_decomps(qp.ctrl(qp.MultiRZ(0.5, [0, 1]), control=[3, 4, 5]), num_work_wires=2)
Decomposition 0 (name: single_work_wire)
0: ──────────╭MultiRZ(0.50)───────┤
1: ──────────├MultiRZ(0.50)───────┤
3: ───────╭●─│──────────────╭●────┤
4: ───────├●─│──────────────├●────┤
5: ───────├●─│──────────────├●────┤
     |0>├─╰X─╰●─────────────╰X──┤
Estimated First-Level Expansion Gates: {Controlled(MultiRZ(AbstractArray((), float64, weak_type=True), wires=AbstractWires(2)), control_wires=AbstractWires(1), control_values=AbstractArray((1,), bool, weak_type=True)): 1, MultiControlledX(wires=AbstractWires(4), control_values=AbstractArray((3,), bool)): 2, PauliX: 3}
Actual First-Level Expansion Gates: {Controlled(MultiRZ(AbstractArray((), float64, weak_type=True), wires=AbstractWires(2)), control_wires=AbstractWires(1), control_values=AbstractArray((1,), bool)): 1, MultiControlledX(wires=AbstractWires(4), control_values=AbstractArray((3,), bool)): 2}
Wire Allocations: {'zero': 1}
Full Expansion Gates: {CNOT: 26, GlobalPhase: 68, MidMeasure: 2, RX: 20, RY: 10, RZ: 42}
Weighted Cost: 3628.0

Decomposition 1 (name: many_zeroed_work_wires)
Not applicable (provided operator instance does not meet all conditions for this rule).

Decomposition 2 (name: controlled(to_paulirot))
0: ─╭RZZ(0.50)─┤
1: ─├RZZ(0.50)─┤
3: ─├●─────────┤
4: ─├●─────────┤
5: ─╰●─────────┤
Estimated First-Level Expansion Gates: {Controlled(PauliRot(theta=AbstractArray((), float64, weak_type=True), pauli_word=ZZ, wires=AbstractWires(2)), control_wires=AbstractWires(3), control_values=AbstractArray((3,), bool, weak_type=True)): 1, PauliX: 3}
Actual First-Level Expansion Gates: {Controlled(PauliRot(theta=AbstractArray((), float64, weak_type=True), pauli_word=ZZ, wires=AbstractWires(2)), control_wires=AbstractWires(3), control_values=AbstractArray((3,), bool)): 1}
Full Expansion Gates: {CNOT: 46, GlobalPhase: 25, RX: 23, RZ: 22}
Weighted Cost: 2296.0

CHOSEN: Decomposition 3 (name: controlled(cnot_ladder))
0: ─╭MultiRZLadder@RZ(0.50)@MultiRZLadder─┤
1: ─├MultiRZLadder@RZ(0.50)@MultiRZLadder─┤
3: ─├●────────────────────────────────────┤
4: ─├●────────────────────────────────────┤
5: ─╰●────────────────────────────────────┤
Estimated First-Level Expansion Gates: {Controlled(MultiRZLadder(wires=AbstractWires(2), reverse=True) @ RZ @ MultiRZLadder(wires=AbstractWires(2), reverse=False), control_wires=AbstractWires(3), control_values=AbstractArray((3,), bool, weak_type=True)): 1, PauliX: 3}
Actual First-Level Expansion Gates: {Controlled(MultiRZLadder(wires=AbstractWires(2), reverse=True) @ RZ @ MultiRZLadder(wires=AbstractWires(2), reverse=False), control_wires=AbstractWires(3), control_values=AbstractArray((3,), bool)): 1}
Full Expansion Gates: {CNOT: 46, GlobalPhase: 19, RX: 17, RZ: 22}
Weighted Cost: 1996.0

The CNOT ladder of the base operator stays unchanged, and the control applies only to the RZ:

>>> rule = qp.list_decomps(qp.MultiRZ)["cnot_ladder"]
>>> cob = qp.testing.decomp_rule_to_tape(qp.MultiRZ(0.5, [0, 1]), rule).operations[0]
>>> inspector.inspect_decomps(qp.ctrl(cob, control=[3, 4, 5]), num_work_wires=2)
CHOSEN: Decomposition 0 (name: control_target)
0: ─╭MultiRZLadder─╭RZ(0.50)─╭MultiRZLadder─┤
1: ─╰MultiRZLadder─│─────────╰MultiRZLadder─┤
3: ────────────────├●───────────────────────┤
4: ────────────────├●───────────────────────┤
5: ────────────────╰●───────────────────────┤
Estimated First-Level Expansion Gates: {Controlled(RZ, control_wires=AbstractWires(3), control_values=AbstractArray((3,), bool, weak_type=True)): 1, MultiRZLadder(wires=AbstractWires(2), reverse=False): 1, MultiRZLadder(wires=AbstractWires(2), reverse=True): 1, PauliX: 3}
Actual First-Level Expansion Gates: {Controlled(RZ, control_wires=AbstractWires(3), control_values=AbstractArray((3,), bool)): 1, MultiRZLadder(wires=AbstractWires(2), reverse=False): 1, MultiRZLadder(wires=AbstractWires(2), reverse=True): 1}
Full Expansion Gates: {CNOT: 46, GlobalPhase: 16, RX: 14, RZ: 22}
Weighted Cost: 1846.0

For applicable decompositions, the “First-Level Expansion” label refers to the operators immediately produced by the decomposition rule, whereas the “Full Expansion” refers to the circuit produced by decomposing the operator all the way down to the target gate set. The weighted cost of the decomposition, computed based on the full expansion, is also displayed.

In addition to the operators at the top level of the circuit, we can also inspect the graph for how intermediate operators are decomposed. For example, let’s look at the single-controlled MultiRZ produced in the decomposition of the controlled MultiRZ (notice how num_work_wires is set to 1 here, since decomposition of the top-level operator already used one of the work wires in the budget, so this inner operator has one fewer work wire available to it):

>>> inspector.inspect_decomps(qp.ctrl(qp.MultiRZ(0.5, [0, 1]), control=2), num_work_wires=1)
Decomposition 0 (name: controlled(to_paulirot))
0: ─╭RZZ(0.50)─┤
1: ─├RZZ(0.50)─┤
2: ─╰●─────────┤
Estimated First-Level Expansion Gates: {Controlled(PauliRot(theta=AbstractArray((), float64, weak_type=True), pauli_word=ZZ, wires=AbstractWires(2)), control_wires=AbstractWires(1), control_values=AbstractArray((1,), bool, weak_type=True)): 1, PauliX: 1}
Actual First-Level Expansion Gates: {Controlled(PauliRot(theta=AbstractArray((), float64, weak_type=True), pauli_word=ZZ, wires=AbstractWires(2)), control_wires=AbstractWires(1), control_values=AbstractArray((1,), bool)): 1}
Full Expansion Gates: {CNOT: 4, GlobalPhase: 5, RX: 5, RZ: 2}
Weighted Cost: 354.0

CHOSEN: Decomposition 1 (name: controlled(cnot_ladder))
0: ─╭MultiRZLadder@RZ(0.50)@MultiRZLadder─┤
1: ─├MultiRZLadder@RZ(0.50)@MultiRZLadder─┤
2: ─╰●────────────────────────────────────┤
Estimated First-Level Expansion Gates: {Controlled(MultiRZLadder(wires=AbstractWires(2), reverse=True) @ RZ @ MultiRZLadder(wires=AbstractWires(2), reverse=False), control_wires=AbstractWires(1), control_values=AbstractArray((1,), bool, weak_type=True)): 1, PauliX: 1}
Actual First-Level Expansion Gates: {Controlled(MultiRZLadder(wires=AbstractWires(2), reverse=True) @ RZ @ MultiRZLadder(wires=AbstractWires(2), reverse=False), control_wires=AbstractWires(1), control_values=AbstractArray((1,), bool)): 1}
Full Expansion Gates: {CNOT: 4, GlobalPhase: 3, RX: 3, RZ: 2}
Weighted Cost: 254.0

As above, one more decomposition of that change of basis leaves the ladder bare:

>>> inspector.inspect_decomps(qp.ctrl(cob, control=2), num_work_wires=1)
CHOSEN: Decomposition 0 (name: control_target)
0: ─╭MultiRZLadder─╭RZ(0.50)─╭MultiRZLadder─┤
1: ─╰MultiRZLadder─│─────────╰MultiRZLadder─┤
2: ────────────────╰●───────────────────────┤
Estimated First-Level Expansion Gates: {Controlled(RZ, control_wires=AbstractWires(1), control_values=AbstractArray((1,), bool, weak_type=True)): 1, MultiRZLadder(wires=AbstractWires(2), reverse=False): 1, MultiRZLadder(wires=AbstractWires(2), reverse=True): 1, PauliX: 1}
Actual First-Level Expansion Gates: {Controlled(RZ, control_wires=AbstractWires(1), control_values=AbstractArray((1,), bool)): 1, MultiRZLadder(wires=AbstractWires(2), reverse=False): 1, MultiRZLadder(wires=AbstractWires(2), reverse=True): 1}
Full Expansion Gates: {CNOT: 4, GlobalPhase: 2, RX: 2, RZ: 2}
Weighted Cost: 204.0

This can be useful to find out why a circuit cannot be decomposed:

@qp.decomp_inspector(gate_set={"RZ", "RX", "CNOT", "PPR_4"}, num_work_wires=2)
@qp.qnode(qp.device("default.qubit"))
def circuit():
    qp.PauliRot(0.5, "XYZ", [0, 1, 2])
    return qp.probs()

inspector = circuit()
>>> inspector.inspect_decomps(qp.PauliRot(0.5, "XYZ", [0, 1, 2]))
Decomposition 0 (name: basis_change_multirz)
0: ─╭PauliRotBasis@MultiRZ(0.50)@PauliRotBasis─┤
1: ─├PauliRotBasis@MultiRZ(0.50)@PauliRotBasis─┤
2: ─╰PauliRotBasis@MultiRZ(0.50)@PauliRotBasis─┤
First-Level Expansion Gates: {PauliRotBasis(sign=-1, pauli_word=XYZ, wires=AbstractWires(3)) @ MultiRZ(AbstractArray((), float64, weak_type=True), wires=AbstractWires(3)) @ PauliRotBasis(sign=1, pauli_word=XYZ, wires=AbstractWires(3)): 1}
Missing Ops: {PauliRotBasis(sign=-1, pauli_word=XYZ, wires=AbstractWires(3)) @ MultiRZ(AbstractArray((), float64, weak_type=True), wires=AbstractWires(3)) @ PauliRotBasis(sign=1, pauli_word=XYZ, wires=AbstractWires(3))}

That missing operator is a change of basis, conjugating the MultiRZ with a Hadamard/PPR_4 basis change. The MultiRZ is available, so splitting the basis change surfaces the root problem:

>>> rule = qp.list_decomps(qp.PauliRot)["basis_change_multirz"]
>>> pauli_cob = qp.testing.decomp_rule_to_tape(qp.PauliRot(0.5, "XYZ", [0, 1, 2]), rule).operations[0]
>>> to_basis = pauli_cob.compute_op
>>> inspector.inspect_decomps(to_basis)
Decomposition 0 (name: PauliRotBasis_decomp)
0: ──H───────────┤
1: ──PPR(π/4, X)─┤
First-Level Expansion Gates: {Hadamard: 1, PPR_4(1, 'X', wires=AbstractWires(1)): 1}
Missing Ops: {Hadamard}

That missing operator is the Hadamard. The graph has no decomposition for it into this gate set, which is why PauliRot stops here. Inspecting it directly:

>>> inspector.inspect_decomps(qp.Hadamard(0))
Decomposition 0 (name: to_ppr)
0: ──PPR(π/2, Z)──PPR(π/4, Y)──GlobalPhase(-1.57)─┤
First-Level Expansion Gates: {GlobalPhase: 1, PPR_2(1, 'Z', wires=AbstractWires(1)): 1, PPR_4(1, 'Y', wires=AbstractWires(1)): 1}
Missing Ops: {GlobalPhase, PPR_2(1, 'Z', wires=AbstractWires(1))}

Decomposition 1 (name: to_ppm)
Insufficient work wires: requires 1 but only 0 available.

Decomposition 2 (name: to_rz_ry)
0: ──RZ(3.14)──RY(1.57)──GlobalPhase(-1.57)─┤
First-Level Expansion Gates: {GlobalPhase: 1, RY: 1, RZ: 1}
Missing Ops: {GlobalPhase}

Decomposition 3 (name: to_rz_rx)
0: ──RZ(1.57)──RX(1.57)──RZ(1.57)──GlobalPhase(-1.57)─┤
First-Level Expansion Gates: {GlobalPhase: 1, RX: 1, RZ: 2}
Missing Ops: {GlobalPhase}

Now it’s finally clear that the reason why PauliRot could not be decomposed was that GlobalPhase is missing from the target gate set.

Some decomposition rules make use of dynamically allocated work wires. For example:

>>> qp.inspect_decomps(qp.MultiControlledX([0, 1, 2, 3]), "one_zeroed_work_wire")
Decomposition 0 (name: one_zeroed_work_wire)
0: ───────╭●─────●╮────┤
1: ───────├●─────●┤────┤
2: ───────│──╭●───│────┤
3: ───────│──├X───│────┤
     |0>├─╰⊕─╰●──⊕╯──┤
Estimated Gate Count: {Adjoint(TemporaryAND): 1, PauliX: 3, TemporaryAND: 1, Toffoli: 1}
Actual Gate Count: {Adjoint(TemporaryAND): 1, TemporaryAND: 1, Toffoli: 1}
Wire Allocations: {'zero': 1}

These rules can only be selected if there are enough unused wires left on the device for allocation. In order to account for this, the decompose() transform takes a num_work_wires argument which acts as a work wire budget. It ensures that no more than num_work_wires number of work wires can be simultaneously allocated during the decomposition. Consider the following circuit:

rotations = qp.gate_sets.ROTATIONS_PLUS_CNOT | {qp.RX: 50, qp.RY: 50, qp.RZ: 50}

@qp.decomp_inspector(gate_set=rotations, num_work_wires=2)
@qp.qnode(qp.device("default.qubit"))
def circuit():
    qp.ctrl(qp.MultiRZ(0.5, [0, 1]), control=[3, 4, 5, 6])
    qp.MultiControlledX([2, 3, 4, 5, 6])
    return qp.probs()

inspector = circuit()

When calling inspect_decomps, we also need to provide the work wire allocation budget:

>>> op = qp.ctrl(qp.MultiRZ(0.5, [0, 1]), control=[3, 4, 5, 6])
>>> inspector.inspect_decomps(op, num_work_wires=2)
Decomposition 0 (name: single_work_wire)
0: ──────────╭MultiRZ(0.50)───────┤
1: ──────────├MultiRZ(0.50)───────┤
3: ───────╭●─│──────────────╭●────┤
4: ───────├●─│──────────────├●────┤
5: ───────├●─│──────────────├●────┤
6: ───────├●─│──────────────├●────┤
     |0>├─╰X─╰●─────────────╰X──┤
Estimated First-Level Expansion Gates: {Controlled(MultiRZ(AbstractArray((), float64, weak_type=True), wires=AbstractWires(2)), control_wires=AbstractWires(1), control_values=AbstractArray((1,), bool, weak_type=True)): 1, MultiControlledX(wires=AbstractWires(5), control_values=AbstractArray((4,), bool)): 2, PauliX: 4}
Actual First-Level Expansion Gates: {Controlled(MultiRZ(AbstractArray((), float64, weak_type=True), wires=AbstractWires(2)), control_wires=AbstractWires(1), control_values=AbstractArray((1,), bool)): 1, MultiControlledX(wires=AbstractWires(5), control_values=AbstractArray((4,), bool)): 2}
Wire Allocations: {'zero': 1}
Full Expansion Gates: {CNOT: 50, GlobalPhase: 99, MidMeasure: 2, RX: 27, RY: 18, RZ: 78}
Weighted Cost: 6202.0

Decomposition 1 (name: many_zeroed_work_wires)
Not applicable (provided operator instance does not meet all conditions for this rule).

Decomposition 2 (name: controlled(to_paulirot))
0: ─╭RZZ(0.50)─┤
1: ─├RZZ(0.50)─┤
3: ─├●─────────┤
4: ─├●─────────┤
5: ─├●─────────┤
6: ─╰●─────────┤
Estimated First-Level Expansion Gates: {Controlled(PauliRot(theta=AbstractArray((), float64, weak_type=True), pauli_word=ZZ, wires=AbstractWires(2)), control_wires=AbstractWires(4), control_values=AbstractArray((4,), bool, weak_type=True)): 1, PauliX: 4}
Actual First-Level Expansion Gates: {Controlled(PauliRot(theta=AbstractArray((), float64, weak_type=True), pauli_word=ZZ, wires=AbstractWires(2)), control_wires=AbstractWires(4), control_values=AbstractArray((4,), bool)): 1}
Full Expansion Gates: {CNOT: 134, GlobalPhase: 33, RX: 31, RZ: 46}
Weighted Cost: 3984.0

CHOSEN: Decomposition 3 (name: controlled(cnot_ladder))
0: ─╭MultiRZLadder@RZ(0.50)@MultiRZLadder─┤
1: ─├MultiRZLadder@RZ(0.50)@MultiRZLadder─┤
3: ─├●────────────────────────────────────┤
4: ─├●────────────────────────────────────┤
5: ─├●────────────────────────────────────┤
6: ─╰●────────────────────────────────────┤
Estimated First-Level Expansion Gates: {Controlled(MultiRZLadder(wires=AbstractWires(2), reverse=True) @ RZ @ MultiRZLadder(wires=AbstractWires(2), reverse=False), control_wires=AbstractWires(4), control_values=AbstractArray((4,), bool, weak_type=True)): 1, PauliX: 4}
Actual First-Level Expansion Gates: {Controlled(MultiRZLadder(wires=AbstractWires(2), reverse=True) @ RZ @ MultiRZLadder(wires=AbstractWires(2), reverse=False), control_wires=AbstractWires(4), control_values=AbstractArray((4,), bool)): 1}
Full Expansion Gates: {CNOT: 134, GlobalPhase: 25, RX: 23, RZ: 46}
Weighted Cost: 3584.0

Decomposing the chosen change of basis once more again leaves the CNOT ladder bare:

>>> inspector.inspect_decomps(qp.ctrl(cob, control=[3, 4, 5, 6]), num_work_wires=2)
CHOSEN: Decomposition 0 (name: control_target)
0: ─╭MultiRZLadder─╭RZ(0.50)─╭MultiRZLadder─┤
1: ─╰MultiRZLadder─│─────────╰MultiRZLadder─┤
3: ────────────────├●───────────────────────┤
4: ────────────────├●───────────────────────┤
5: ────────────────├●───────────────────────┤
6: ────────────────╰●───────────────────────┤
Estimated First-Level Expansion Gates: {Controlled(RZ, control_wires=AbstractWires(4), control_values=AbstractArray((4,), bool, weak_type=True)): 1, MultiRZLadder(wires=AbstractWires(2), reverse=False): 1, MultiRZLadder(wires=AbstractWires(2), reverse=True): 1, PauliX: 4}
Actual First-Level Expansion Gates: {Controlled(RZ, control_wires=AbstractWires(4), control_values=AbstractArray((4,), bool)): 1, MultiRZLadder(wires=AbstractWires(2), reverse=False): 1, MultiRZLadder(wires=AbstractWires(2), reverse=True): 1}
Full Expansion Gates: {CNOT: 134, GlobalPhase: 21, RX: 19, RZ: 46}
Weighted Cost: 3384.0

Similarly, for the MultiControlledX in the circuit:

>>> op = qp.MultiControlledX([2, 3, 4, 5, 6])
>>> inspector.inspect_decomps(op, num_work_wires=2)
Decomposition 0 (name: no_work_wires)
2: ────╭●───────────────────╭●──────────────────────╭●──────────────────┤
3: ────├●───────────────────├●──────────────────────├●──────────────────┤
4: ────│─────────╭●─────────│─────────╭●────────────├●──────────────────┤
5: ────│─────────├●─────────│─────────├●────────────├●──────────────────┤
6: ──H─╰X──U(M0)─╰X──U(M0)†─╰X──U(M0)─╰X──U(M0)†──H─╰GlobalPhase(-1.57)─┤
M0 =
[[ 9.23879533e-01+0.38268343j -5.34910791e-34+0.j        ]
 [ 5.34910791e-34+0.j          9.23879533e-01-0.38268343j]]
Estimated First-Level Expansion Gates: {Adjoint(QubitUnitary(U=AbstractArray((2, 2), complex128, weak_type=True), wires=AbstractWires(1))): 2, Controlled(GlobalPhase, control_wires=AbstractWires(4), control_values=AbstractArray((4,), bool)): 1, Hadamard: 2, MultiControlledX(wires=AbstractWires(3), control_values=AbstractArray((2,), bool), work_wires=AbstractWires(2), work_wire_type=borrowed): 4, PauliX: 4, QubitUnitary(U=AbstractArray((2, 2), complex128, weak_type=True), wires=AbstractWires(1)): 2}
Actual First-Level Expansion Gates: {Adjoint(QubitUnitary(U=AbstractArray((2, 2), complex128, weak_type=True), wires=AbstractWires(1))): 2, Controlled(GlobalPhase, control_wires=AbstractWires(4), control_values=AbstractArray((4,), bool)): 1, Hadamard: 2, MultiControlledX(wires=AbstractWires(3), control_values=AbstractArray((2,), bool), work_wires=AbstractWires(2), work_wire_type=borrowed): 4, QubitUnitary(U=AbstractArray((2, 2), complex128, weak_type=True), wires=AbstractWires(1)): 2}
Full Expansion Gates: {CNOT: 58, GlobalPhase: 62, RX: 27, RY: 14, RZ: 53}
Weighted Cost: 4758.0

Decomposition 1 (name: one_borrowed_work_wire)
2: ────╭●──────────────────╭●──────────────────┤
3: ────├●─╭X──X─╭●──X†─╭X†─├●─╭X──X─╭●──X†─╭X†─┤
4: ────│──├●────│──────├X†─│──├●────│──────├X†─┤
5: ────│──╰●────│──────╰X†─│──╰●────│──────╰X†─┤
6: ────│────────├X─────────│────────├X─────────┤
     ├─╰X───────╰●─────────╰X───────╰●──┤
Estimated First-Level Expansion Gates: {Adjoint(PauliX): 2, Adjoint(Toffoli): 2, PauliX: 6, Toffoli: 6}
Actual First-Level Expansion Gates: {Adjoint(PauliX): 2, Adjoint(Toffoli): 2, PauliX: 2, Toffoli: 6}
Wire Allocations: {'any': 1}
Full Expansion Gates: {CNOT: 48, GlobalPhase: 80, RX: 8, RY: 16, RZ: 72}
Weighted Cost: 4848.0

Decomposition 2 (name: one_zeroed_work_wire)
2: ───────╭●───────────────────●╮────┤
3: ───────├●─╭X──X─╭●──X†─╭X†──●┤────┤
4: ───────│──├●────│──────├X†───│────┤
5: ───────│──╰●────│──────╰X†───│────┤
6: ───────│────────├X───────────│────┤
     |0>├─╰⊕───────╰●──────────⊕╯──┤
Estimated First-Level Expansion Gates: {Adjoint(PauliX): 1, Adjoint(TemporaryAND): 1, Adjoint(Toffoli): 1, PauliX: 5, TemporaryAND: 1, Toffoli: 2}
Actual First-Level Expansion Gates: {Adjoint(PauliX): 1, Adjoint(TemporaryAND): 1, Adjoint(Toffoli): 1, PauliX: 1, TemporaryAND: 1, Toffoli: 2}
Wire Allocations: {'zero': 1}
Full Expansion Gates: {CNOT: 23, GlobalPhase: 46, MidMeasure: 1, RX: 10, RY: 9, RZ: 38}
Weighted Cost: 2874.0

Decomposition 3 (name: two_borrowed_work_wires)
2: ────╭●──────────────────╭●──────────────────┤
3: ────├●──X─╭X─╭●─╭X†──X†─├●──X─╭X─╭●─╭X†──X†─┤
4: ────│─────├●─│──├X†─────│─────├●─│──├X†─────┤
5: ────│─────╰●─│──╰X†─────│─────╰●─│──╰X†─────┤
6: ────│────────├X─────────│────────├X─────────┤
     ├─╰X───────╰●─────────╰X───────╰●──┤
     ├──────────────────────────────────┤
Estimated First-Level Expansion Gates: {Adjoint(PauliX): 2, Adjoint(Toffoli): 2, PauliX: 6, Toffoli: 6}
Actual First-Level Expansion Gates: {Adjoint(PauliX): 2, Adjoint(Toffoli): 2, PauliX: 2, Toffoli: 6}
Wire Allocations: {'any': 2}
Full Expansion Gates: {CNOT: 48, GlobalPhase: 80, RX: 8, RY: 16, RZ: 72}
Weighted Cost: 4848.0

Decomposition 4 (name: two_zeroed_work_wires)
2: ───────╭●───────────────────●╮────┤
3: ───────├●──X─╭X─╭●─╭X†──X†──●┤────┤
4: ───────│─────├●─│──├X†───────│────┤
5: ───────│─────╰●─│──╰X†───────│────┤
6: ───────│────────├X───────────│────┤
     |0>├─╰⊕───────╰●──────────⊕╯──┤
     |0>├──────────────────────────┤
Estimated First-Level Expansion Gates: {Adjoint(PauliX): 1, Adjoint(TemporaryAND): 1, Adjoint(Toffoli): 1, PauliX: 5, TemporaryAND: 1, Toffoli: 2}
Actual First-Level Expansion Gates: {Adjoint(PauliX): 1, Adjoint(TemporaryAND): 1, Adjoint(Toffoli): 1, PauliX: 1, TemporaryAND: 1, Toffoli: 2}
Wire Allocations: {'zero': 2}
Full Expansion Gates: {CNOT: 23, GlobalPhase: 46, MidMeasure: 1, RX: 10, RY: 9, RZ: 38}
Weighted Cost: 2874.0

Decomposition 5 (name: many_borrowed_work_wires)
2: ────╭●──────────╭●─────────────┤
3: ────│──╭●────╭●─│──╭●────╭●────┤
4: ────│──│──╭●─│──│──│──╭●─│─────┤
5: ────│──│──├●─│──│──│──├●─│─────┤
6: ────├X─│──│──│──├X─│──│──│─────┤
     ├─╰●─├X─│──├X─╰●─├X─│──├X──┤
     ├────╰●─╰X─╰●────╰●─╰X─╰●──┤
Estimated First-Level Expansion Gates: {MultiControlledX(wires=AbstractWires(3), control_values=AbstractArray((2,), bool)): 1, PauliX: 4, Toffoli: 7}
Actual First-Level Expansion Gates: {MultiControlledX(wires=AbstractWires(3), control_values=AbstractArray((2,), bool)): 1, Toffoli: 7}
Wire Allocations: {'any': 2}
Full Expansion Gates: {CNOT: 48, GlobalPhase: 78, RX: 6, RY: 16, RZ: 72}
Weighted Cost: 4748.0

CHOSEN: Decomposition 6 (name: many_zeroed_work_wires)
2: ─────────────╭●────────────┤
3: ──────────╭●─│───●╮────────┤
4: ───────╭●─│──│────│──●╮────┤
5: ───────├●─│──│────│──●┤────┤
6: ───────│──│──├X───│───│────┤
     |0>├─│──├⊕─╰●──⊕┤───│──┤
     |0>├─╰⊕─╰●─────●╯──⊕╯──┤
Estimated First-Level Expansion Gates: {Adjoint(TemporaryAND): 2, MultiControlledX(wires=AbstractWires(3), control_values=AbstractArray((2,), bool)): 1, PauliX: 4, TemporaryAND: 2}
Actual First-Level Expansion Gates: {Adjoint(TemporaryAND): 2, MultiControlledX(wires=AbstractWires(3), control_values=AbstractArray((2,), bool)): 1, TemporaryAND: 2}
Wire Allocations: {'zero': 2}
Full Expansion Gates: {CNOT: 16, GlobalPhase: 41, MidMeasure: 2, RX: 14, RY: 8, RZ: 31}
Weighted Cost: 2668.0

We can see that the chosen decomposition rule for the MultiControlledX uses two work wires. However, not every MultiControlledX in the circuit can be decomposed the same way. For example, notice that the chosen decomposition rule for the controlled MultiRZ takes a work wire from the dynamic allocation budget, therefore, within the region of the decomposition rule, the MultiControlledX has one fewer work wire available to it. We can inspect how the graph chose a decomposition rule for the inner MultiControlledX by changing the num_work_wires argument:

>>> op = qp.MultiControlledX([2, 3, 4, 5, 6])  # concrete wire labels don't matter
>>> inspector.inspect_decomps(op, num_work_wires=1)
Decomposition 0 (name: no_work_wires)
2: ────╭●───────────────────╭●──────────────────────╭●──────────────────┤
3: ────├●───────────────────├●──────────────────────├●──────────────────┤
4: ────│─────────╭●─────────│─────────╭●────────────├●──────────────────┤
5: ────│─────────├●─────────│─────────├●────────────├●──────────────────┤
6: ──H─╰X──U(M0)─╰X──U(M0)†─╰X──U(M0)─╰X──U(M0)†──H─╰GlobalPhase(-1.57)─┤
M0 =
[[ 9.23879533e-01+0.38268343j -5.34910791e-34+0.j        ]
 [ 5.34910791e-34+0.j          9.23879533e-01-0.38268343j]]
Estimated First-Level Expansion Gates: {Adjoint(QubitUnitary(U=AbstractArray((2, 2), complex128, weak_type=True), wires=AbstractWires(1))): 2, Controlled(GlobalPhase, control_wires=AbstractWires(4), control_values=AbstractArray((4,), bool)): 1, Hadamard: 2, MultiControlledX(wires=AbstractWires(3), control_values=AbstractArray((2,), bool), work_wires=AbstractWires(2), work_wire_type=borrowed): 4, PauliX: 4, QubitUnitary(U=AbstractArray((2, 2), complex128, weak_type=True), wires=AbstractWires(1)): 2}
Actual First-Level Expansion Gates: {Adjoint(QubitUnitary(U=AbstractArray((2, 2), complex128, weak_type=True), wires=AbstractWires(1))): 2, Controlled(GlobalPhase, control_wires=AbstractWires(4), control_values=AbstractArray((4,), bool)): 1, Hadamard: 2, MultiControlledX(wires=AbstractWires(3), control_values=AbstractArray((2,), bool), work_wires=AbstractWires(2), work_wire_type=borrowed): 4, QubitUnitary(U=AbstractArray((2, 2), complex128, weak_type=True), wires=AbstractWires(1)): 2}
Full Expansion Gates: {CNOT: 58, GlobalPhase: 62, RX: 27, RY: 14, RZ: 53}
Weighted Cost: 4758.0

Decomposition 1 (name: one_borrowed_work_wire)
2: ────╭●──────────────────╭●──────────────────┤
3: ────├●─╭X──X─╭●──X†─╭X†─├●─╭X──X─╭●──X†─╭X†─┤
4: ────│──├●────│──────├X†─│──├●────│──────├X†─┤
5: ────│──╰●────│──────╰X†─│──╰●────│──────╰X†─┤
6: ────│────────├X─────────│────────├X─────────┤
     ├─╰X───────╰●─────────╰X───────╰●──┤
Estimated First-Level Expansion Gates: {Adjoint(PauliX): 2, Adjoint(Toffoli): 2, PauliX: 6, Toffoli: 6}
Actual First-Level Expansion Gates: {Adjoint(PauliX): 2, Adjoint(Toffoli): 2, PauliX: 2, Toffoli: 6}
Wire Allocations: {'any': 1}
Full Expansion Gates: {CNOT: 48, GlobalPhase: 80, RX: 8, RY: 16, RZ: 72}
Weighted Cost: 4848.0

CHOSEN: Decomposition 2 (name: one_zeroed_work_wire)
2: ───────╭●───────────────────●╮────┤
3: ───────├●─╭X──X─╭●──X†─╭X†──●┤────┤
4: ───────│──├●────│──────├X†───│────┤
5: ───────│──╰●────│──────╰X†───│────┤
6: ───────│────────├X───────────│────┤
     |0>├─╰⊕───────╰●──────────⊕╯──┤
Estimated First-Level Expansion Gates: {Adjoint(PauliX): 1, Adjoint(TemporaryAND): 1, Adjoint(Toffoli): 1, PauliX: 5, TemporaryAND: 1, Toffoli: 2}
Actual First-Level Expansion Gates: {Adjoint(PauliX): 1, Adjoint(TemporaryAND): 1, Adjoint(Toffoli): 1, PauliX: 1, TemporaryAND: 1, Toffoli: 2}
Wire Allocations: {'zero': 1}
Full Expansion Gates: {CNOT: 23, GlobalPhase: 46, MidMeasure: 1, RX: 10, RY: 9, RZ: 38}
Weighted Cost: 2874.0

Decomposition 3 (name: two_borrowed_work_wires)
Insufficient work wires: requires 2 but only 1 available.

Decomposition 4 (name: two_zeroed_work_wires)
Insufficient work wires: requires 2 but only 1 available.

Decomposition 5 (name: many_borrowed_work_wires)
Insufficient work wires: requires 2 but only 1 available.

Decomposition 6 (name: many_zeroed_work_wires)
Insufficient work wires: requires 2 but only 1 available.

Contents

Using PennyLane

Release news

Development

API

Internals