Skip to content

Commit

Permalink
Separate Qobj into PulseQobj and QASM Qobj (Qiskit#1969)
Browse files Browse the repository at this point in the history
* add pulse and QASM models to qiskit.qobj

* add PulseQobj and QASMQobj to qiskit.qobj

* add interface of assemble_schedules

* add unit test

* resolve errors

* remove duplicated dict key in unittest

* modify imports

* PulseQobj/QASMQobj inherit from Qobj

* add custom validator for measurement options

* add instruction, header, config to BaseQobjExperimentSchema

* update class names (remove Base- prefix, and QASM > Qasm)

* remove PulseQobjHeader, PulseQobjExperimentHeader, QasmQobjHeader, QasmQobjExperimentHeader,

* separate models into specific modules

* Add minor changes

* add change log

* add minor changes
  • Loading branch information
nkanazawa1989 authored and diego-plan9 committed Mar 27, 2019
1 parent 3365e82 commit b71b3f3
Show file tree
Hide file tree
Showing 15 changed files with 708 additions and 141 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ Changed
rest of spec-defined entities. (#1909).
- The rzz gate is now represented as a line when printed in text (#1957).
- Text drawer has support for multi-q gates (#1939).
- Separate ``Qobj`` into ``PulseQobj`` and ``QasmQobj`` (#1969).

Deprecated
----------
Expand Down
26 changes: 13 additions & 13 deletions qiskit/compiler/assembler.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@

"""Assemble function for converting a list of circuits into a qobj"""
import uuid
import sympy

import numpy
import sympy

from qiskit.circuit.quantumcircuit import QuantumCircuit
from qiskit.qobj import Qobj, QobjConfig, QobjExperiment, QobjInstruction, QobjHeader
from qiskit.qobj import QobjExperimentConfig, QobjExperimentHeader, QobjConditional
from qiskit.compiler.run_config import RunConfig
from qiskit.qobj.utils import QobjType
from qiskit.qobj import (QasmQobj, QobjExperimentHeader, QobjHeader,
QasmQobjInstruction, QasmQobjExperimentConfig, QasmQobjExperiment,
QasmQobjConfig, QobjConditional)


def assemble_circuits(circuits, run_config=None, qobj_header=None, qobj_id=None):
Expand All @@ -27,14 +28,14 @@ def assemble_circuits(circuits, run_config=None, qobj_header=None, qobj_id=None)
qobj_id (int): identifier for the generated qobj
Returns:
Qobj: the Qobj to be run on the backends
QasmQobj: the Qobj to be run on the backends
"""
qobj_header = qobj_header or QobjHeader()
run_config = run_config or RunConfig()
if isinstance(circuits, QuantumCircuit):
circuits = [circuits]

userconfig = QobjConfig(**run_config.to_dict())
userconfig = QasmQobjConfig(**run_config.to_dict())
experiments = []
max_n_qubits = 0
max_memory_slots = 0
Expand Down Expand Up @@ -68,11 +69,11 @@ def assemble_circuits(circuits, run_config=None, qobj_header=None, qobj_id=None)
creg_sizes=creg_sizes,
name=circuit.name)
# TODO: why do we need n_qubits and memory_slots in both the header and the config
experimentconfig = QobjExperimentConfig(n_qubits=n_qubits, memory_slots=memory_slots)
experimentconfig = QasmQobjExperimentConfig(n_qubits=n_qubits, memory_slots=memory_slots)

instructions = []
for opt in circuit.data:
current_instruction = QobjInstruction(name=opt.name)
current_instruction = QasmQobjInstruction(name=opt.name)
if opt.qargs:
qubit_indices = [qubit_labels.index([qubit[0].name, qubit[1]])
for qubit in opt.qargs]
Expand Down Expand Up @@ -107,8 +108,8 @@ def assemble_circuits(circuits, run_config=None, qobj_header=None, qobj_id=None)
val="0x%X" % opt.control[1])

instructions.append(current_instruction)
experiments.append(QobjExperiment(instructions=instructions, header=experimentheader,
config=experimentconfig))
experiments.append(QasmQobjExperiment(instructions=instructions, header=experimentheader,
config=experimentconfig))
if n_qubits > max_n_qubits:
max_n_qubits = n_qubits
if memory_slots > max_memory_slots:
Expand All @@ -117,6 +118,5 @@ def assemble_circuits(circuits, run_config=None, qobj_header=None, qobj_id=None)
userconfig.memory_slots = max_memory_slots
userconfig.n_qubits = max_n_qubits

return Qobj(qobj_id=qobj_id or str(uuid.uuid4()), config=userconfig,
experiments=experiments, header=qobj_header,
type=QobjType.QASM.value)
return QasmQobj(qobj_id=qobj_id or str(uuid.uuid4()), config=userconfig,
experiments=experiments, header=qobj_header)
18 changes: 14 additions & 4 deletions qiskit/qobj/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,19 @@

"""Module for the Qobj structure."""

from .qobj import Qobj
from .models import (QobjConfig, QobjExperiment, QobjInstruction, QobjHeader,
QobjExperimentHeader, QobjConditional, QobjExperimentConfig)
from .exceptions import QobjValidationError
from qiskit.qobj.models.base import (QobjInstruction, QobjExperimentHeader, QobjExperimentConfig,
QobjExperiment, QobjConfig, QobjHeader)

from qiskit.qobj.models.pulse import (PulseQobjInstruction, PulseQobjExperimentConfig,
PulseQobjExperiment, PulseQobjConfig,
QobjMeasurementOption, QobjPulseLibrary)

from qiskit.qobj.models.qasm import (QasmQobjInstruction, QasmQobjExperimentConfig,
QasmQobjExperiment, QasmQobjConfig,
QobjConditional)

from ._validation import validate_qobj_against_schema

from .exceptions import QobjValidationError

from .qobj import Qobj, QasmQobj, PulseQobj
73 changes: 12 additions & 61 deletions qiskit/qobj/models.py → qiskit/qobj/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,56 +5,33 @@
# This source code is licensed under the Apache License, Version 2.0 found in
# the LICENSE.txt file in the root directory of this source tree.

"""Models for Qobj and its related components."""
"""The generic qobj models."""

from marshmallow.validate import Length, Range, Regexp
from marshmallow.validate import Length, Range

from qiskit.validation.base import BaseModel, BaseSchema, bind_schema
from qiskit.validation.fields import (Integer, List, Nested, String,
InstructionParameter)


class QobjConditionalSchema(BaseSchema):
"""Schema for QobjConditional."""

# Required properties.
mask = String(required=True, validate=Regexp('^0x([0-9A-Fa-f])+$'))
type = String(required=True)
val = String(required=True, validate=Regexp('^0x([0-9A-Fa-f])+$'))
from qiskit.validation import BaseSchema, bind_schema, BaseModel
from qiskit.validation.fields import String, Nested, Integer


class QobjInstructionSchema(BaseSchema):
"""Schema for QobjInstruction."""
"""Base Schema for QobjInstruction."""

# Required properties.
# Required properties
name = String(required=True)

# Optional properties.
qubits = List(Integer(validate=Range(min=0)),
validate=Length(min=1))
params = List(InstructionParameter())
memory = List(Integer(validate=Range(min=0)),
validate=Length(min=1))
conditional = Nested(QobjConditionalSchema)


class QobjExperimentHeaderSchema(BaseSchema):
"""Schema for QobjExperimentHeader."""
"""Base Schema for QobjExperimentHeader."""
pass


class QobjExperimentConfigSchema(BaseSchema):
"""Schema for QobjExperimentConfig."""

# Required properties.

# Optional properties.
memory_slots = Integer(validate=Range(min=0))
n_qubits = Integer(validate=Range(min=1))
"""Base Schema for QobjExperimentConfig."""
pass


class QobjExperimentSchema(BaseSchema):
"""Schema for QobjExperiment."""
"""Base Schema for QobjExperiment."""

# Required properties.
instructions = Nested(QobjInstructionSchema, required=True, many=True,
Expand All @@ -66,49 +43,23 @@ class QobjExperimentSchema(BaseSchema):


class QobjConfigSchema(BaseSchema):
"""Schema for QobjConfig."""

# Required properties.
"""Base Schema for QobjConfig."""

# Optional properties.
max_credits = Integer()
seed = Integer()
memory_slots = Integer(validate=Range(min=0))
n_qubits = Integer(validate=Range(min=1))
shots = Integer(validate=Range(min=1))


class QobjHeaderSchema(BaseSchema):
"""Schema for QobjHeader."""

# Required properties.
"""Base Schema for QobjHeader."""

# Optional properties.
backend_name = String()
backend_version = String()


@bind_schema(QobjConditionalSchema)
class QobjConditional(BaseModel):
"""Model for QobjConditional.
Please note that this class only describes the required fields. For the
full description of the model, please check ``QobjConditionalSchema``.
Attributes:
mask (str): hexadecimal mask of the conditional
type (str): type of the conditional
val (str): hexadecimal value of the conditional
"""
def __init__(self, mask, type, val, **kwargs):
# pylint: disable=redefined-builtin
self.mask = mask
self.type = type
self.val = val

super().__init__(**kwargs)


@bind_schema(QobjInstructionSchema)
class QobjInstruction(BaseModel):
"""Model for QobjInstruction.
Expand Down
Loading

0 comments on commit b71b3f3

Please sign in to comment.