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

Serializable Qobj + Pass cpp/projectq simulator tests #397

Merged
merged 4 commits into from
Apr 16, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 4 additions & 3 deletions qiskit/_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def execute(list_of_circuits, backend, compile_config=None,
qobj = compile(list_of_circuits, backend, compile_config)

# XXX When qobj is done this should replace q_job
q_job = QuantumJob(qobj, preformatted=True, resources={
q_job = QuantumJob(qobj, backend=backend, preformatted=True, resources={
'max_credits': qobj['config']['max_credits'], 'wait': wait, 'timeout': timeout})
result = backend.run(q_job)
return result
Expand Down Expand Up @@ -125,8 +125,9 @@ def compile(list_of_circuits, backend, compile_config=None):
qobj_id = "".join([random.choice(string.ascii_letters + string.digits)
for n in range(30)])
qobj['id'] = qobj_id
qobj["config"] = {"max_credits": max_credits, 'backend': backend,
"shots": shots}
qobj['config'] = {'max_credits': max_credits,
'shots': shots,
'backend_name': backend_name}

# TODO This backend needs HPC parameters to be passed in order to work
if backend_name == 'ibmqx_hpc_qasm_simulator':
Expand Down
10 changes: 10 additions & 0 deletions qiskit/_jobprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from concurrent import futures
from threading import Lock

from ._qiskiterror import QISKitError
from ._compiler import compile_circuit
from ._result import Result

Expand All @@ -35,9 +36,18 @@ def run_backend(q_job):

Returns:
Result: Result object.

Raises:
QISKitError: if the backend is malformed
"""
backend = q_job.backend
qobj = q_job.qobj
backend_name = qobj['config']['backend_name']
if not backend:
raise QISKitError("No backend instance to run on.")
if backend_name != backend.configuration['name']:
raise QISKitError('non-matching backends specified in Qobj '
'object and json')
if backend.configuration.get('local'): # remove condition when api gets qobj
for circuit in qobj['circuits']:
if circuit['compiled_circuit'] is None:
Expand Down
14 changes: 5 additions & 9 deletions qiskit/_quantumjob.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import string

# stable modules
from qiskit import QISKitError
from ._quantumcircuit import QuantumCircuit
from .qasm import Qasm

Expand All @@ -35,7 +34,7 @@ class QuantumJob():

# TODO We need to create more tests for checking all possible inputs.
# TODO Make this interface clearer -- circuits could be many things!
def __init__(self, circuits, backend=None,
def __init__(self, circuits, backend,
circuit_config=None, seed=None,
resources=None,
shots=1024, names=None,
Expand All @@ -45,8 +44,7 @@ def __init__(self, circuits, backend=None,
circuits (QuantumCircuit|DagCircuit | list(QuantumCircuit|DagCircuit)):
QuantumCircuit|DagCircuit or list of QuantumCircuit|DagCircuit.
If preformatted=True, this is a raw qobj.
backend (BaseBackend): The backend to run the circuit on, required
if preformatted=False.
backend (BaseBackend): The backend to run the circuit on, required.
circuit_config (dict): Circuit configuration.
seed (int): The intial seed the simulatros use.
resources (dict): Resource requirements of job.
Expand Down Expand Up @@ -82,12 +80,10 @@ def __init__(self, circuits, backend=None,
# circuits is actually a qobj...validate (not ideal but convenient)
self.qobj = circuits
else:
if not backend:
raise QISKitError('backend needs to be specified if '
'preformatted==True.')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also remove the

         Raises:
             QISKitError: if preformatted==True but no backend is specified.

bit from the docstring, for consistency?

self.qobj = self._create_qobj(circuits, circuit_config, backend,
seed, resources, shots, do_compile)
self.backend = self.qobj['config']['backend']
self.backend_name = self.qobj['config']['backend_name']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.backend_name is not being used elsewhere, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep. removed.

self.backend = backend
self.resources = resources
self.seed = seed
self.result = None
Expand Down Expand Up @@ -146,7 +142,7 @@ def _create_qobj(self, circuits, circuit_config, backend, seed,
'config': {
'max_credits': resources['max_credits'],
'shots': shots,
'backend': backend
'backend_name': backend.configuration['name']
},
'circuits': circuit_records}

Expand Down
5 changes: 3 additions & 2 deletions qiskit/_quantumprogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -1047,7 +1047,7 @@ def get_execution_list(self, qobj, print_func=print):
execution_list = []

print_func("id: %s" % qobj['id'])
print_func("backend: %s" % qobj['config']['backend'])
print_func("backend: %s" % qobj['config']['backend_name'])
print_func("qobj config:")
for key in qobj['config']:
if key != 'backend':
Expand Down Expand Up @@ -1229,7 +1229,8 @@ def run_batch_async(self, qobj_list, wait=5, timeout=120, callback=None):
def _run_internal(self, qobj_list, wait=5, timeout=60, callback=None):
q_job_list = []
for qobj in qobj_list:
q_job = QuantumJob(qobj, preformatted=True, resources={
backend = qiskit.wrapper.get_backend(qobj['config']['backend_name'])
q_job = QuantumJob(qobj, backend=backend, preformatted=True, resources={
'max_credits': qobj['config']['max_credits'], 'wait': wait,
'timeout': timeout})
q_job_list.append(q_job)
Expand Down
4 changes: 2 additions & 2 deletions qiskit/_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ def __iadd__(self, other):
# sessions)
my_config = copy.deepcopy(self._qobj['config'])
other_config = copy.deepcopy(other._qobj['config'])
my_backend = my_config.pop('backend').configuration['name']
other_backend = other_config.pop('backend').configuration['name']
my_backend = my_config.pop('backend_name')
other_backend = other_config.pop('backend_name')

if my_config == other_config and my_backend == other_backend:
if isinstance(self._qobj['id'], str):
Expand Down
10 changes: 5 additions & 5 deletions qiskit/backends/ibmq/ibmqbackend.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def run(self, q_job):

seed0 = qobj['circuits'][0]['config']['seed']
hpc = None
if (qobj['config']['backend'] == 'ibmqx_hpc_qasm_simulator' and
if (qobj['config']['backend_name'] == 'ibmqx_hpc_qasm_simulator' and
'hpc' in qobj['config']):
try:
# Use CamelCase when passing the hpc parameters to the API.
Expand All @@ -103,7 +103,7 @@ def run(self, q_job):

# TODO: this should be self._configuration['name'] - need to check that
# it is always the case.
backend_name = qobj['config']['backend'].configuration['name']
backend_name = qobj['config']['backend_name']
output = self._api.run_job(api_jobs, backend_name,
shots=qobj['config']['shots'],
max_credits=qobj['config']['max_credits'],
Expand All @@ -113,13 +113,13 @@ def run(self, q_job):
raise ResultError(output['error'])

logger.info('Running qobj: %s on remote backend %s with job id: %s',
qobj["id"], qobj['config']['backend'], output['id'])
qobj["id"], qobj['config']['backend_name'], output['id'])
job_result = _wait_for_job(output['id'], self._api, wait=wait,
timeout=timeout)
logger.info('Got a result for qobj: %s from remote backend %s with job id: %s',
qobj["id"], qobj['config']['backend'], output['id'])
qobj["id"], qobj['config']['backend_name'], output['id'])
job_result['name'] = qobj['id']
job_result['backend'] = qobj['config']['backend']
job_result['backend'] = qobj['config']['backend_name']
this_result = Result(job_result, qobj)
return this_result

Expand Down
2 changes: 1 addition & 1 deletion test/performance/vqe.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def vqe(molecule='H2', depth=6, max_trials=200, shots=1):
if shots != 1:
H = group_paulis(pauli_list)

entangler_map = qp.configuration(device)['coupling_map']
entangler_map = qp.get_backend_configuration(device)['coupling_map']

if entangler_map == 'all-to-all':
entangler_map = {i: [j for j in range(n_qubits) if j != i] for i in range(n_qubits)}
Expand Down
2 changes: 1 addition & 1 deletion test/python/qobj/cpp_conditionals.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"config": {
"shots": 1,
"seed": 1,
"backend": "local_qasm_simulator_cpp"
"backend_name": "local_qiskit_simulator"
},
"circuits": [
{
Expand Down
2 changes: 1 addition & 1 deletion test/python/qobj/cpp_measure_opt.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"config": {
"shots": 2000,
"seed": 1,
"backend": "local_qasm_simulator_cpp"
"backend_name": "local_qiskit_simulator"
},
"circuits": [
{
Expand Down
2 changes: 1 addition & 1 deletion test/python/qobj/cpp_reset.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"config": {
"shots": 1,
"seed": 1,
"backend": "local_qasm_simulator_cpp"
"backend_name": "local_qiskit_simulator"
},
"circuits": [
{
Expand Down
2 changes: 1 addition & 1 deletion test/python/qobj/cpp_save_load.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"config": {
"shots": 1,
"seed": 1,
"backend": "local_qasm_simulator_cpp"
"backend_name": "local_qiskit_simulator"
},
"circuits": [
{
Expand Down
2 changes: 1 addition & 1 deletion test/python/qobj/cpp_single_qubit_gates.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"config": {
"shots": 1,
"seed": 1,
"backend": "local_qasm_simulator_cpp"
"backend_name": "local_qiskit_simulator"
},
"circuits": [
{
Expand Down
2 changes: 1 addition & 1 deletion test/python/qobj/cpp_two_qubit_gates.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"config": {
"shots": 1,
"seed": 1,
"backend": "local_qasm_simulator_cpp"
"backend_name": "local_qiskit_simulator"
},
"circuits": [
{
Expand Down
Loading