Skip to content

Runtime panic in basis translator #16251

Description

@IlanIwumbwe

Environment

  • Qiskit version: 2.3.1
  • Python version: 3.12.3
  • Operating system: Ubuntu 24.04

What is happening?

The basis translator panics due to an unreachable code assumption when unitary box is followed by ch gate.

How can we reproduce the issue?

Run the code below

import numpy as np
from qiskit import QuantumCircuit, QuantumRegister, transpile
from qiskit_aer import AerSimulator
from qiskit.circuit.library import UnitaryGate
from diff_testing.qiskit import qiskitTesting

raw = [
    (-0.068923838225880649, 0.39402146061788901), (0.045841322661181898, 0.22199934610258834),
    (-0.11337042418940241, -0.3139472011099117), (-0.75945103795019053, -0.31690673605053044),
    (0.025779187173381977, 0.54497233161091796), (-0.24617679121730707, 0.031594719204278851),
    (-0.53932713863806891, 0.51133286797612465), (0.22501376165717366, -0.19435301693636139),
    (0.13162380925331377, -0.51514205272437275), (0.40109357456892902, 0.43507806374564423),
    (-0.52108802259836462, 0.013929325325622343), (0.069767126505542343, -0.30089974393323388),
    (0.4869562334177967, 0.15004882362789923), (0.3540406268839334, -0.64148256566608364),
    (-0.07538800006726866, -0.24256779163052269), (0.12552552506489034, -0.35104869775012465),
]

U = np.array([complex(r, i) for r, i in raw], dtype=complex).reshape(4, 4)
unitary_4 = UnitaryGate(U, label="unitary_4")

reg_907 = QuantumRegister(1, name="reg_907")
reg_919 = QuantumRegister(1, name="reg_919")
reg_931 = QuantumRegister(2, name="reg_931")

main_circuit = QuantumCircuit(reg_907, reg_919, reg_931)

main_circuit.append(unitary_4, [reg_907[0], reg_931[1]])
main_circuit.ch(reg_931[0], reg_919[0])
main_circuit.measure_all()

backend = AerSimulator()
circ_prime = transpile(main_circuit, backend, optimization_level=0)

That produces

thread '<unnamed>' (833870) panicked at crates/transpiler/src/passes/basis_translator/compose_transforms.rs:181:9:
internal error: entered unreachable code: Having a unitary result from an `EquivalenceLibrary is not possible
stack backtrace:
   0:     0x79e533e9b722 - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::h93773fc827e3113d
   1:     0x79e53340945a - core::fmt::write::hed7b5c73d82ecb7c
   2:     0x79e533e9ac41 - std::io::Write::write_fmt::h6f0185aecf0ed75f
   3:     0x79e533e9a8de - std::panicking::panic_with_hook::hb4bd9ac1123582a0
   4:     0x79e533ec4648 - std::panicking::panic_handler::{{closure}}::hde00dd15f5637fe2
   5:     0x79e533ec45c9 - std::sys::backtrace::__rust_end_short_backtrace::hb72197fa777c1785
   6:     0x79e533ec45bc - __rustc[4425a7e20b4c8619]::rust_begin_unwind
   7:     0x79e5333d9cab - core::panicking::panic_fmt::ha59b517dd231f4da
   8:     0x79e5335966dc - qiskit_transpiler::passes::basis_translator::compose_transforms::compose_transforms::h2078dd74c090a764
   9:     0x79e53349dd09 - qiskit_transpiler::passes::basis_translator::run_basis_translator::h35ddc508f16f301a
  10:     0x79e533573216 - qiskit_transpiler::passes::basis_translator::__pyfunction_py_run_basis_translator::he786c17cc22f7928
  11:     0x79e5332dd6bf - pyo3::impl_::trampoline::trampoline::ha873bd885e6869e2
  12:     0x79e5334e6590 - qiskit_transpiler::passes::basis_translator::<impl qiskit_transpiler::passes::basis_translator::py_run_basis_translator::MakeDef>::_PYO3_DEF::trampoline::hc69781c631f45d45
  13:           0x5da3e4 - _PyEval_EvalFrameDefault
  14:           0x5d543b - PyEval_EvalCode
  15:           0x5d322c - <unknown>
  16:           0x581d1d - <unknown>
  17:           0x549975 - PyObject_Vectorcall
  18:           0x5d6f09 - _PyEval_EvalFrameDefault
  19:           0x6bcb52 - <unknown>
  20:           0x6bc782 - Py_RunMain
  21:           0x6bc3ed - Py_BytesMain
  22:     0x79e55ee2a1ca - __libc_start_call_main
                               at ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16
  23:     0x79e55ee2a28b - __libc_start_main_impl
                               at ./csu/../csu/libc-start.c:360:3
  24:           0x6576c5 - _start
  25:                0x0 - <unknown>
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/home/ilan/programming/QFpp/test.py", line 32, in <module>
    circ_prime = transpile(main_circuit, backend, optimization_level=0)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ilan/programming/QFpp/.venv/lib/python3.12/site-packages/qiskit/compiler/transpiler.py", line 291, in transpile
    out_circuits = pm.run(circuits, callback=callback, num_processes=num_processes)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ilan/programming/QFpp/.venv/lib/python3.12/site-packages/qiskit/transpiler/passmanager.py", line 420, in run
    return super().run(circuits, output_name, callback, num_processes=num_processes)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ilan/programming/QFpp/.venv/lib/python3.12/site-packages/qiskit/transpiler/passmanager.py", line 457, in wrapper
    return meth(*meth_args, **meth_kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ilan/programming/QFpp/.venv/lib/python3.12/site-packages/qiskit/transpiler/passmanager.py", line 197, in run
    return super().run(
           ^^^^^^^^^^^^
  File "/home/ilan/programming/QFpp/.venv/lib/python3.12/site-packages/qiskit/passmanager/passmanager.py", line 238, in run
    _run_workflow(
  File "/home/ilan/programming/QFpp/.venv/lib/python3.12/site-packages/qiskit/passmanager/passmanager.py", line 311, in _run_workflow
    passmanager_ir, final_state = flow_controller.execute(
                                  ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ilan/programming/QFpp/.venv/lib/python3.12/site-packages/qiskit/passmanager/base_tasks.py", line 218, in execute
    passmanager_ir, state = next_task.execute(
                            ^^^^^^^^^^^^^^^^^^
  File "/home/ilan/programming/QFpp/.venv/lib/python3.12/site-packages/qiskit/transpiler/basepasses.py", line 167, in execute
    new_dag, state = super().execute(
                     ^^^^^^^^^^^^^^^^
  File "/home/ilan/programming/QFpp/.venv/lib/python3.12/site-packages/qiskit/passmanager/base_tasks.py", line 98, in execute
    ret = self.run(passmanager_ir)
          ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ilan/programming/QFpp/.venv/lib/python3.12/site-packages/qiskit/transpiler/passes/basis/basis_translator.py", line 120, in run
    out = base_run(
          ^^^^^^^^^
pyo3_runtime.PanicException: internal error: entered unreachable code: Having a unitary result from an `EquivalenceLibrary is not possible
  • Rerunning the circuit but with the ch gate removed works.
  • Replacing the ch operation with
    main_circuit.x(reg_931[0])
    also works.

What should happen?

Code should compile normally

Any suggestions?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions