qp.transforms.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
DecompGraphInspectorfor querying the decomposition decisions made for a given circuit and target gate set.Note
This function is only relevant when the new experimental graph-based decomposition system (introduced in v0.41) is enabled via
enable_graph(). This new way of doing decompositions is generally more resource efficient and accommodates multiple alternative decomposition rules for an operator.- 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 to0.minimize_work_wires (bool) – If
True, minimize the number of work wires simultaneously allocated throughout the circuit. Defaults toFalse.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:
Examples
Applying the
decomp_inspectortransform on a circuit will return aDecompGraphInspectorobject constructed from operators in the circuit. These examples weightRX,RY, andRZat50and leaveCNOTat1, so a rotation costs as much as fifty Cliffords.qp.decomposition.enable_graph() 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: ctrl_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: ctrl_many_zeroed_work_wires) Not applicable (provided operator instance does not meet all conditions for this rule). Decomposition 2 (name: controlled(_multi_rz_to_ppr)) 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(_multi_rz_decomposition)) 0: ─╭(X)@RZ(0.50)@(X)─┤ 1: ─├(X)@RZ(0.50)@(X)─┤ 3: ─├●────────────────┤ 4: ─├●────────────────┤ 5: ─╰●────────────────┤ Estimated First-Level Expansion Gates: {Controlled((CNOT) @ RZ @ (CNOT), control_wires=AbstractWires(3), control_values=AbstractArray((3,), bool, weak_type=True)): 1, PauliX: 3} Actual First-Level Expansion Gates: {Controlled((CNOT) @ RZ @ (CNOT), 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:>>> cnot = qp.CNOT([1, 0]) >>> cob = qp.change_op_basis(cnot, qp.RZ(0.5, wires=0), cnot) >>> inspector.inspect_decomps(qp.ctrl(cob, control=[3, 4, 5]), num_work_wires=2) CHOSEN: Decomposition 0 (name: flip_zero_ctrl_values(_controlled_change_op_basis_decomposition)) 0: ─╭X─╭RZ(0.50)─╭X─┤ 1: ─╰●─│─────────╰●─┤ 3: ────├●───────────┤ 4: ────├●───────────┤ 5: ────╰●───────────┤ Estimated First-Level Expansion Gates: {CNOT: 2, Controlled(RZ, control_wires=AbstractWires(3), control_values=AbstractArray((3,), bool, weak_type=True)): 1, PauliX: 3} Actual First-Level Expansion Gates: {CNOT: 2, Controlled(RZ, control_wires=AbstractWires(3), control_values=AbstractArray((3,), bool)): 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
MultiRZproduced in the decomposition of the controlledMultiRZ(notice hownum_work_wiresis 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(_multi_rz_to_ppr)) 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(_multi_rz_decomposition)) 0: ─╭(X)@RZ(0.50)@(X)─┤ 1: ─├(X)@RZ(0.50)@(X)─┤ 2: ─╰●────────────────┤ Estimated First-Level Expansion Gates: {Controlled((CNOT) @ RZ @ (CNOT), control_wires=AbstractWires(1), control_values=AbstractArray((1,), bool, weak_type=True)): 1, PauliX: 1} Actual First-Level Expansion Gates: {Controlled((CNOT) @ RZ @ (CNOT), 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: flip_zero_ctrl_values(_controlled_change_op_basis_decomposition)) 0: ─╭X─╭RZ(0.50)─╭X─┤ 1: ─╰●─│─────────╰●─┤ 2: ────╰●───────────┤ Estimated First-Level Expansion Gates: {CNOT: 2, Controlled(RZ, control_wires=AbstractWires(1), control_values=AbstractArray((1,), bool, weak_type=True)): 1, PauliX: 1} Actual First-Level Expansion Gates: {CNOT: 2, Controlled(RZ, control_wires=AbstractWires(1), control_values=AbstractArray((1,), bool)): 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.decomposition.enable_graph() @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: _pauli_rot_decomposition) 0: ─╭(PPR(-π/4, X)@H)@MultiRZ(0.50)@(PPR(π/4, X)@H)─┤ 1: ─├(PPR(-π/4, X)@H)@MultiRZ(0.50)@(PPR(π/4, X)@H)─┤ 2: ─╰(PPR(-π/4, X)@H)@MultiRZ(0.50)@(PPR(π/4, X)@H)─┤ First-Level Expansion Gates: {(PPR_4(-1, 'X', wires=AbstractWires(1)) @ Hadamard) @ MultiRZ(AbstractArray((), float64, weak_type=True), wires=AbstractWires(3)) @ (PPR_4(1, 'X', wires=AbstractWires(1)) @ Hadamard): 1} Missing Ops: {(PPR_4(-1, 'X', wires=AbstractWires(1)) @ Hadamard) @ MultiRZ(AbstractArray((), float64, weak_type=True), wires=AbstractWires(3)) @ (PPR_4(1, 'X', wires=AbstractWires(1)) @ Hadamard)}
That missing operator is a change of basis, conjugating the
MultiRZwith a Hadamard/PPR_4basis change. TheMultiRZis available, so splitting the basis change surfaces the root problem:>>> to_basis = qp.prod(qp.PPR_4(1, "X", wires=1), qp.Hadamard(wires=0)) >>> inspector.inspect_decomps(to_basis) Decomposition 0 (name: _prod2_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 whyPauliRotstops here. Inspecting it directly:>>> inspector.inspect_decomps(qp.Hadamard(0)) Decomposition 0 (name: _hadamard_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: _hadamard_ppm) Insufficient work wires: requires 1 but only 0 available. Decomposition 2 (name: _hadamard_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: _hadamard_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
PauliRotcould not be decomposed was thatGlobalPhaseis missing from the target gate set.Working with a dynamic work wire allocation budget
Some decomposition rules make use of dynamically allocated work wires. For example:
>>> qp.inspect_decomps(qp.MultiControlledX([0, 1, 2, 3]), "one_zeroed_worker") Decomposition 0 (name: one_zeroed_worker) 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 anum_work_wiresargument which acts as a work wire budget. It ensures that no more thannum_work_wiresnumber 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: ctrl_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: ctrl_many_zeroed_work_wires) Not applicable (provided operator instance does not meet all conditions for this rule). Decomposition 2 (name: controlled(_multi_rz_to_ppr)) 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(_multi_rz_decomposition)) 0: ─╭(X)@RZ(0.50)@(X)─┤ 1: ─├(X)@RZ(0.50)@(X)─┤ 3: ─├●────────────────┤ 4: ─├●────────────────┤ 5: ─├●────────────────┤ 6: ─╰●────────────────┤ Estimated First-Level Expansion Gates: {Controlled((CNOT) @ RZ @ (CNOT), control_wires=AbstractWires(4), control_values=AbstractArray((4,), bool, weak_type=True)): 1, PauliX: 4} Actual First-Level Expansion Gates: {Controlled((CNOT) @ RZ @ (CNOT), 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: flip_zero_ctrl_values(_controlled_change_op_basis_decomposition)) 0: ─╭X─╭RZ(0.50)─╭X─┤ 1: ─╰●─│─────────╰●─┤ 3: ────├●───────────┤ 4: ────├●───────────┤ 5: ────├●───────────┤ 6: ────╰●───────────┤ Estimated First-Level Expansion Gates: {CNOT: 2, Controlled(RZ, control_wires=AbstractWires(4), control_values=AbstractArray((4,), bool, weak_type=True)): 1, PauliX: 4} Actual First-Level Expansion Gates: {CNOT: 2, Controlled(RZ, control_wires=AbstractWires(4), control_values=AbstractArray((4,), bool)): 1} Full Expansion Gates: {CNOT: 134, GlobalPhase: 21, RX: 19, RZ: 46} Weighted Cost: 3384.0
Similarly, for the
MultiControlledXin the circuit:>>> op = qp.MultiControlledX([2, 3, 4, 5, 6]) >>> inspector.inspect_decomps(op, num_work_wires=2) Decomposition 0 (name: decompose_mcx_with_no_worker) 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: 23, RY: 10, RZ: 61} Weighted Cost: 4758.0 Decomposition 1 (name: one_borrowed_worker) 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_worker) 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_workers) 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_workers) 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_workers) 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_workers) 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
MultiControlledXuses two work wires. However, not everyMultiControlledXin the circuit can be decomposed the same way. For example, notice that the chosen decomposition rule for the controlledMultiRZtakes a work wire from the dynamic allocation budget, therefore, within the region of the decomposition rule, theMultiControlledXhas one fewer work wire available to it. We can inspect how the graph chose a decomposition rule for the innerMultiControlledXby changing thenum_work_wiresargument:>>> 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: decompose_mcx_with_no_worker) 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: 23, RY: 10, RZ: 61} Weighted Cost: 4758.0 Decomposition 1 (name: one_borrowed_worker) 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_worker) 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_workers) Insufficient work wires: requires 2 but only 1 available. Decomposition 4 (name: two_zeroed_workers) Insufficient work wires: requires 2 but only 1 available. Decomposition 5 (name: many_borrowed_workers) Insufficient work wires: requires 2 but only 1 available. Decomposition 6 (name: many_zeroed_workers) Insufficient work wires: requires 2 but only 1 available.