Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parsing of seed_transpiler in generate_preset_pass_manager (backport #12980) #12996

Merged
merged 3 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading