Skip to content

Commit

Permalink
Add printable string representation for new Qobj objects
Browse files Browse the repository at this point in the history
This commit adds a printable string representation for Qobj objects.
In Qiskit#3383 we rebuilt the Qobj objects without using marshmallow for
performance and ease of working with. However, as part of that no human
readable representation for the objects was added. This commit corrects
that oversight by adding a __str__ parameter to these new classes.
  • Loading branch information
mtreinish committed Mar 12, 2020
1 parent 3976fda commit ef6af64
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
38 changes: 38 additions & 0 deletions qiskit/qobj/pulse_qobj.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import copy
import json
import pprint

import numpy

Expand Down Expand Up @@ -171,6 +172,16 @@ def to_dict(self):
x.to_dict() for x in self.discriminators]
return out_dict

def __str__(self):
out = "Instruction: %s\n" % self.name
out += "\t\tt0: %s\n" % self.t0
for attr in ['ch', 'conditional', 'val', 'phase', 'duration',
'qubits', 'memory_slot', 'register_slot',
'label', 'type', 'pulse_shape', 'parameters']:
if hasattr(self, attr):
out += '\t\t%s: %s\n' % (attr, getattr(self, attr))
return out

@classmethod
def from_dict(cls, data):
"""Create a new PulseQobjExperimentConfig object from a dictionary.
Expand Down Expand Up @@ -319,6 +330,22 @@ def to_dict(self):
out_dict['header'] = self.header.to_dict()
return out_dict

def __str__(self):
out = '\nPulse Experiment:\n'
if hasattr(self, 'config'):
config = pprint.pformat(self.config.to_dict())
else:
config = '{}'
if hasattr(self, 'header'):
header = pprint.pformat(self.header.to_dict() or {})
else:
header = '{}'
out += 'Header:\n%s\n' % header
out += 'Config:\n%s\n\n' % config
for instruction in self.instructions:
out += '\t%s\n' % instruction
return out

@classmethod
def from_dict(cls, data):
"""Create a new PulseQobjExperiment object from a dictionary.
Expand Down Expand Up @@ -451,6 +478,17 @@ def default(self, obj):
json_str = json.dumps(out_dict, cls=PulseQobjEncoder)
validator(json.loads(json_str))

def __str__(self):
out = "Pulse Qobj: %s:\n" % self.qobj_id
config = pprint.pformat(self.config.to_dict())
out += "Config: %s\n" % str(config)
header = pprint.pformat(self.header.to_dict())
out += "Header: %s\n" % str(header)
out += "Experiments:\n"
for experiment in self.experiments:
out += "%s" % str(experiment)
return out

def to_dict(self, validate=False):
"""Return a dictionary format representation of the Pulse Qobj.
Expand Down
31 changes: 31 additions & 0 deletions qiskit/qobj/qasm_qobj.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"""Module providing definitions of QASM Qobj classes."""

import os
import pprint
from types import SimpleNamespace

import json
Expand Down Expand Up @@ -122,6 +123,15 @@ def to_dict(self):

return out_dict

def __str__(self):
out = "Instruction: %s\n" % self.name
for attr in ['params', 'qubits', 'register', 'memory', '_condition',
'conditional', 'label', 'mask', 'relation', 'val',
'snapshot_type']:
if hasattr(self, attr):
out += '\t\t%s: %s\n' % (attr, getattr(self, attr))
return out

@classmethod
def from_dict(cls, data):
"""Create a new QasmQobjInstruction object from a dictionary.
Expand Down Expand Up @@ -161,6 +171,16 @@ def __init__(self, config=None, header=None, instructions=None):
self.header = header or QasmQobjExperimentHeader()
self.instructions = instructions or []

def __str__(self):
out = '\nQASM Experiment:\n'
config = pprint.pformat(self.config.to_dict())
header = pprint.pformat(self.header.to_dict())
out += 'Header:\n%s\n' % header
out += 'Config:\n%s\n\n' % config
for instruction in self.instructions:
out += '\t%s\n' % instruction
return out

def to_dict(self):
"""Return a dictionary format representation of the Experiment.
Expand Down Expand Up @@ -364,6 +384,17 @@ def __init__(self, qobj_id=None, config=None, experiments=None,
self.experiments = experiments or []
self.qobj_id = qobj_id

def __str__(self):
out = "QASM Qobj: %s:\n" % self.qobj_id
config = pprint.pformat(self.config.to_dict())
out += "Config: %s\n" % str(config)
header = pprint.pformat(self.header.to_dict())
out += "Header: %s\n" % str(header)
out += "Experiments:\n"
for experiment in self.experiments:
out += "%s" % str(experiment)
return out

def to_dict(self, validate=False):
"""Return a dictionary format representation of the QASM Qobj.
Expand Down

0 comments on commit ef6af64

Please sign in to comment.