qiskit/releasenotes/notes/0.13/qobj-no-marshmallow-3f2b3c7...

146 lines
6.2 KiB
YAML

---
upgrade:
- |
`fastjsonschema <https://pypi.org/project/fastjsonschema/>`_ is added as a
dependency. This is used for much faster validation of qobj dictionaries
against the JSON schema when the ``to_dict()`` method is called on qobj
objects with the ``validate`` keyword argument set to ``True``.
- |
The qobj construction classes in :mod:`qiskit.qobj` will no longer validate
against the qobj jsonschema by default. These include the following classes:
* :class:`qiskit.qobj.QasmQobjInstruction`
* :class:`qiskit.qobj.QobjExperimentHeader`
* :class:`qiskit.qobj.QasmQobjExperimentConfig`
* :class:`qiskit.qobj.QasmQobjExperiment`
* :class:`qiskit.qobj.QasmQobjConfig`
* :class:`qiskit.qobj.QobjHeader`
* :class:`qiskit.qobj.PulseQobjInstruction`
* :class:`qiskit.qobj.PulseQobjExperimentConfig`
* :class:`qiskit.qobj.PulseQobjExperiment`
* :class:`qiskit.qobj.PulseQobjConfig`
* :class:`qiskit.qobj.QobjMeasurementOption`
* :class:`qiskit.qobj.PulseLibraryItem`
* :class:`qiskit.qobj.QasmQobjInstruction`
* :class:`qiskit.qobj.QasmQobjExperimentConfig`
* :class:`qiskit.qobj.QasmQobjExperiment`
* :class:`qiskit.qobj.QasmQobjConfig`
* :class:`qiskit.qobj.QasmQobj`
* :class:`qiskit.qobj.PulseQobj`
If you were relying on this validation or would like to validate them
against the qobj schema this can be done by setting the ``validate`` kwarg
to ``True`` on :meth:`~qiskit.qobj.QasmQobj.to_dict` method from either of
the top level Qobj classes :class:`~qiskit.qobj.QasmQobj` or
:class:`~qiskit.qobj.PulseQobj`. For example:
.. code-block:
from qiskit import qobj
my_qasm = qobj.QasmQobj(
qobj_id='12345',
header=qobj.QobjHeader(),
config=qobj.QasmQobjConfig(shots=1024, memory_slots=2,
max_credits=10),
experiments=[
qobj.QasmQobjExperiment(instructions=[
qobj.QasmQobjInstruction(name='u1', qubits=[1],
params=[0.4]),
qobj.QasmQobjInstruction(name='u2', qubits=[1],
params=[0.4, 0.2])
])
]
)
qasm_dict = my_qasm.to_dict(validate=True)
which will validate the output dictionary against the Qobj jsonschema.
- |
The output dictionary from :meth:`qiskit.qobj.QasmQobj.to_dict` and
:meth:`qiskit.qobj.PulseQobj.to_dict` is no longer in a format for direct
json serialization as expected by IBMQ's API. These Qobj objects are
the current format we use for passing experiments to providers/backends
and while having a dictionary format that could just be passed to the IBMQ
API directly was moderately useful for ``qiskit-ibmq-provider``, it made
things more difficult for other providers. Especially for providers that
wrap local simulators. Moving forward the definitions of what is passed
between providers and the IBMQ API request format will be further decoupled
(in a backwards compatible manner) which should ease the burden of writing
providers and backends.
In practice, the only functional difference between the output of these
methods now and previous releases is that complex numbers are represented
with the ``complex`` type and numpy arrays are not silently converted to
list anymore. If you were previously calling ``json.dumps()`` directly on
the output of ``to_dict()`` after this release a custom json encoder will
be needed to handle these cases. For example::
import json
from qiskit.circuit import ParameterExpression
from qiskit import qobj
my_qasm = qobj.QasmQobj(
qobj_id='12345',
header=qobj.QobjHeader(),
config=qobj.QasmQobjConfig(shots=1024, memory_slots=2,
max_credits=10),
experiments=[
qobj.QasmQobjExperiment(instructions=[
qobj.QasmQobjInstruction(name='u1', qubits=[1],
params=[0.4]),
qobj.QasmQobjInstruction(name='u2', qubits=[1],
params=[0.4, 0.2])
])
]
)
qasm_dict = my_qasm.to_dict()
class QobjEncoder(json.JSONEncoder):
"""A json encoder for pulse qobj"""
def default(self, obj):
# Convert numpy arrays:
if hasattr(obj, 'tolist'):
return obj.tolist()
# Use Qobj complex json format:
if isinstance(obj, complex):
return (obj.real, obj.imag)
if isinstance(obj, ParameterExpression):
return float(obj)
return json.JSONEncoder.default(self, obj)
json_str = json.dumps(qasm_dict, cls=QobjEncoder)
will generate a json string in the same exact manner that
``json.dumps(my_qasm.to_dict())`` did in previous releases.
other:
- |
The qasm and pulse qobj classes:
* :class:`~qiskit.qobj.QasmQobjInstruction`
* :class:`~qiskit.qobj.QobjExperimentHeader`
* :class:`~qiskit.qobj.QasmQobjExperimentConfig`
* :class:`~qiskit.qobj.QasmQobjExperiment`
* :class:`~qiskit.qobj.QasmQobjConfig`
* :class:`~qiskit.qobj.QobjHeader`
* :class:`~qiskit.qobj.PulseQobjInstruction`
* :class:`~qiskit.qobj.PulseQobjExperimentConfig`
* :class:`~qiskit.qobj.PulseQobjExperiment`
* :class:`~qiskit.qobj.PulseQobjConfig`
* :class:`~qiskit.qobj.QobjMeasurementOption`
* :class:`~qiskit.qobj.PulseLibraryItem`
* :class:`~qiskit.qobj.QasmQobjInstruction`
* :class:`~qiskit.qobj.QasmQobjExperimentConfig`
* :class:`~qiskit.qobj.QasmQobjExperiment`
* :class:`~qiskit.qobj.QasmQobjConfig`
* :class:`~qiskit.qobj.QasmQobj`
* :class:`~qiskit.qobj.PulseQobj`
from :mod:`qiskit.qobj` have all been reimplemented without using the
marsmallow library. These new implementations are designed to be drop-in
replacement (except for as noted in the upgrade release notes) but
specifics inherited from marshmallow may not work. Please file issues for
any incompatibilities found.