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

Overflow error: Seed Transpiler Validation #12938

Closed
glanzz opened this issue Aug 10, 2024 · 4 comments · Fixed by #12980
Closed

Overflow error: Seed Transpiler Validation #12938

glanzz opened this issue Aug 10, 2024 · 4 comments · Fixed by #12980
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@glanzz
Copy link

glanzz commented Aug 10, 2024

Environment

  • Qiskit version: 1.1.1
  • Python version: 3.11
  • Operating system: MacOS

What is happening?

The pass manager crashes when i am provided with negative seed_transpiler value to the for layout method is 'sabre' even for normal circuit.
The seed_transpiler is not validated properly.
The issue is also found when run with complex circuit with more number of two qubit gates.

How can we reproduce the issue?

from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
from qiskit import QuantumCircuit
from qiskit_ibm_runtime.fake_provider import FakeSherbrooke
qc = QuantumCircuit(3)
qc.x(0)
qc.x(1)
qc.x(2)
qc.measure_all()

backend = FakeSherbrooke()
options = {
  'layout_method': 'sabre',
  'seed_transpiler': -1,
}

pass_manager = generate_preset_pass_manager(backend=backend, optimization_level=i, **options)
transpiled_circuit = pass_manager.run(qc)

print(backend.run(transpiled_circuit, shots=100).result().get_counts())

What should happen?

There should be an unsigned int overflowerror

Traceback (most recent call last):
  File "/Users/xxx/Projects/circuit_generator/gen_mod.py", line 36, in <module>
    transpiled_circuit = pass_manager.run(qc)
                         ^^^^^^^^^^^^^^^^^^^^
  File "/Users/xxx/Projects/circuit_generator/venv/lib/python3.11/site-packages/qiskit/transpiler/passmanager.py", line 440, in run
    return super().run(circuits, output_name, callback, num_processes=num_processes)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/xxx/Projects/circuit_generator/venv/lib/python3.11/site-packages/qiskit/transpiler/passmanager.py", line 463, in wrapper
    return meth(*meth_args, **meth_kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/xxx/Projects/circuit_generator/venv/lib/python3.11/site-packages/qiskit/transpiler/passmanager.py", line 225, in run
    return super().run(
           ^^^^^^^^^^^^
  File "/Users/xxx/Projects/circuit_generator/venv/lib/python3.11/site-packages/qiskit/passmanager/passmanager.py", line 231, in run
    out = [
          ^
  File "/Users/xxx/Projects/circuit_generator/venv/lib/python3.11/site-packages/qiskit/passmanager/passmanager.py", line 232, in <listcomp>
    _run_workflow(program=program, pass_manager=self, callback=callback, **kwargs)
  File "/Users/xxx/Projects/circuit_generator/venv/lib/python3.11/site-packages/qiskit/passmanager/passmanager.py", line 292, in _run_workflow
    passmanager_ir, final_state = flow_controller.execute(
                                  ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/xxx/Projects/circuit_generator/venv/lib/python3.11/site-packages/qiskit/passmanager/base_tasks.py", line 218, in execute
    passmanager_ir, state = next_task.execute(
                            ^^^^^^^^^^^^^^^^^^
  File "/Users/xxx/Projects/circuit_generator/venv/lib/python3.11/site-packages/qiskit/passmanager/base_tasks.py", line 218, in execute
    passmanager_ir, state = next_task.execute(
                            ^^^^^^^^^^^^^^^^^^
  File "/Users/xxx/Projects/circuit_generator/venv/lib/python3.11/site-packages/qiskit/transpiler/basepasses.py", line 195, in execute
    new_dag, state = super().execute(
                     ^^^^^^^^^^^^^^^^
  File "/Users/xxx/Projects/circuit_generator/venv/lib/python3.11/site-packages/qiskit/passmanager/base_tasks.py", line 98, in execute
    ret = self.run(passmanager_ir)
          ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/xxx/Projects/circuit_generator/venv/lib/python3.11/site-packages/qiskit/transpiler/passes/layout/sabre_layout.py", line 269, in run
    components = disjoint_utils.run_pass_over_connected_components(dag, target, inner_run)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/xxx/Projects/circuit_generator/venv/lib/python3.11/site-packages/qiskit/transpiler/passes/layout/disjoint_utils.py", line 51, in run_pass_over_connected_components
    return [run_func(dag, cmap_components[0])]
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/xxx/Projects/circuit_generator/venv/lib/python3.11/site-packages/qiskit/transpiler/passes/layout/sabre_layout.py", line 397, in _inner_run
    (initial_layout, final_permutation, sabre_result) = sabre_layout_and_routing(
                                                        ^^^^^^^^^^^^^^^^^^^^^^^^^
OverflowError: can't convert negative int to unsigned

Any suggestions?

Validation of seed_transpiler value in generate_preset_pass_manager function

@glanzz glanzz added the bug Something isn't working label Aug 10, 2024
@1ucian0 1ucian0 added the good first issue Good for newcomers label Aug 12, 2024
@1ucian0
Copy link
Member

1ucian0 commented Aug 12, 2024

This is a very interesting and unexpected bug introduced as part of the oxidation. Probably can be fixed in python-land by abs(seed). Or, @mtreinish, should negative seeds be supported by rusted passes?

@jakelishman
Copy link
Member

Seeds are almost universally understood as positive integers, but in reality all that should matter is that they're a random sequence of bits. numpy.random.default_rng() will crash with a very similar error if given a negative number.

I think erroring out is the better course of action here; "fixing up" human-provided seeds to make them suitably random is well-known to be very very tricky. Using something like abs(seed) means that things like transpile(qc, seed_transpiler=x) for x in range(-10, 10) will unexpectedly use the same seed for nine pairs of runs. It's safer just to error, like other libraries do, and tell the user to give a correct input.

@1ucian0
Copy link
Member

1ucian0 commented Aug 12, 2024

Got it. The fix should raise a ValueError: expected non-negative integer as a seed.

@jakelishman
Copy link
Member

Yeah, that's fine, if we think the error message needs improving.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

3 participants