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.22k stars 2.36k forks source link

Numerical instability with calibrations #6568

Open eggerdj opened 3 years ago

eggerdj commented 3 years ago

Information

What is the current behavior?

When adding calibrations to a circuit we can have situations where the numerical precision of the parameters cause transpiler issues.

Steps to reproduce the problem

The following code works for the end amplitude of 0.9 but fails for 0.95 with the error qiskit.exceptions.QiskitError: "Cannot unroll the circuit to the given basis, ['id', 'rz', 'sx', 'x']. Instruction MyGate not found in equivalence library and no rule found to expand."

import numpy as np

from qiskit import QuantumCircuit
from qiskit.circuit import Gate, Parameter
import qiskit.pulse as pulse
from qiskit import transpile
from qiskit.test.mock import FakeArmonk

backend = FakeArmonk()

param = Parameter("param")
with pulse.build(backend=backend, name="sched") as sched:
    pulse.set_frequency(param, pulse.DriveChannel(0))

qc = QuantumCircuit(1)
gate = Gate(name="MyGate", num_qubits=1, params=[param])

qc.append(gate, (0,))
qc.add_calibration(gate, (3,), schedule=sched, params=[param])
qc.measure_active()

circs = []
for amp in np.linspace(0, 0.90, 5):  # fails for 0.95, works for 0.90
    circ = qc.assign_parameters({param: amp}, inplace=False)    
    circs.append(circ)

out = transpile(circs, backend, optimization_level=0, initial_layout=(3,))

What is the expected behavior?

The code above should not fail.

Suggested solutions

Perhaps rounding the parameters in the calibrations to 8 decimals would solve the issue.

ishworii commented 3 years ago

Which qiskit version are you using, the above code ran fine on my build, with 0.9 and 0.95 I am using '0.18.0.dev0+25df5ba' , which I recently built from source.

eggerdj commented 3 years ago

See the commit specified above.

nkanazawa1989 commented 3 years ago

This seems to be an issue of data structure of circuit calibration field, i.e. this is a dictionary with the assigned parameter value as a part of key Dict[gate name: str][Tuple[Tuple[qubit: int, ...], Tuple[parameter value: float, ...]], Schedule].

Perhaps this line? https://github.com/Qiskit/qiskit-terra/blob/5fad07e9c537e7f254cbd099e129a1eb427176cd/qiskit/dagcircuit/dagcircuit.py#L231-L232

taalexander commented 3 years ago

Any bump on this?