-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pass to remove diagonal gates before measurement (#2208)
* rz_and_z_optimization * bug fixing and more testing * changelog * lint * RemoveRZandZbeforeMeasure -> RemoveDiagonalGatesBeforeMeasure * changelog * invalid-name * test * diagonal_control_gates * CrzGate, Cu1Gate, RZZGate * lint * from qiskit.compiler import transpile * remove_diagonal_gates_before_measurere * diagonal_control_gates -> diagonal_2q_gates * quantum_predecessors * use quantum_predecessors * test_optimize_1rzz_2measure docstring * lint
- Loading branch information
Luciano
authored
Apr 27, 2019
1 parent
e673d1c
commit a85c741
Showing
6 changed files
with
476 additions
and
0 deletions.
There are no files selected for viewing
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
44 changes: 44 additions & 0 deletions
44
qiskit/transpiler/passes/remove_diagonal_gates_before_measure.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,44 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
# Copyright 2019, IBM. | ||
# | ||
# This source code is licensed under the Apache License, Version 2.0 found in | ||
# the LICENSE.txt file in the root directory of this source tree. | ||
|
||
|
||
""" | ||
Transpiler pass to remove diagonal gates (like RZ, T, Z, etc) before | ||
a measurement. Including diagonal 2Q gates. | ||
""" | ||
|
||
from qiskit.circuit import Measure | ||
from qiskit.extensions.standard import RZGate, ZGate, TGate, SGate, TdgGate, SdgGate, U1Gate,\ | ||
CzGate, CrzGate, Cu1Gate, RZZGate | ||
from qiskit.transpiler.basepasses import TransformationPass | ||
|
||
|
||
class RemoveDiagonalGatesBeforeMeasure(TransformationPass): | ||
"""Remove diagonal gates (like RZ, T, Z, etc) before a measurement. | ||
Including diagonal 2Q gates.""" | ||
|
||
def run(self, dag): | ||
"""Return a new circuit that has been optimized.""" | ||
diagonal_1q_gates = (RZGate, ZGate, TGate, SGate, TdgGate, SdgGate, U1Gate) | ||
diagonal_2q_gates = (CzGate, CrzGate, Cu1Gate, RZZGate) | ||
|
||
nodes_to_remove = set() | ||
for measure in dag.op_nodes(Measure): | ||
predecessor = dag.quantum_predecessors(measure)[0] | ||
|
||
if predecessor.type == 'op' and isinstance(predecessor.op, diagonal_1q_gates): | ||
nodes_to_remove.add(predecessor) | ||
|
||
if predecessor.type == 'op' and isinstance(predecessor.op, diagonal_2q_gates): | ||
successors = dag.quantum_successors(predecessor) | ||
if all([s.type == 'op' and isinstance(s.op, Measure) for s in successors]): | ||
nodes_to_remove.add(predecessor) | ||
|
||
for node_to_remove in nodes_to_remove: | ||
dag.remove_op_node(node_to_remove) | ||
|
||
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
Oops, something went wrong.