slicing (qiskit_addon_utils.slicing)¶
Utility methods for circuit slicing.
For more information, check out the how-to guide which discusses this submodule.
- combine_slices(slices, include_barriers=False)[source]¶
Combine N-qubit slices of a circuit into a single circuit.
- Parameters:
slices (Sequence[QuantumCircuit]) – The N-qubit circuit slices.
include_barriers (bool) – If
True, place barriers between each slice.
- Returns:
A
QuantumCircuitwith the slices appended in sequential order.- Raises:
ValueError – Two input slices were defined on different numbers of qubits.
- Return type:
QuantumCircuit | None
- slice_by_barriers(circuit)[source]¶
Split a
QuantumCircuitinto slices around full-circuit barriers.Barriers which do not act on all circuit qubits will be treated as normal operations and included in the slices. Barriers which act on all qubits will be interpreted as slice locations and will not be included in the output slices.
- Parameters:
circuit (QuantumCircuit) – The circuit to be split.
- Returns:
A sequence of
QuantumCircuitobjects, one for each slice.- Return type:
- slice_by_coloring(circuit, coloring)[source]¶
Split a
QuantumCircuitinto slices using the provided edge coloring.Two-qubit gates acting on identically colored qubit connections (edges) will be grouped greedily into slices using
CollectOpColor. This will be done in order of increasing color value (the integer values which each edge is mapped to).Warning
Note, that this does not mean that low valued color slices are guaranteed to be left-most in your circuit. Below is an example to emphasize this.
>>> from qiskit import QuantumCircuit >>> circuit = QuantumCircuit(5) >>> _ = circuit.cx(0, 1) >>> _ = circuit.cx(3, 4) >>> _ = circuit.cx(2, 3) >>> circuit.draw() q_0: ──■─────── ┌─┴─┐ q_1: ┤ X ├───── └───┘ q_2: ───────■── ┌─┴─┐ q_3: ──■──┤ X ├ ┌─┴─┐└───┘ q_4: ┤ X ├───── └───┘ >>> coloring = {(0, 1): 0, (2, 3): 0, (3, 4): 1} >>> from qiskit_addon_utils.slicing import combine_slices, slice_by_coloring >>> slices = slice_by_coloring(circuit, coloring) # for illustration purposes, we are recombining the slices with barriers >>> recombined = combine_slices(slices, include_barriers=True) >>> recombined.draw() ░ q_0: ──────░───■── ░ ┌─┴─┐ q_1: ──────░─┤ X ├ ░ └───┘ q_2: ──────░───■── ░ ┌─┴─┐ q_3: ──■───░─┤ X ├ ┌─┴─┐ ░ └───┘ q_4: ┤ X ├─░────── └───┘ ░
Single-qubit gates will be collected into a single slice using
CollectOpSize.- Parameters:
- Returns:
A sequence of
QuantumCircuitobjects, one for each slice.- Raises:
ValueError – The input edge coloring is invalid.
ValueError – Could not assign a color to circuit instruction.
- Return type:
- slice_by_depth(circuit, max_slice_depth)[source]¶
Split a
QuantumCircuitinto slices based on depth.This function transforms the input circuit into a
DAGCircuitand batches the sequence of depth-1 layers output fromlayers()into slices of depth not exceedingmax_slice_depth. This is achieved by composing layers into slices until the max slice depth is reached and then starting a new slice with the next layer. The final slice may be composed of fewer thanmax_slice_depthlayers.- Parameters:
circuit (QuantumCircuit) – The circuit to be split.
max_slice_depth (int) – The maximum depth of a given slice.
- Returns:
A sequence of
QuantumCircuitobjects, one for each slice.- Return type:
- slice_by_gate_types(circuit)[source]¶
Split a
QuantumCircuitinto depth-1 slices of operations of the same type.Warning
Note: Adjacent slices sharing no qubits in common may be ordered arbitrarily.
- Parameters:
circuit (QuantumCircuit) – The circuit to be split.
- Returns:
A sequence of
QuantumCircuitobjects, one for each slice.- Return type: