Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use validated models for Result, update public API #1360

Merged
merged 16 commits into from
Nov 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 9 additions & 19 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ Changed
- ``backend.status()``(#1301).
- ``backend.configuration()`` (and ``__init__``) (#1323).
- ``backend.properties()`` (#1331).
- ``qiskit.Result`` (#1360).
- ``backend.provider()`` is now a method instead of a property (#1312).
- Remove local backend (Aer) fallback (#1303)

Expand All @@ -90,24 +91,7 @@ Deprecated
kwargs ``plot_barriers`` and ``reverse_bits`` should be used instead. (#1180)
- The transpiler methods do not support emitting multiple output `format`
anymore (#1319).


Removed
"""""""

- ``matplotlib`` is no longer in the package requirements and is now an optional
dependency. In order to use any matplotlib based visualizations (which
includes the `qiskit.tools.visualization.circuit_drawer()` `mpl` output,
`qiskit.tools.visualization.plot_state`,
`qiskit.tools.visualization.plot_histogram`, and
`qiskit.tools.visualization.plot_bloch_vector` you will now need to ensure
you manually install and configure matplotlib independently.
- The ``basis`` kwarg for the ``circuit_drawer()`` function to provide an
alternative list of basis gates has been removed. Instead users should adjust
the basis gates prior to visualizing the circuit. (#1151)
- ``backend.parameters()`` and ``backend.calibration()`` have been fully
deprecated, in favour of ``backend.properties()`` (#1305).

- Several methods of ``qiskit.Result`` have been deprecated (#1360).

Fixed
"""""
Expand Down Expand Up @@ -143,7 +127,13 @@ Removed
- The ``basis`` kwarg for the ``circuit_drawer()`` function to provide an
alternative list of basis gates has been removed. Instead users should adjust
the basis gates prior to visualizing the circuit. (#1151)

- ``backend.parameters()`` and ``backend.calibration()`` have been fully
deprecated, in favour of ``backend.properties()`` (#1305).
- The ``qiskit.tools.file_io`` module has been removed. Conversion between
``qiskit.Result`` and json can be achieved using ``.to_dict()`` and
``.from_dict()`` directly (#1360).
- The ``qiskit.Result`` class method for ``len()`` and indexing have been
removed, along with the functions that perform post-processing (#1351).

`0.6.0`_ - 2018-10-04
^^^^^^^^^^^^^^^^^^^^^
Expand Down
6 changes: 2 additions & 4 deletions qiskit/backends/aer/qasm_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ def _run_job(self, job_id, qobj):
result['job_id'] = job_id
copy_qasm_from_qobj_into_result(qobj, result)

return result_from_old_style_dict(
result, [circuit.header.name for circuit in qobj.experiments])
return result_from_old_style_dict(result)

def _validate(self, qobj):
for experiment in qobj.experiments:
Expand Down Expand Up @@ -209,8 +208,7 @@ def _run_job(self, job_id, qobj):
result = run(qobj, self._configuration.exe)
result['job_id'] = job_id

return result_from_old_style_dict(
result, [circuit.header.name for circuit in qobj.experiments])
return result_from_old_style_dict(result)

def _validate(self):
return
Expand Down
3 changes: 1 addition & 2 deletions qiskit/backends/aer/qasm_simulator_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,7 @@ def _run_job(self, job_id, qobj):

copy_qasm_from_qobj_into_result(qobj, result)

return result_from_old_style_dict(
result, [circuit.header.name for circuit in qobj.experiments])
return result_from_old_style_dict(result)

def run_circuit(self, circuit):
"""Run a circuit and return a single Result.
Expand Down
8 changes: 4 additions & 4 deletions qiskit/backends/aer/statevector_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,18 @@ def _run_job(self, job_id, qobj):
for experiment in qobj.experiments:
del experiment.instructions[-1]
# Extract final state snapshot and move to 'statevector' data field
for experiment_result in result.results.values():
snapshots = experiment_result.snapshots
for experiment_result in result.results:
snapshots = experiment_result.data.snapshots.to_dict()
if str(final_state_key) in snapshots:
final_state_key = str(final_state_key)
# Pop off final snapshot added above
final_state = snapshots.pop(final_state_key, None)
final_state = final_state['statevector'][0]
# Add final state to results data
experiment_result.data['statevector'] = final_state
experiment_result.data.statevector = final_state
# Remove snapshot dict if empty
if snapshots == {}:
experiment_result.data.pop('snapshots', None)
delattr(experiment_result.data, 'snapshots')
return result

def _validate(self, qobj):
Expand Down
8 changes: 4 additions & 4 deletions qiskit/backends/aer/statevector_simulator_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,18 @@ def _run_job(self, job_id, qobj):
for experiment in qobj.experiments:
del experiment.instructions[-1]
# Extract final state snapshot and move to 'statevector' data field
for experiment_result in result.results.values():
snapshots = experiment_result.snapshots
for experiment_result in result.results:
snapshots = experiment_result.data.snapshots.to_dict()
if str(final_state_key) in snapshots:
final_state_key = str(final_state_key)
# Pop off final snapshot added above
final_state = snapshots.pop(final_state_key, None)
final_state = final_state['statevector'][0]
# Add final state to results data
experiment_result.data['statevector'] = final_state
experiment_result.data.statevector = final_state
# Remove snapshot dict if empty
if snapshots == {}:
experiment_result.data.pop('snapshots', None)
delattr(experiment_result.data, 'snapshots')
return result

def _validate(self, qobj):
Expand Down
8 changes: 4 additions & 4 deletions qiskit/backends/aer/unitary_simulator_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,7 @@ def _run_job(self, job_id, qobj):
'time_taken': (end - start)}
copy_qasm_from_qobj_into_result(qobj, result)

return result_from_old_style_dict(
result, [circuit.header.name for circuit in qobj.experiments])
return result_from_old_style_dict(result)

def run_circuit(self, circuit):
"""Apply the single-qubit gate.
Expand Down Expand Up @@ -277,8 +276,9 @@ def run_circuit(self, circuit):
result['status'] = 'ERROR'
return result
# Reshape unitary rank-2n tensor back to a matrix
result['data']['unitary'] = np.reshape(self._unitary_state,
2 * [2 ** self._number_of_qubits])
tmp = np.reshape(self._unitary_state, 2 * [2 ** self._number_of_qubits])
# Convert complex numbers to pair of (real, imag)
result['data']['unitary'] = np.stack((tmp.real, tmp.imag), axis=-1)
result['status'] = 'DONE'
result['success'] = True
result['shots'] = 1
Expand Down
21 changes: 8 additions & 13 deletions qiskit/backends/ibmq/ibmqjob.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
from qiskit.backends.jobstatus import JobStatus, JOB_FINAL_STATES
from qiskit.result import Result
from qiskit.result._utils import result_from_old_style_dict
from qiskit.qobj import Result as QobjResult
from qiskit.qobj import ExperimentResult as QobjExperimentResult
from qiskit.qobj import validate_qobj_against_schema

from .api import ApiError
Expand Down Expand Up @@ -227,14 +225,7 @@ def _wait_for_result(self, timeout=None, wait=5):
return job_response

def _result_from_job_response(self, job_response):
experiment_results = []
result_json = job_response['qObjectResult']
for experiment_result_json in result_json['results']:
qobj_experiment_result = QobjExperimentResult(**experiment_result_json)
experiment_results.append(qobj_experiment_result)

result_kwargs = {**result_json, 'results': experiment_results}
return Result(QobjResult(**result_kwargs))
return Result.from_dict(job_response['qObjectResult'])

def cancel(self):
"""Attempt to cancel a job.
Expand Down Expand Up @@ -517,13 +508,17 @@ def _result_from_job_response(self, job_response):
experiment_results = []
for circuit_result in job_response['qasms']:
this_result = {'data': circuit_result['data'],
'name': circuit_result.get('name'),
'compiled_circuit_qasm': circuit_result.get('qasm'),
'status': circuit_result['status'],
'success': circuit_result['status'] == 'DONE',
'shots': job_response['shots']}
if 'metadata' in circuit_result:
this_result['metadata'] = circuit_result['metadata']
if 'header' in circuit_result['metadata'].get('compiled_circuit', {}):
this_result['header'] = \
circuit_result['metadata']['compiled_circuit']['header']
else:
this_result['header'] = {}
experiment_results.append(this_result)

return result_from_old_style_dict({
Expand All @@ -532,8 +527,8 @@ def _result_from_job_response(self, job_response):
'used_credits': job_response.get('usedCredits'),
'result': experiment_results,
'backend_name': self.backend().name(),
'success': job_response['status'] == 'DONE'
}, [circuit_result['name'] for circuit_result in job_response['qasms']])
'success': job_response['status'] == 'COMPLETED'
})


def _reorder_bits(job_data):
Expand Down
1 change: 0 additions & 1 deletion qiskit/qobj/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@
QobjItem, QobjHeader, QobjExperimentHeader)
from ._converter import qobj_to_dict
from ._validation import validate_qobj_against_schema, QobjValidationError
from ._result import Result, ExperimentResult
74 changes: 0 additions & 74 deletions qiskit/qobj/_result.py

This file was deleted.

8 changes: 8 additions & 0 deletions qiskit/quantum_info/analyzation/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# -*- coding: utf-8 -*-

# Copyright 2018, IBM.
#
# 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.

"""Module for functions for post processing results."""
10 changes: 2 additions & 8 deletions qiskit/quantum_info/analyzation/average.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
# -*- coding: utf-8 -*-

# Copyright 2017, IBM.
# Copyright 2018, IBM.
#
# 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.

# pylint: disable=invalid-name,anomalous-backslash-in-string

"""
A collection of useful functions for post processing results.


"""
"""A collection of useful functions for post processing results."""


def average_data(counts, observable):
Expand Down
2 changes: 1 addition & 1 deletion qiskit/result/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@

"""Module for working with results."""

from ._result import Result
from .result import Result
from ._resulterror import ResultError
Loading