Skip to content

Commit

Permalink
Merge b1ca039 into 7bc2bfd
Browse files Browse the repository at this point in the history
  • Loading branch information
nonhermitian committed Aug 18, 2023
2 parents 7bc2bfd + b1ca039 commit bf2dc5c
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 11 deletions.
4 changes: 2 additions & 2 deletions qiskit/transpiler/preset_passmanagers/level1.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,10 @@ def _swap_mapped(property_set):
target is not None and target.get_non_global_operation_names(strict_direction=True)
):
pre_optimization = common.generate_pre_op_passmanager(
target, coupling_map, remove_reset_in_zero=True
target, coupling_map, remove_reset_in_zero=False
)
else:
pre_optimization = common.generate_pre_op_passmanager(remove_reset_in_zero=True)
pre_optimization = common.generate_pre_op_passmanager(remove_reset_in_zero=False)
if optimization_method is None:
optimization = PassManager()
unroll = [pass_ for x in translation.passes() for pass_ in x["passes"]]
Expand Down
4 changes: 2 additions & 2 deletions qiskit/transpiler/preset_passmanagers/level2.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,9 @@ def _swap_mapped(property_set):
if (coupling_map and not coupling_map.is_symmetric) or (
target is not None and target.get_non_global_operation_names(strict_direction=True)
):
pre_optimization = common.generate_pre_op_passmanager(target, coupling_map, True)
pre_optimization = common.generate_pre_op_passmanager(target, coupling_map, False)
else:
pre_optimization = common.generate_pre_op_passmanager(remove_reset_in_zero=True)
pre_optimization = common.generate_pre_op_passmanager(remove_reset_in_zero=False)
if optimization_method is None:
optimization = PassManager()
unroll = [pass_ for x in translation.passes() for pass_ in x["passes"]]
Expand Down
6 changes: 2 additions & 4 deletions qiskit/transpiler/preset_passmanagers/level3.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
from qiskit.transpiler.passes import MinimumPoint
from qiskit.transpiler.passes import Depth
from qiskit.transpiler.passes import Size
from qiskit.transpiler.passes import RemoveResetInZeroState
from qiskit.transpiler.passes import Optimize1qGatesDecomposition
from qiskit.transpiler.passes import CommutativeCancellation
from qiskit.transpiler.passes import OptimizeSwapBeforeMeasure
Expand Down Expand Up @@ -207,7 +206,6 @@ def _opt_control(property_set):
unitary_synthesis_plugin_config,
hls_config,
)
init.append(RemoveResetInZeroState())
init.append(OptimizeSwapBeforeMeasure())
init.append(RemoveDiagonalGatesBeforeMeasure())
if coupling_map or initial_layout:
Expand Down Expand Up @@ -268,7 +266,7 @@ def _unroll_condition(property_set):
if (coupling_map and not coupling_map.is_symmetric) or (
target is not None and target.get_non_global_operation_names(strict_direction=True)
):
pre_optimization = common.generate_pre_op_passmanager(target, coupling_map, True)
pre_optimization = common.generate_pre_op_passmanager(target, coupling_map, False)
_direction = [
pass_
for x in common.generate_pre_op_passmanager(target, coupling_map).passes()
Expand All @@ -280,7 +278,7 @@ def _unroll_condition(property_set):
do_while=_opt_control,
)
else:
pre_optimization = common.generate_pre_op_passmanager(remove_reset_in_zero=True)
pre_optimization = common.generate_pre_op_passmanager(remove_reset_in_zero=False)
optimization.append(
_opt + _unroll_if_out_of_basis + _minimum_point_check, do_while=_opt_control
)
Expand Down
6 changes: 6 additions & 0 deletions releasenotes/notes/keep-initial-resets-a5f75cb16c8edb57.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
features:
- |
Disables the use of :class:`.RemoveResetInZeroState` in the preset passmanagers.
This better aligns circuits to the notion of arbitrary initial states
unless explicitly set to zeros with resets.
7 changes: 4 additions & 3 deletions test/python/compiler/test_transpiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -802,14 +802,15 @@ def test_move_measurements(self):
)
self.assertTrue(is_last_measure)

def test_initialize_reset_should_be_removed(self):
"""The reset in front of initializer should be removed when zero state"""
def test_initialize_reset_is_not_removed(self):
"""The reset in front of initializer should NOT be removed at beginning"""
qr = QuantumRegister(1, "qr")
qc = QuantumCircuit(qr)
qc.initialize([1.0 / math.sqrt(2), 1.0 / math.sqrt(2)], [qr[0]])
qc.initialize([1.0 / math.sqrt(2), -1.0 / math.sqrt(2)], [qr[0]])

expected = QuantumCircuit(qr)
expected.reset(qr[0])
expected.append(U3Gate(np.pi / 2, 0, 0), [qr[0]])
expected.reset(qr[0])
expected.append(U3Gate(np.pi / 2, -np.pi, 0), [qr[0]])
Expand All @@ -828,7 +829,7 @@ def test_initialize_FakeMelbourne(self):
out_dag = circuit_to_dag(out)
reset_nodes = out_dag.named_nodes("reset")

self.assertEqual(reset_nodes, [])
self.assertEqual(len(reset_nodes), 3)

def test_non_standard_basis(self):
"""Test a transpilation with a non-standard basis"""
Expand Down
29 changes: 29 additions & 0 deletions test/python/compiler/test_transpiler_resets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2023.
#
# 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.

"""Tests for resets in the transpiler"""
from qiskit import QuantumCircuit, transpile
from qiskit.test import QiskitTestCase


class TestResetsTranspiler(QiskitTestCase):
"""Tests resets and transpilation"""

def test_init_resets_kept_preset_passmanagers(self):
"""Test initial resets kept at all preset transpilation levels"""
num_qubits = 5
qc = QuantumCircuit(num_qubits)
qc.reset(range(num_qubits))

for level in range(4):
num_resets = transpile(qc, optimization_level=level).count_ops()["reset"]
self.assertEqual(num_resets, num_qubits)

0 comments on commit bf2dc5c

Please sign in to comment.