Skip to content

Commit

Permalink
Merge pull request #84 from ewinston/abc-backend
Browse files Browse the repository at this point in the history
introduce abstract base class for backends
  • Loading branch information
diego-plan9 authored Oct 4, 2017
2 parents 7d9c2e0 + 1dfc7de commit 949fe46
Show file tree
Hide file tree
Showing 25 changed files with 495 additions and 372 deletions.
8 changes: 4 additions & 4 deletions doc/_templates/better-apidoc/package.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@
{%- endif -%}
{%- endfor -%}

{# Bypass the automatic discovery of simulators at qiskit.simulators #}
{%- if fullname == 'qiskit.simulators' -%}
{%- set imported_modules = ['_localsimulator', '_qasm_cpp_simulator',
{# Bypass the automatic discovery of simulators at qiskit.backends #}
{%- if fullname == 'qiskit.backends' -%}
{%- set imported_modules = ['_qasm_cpp_simulator',
'_qasmsimulator', '_unitarysimulator'] -%}
{%- set imported_classes = [] -%}
{%- set imported_classes = ['BaseBackend'] -%}
{%- endif -%}

{% if imported_modules %}
Expand Down
2 changes: 1 addition & 1 deletion doc/dev_introduction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ The directory also contains internal modules that are still under development:
graphs
- a :py:mod:`mapper <qiskit.mapper>` module for mapping all-to-all circuits to
run on devices with fixed couplings
- a :py:mod:`simulators <qiskit.simulators>` module contains quantum circuit
- a :py:mod:`backends <qiskit.backends>` module contains quantum circuit
simulators
- a *tools* directory contains methods for applications, analysis, and visualization

Expand Down
2 changes: 0 additions & 2 deletions qiskit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,4 @@
from ._quantumprogram import QuantumProgram
from ._result import Result


__version__ = '0.4.0'

25 changes: 9 additions & 16 deletions qiskit/_jobprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@
import sys
import time

from IBMQuantumExperience import IBMQuantumExperience

import qiskit.backends as backends
from qiskit._result import Result
from qiskit._resulterror import ResultError
# Stable Modules
from qiskit import QISKitError
# Local Simulator Modules
from qiskit import simulators
# compiler module
from qiskit import _openquantumcompiler as openquantumcompiler
from IBMQuantumExperience.IBMQuantumExperience import (IBMQuantumExperience,
ApiError)

def run_local_simulator(qobj):
def run_local_backend(qobj):
"""Run a program of compiled quantum circuits on the local machine.
Args:
Expand All @@ -40,9 +37,9 @@ def run_local_simulator(qobj):
compiled_circuit = openquantumcompiler.compile(circuit['circuit'],
format='json')
circuit['compiled_circuit'] = compiled_circuit
local_simulator = simulators.LocalSimulator(qobj)
local_simulator.run()
return local_simulator.result()
BackendClass = backends.get_backend_class(qobj['config']['backend'])
backend = BackendClass(qobj)
return backend.run()

def run_remote_backend(qobj, api, wait=5, timeout=60, silent=True):
"""
Expand Down Expand Up @@ -124,10 +121,6 @@ def _wait_for_job(jobid, api, wait=5, timeout=60, silent=True):
'status': job_result['qasms'][index]['status']})
return {'status': job_result['status'], 'result': job_result_return}

def local_backends():
"""Get the local backends."""
return simulators._localsimulator.local_backends()

def remote_backends(api):
"""Get the remote backends.
Expand Down Expand Up @@ -160,7 +153,7 @@ def __init__(self, q_jobs, callback, max_workers=1, token=None, url=None, api=No
self.q_jobs = q_jobs
self.max_workers = max_workers
# check whether any jobs are remote
self._local_backends = local_backends()
self._local_backends = backends.local_backends()
self.online = any(qj.backend not in self._local_backends for qj in q_jobs)
self.futures = {}
self.lock = Lock()
Expand Down Expand Up @@ -224,7 +217,7 @@ def submit(self, wait=5, timeout=120, silent=True):
executor = self.executor_class(max_workers=self.max_workers)
for q_job in self.q_jobs:
if q_job.backend in self._local_backends:
future = executor.submit(run_local_simulator,
future = executor.submit(run_local_backend,
q_job.qobj)
elif self.online and q_job.backend in self._online_backends:
future = executor.submit(run_remote_backend,
Expand Down
1 change: 0 additions & 1 deletion qiskit/_openquantumcompiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import qiskit.mapper as mapper
from qiskit._qiskiterror import QISKitError


def compile(qasm_circuit, basis_gates='u1,u2,u3,cx,id', coupling_map=None,
initial_layout=None, silent=True, get_layout=False, format='dag'):
"""Compile the circuit.
Expand Down
15 changes: 7 additions & 8 deletions qiskit/_quantumjob.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import random
import string

from qiskit import _openquantumcompiler as openquantumcompiler
from qiskit.simulators import _localsimulator as localsimulators
import qiskit.backends as backends

class QuantumJob():
"""Creates a quantum circuit job"""
Expand Down Expand Up @@ -73,15 +72,15 @@ def __init__(self, circuits, backend='local_qasm_simulator',
if preformatted:
self.qobj = circuits
else:
self.qobj = self._create_qobj(circuits, circuit_config, backend,
self.qobj = self._create_qobj(circuits, circuit_config, backend,
seed, resources, shots, do_compile)

self.backend = self.qobj['config']['backend']
self.resources = {'max_credits': self.qobj['config']['max_credits']}
self.seed = seed
self.result = None
def _create_qobj(self, circuits, circuit_config, backend, seed,

def _create_qobj(self, circuits, circuit_config, backend, seed,
resources, shots, do_compile):
# local and remote backends currently need different
# compilied circuit formats
Expand All @@ -90,7 +89,7 @@ def _create_qobj(self, circuits, circuit_config, backend, seed,
for circuit in circuits:
formatted_circuits.append(None)
else:
if backend in localsimulators.local_backends():
if backend in backends.local_backends():
for circuit in self.circuits:
formatted_circuits.append(openquantumcompiler.dag2json(circuit))
else:
Expand All @@ -105,7 +104,7 @@ def _create_qobj(self, circuits, circuit_config, backend, seed,
'layout': None,
'seed': seed}
circuit_config = [config] * len(self.circuits)

for circuit, fcircuit, name, config in zip(self.circuits,
formatted_circuits,
self.names,
Expand All @@ -126,7 +125,7 @@ def _create_qobj(self, circuits, circuit_config, backend, seed,
'backend': backend
},
'circuits': circuit_records}

def _generate_job_id(self, length=10):
return ''.join([random.choice(
string.ascii_letters + string.digits) for i in range(length)])
15 changes: 4 additions & 11 deletions qiskit/_quantumprogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
from . import mapper

# Local Simulator Modules
from . import simulators
import qiskit.backends

from . import _openquantumcompiler as openquantumcompiler

Expand Down Expand Up @@ -99,7 +99,7 @@ def __init__(self, specs=None):
self.__init_circuit = None # stores the intial quantum circuit of the
# program
self.__ONLINE_BACKENDS = []
self.__LOCAL_BACKENDS = self.local_backends()
self.__LOCAL_BACKENDS = qiskit.backends.local_backends()
self.mapper = mapper
if specs:
self.__init_specs(specs)
Expand Down Expand Up @@ -540,10 +540,6 @@ def available_backends(self):
"""All the backends that are seen by QISKIT."""
return self.__ONLINE_BACKENDS + self.__LOCAL_BACKENDS

def local_backends(self):
"""Get the local backends."""
return simulators._localsimulator.local_backends()

def online_backends(self):
"""Get the online backends.
Expand Down Expand Up @@ -660,11 +656,8 @@ def get_backend_configuration(self, backend, list_format=False):
cmap = configuration[key]
configuration_edit[new_key] = cmap
return configuration_edit
for configuration in simulators.local_configuration:
if configuration['name'] == backend:
return configuration
raise LookupError(
'backend configuration for "{0}" not found'.format(backend))
else:
return qiskit.backends.get_backend_configuration(backend)

def get_backend_calibration(self, backend):
"""Return the online backend calibrations.
Expand Down
8 changes: 8 additions & 0 deletions qiskit/_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ def circuit_statuses(self):
return [circuit_result['status']
for circuit_result in self.__result['result']]

def get_circuit_status(self, icircuit):
"""Return the status of circuit at index icircuit.
Args:
icircuit (int): index of circuit
"""
return self.__result['result'][icircuit]['status']

def get_ran_qasm(self, name):
"""Get the ran qasm for the named circuit and backend.
Expand Down
6 changes: 6 additions & 0 deletions qiskit/backends/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from ._basebackend import BaseBackend
from ._backendutils import (get_backend_class,
get_backend_configuration,
local_backends,
register_backend,
remote_backends)
Loading

0 comments on commit 949fe46

Please sign in to comment.