Skip to content

Commit

Permalink
Add missing parameter from QasmQobjInstruction (Qiskit#3953)
Browse files Browse the repository at this point in the history
* Add missing parameter from QasmQobjInstruction

In Qiskit#3383 we moved away from using marshmallow for Qobj objects to flat
classes with normal attributes. However, in that migration we missed one
parameter for the qasm qobj instruction class which had no test coverage
in terra (and is only used by Aer), snapshot_type. This commit corrects
the oversight and adds test coverage to ensure we don't miss it in the
future.

* Fix lint
  • Loading branch information
mtreinish authored Mar 11, 2020
1 parent 2cab03a commit 94d3368
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
9 changes: 7 additions & 2 deletions qiskit/qobj/qasm_qobj.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class QasmQobjInstruction:

def __init__(self, name, params=None, qubits=None, register=None,
memory=None, condition=None, conditional=None, label=None,
mask=None, relation=None, val=None):
mask=None, relation=None, val=None, snapshot_type=None):
"""Instatiate a new QasmQobjInstruction object.
Args:
Expand Down Expand Up @@ -67,6 +67,8 @@ def __init__(self, name, params=None, qubits=None, register=None,
``!=`` (not equals).
val (int): Value to which to compare the masked register. In other
words, the output of the function is ``(register AND mask)``
snapshot_type (str): For snapshot instructions the type of snapshot
to use
"""
self.name = name
if params is not None:
Expand All @@ -89,6 +91,8 @@ def __init__(self, name, params=None, qubits=None, register=None,
self.relation = relation
if val is not None:
self.val = val
if snapshot_type is not None:
self.snapshot_type = snapshot_type

def to_dict(self):
"""Return a dictionary format representation of the Instruction.
Expand All @@ -98,7 +102,8 @@ def to_dict(self):
"""
out_dict = {'name': self.name}
for attr in ['params', 'qubits', 'register', 'memory', '_condition',
'conditional', 'label', 'mask', 'relation', 'val']:
'conditional', 'label', 'mask', 'relation', 'val',
'snapshot_type']:
if hasattr(self, attr):
out_dict[attr] = getattr(self, attr)

Expand Down
69 changes: 69 additions & 0 deletions test/python/qobj/test_qobj.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,75 @@ def test_from_dict_per_class(self):
with self.subTest(msg=str(qobj_class)):
self.assertEqual(qobj_item, qobj_class.from_dict(expected_dict))

def test_snapshot_instruction_to_dict(self):
"""Test snapshot instruction to dict."""
valid_qobj = QasmQobj(
qobj_id='12345',
header=QobjHeader(),
config=QasmQobjConfig(shots=1024, memory_slots=2, max_credits=10),
experiments=[
QasmQobjExperiment(instructions=[
QasmQobjInstruction(name='u1', qubits=[1], params=[0.4]),
QasmQobjInstruction(name='u2', qubits=[1], params=[0.4, 0.2]),
QasmQobjInstruction(name='snapshot', qubits=[1],
snapshot_type='statevector',
label='my_snap')
])
]
)
res = valid_qobj.to_dict(validate=True)
expected_dict = {
'qobj_id': '12345',
'type': 'QASM',
'schema_version': '1.1.0',
'header': {},
'config': {'max_credits': 10, 'memory_slots': 2, 'shots': 1024},
'experiments': [
{'instructions': [
{'name': 'u1', 'params': [0.4], 'qubits': [1]},
{'name': 'u2', 'params': [0.4, 0.2], 'qubits': [1]},
{'name': 'snapshot', 'qubits': [1],
'snapshot_type': 'statevector', 'label': 'my_snap'}
],
'config': {},
'header': {}}
],
}
self.assertEqual(expected_dict, res)

def test_snapshot_instruction_from_dict(self):
"""Test snapshot instruction from dict."""
expected_qobj = QasmQobj(
qobj_id='12345',
header=QobjHeader(),
config=QasmQobjConfig(shots=1024, memory_slots=2, max_credits=10),
experiments=[
QasmQobjExperiment(instructions=[
QasmQobjInstruction(name='u1', qubits=[1], params=[0.4]),
QasmQobjInstruction(name='u2', qubits=[1], params=[0.4, 0.2]),
QasmQobjInstruction(name='snapshot', qubits=[1],
snapshot_type='statevector',
label='my_snap')
])
]
)
qobj_dict = {
'qobj_id': '12345',
'type': 'QASM',
'schema_version': '1.1.0',
'header': {},
'config': {'max_credits': 10, 'memory_slots': 2, 'shots': 1024},
'experiments': [
{'instructions': [
{'name': 'u1', 'params': [0.4], 'qubits': [1]},
{'name': 'u2', 'params': [0.4, 0.2], 'qubits': [1]},
{'name': 'snapshot', 'qubits': [1],
'snapshot_type': 'statevector', 'label': 'my_snap'}
]}
],
}
self.assertEqual(expected_qobj, QasmQobj.from_dict(qobj_dict))

def test_simjob_raises_error_when_sending_bad_qobj(self):
"""Test SimulatorJob is denied resource request access when given an invalid Qobj instance.
"""
Expand Down

0 comments on commit 94d3368

Please sign in to comment.