forked from Qiskit/qiskit
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
63 additions
and
0 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
qiskit/transpiler/passes/optimization/dynamic/replace_swap_with_zero_state.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,63 @@ | ||
# This code is part of Qiskit. | ||
# | ||
# (C) Copyright IBM 2017, 2019. | ||
# | ||
# 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. | ||
|
||
"""Replace Swap gate in case of qubit in zero state with dynamic protocol.""" | ||
|
||
from qiskit.circuit import ClassicalRegister, QuantumCircuit, QuantumRegister, Reset | ||
from qiskit.circuit.library import CXGate, HGate, Measure, SwapGate | ||
from qiskit.circuit.controlflow.if_else import IfElseOp | ||
from qiskit.dagcircuit import DAGCircuit, DAGInNode | ||
from qiskit.transpiler.basepasses import TransformationPass | ||
|
||
|
||
class ReplaceSwapWithZeroState(TransformationPass): | ||
"""Replace Swap gate in case of qubit in zero state with | ||
dynamic protocol.""" | ||
|
||
def run(self, dag): | ||
"""Run the ReplaceSwapWithZeroState pass on `dag`. | ||
Args: | ||
dag (DAGCircuit): the DAG to be optimized. | ||
Returns: | ||
DAGCircuit: the optimized DAG. | ||
""" | ||
swaps = dag.op_nodes(SwapGate) | ||
for swap in swaps: | ||
predecessor = next(dag.predecessors(swap)) | ||
if isinstance(predecessor, DAGInNode) or isinstance(predecessor, Reset): | ||
zero_qubit = predecessor.wire.index | ||
for qarg in swap.qargs: | ||
if qarg.index != zero_qubit: | ||
data_qubit = qarg.index | ||
|
||
mini_dag = DAGCircuit() | ||
qreg = QuantumRegister(2) | ||
creg = ClassicalRegister(1) | ||
mini_dag.add_qreg(qreg) | ||
mini_dag.add_creg(creg) | ||
|
||
mini_dag.apply_operation_back(CXGate(), [qreg[1], qreg[0]]) | ||
mini_dag.apply_operation_back(HGate(), [qreg[1]]) | ||
mini_dag.apply_operation_back(Measure(), [qreg[1]], [creg[0]]) | ||
|
||
true_body = QuantumCircuit(qreg) | ||
true_body.z(0) | ||
true_body.x(1) | ||
|
||
mini_dag.apply_operation_back( | ||
IfElseOp((creg[0], 1), true_body), [qreg[0], qreg[1]], [creg[0]] | ||
) | ||
dag.substitute_node_with_dag(swap, mini_dag, wires=[qreg[0], qreg[1], creg[0]]) | ||
|
||
return dag |