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.31k stars 2.38k forks source link

Target.from_configuration doesn't take measure instruction #10168

Open nkanazawa1989 opened 1 year ago

nkanazawa1989 commented 1 year ago

Environment

What is happening?

When we create Target from the legacy models with Target.from_configuration, it doesn't work with circuit scheduler.

How can we reproduce the issue?

A factory method Target.from_configuration introduced in 0.24 doesn't handle measurement instructions properly. This method only creates instruction entries for basis gates, however, IBM backends don't contain "measure" in the basis gates.

from qiskit.providers.fake_provider import FakeHanoi
FakeHanoi().configuration().basis_gates  # ['id', 'rz', 'sx', 'x', 'cx', 'reset']

This causes a bug in the circuit scheduler chain.

from qiskit.providers.fake_provider import FakeHanoi
from qiskit.circuit import QuantumCircuit
from qiskit.transpiler.target import Target
from qiskit.transpiler.coupling import CouplingMap
from qiskit.transpiler.timing_constraints import TimingConstraints
from qiskit.transpiler.passmanager import PassManager
from qiskit.transpiler.passes import (
    TimeUnitConversion,
    ALAPScheduleAnalysis,
    PadDelay,
)

backend = FakeHanoi()

configuration = backend.configuration()
pulse_defaults = backend.defaults()
properties = backend.properties()

target = Target.from_configuration(
    basis_gates=configuration.basis_gates,
    num_qubits=configuration.num_qubits,
    coupling_map=CouplingMap(configuration.coupling_map),
    inst_map=pulse_defaults.instruction_schedule_map,
    backend_properties=properties,
    dt=configuration.dt,
    timing_constraints=TimingConstraints(**configuration.timing_constraints),
)

scheduler = PassManager()
scheduler.append(
    [
        TimeUnitConversion(target=target),
        ALAPScheduleAnalysis(target=target),
        PadDelay(),
    ]
)

qc = QuantumCircuit(1, 1)
qc.x(0)
qc.measure(0, 0)

scheduler.run(qc)

The last line raises

TranspilerError: 'Duration of measure on qubits [0] is not found.'

because of the missing measure instruction. Even if we manually add "measure" to the basis gates, scheduler chain doesn't work because the factory method only checks the gate length property, i.e. "measure" duration is separately provided from gate length in IBM Provider model.

https://github.com/Qiskit/qiskit-terra/blob/5013fe2239290414f2cfaafae13c6a9c09ddbbda/qiskit/transpiler/target.py#L1362

What should happen?

Target should have filled InstructionProperties for measure instructions.

Any suggestions?

The logic I implemented for IBM Provider is robust to missing operations in basis gates.

https://github.com/Qiskit/qiskit-ibm-provider/pull/413

mtreinish commented 1 year ago

Oh, this is a big oversight in the constructor method, you're right from_configuration completely overlooks that in the old data model measure was implicitly available on all qubits. We need to be adding measure by default to the output Target in all cases when from_configuration is used.

MozammilQ commented 12 months ago

@nkanazawa1989 , can I try solving this issue?

MozammilQ commented 11 months ago

I have got permission to work on this issue, so I am currently working on this issue :)