Skip to content

Commit

Permalink
Parsing of seed_transpiler in generate_preset_pass_manager (backp…
Browse files Browse the repository at this point in the history
…ort #12980) (#12996)

* Parsing of `seed_transpiler` in `generate_preset_pass_manager` (#12980)

* Raise ValueError if seed_transpiler is negative

* Fix code formatting

* Implement suggested changes

* Update releasenotes/notes/fix-negative-seed-pm-2813a62a020da115.yaml

Co-authored-by: Elena Peña Tapia <[email protected]>

* Update qiskit/transpiler/preset_passmanagers/generate_preset_pass_manager.py

Co-authored-by: Elena Peña Tapia <[email protected]>

---------

Co-authored-by: Elena Peña Tapia <[email protected]>
(cherry picked from commit 5c8edd4)

* Update test/python/transpiler/test_preset_passmanagers.py

* Update test/python/transpiler/test_preset_passmanagers.py

---------

Co-authored-by: jschuhmac <[email protected]>
Co-authored-by: Elena Peña Tapia <[email protected]>
  • Loading branch information
3 people authored Aug 20, 2024
1 parent c53c1c8 commit aff2efc
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ def generate_preset_pass_manager(
# Parse non-target dependent pm options
initial_layout = _parse_initial_layout(initial_layout)
approximation_degree = _parse_approximation_degree(approximation_degree)
seed_transpiler = _parse_seed_transpiler(seed_transpiler)

pm_options = {
"target": target,
Expand Down Expand Up @@ -516,3 +517,11 @@ def _parse_approximation_degree(approximation_degree):
if approximation_degree < 0.0 or approximation_degree > 1.0:
raise TranspilerError("Approximation degree must be in [0.0, 1.0]")
return approximation_degree


def _parse_seed_transpiler(seed_transpiler):
if seed_transpiler is None:
return None
if not isinstance(seed_transpiler, int) or seed_transpiler < 0:
raise ValueError("Expected non-negative integer as seed for transpiler.")
return seed_transpiler
4 changes: 4 additions & 0 deletions releasenotes/notes/fix-negative-seed-pm-2813a62a020da115.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
fixes:
- |
Fixed the behavior of :meth:`.generate_preset_pass_manager` to raise a `ValueError` exception if not provided with a non-negative integer `seed_transpiler` argument.
12 changes: 12 additions & 0 deletions test/python/transpiler/test_preset_passmanagers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1511,6 +1511,18 @@ def test_generate_preset_pass_manager_with_list_initial_layout(self, optimizatio
self.assertIsInstance(pm_object, PassManager)
self.assertEqual(tqc_list, tqc_obj)

def test_parse_seed_transpiler_raises_value_error(self):
"""Test that seed for transpiler is non-negative integer."""
with self.assertRaisesRegex(
ValueError, "Expected non-negative integer as seed for transpiler."
):
generate_preset_pass_manager(optimization_level=1, seed_transpiler=-1)

with self.assertRaisesRegex(
ValueError, "Expected non-negative integer as seed for transpiler."
):
generate_preset_pass_manager(optimization_level=2, seed_transpiler=0.1)


@ddt
class TestIntegrationControlFlow(QiskitTestCase):
Expand Down

0 comments on commit aff2efc

Please sign in to comment.