pdhoolia / 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
0 stars 0 forks source link

Cannot schedule circuits using AerSimulator and QasmSimulator #11

Open pdhoolia opened 5 days ago

pdhoolia commented 5 days ago

What is happening?

AerSimulator and QasmSimulator cannot transpile most gates (except for barrier and id) when using the parameter scheduling_method='asap'. The following error message is displayed:

qiskit.transpiler.exceptions.TranspilerError: 'Duration of x on qubits [0] is not found.'

This is because the node.op.duration for these gates is set to None, whereas barrier and id gates have a duration value of 0.

How can we reproduce the issue?

from qiskit.circuit import QuantumCircuit
from qiskit import transpile

qc = QuantumCircuit(1)
# qc.id(0)      # The value of node.op.duration is 0
# qc.barrier()  # The value of node.op.duration is 0
qc.x(0)         # The value of node.op.duration is None
qc.measure_all()# The value of node.op.duration is None

from qiskit_aer import AerSimulator, QasmSimulator
backend1 = AerSimulator() # Failed
# backend2 = QasmSimulator() # Failed

qc1 = transpile(qc, optimization_level=1, backend=backend1, scheduling_method='asap')

What should happen?

Assigning default values to all gates would be better.

pdhoolia commented 5 days ago

To address the issue where AerSimulator and QasmSimulator cannot transpile most gates when using the parameter scheduling_method='asap', we need to ensure that all gates have a defined duration. The error message indicates that the duration for some gates (like x and measure_all) is set to None, which causes the transpiler to fail.

Here is the plan to address the issue:

  1. Default Duration Assignment: We need to assign a default duration to gates that currently have None as their duration. This can be done by setting a default duration value for these gates.

  2. Modify QuantumCircuit Class: Update the QuantumCircuit class to ensure that every gate operation has a duration assigned. This can be done by modifying the append method or setting durations during gate initialization.

  3. Update Transpiler Logic: Ensure that the transpiler can handle default durations properly. This might involve updating the logic where durations are checked or used in scheduling.

Let's implement these changes:

Step 1: Assign Default Duration

We'll add a default duration to the gates that currently do not have one. For simplicity, let's assume a default duration of 1 unit for gates without a specific duration.

Step 2: Modify QuantumCircuit Class

Add logic to assign a default duration to gates when they are appended to the circuit:

# Update in the QuantumCircuit class
def append(
    self,
    instruction: Operation | CircuitInstruction,
    qargs: Sequence[QubitSpecifier] | None = None,
    cargs: Sequence[ClbitSpecifier] | None = None,
    *,
    copy: bool = True,
) -> InstructionSet:
    # Existing code...

    # Set default duration if not set
    if instruction.duration is None:
        instruction.duration = 1  # Default duration

    # Existing code...

Step 3: Ensure Transpiler Handles Default Durations

Ensure that the transpiler can handle the default duration value correctly. If the transpiler logic needs to be updated, ensure it respects the default duration values.

Step 4: Test the Solution

After implementing these changes, test the solution using the provided reproduction code to ensure that the error is resolved and that circuits can be successfully transpiled with scheduling_method='asap'.

By implementing these changes, we ensure that all gates have a defined duration, which should resolve the transpiler error related to missing duration information.