Qiskit / qiskit

Qiskit is an open-source SDK for working with quantum computers at the level of extended quantum circuits, operators, and primitives.
https://www.ibm.com/quantum/qiskit
Apache License 2.0
5.04k stars 2.32k forks source link

Optimisation failure #12934

Open glanzz opened 1 month ago

glanzz commented 1 month ago

Environment

What is happening?

The transpiler pass failed when i was generating one of the circuits with options {'layout_method': 'trivial', 'seed_transpiler': 1235, 'routing_method': 'stochastic', 'translation_method': 'translator'} on AerSimulator

        ┌───┐┌───┐           ┌───┐┌───┐┌───┐┌───┐          ┌───┐┌───┐┌───┐┌───┐           ┌─────┐ ░ ┌─┐      
   q_0: ┤ X ├┤ T ├──■───■──X─┤ H ├┤ H ├┤ X ├┤ Z ├──■────■──┤ Z ├┤ X ├┤ H ├┤ H ├─X──■───■──┤ Tdg ├─░─┤M├──────
        ├───┤└───┘┌─┴─┐ │  │ └───┘└───┘└───┘└───┘┌─┴─┐┌─┴─┐└───┘└───┘└───┘└───┘ │  │ ┌─┴─┐└─────┘ ░ └╥┘┌─┐   
   q_1: ┤ X ├─────┤ X ├─■──X─────────────────────┤ X ├┤ X ├─────────────────────X──■─┤ X ├────────░──╫─┤M├───
        ├───┤     └───┘                          └─┬─┘└─┬─┘                          └───┘        ░  ║ └╥┘┌─┐
   q_2: ┤ X ├──────────────────────────────────────■────■─────────────────────────────────────────░──╫──╫─┤M├
        └───┘                                                                                     ░  ║  ║ └╥┘
meas: 3/═════════════════════════════════════════════════════════════════════════════════════════════╩══╩══╩═
                                                                                                     0  1  2 

But i was not able to reproduce the error later on, i was finding the best possible compiler option to run on AerSimulator with multiple other options and it failed at this particular options.

How can we reproduce the issue?

from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
from qiskit_aer import StatevectorSimulator, AerSimulator
from qiskit import QuantumCircuit
qc = QuantumCircuit(3)
qc.x(0)
qc.x(1)
qc.x(2)
qc.t(0)
qc.cx(0, 1)
qc.cz(0, 1)
qc.swap(0, 1)
qc.h(0)
qc.h(0)
qc.x(0)
qc.z(0)
qc.ccx(2,0,1)

qc.ccx(2,0,1)
qc.z(0)
qc.x(0)
qc.h(0)
qc.h(0)
qc.swap(0, 1)
qc.cz(0, 1)
qc.cx(0, 1)
qc.tdg(0)

qc.measure_all()
print(qc)
backend = StatevectorSimulator()
options = {'layout_method': 'trivial', 'seed_transpiler': 1235, 'routing_method': 'stochastic', 'translation_method': 'translator'}
for i in range(4):
  print(qc)
  pass_manager = generate_preset_pass_manager(backend=backend, optimization_level=i, **options)
  transpiled_circuit = pass_manager.run(qc)
  backend.run(transpiled_circuit, shots=100).result().get_counts()

What should happen?

Traceback (most recent call last): File "/Users/xxx/Projects/circuit_generator/genmod.py", line 246, in pass_manager = generate_preset_pass_manager(backend=backend, optimization_level=optimization_level, **options)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/xxx/Projects/circuit_generator/venv/lib/python3.11/site-packages/qiskit/transpiler/preset_passmanagers/init.py", line 279, in generate_preset_pass_manager pm = level_0_pass_manager(pm_config) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/xxx/Projects/circuit_generator/venv/lib/python3.11/site-packages/qiskit/transpiler/preset_passmanagers/level0.py", line 84, in level_0_pass_manager sched = plugin_manager.get_passmanager_stage( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/xxx/Projects/circuit_generator/venv/lib/python3.11/site-packages/qiskit/transpiler/preset_passmanagers/plugin.py", line 263, in get_passmanager_stage return self._build_pm( ^^^^^^^^^^^^^^^ File "/Users/xxx/Projects/circuit_generator/venv/lib/python3.11/site-packages/qiskit/transpiler/preset_passmanagers/plugin.py", line 280, in _build_pm return plugin_obj.obj.pass_manager(pm_config, optimization_level) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/xxx/Projects/circuit_generator/venv/lib/python3.11/site-packages/qiskit/transpiler/preset_passmanagers/builtin_plugins.py", line 658, in pass_manager return common.generate_scheduling( ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/xxx/Projects/circuit_generator/venv/lib/python3.11/site-packages/qiskit/transpiler/preset_passmanagers/common.py", line 573, in generate_scheduling timing_constraints.granularity != 1 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'NoneType' object has no attribute 'granularity'

Any suggestions?

No response

Cryoris commented 4 weeks ago

Hmm I can't reproduce this on either of 1.1.0, 1.1.2 or main and the AerSimulator or StatevectorSimulator, also using Python 3.11(.9) and macOS. What version of qiskit_aer are you using? Does updating it resolve the problem?