Skip to content

Commit

Permalink
Adding compiled QASM to the result (#688)
Browse files Browse the repository at this point in the history
* Adding the regression test

* Adding compiled QASM to the result

* Disable CPP backends if not available

* Avoid copying unexistent QASM code

* Moving `get_ran_qasm` test into the `test_wrapper.py` test suite.

* add qasm to the unitary simulator too
  • Loading branch information
delapuente authored and ajavadia committed Aug 4, 2018
1 parent 6a83d9f commit cf06614
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Removed

Fixed
-----
- Fixed ``get_ran_qasm`` methods on ``Result`` instances (#688)
- Fixed ``probabilities_ket`` computation in C++ simulator. (#580)
- Fixed bug in the definition of ``cswap`` gate and its test (#685).
- Fixed the examples to be compatible with version 0.5+ (#672)
Expand Down
24 changes: 24 additions & 0 deletions qiskit/_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@

"""Module for working with Results."""

import logging
import copy
import numpy
from ._qiskiterror import QISKitError
from ._quantumcircuit import QuantumCircuit


logger = logging.getLogger(__name__)


class Result(object):
""" Result Class.
Expand Down Expand Up @@ -414,3 +418,23 @@ def get_qubitpol_vs_xval(self, nqubits, xvals_dict=None):
circuit_name, z_dicts[qubit_ind])

return qubitpol, xvals


def copy_qasm_from_qobj_into_result(qobj, result):
"""Find the QASMs belonging to the Qobj experiments and copy them
into the corresponding result entries."""
for experiment in qobj.experiments:
name = experiment.header.name
qasm = getattr(experiment.header, 'compiled_circuit_qasm', None)
experiment_result = _find_experiment_result(result, name)
if qasm and experiment_result:
experiment_result['compiled_circuit_qasm'] = qasm


def _find_experiment_result(result, name):
for experiment_result in result['result']:
if experiment_result['name'] == name:
return experiment_result

logger.warning('No result found for experiment %s', name)
return None
3 changes: 2 additions & 1 deletion qiskit/backends/local/qasm_simulator_cpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import numpy as np

from qiskit._result import Result
from qiskit._result import Result, copy_qasm_from_qobj_into_result
from qiskit.backends import BaseBackend
from qiskit.backends.local.localjob import LocalJob
from qiskit.qobj import qobj_to_dict
Expand Down Expand Up @@ -77,6 +77,7 @@ def run(self, qobj):
def _run_job(self, qobj):
self._validate(qobj)
result = run(qobj, self._configuration['exe'])
copy_qasm_from_qobj_into_result(qobj, result)
return Result(result)

def _validate(self, qobj):
Expand Down
4 changes: 3 additions & 1 deletion qiskit/backends/local/qasm_simulator_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@

import numpy as np

from qiskit._result import Result
from qiskit._result import Result, copy_qasm_from_qobj_into_result
from qiskit.backends import BaseBackend
from qiskit.backends.local.localjob import LocalJob
from ._simulatorerror import SimulatorError
Expand Down Expand Up @@ -290,6 +290,8 @@ def _run_job(self, qobj):
'status': 'COMPLETED',
'success': True,
'time_taken': (end - start)}

copy_qasm_from_qobj_into_result(qobj, result)
return Result(result)

def run_circuit(self, circuit):
Expand Down
16 changes: 13 additions & 3 deletions qiskit/backends/local/unitary_simulator_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,11 @@
"""
import logging
import uuid
import time

import numpy as np

from qiskit._result import Result
from qiskit._result import Result, copy_qasm_from_qobj_into_result
from qiskit.backends import BaseBackend
from qiskit.backends.local.localjob import LocalJob
from ._simulatortools import enlarge_single_opt, enlarge_two_opt, single_gate_matrix
Expand Down Expand Up @@ -161,11 +162,20 @@ def _run_job(self, qobj):
Result: Result object
"""
result_list = []
start = time.time()
for circuit in qobj.experiments:
result_list.append(self.run_circuit(circuit))
end = time.time()
job_id = str(uuid.uuid4())
return Result(
{'job_id': job_id, 'result': result_list, 'status': 'COMPLETED'})
result = {'backend': self._configuration['name'],
'id': qobj.qobj_id,
'job_id': job_id,
'result': result_list,
'status': 'COMPLETED',
'sucsess': True,
'time_taken': (end - start)}
copy_qasm_from_qobj_into_result(qobj, result)
return Result(result)

def run_circuit(self, circuit):
"""Apply the single-qubit gate.
Expand Down
34 changes: 33 additions & 1 deletion test/python/test_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,24 @@

import qiskit.wrapper
from qiskit import QISKitError
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
from qiskit.backends.ibmq import IBMQProvider
from qiskit.wrapper import registered_providers
from qiskit.wrapper import registered_providers, execute
from ._mockutils import DummyProvider
from .common import QiskitTestCase, requires_qe_access
from .common import is_cpp_simulator_available
from .test_backends import remove_backends_from_list


class TestWrapper(QiskitTestCase):
"""Wrapper test case."""
def setUp(self):
q = QuantumRegister(3)
c = ClassicalRegister(3)
self.circuit = QuantumCircuit(q, c)
self.circuit.ccx(q[0], q[1], q[2])
self.circuit.measure(q, c)

@requires_qe_access
def test_wrapper_register_ok(self, QE_TOKEN, QE_URL, hub, group, project):
"""Test wrapper.register()."""
Expand Down Expand Up @@ -110,6 +119,29 @@ class SecondDummyProvider(DummyProvider):
self.assertEqual(dummy_backend,
qiskit.wrapper.get_backend('local_dummy_simulator'))

def test_local_execute_and_get_ran_qasm(self):
"""Check if the local backend return the ran qasm."""

cpp_simulators = [
'local_qasm_simulator_cpp',
'local_statevector_simulator_cpp'
]

python_simulators = [
'local_qasm_simulator_py',
'local_statevector_simulator_py',
'local_unitary_simulator_py'
]

local_simulators = python_simulators
if is_cpp_simulator_available():
local_simulators += cpp_simulators

for backend_name in local_simulators:
with self.subTest(backend_name=backend_name):
result = execute(self.circuit, 'local_qasm_simulator').result()
self.assertIsNotNone(result.get_ran_qasm(self.circuit.name))


if __name__ == '__main__':
unittest.main(verbosity=2)

0 comments on commit cf06614

Please sign in to comment.