Skip to content

Commit

Permalink
Avoid to_dict() when possible in pulse system model (Qiskit#695)
Browse files Browse the repository at this point in the history
* Avoid to_dict() when possible in pulse system model

As fall out from Qiskit/qiskit#4016 there was a bug in the
to_dict() method of the backend configuration that was resulting in
UChannelLO objects not getting converted to their dict form in the
output of to_dict(). While this is being fixed in
Qiskit/qiskit#4124 this commit side steps the issue by adapting
the access patterns in the pulse system model to rely on class attribute
access and getattr() instead of dict access and get().

Co-authored-by: Christopher J. Wood <[email protected]>
  • Loading branch information
2 people authored and hhorii committed Apr 16, 2020
1 parent 9ee788a commit 6598778
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions qiskit/providers/aer/pulse/pulse_system_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,23 +110,23 @@ def from_backend(cls, backend, subsystem_list=None):
raise AerError("{} is not a Qiskit backend".format(backend))

# get relevant information from backend
defaults = backend.defaults().to_dict()
config = backend.configuration().to_dict()
defaults = backend.defaults()
config = backend.configuration()

if not config['open_pulse']:
if not config.open_pulse:
raise AerError('{} is not an open pulse backend'.format(backend))

# draw defaults
qubit_freq_est = defaults.get('qubit_freq_est', None)
meas_freq_est = defaults.get('meas_freq_est', None)
qubit_freq_est = getattr(defaults, 'qubit_freq_est', None)
meas_freq_est = getattr(defaults, 'meas_freq_est', None)

# draw from configuration
# if no subsystem_list, use all for device
subsystem_list = subsystem_list or list(range(config['n_qubits']))
ham_string = config['hamiltonian']
subsystem_list = subsystem_list or list(range(config.n_qubits))
ham_string = config.hamiltonian
hamiltonian = HamiltonianModel.from_dict(ham_string, subsystem_list)
u_channel_lo = config.get('u_channel_lo', None)
dt = config.get('dt', None)
u_channel_lo = getattr(config, 'u_channel_lo', None)
dt = getattr(config, 'dt', None)

control_channel_labels = [None] * len(u_channel_lo)
# populate control_channel_dict
Expand All @@ -152,11 +152,14 @@ def from_backend(cls, backend, subsystem_list=None):
# construct string for u channel
u_string = ''
for u_term_dict in u_lo:
scale = u_term_dict.get('scale', [1.0, 0])
q_idx = u_term_dict.get('q')
scale = getattr(u_term_dict, 'scale', [1.0, 0])
q_idx = getattr(u_term_dict, 'q')
if len(u_string) > 0:
u_string += ' + '
u_string += str(scale[0] + scale[1] * 1j) + 'q' + str(q_idx)
if isinstance(scale, complex):
u_string += str(scale) + 'q' + str(q_idx)
else:
u_string += str(scale[0] + scale[1] * 1j) + 'q' + str(q_idx)
control_channel_labels[u_idx] = {'driven_q': drive_idx, 'freq': u_string}

return cls(hamiltonian=hamiltonian,
Expand Down

0 comments on commit 6598778

Please sign in to comment.