|View source on GitHub|
Merges operations in a circuit by calling
merge_func iteratively on operations.
cirq.merge_operations( circuit: CIRCUIT_TYPE, merge_func: Callable[[ops.Operation, ops.Operation], Optional[ops.Operation]], *, tags_to_ignore: Sequence[Hashable] = (), deep: bool = False ) -> CIRCUIT_TYPE
Used in the notebooks
|Used in the tutorials|
Two operations op1 and op2 are merge-able if
- There is no other operations between op1 and op2 in the circuit - is_subset(op1.qubits, op2.qubits) or is_subset(op2.qubits, op1.qubits)
merge_func is a callable which, given two merge-able operations
op1 and op2, decides whether they should be merged into a single operation
or not. If not, it should return None, else it should return the single merged
The method iterates on the input circuit moment-by-moment from left to right and attempts to repeatedly merge each operation in the latest moment with all the corresponding merge-able operations to its left.
If op1 and op2 are merged, both op1 and op2 are deleted from the circuit and
merged_op is inserted at the index corresponding to the larger
of op1/op2. If both op1 and op2 act on the same number of qubits,
inserted in the smaller moment index to minimize circuit depth.
The number of calls to
merge_func is O(N), where N = Total no. of operations, because:
- Every time the `merge_func` returns a new operation, the number of operations in the circuit reduce by 1 and hence this can happen at most O(N) times - Every time the `merge_func` returns None, the current operation is inserted into the frontier and we go on to process the next operation, which can also happen at-most O(N) times.
|Copy of input circuit with merged operations.|
|ValueError if the merged operation acts on new qubits outside the set of qubits corresponding to the original operations to be merged.|