forked from Qiskit/qiskit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into move-equivalence
- Loading branch information
Showing
10 changed files
with
448 additions
and
56 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
qiskit/transpiler/passes/optimization/split_2q_unitaries.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# This code is part of Qiskit. | ||
# | ||
# (C) Copyright IBM 2017, 2024. | ||
# | ||
# This code is licensed under the Apache License, Version 2.0. You may | ||
# obtain a copy of this license in the LICENSE.txt file in the root directory | ||
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. | ||
# | ||
# Any modifications or derivative works of this code must retain this | ||
# copyright notice, and modified files need to carry a notice indicating | ||
# that they have been altered from the originals. | ||
"""Splits each two-qubit gate in the `dag` into two single-qubit gates, if possible without error.""" | ||
from typing import Optional | ||
|
||
from qiskit.transpiler.basepasses import TransformationPass | ||
from qiskit.circuit.quantumcircuitdata import CircuitInstruction | ||
from qiskit.dagcircuit.dagcircuit import DAGCircuit, DAGOpNode | ||
from qiskit.circuit.library.generalized_gates import UnitaryGate | ||
from qiskit.synthesis.two_qubit.two_qubit_decompose import TwoQubitWeylDecomposition | ||
|
||
|
||
class Split2QUnitaries(TransformationPass): | ||
"""Attempt to splits two-qubit gates in a :class:`.DAGCircuit` into two single-qubit gates | ||
This pass will analyze all the two qubit gates in the circuit and analyze the gate's unitary | ||
matrix to determine if the gate is actually a product of 2 single qubit gates. In these | ||
cases the 2q gate can be simplified into two single qubit gates and this pass will | ||
perform this optimization and will replace the two qubit gate with two single qubit | ||
:class:`.UnitaryGate`. | ||
""" | ||
|
||
def __init__(self, fidelity: Optional[float] = 1.0 - 1e-16): | ||
"""Split2QUnitaries initializer. | ||
Args: | ||
fidelity (float): Allowed tolerance for splitting two-qubit unitaries and gate decompositions | ||
""" | ||
super().__init__() | ||
self.requested_fidelity = fidelity | ||
|
||
def run(self, dag: DAGCircuit): | ||
"""Run the Split2QUnitaries pass on `dag`.""" | ||
for node in dag.topological_op_nodes(): | ||
# skip operations without two-qubits and for which we can not determine a potential 1q split | ||
if ( | ||
len(node.cargs) > 0 | ||
or len(node.qargs) != 2 | ||
or node.matrix is None | ||
or node.is_parameterized() | ||
): | ||
continue | ||
|
||
decomp = TwoQubitWeylDecomposition(node.op, fidelity=self.requested_fidelity) | ||
if ( | ||
decomp._inner_decomposition.specialization | ||
== TwoQubitWeylDecomposition._specializations.IdEquiv | ||
): | ||
new_dag = DAGCircuit() | ||
new_dag.add_qubits(node.qargs) | ||
|
||
ur = decomp.K1r | ||
ur_node = DAGOpNode.from_instruction( | ||
CircuitInstruction(UnitaryGate(ur), qubits=(node.qargs[0],)), dag=new_dag | ||
) | ||
|
||
ul = decomp.K1l | ||
ul_node = DAGOpNode.from_instruction( | ||
CircuitInstruction(UnitaryGate(ul), qubits=(node.qargs[1],)), dag=new_dag | ||
) | ||
new_dag._apply_op_node_back(ur_node) | ||
new_dag._apply_op_node_back(ul_node) | ||
new_dag.global_phase = decomp.global_phase | ||
dag.substitute_node_with_dag(node, new_dag) | ||
elif ( | ||
decomp._inner_decomposition.specialization | ||
== TwoQubitWeylDecomposition._specializations.SWAPEquiv | ||
): | ||
# TODO maybe also look into swap-gate-like gates? Things to consider: | ||
# * As the qubit mapping may change, we'll always need to build a new dag in this pass | ||
# * There may not be many swap-gate-like gates in an arbitrary input circuit | ||
# * Removing swap gates from a user-routed input circuit here is unexpected | ||
pass | ||
return dag |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
releasenotes/notes/peephole-before-routing-c3d184b740bb7a8b.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
--- | ||
features_transpiler: | ||
- | | ||
Added a new pass :class:`.Split2QUnitaries` that iterates over all two-qubit gates or unitaries in a | ||
circuit and replaces them with two single-qubit unitaries, if possible without introducing errors, i.e. | ||
the two-qubit gate/unitary is actually a (kronecker) product of single-qubit unitaries. | ||
- | | ||
The passes :class:`.Collect2qBlocks`, :class:`.ConsolidateBlocks` and :class:`.Split2QUnitaries` have been | ||
added to the ``init`` stage of the preset pass managers with optimization level 2 and optimization level 3. | ||
The modification of the `init` stage should allow for a more efficient routing for quantum circuits that either: | ||
* contain two-qubit unitaries/gates that are actually a product of single-qubit gates | ||
* contain multiple two-qubit gates in a continuous block of two-qubit gates. | ||
In the former case, the routing of the two-qubit gate can simply be skipped as no real interaction | ||
between a pair of qubits occurs. In the latter case, the lookahead space of routing algorithms is not | ||
'polluted' by superfluous two-qubit gates, i.e. for routing it is sufficient to only consider one single | ||
two-qubit gate per continuous block of two-qubit gates. These passes are not run if the pass | ||
managers target a :class:`.Target` that has a discrete basis gate set, i.e. all basis gates have are not | ||
parameterized. |
Oops, something went wrong.