diff --git a/qiskit/_compiler.py b/qiskit/_compiler.py index f6eb544af671..b01fa997b39e 100644 --- a/qiskit/_compiler.py +++ b/qiskit/_compiler.py @@ -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 @@ -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': diff --git a/qiskit/_jobprocessor.py b/qiskit/_jobprocessor.py index 996a0ad549ee..4634d812e548 100644 --- a/qiskit/_jobprocessor.py +++ b/qiskit/_jobprocessor.py @@ -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 @@ -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: diff --git a/qiskit/_quantumjob.py b/qiskit/_quantumjob.py index fae9cfff03b9..1136091513bf 100644 --- a/qiskit/_quantumjob.py +++ b/qiskit/_quantumjob.py @@ -21,7 +21,6 @@ import string # stable modules -from qiskit import QISKitError from ._quantumcircuit import QuantumCircuit from .qasm import Qasm @@ -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, @@ -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. @@ -82,12 +80,9 @@ 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.') self.qobj = self._create_qobj(circuits, circuit_config, backend, seed, resources, shots, do_compile) - self.backend = self.qobj['config']['backend'] + self.backend = backend self.resources = resources self.seed = seed self.result = None @@ -146,7 +141,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} diff --git a/qiskit/_quantumprogram.py b/qiskit/_quantumprogram.py index e33006563466..aaf586f01097 100644 --- a/qiskit/_quantumprogram.py +++ b/qiskit/_quantumprogram.py @@ -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': @@ -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) diff --git a/qiskit/_result.py b/qiskit/_result.py index ced30eeae4b6..46bdba154d5c 100644 --- a/qiskit/_result.py +++ b/qiskit/_result.py @@ -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): diff --git a/qiskit/backends/ibmq/ibmqbackend.py b/qiskit/backends/ibmq/ibmqbackend.py index 3ac19cbaebc1..c403951fc608 100644 --- a/qiskit/backends/ibmq/ibmqbackend.py +++ b/qiskit/backends/ibmq/ibmqbackend.py @@ -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. @@ -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'], @@ -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 diff --git a/test/performance/vqe.py b/test/performance/vqe.py index e2b10e26ae20..aa97518b00cd 100644 --- a/test/performance/vqe.py +++ b/test/performance/vqe.py @@ -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)} diff --git a/test/python/qobj/cpp_conditionals.json b/test/python/qobj/cpp_conditionals.json index b7036ca80b8b..0da6e18cbd59 100644 --- a/test/python/qobj/cpp_conditionals.json +++ b/test/python/qobj/cpp_conditionals.json @@ -3,7 +3,7 @@ "config": { "shots": 1, "seed": 1, - "backend": "local_qasm_simulator_cpp" + "backend_name": "local_qiskit_simulator" }, "circuits": [ { diff --git a/test/python/qobj/cpp_measure_opt.json b/test/python/qobj/cpp_measure_opt.json index e0548154b020..a77a2c9f2c00 100644 --- a/test/python/qobj/cpp_measure_opt.json +++ b/test/python/qobj/cpp_measure_opt.json @@ -3,7 +3,7 @@ "config": { "shots": 2000, "seed": 1, - "backend": "local_qasm_simulator_cpp" + "backend_name": "local_qiskit_simulator" }, "circuits": [ { diff --git a/test/python/qobj/cpp_reset.json b/test/python/qobj/cpp_reset.json index 5913c4af0da9..7b7c7c0d03ff 100644 --- a/test/python/qobj/cpp_reset.json +++ b/test/python/qobj/cpp_reset.json @@ -3,7 +3,7 @@ "config": { "shots": 1, "seed": 1, - "backend": "local_qasm_simulator_cpp" + "backend_name": "local_qiskit_simulator" }, "circuits": [ { diff --git a/test/python/qobj/cpp_save_load.json b/test/python/qobj/cpp_save_load.json index a8d4903c03f4..5e6a3231d474 100644 --- a/test/python/qobj/cpp_save_load.json +++ b/test/python/qobj/cpp_save_load.json @@ -3,7 +3,7 @@ "config": { "shots": 1, "seed": 1, - "backend": "local_qasm_simulator_cpp" + "backend_name": "local_qiskit_simulator" }, "circuits": [ { diff --git a/test/python/qobj/cpp_single_qubit_gates.json b/test/python/qobj/cpp_single_qubit_gates.json index a0dd17202d0a..5b36fbe0d00b 100644 --- a/test/python/qobj/cpp_single_qubit_gates.json +++ b/test/python/qobj/cpp_single_qubit_gates.json @@ -3,7 +3,7 @@ "config": { "shots": 1, "seed": 1, - "backend": "local_qasm_simulator_cpp" + "backend_name": "local_qiskit_simulator" }, "circuits": [ { diff --git a/test/python/qobj/cpp_two_qubit_gates.json b/test/python/qobj/cpp_two_qubit_gates.json index 835ea9dc4ec2..2a552aea3d00 100644 --- a/test/python/qobj/cpp_two_qubit_gates.json +++ b/test/python/qobj/cpp_two_qubit_gates.json @@ -3,7 +3,7 @@ "config": { "shots": 1, "seed": 1, - "backend": "local_qasm_simulator_cpp" + "backend_name": "local_qiskit_simulator" }, "circuits": [ { diff --git a/test/python/test_compiler.py b/test/python/test_compiler.py index 3bef8e2aabfd..073882ae219d 100644 --- a/test/python/test_compiler.py +++ b/test/python/test_compiler.py @@ -43,7 +43,7 @@ def test_compile(self): If all correct some should exists. """ - my_backend = QasmSimulator() + backend = QasmSimulator() qubit_reg = qiskit.QuantumRegister(2, name='q') clbit_reg = qiskit.ClassicalRegister(2, name='c') @@ -52,7 +52,7 @@ def test_compile(self): qc.cx(qubit_reg[0], qubit_reg[1]) qc.measure(qubit_reg, clbit_reg) - qobj = qiskit._compiler.compile(qc, my_backend) + qobj = qiskit._compiler.compile(qc, backend) # FIXME should test against the qobj when defined self.assertEqual(len(qobj), 3) @@ -62,7 +62,7 @@ def test_compile_two(self): If all correct some should exists. """ - my_backend = QasmSimulator() + backend = QasmSimulator() qubit_reg = qiskit.QuantumRegister(2, name='q') clbit_reg = qiskit.ClassicalRegister(2, name='c') @@ -72,7 +72,7 @@ def test_compile_two(self): qc.measure(qubit_reg, clbit_reg) qc_extra = qiskit.QuantumCircuit(qubit_reg, clbit_reg, name="extra") qc_extra.measure(qubit_reg, clbit_reg) - qobj = qiskit._compiler.compile([qc, qc_extra], my_backend) + qobj = qiskit._compiler.compile([qc, qc_extra], backend) # FIXME should test against the qobj when defined self.assertEqual(len(qobj), 3) @@ -82,7 +82,7 @@ def test_compile_run(self): If all correct some should exists. """ - my_backend = QasmSimulator() + backend = QasmSimulator() qubit_reg = qiskit.QuantumRegister(2, name='q') clbit_reg = qiskit.ClassicalRegister(2, name='c') @@ -91,8 +91,8 @@ def test_compile_run(self): qc.cx(qubit_reg[0], qubit_reg[1]) qc.measure(qubit_reg, clbit_reg) - qobj = qiskit._compiler.compile(qc, my_backend) - result = my_backend.run(qiskit.QuantumJob(qobj, preformatted=True)) + qobj = qiskit._compiler.compile(qc, backend) + result = backend.run(qiskit.QuantumJob(qobj, backend=backend, preformatted=True)) self.assertIsInstance(result, Result) def test_compile_two_run(self): @@ -100,7 +100,7 @@ def test_compile_two_run(self): If all correct some should exists. """ - my_backend = QasmSimulator() + backend = QasmSimulator() qubit_reg = qiskit.QuantumRegister(2, name='q') clbit_reg = qiskit.ClassicalRegister(2, name='c') @@ -110,8 +110,8 @@ def test_compile_two_run(self): qc.measure(qubit_reg, clbit_reg) qc_extra = qiskit.QuantumCircuit(qubit_reg, clbit_reg, name="extra") qc_extra.measure(qubit_reg, clbit_reg) - qobj = qiskit._compiler.compile([qc, qc_extra], my_backend) - result = my_backend.run(qiskit.QuantumJob(qobj, preformatted=True)) + qobj = qiskit._compiler.compile([qc, qc_extra], backend) + result = backend.run(qiskit.QuantumJob(qobj, backend=backend, preformatted=True)) self.assertIsInstance(result, Result) def test_execute(self): @@ -119,7 +119,7 @@ def test_execute(self): If all correct some should exists. """ - my_backend = QasmSimulator() + backend = QasmSimulator() qubit_reg = qiskit.QuantumRegister(2) clbit_reg = qiskit.ClassicalRegister(2) @@ -128,7 +128,7 @@ def test_execute(self): qc.cx(qubit_reg[0], qubit_reg[1]) qc.measure(qubit_reg, clbit_reg) - results = qiskit._compiler.execute(qc, my_backend) + results = qiskit._compiler.execute(qc, backend) self.assertIsInstance(results, Result) @@ -137,7 +137,7 @@ def test_execute_two(self): If all correct some should exists. """ - my_backend = QasmSimulator() + backend = QasmSimulator() qubit_reg = qiskit.QuantumRegister(2) clbit_reg = qiskit.ClassicalRegister(2) @@ -147,7 +147,7 @@ def test_execute_two(self): qc.measure(qubit_reg, clbit_reg) qc_extra = qiskit.QuantumCircuit(qubit_reg, clbit_reg) qc_extra.measure(qubit_reg, clbit_reg) - results = qiskit._compiler.execute([qc, qc_extra], my_backend) + results = qiskit._compiler.execute([qc, qc_extra], backend) self.assertIsInstance(results, Result) @@ -158,7 +158,7 @@ def test_compile_remote(self, QE_TOKEN, QE_URL): If all correct some should exists. """ provider = IBMQProvider(QE_TOKEN, QE_URL) - my_backend = lowest_pending_jobs( + backend = lowest_pending_jobs( provider.available_backends({'local': False, 'simulator': False})) qubit_reg = qiskit.QuantumRegister(2, name='q') @@ -168,7 +168,7 @@ def test_compile_remote(self, QE_TOKEN, QE_URL): qc.cx(qubit_reg[0], qubit_reg[1]) qc.measure(qubit_reg, clbit_reg) - qobj = qiskit._compiler.compile(qc, my_backend) + qobj = qiskit._compiler.compile(qc, backend) # FIXME should test against the qobj when defined self.assertEqual(len(qobj), 3) @@ -180,7 +180,7 @@ def test_compile_two_remote(self, QE_TOKEN, QE_URL): If all correct some should exists. """ provider = IBMQProvider(QE_TOKEN, QE_URL) - my_backend = lowest_pending_jobs( + backend = lowest_pending_jobs( provider.available_backends({'local': False, 'simulator': False})) qubit_reg = qiskit.QuantumRegister(2, name='q') @@ -191,7 +191,7 @@ def test_compile_two_remote(self, QE_TOKEN, QE_URL): qc.measure(qubit_reg, clbit_reg) qc_extra = qiskit.QuantumCircuit(qubit_reg, clbit_reg, name="extra") qc_extra.measure(qubit_reg, clbit_reg) - qobj = qiskit._compiler.compile([qc, qc_extra], my_backend) + qobj = qiskit._compiler.compile([qc, qc_extra], backend) # FIXME should test against the qobj when defined self.assertEqual(len(qobj), 3) @@ -203,7 +203,7 @@ def test_compile_run_remote(self, QE_TOKEN, QE_URL): If all correct some should exists. """ provider = IBMQProvider(QE_TOKEN, QE_URL) - my_backend = provider.get_backend('ibmqx_qasm_simulator') + backend = provider.get_backend('ibmqx_qasm_simulator') qubit_reg = qiskit.QuantumRegister(2, name='q') clbit_reg = qiskit.ClassicalRegister(2, name='c') qc = qiskit.QuantumCircuit(qubit_reg, clbit_reg, name="bell") @@ -211,8 +211,8 @@ def test_compile_run_remote(self, QE_TOKEN, QE_URL): qc.cx(qubit_reg[0], qubit_reg[1]) qc.measure(qubit_reg, clbit_reg) - qobj = qiskit._compiler.compile(qc, my_backend) - result = my_backend.run(qiskit.QuantumJob(qobj, preformatted=True)) + qobj = qiskit._compiler.compile(qc, backend) + result = backend.run(qiskit.QuantumJob(qobj, backend=backend, preformatted=True)) self.assertIsInstance(result, Result) @requires_qe_access @@ -222,7 +222,7 @@ def test_compile_two_run_remote(self, QE_TOKEN, QE_URL): If all correct some should exists. """ provider = IBMQProvider(QE_TOKEN, QE_URL) - my_backend = provider.get_backend('ibmqx_qasm_simulator') + backend = provider.get_backend('ibmqx_qasm_simulator') qubit_reg = qiskit.QuantumRegister(2, name='q') clbit_reg = qiskit.ClassicalRegister(2, name='c') qc = qiskit.QuantumCircuit(qubit_reg, clbit_reg, name="bell") @@ -231,8 +231,8 @@ def test_compile_two_run_remote(self, QE_TOKEN, QE_URL): qc.measure(qubit_reg, clbit_reg) qc_extra = qiskit.QuantumCircuit(qubit_reg, clbit_reg, name="extra") qc_extra.measure(qubit_reg, clbit_reg) - qobj = qiskit._compiler.compile([qc, qc_extra], my_backend) - result = my_backend.run(qiskit.QuantumJob(qobj, preformatted=True)) + qobj = qiskit._compiler.compile([qc, qc_extra], backend) + result = backend.run(qiskit.QuantumJob(qobj, backend=backend, preformatted=True)) self.assertIsInstance(result, Result) @requires_qe_access @@ -242,7 +242,7 @@ def test_execute_remote(self, QE_TOKEN, QE_URL): If all correct some should exists. """ provider = IBMQProvider(QE_TOKEN, QE_URL) - my_backend = provider.get_backend('ibmqx_qasm_simulator') + backend = provider.get_backend('ibmqx_qasm_simulator') qubit_reg = qiskit.QuantumRegister(2) clbit_reg = qiskit.ClassicalRegister(2) @@ -251,7 +251,7 @@ def test_execute_remote(self, QE_TOKEN, QE_URL): qc.cx(qubit_reg[0], qubit_reg[1]) qc.measure(qubit_reg, clbit_reg) - results = qiskit._compiler.execute(qc, my_backend) + results = qiskit._compiler.execute(qc, backend) self.assertIsInstance(results, Result) @requires_qe_access @@ -261,7 +261,7 @@ def test_execute_two_remote(self, QE_TOKEN, QE_URL): If all correct some should exists. """ provider = IBMQProvider(QE_TOKEN, QE_URL) - my_backend = provider.get_backend('ibmqx_qasm_simulator') + backend = provider.get_backend('ibmqx_qasm_simulator') qubit_reg = qiskit.QuantumRegister(2) clbit_reg = qiskit.ClassicalRegister(2) @@ -271,7 +271,7 @@ def test_execute_two_remote(self, QE_TOKEN, QE_URL): qc.measure(qubit_reg, clbit_reg) qc_extra = qiskit.QuantumCircuit(qubit_reg, clbit_reg) qc_extra.measure(qubit_reg, clbit_reg) - results = qiskit._compiler.execute([qc, qc_extra], my_backend) + results = qiskit._compiler.execute([qc, qc_extra], backend) self.assertIsInstance(results, Result) diff --git a/test/python/test_job_processor.py b/test/python/test_job_processor.py index da4adf3a871c..750b04ef95a1 100644 --- a/test/python/test_job_processor.py +++ b/test/python/test_job_processor.py @@ -116,14 +116,14 @@ def test_init_quantum_job_qobj(self): 'max_credits': 3, 'shots': 1024, 'seed': None, - 'backend': backend}, + 'backend_name': backend.configuration['name']}, 'circuits': [ {'name': 'example', 'compiled_circuit': formatted_circuit, 'layout': None, 'seed': None} ]} - _ = QuantumJob(qobj, preformatted=True) + _ = QuantumJob(qobj, backend=backend, preformatted=True) def test_init_job_processor(self): njobs = 5 diff --git a/test/python/test_local_qasm_simulator_cpp.py b/test/python/test_local_qasm_simulator_cpp.py index 1dbc6efc0287..c1084c614860 100644 --- a/test/python/test_local_qasm_simulator_cpp.py +++ b/test/python/test_local_qasm_simulator_cpp.py @@ -61,7 +61,7 @@ def setUp(self): 'config': { 'max_credits': 3, 'shots': 2000, - 'backend': 'local_qiskit_simulator', + 'backend_name': 'local_qiskit_simulator', 'seed': 1111 }, 'circuits': [ @@ -78,9 +78,6 @@ def setUp(self): 'layout': None, } ]} - self.q_job = QuantumJob(self.qobj, - backend='local_qiskit_simulator', - preformatted=True) # Simulator backend try: self.backend = QasmSimulatorCpp() @@ -88,6 +85,10 @@ def setUp(self): raise unittest.SkipTest( 'cannot find {} in path'.format(fnferr)) + self.q_job = QuantumJob(self.qobj, + backend=self.backend, + preformatted=True) + def test_x90_coherent_error_matrix(self): X90 = np.array([[1, -1j], [-1j, 1]]) / np.sqrt(2) U = x90_error_matrix(0., 0.).dot(X90) @@ -138,7 +139,7 @@ def test_qobj_measure_opt(self): filename = self._get_resource_path('qobj/cpp_measure_opt.json') with open(filename, 'r') as file: q_job = QuantumJob(json.load(file), - backend='local_qasm_simulator_cpp', + backend=self.backend, preformatted=True) result = self.backend.run(q_job) shots = q_job.qobj['config']['shots'] @@ -217,7 +218,7 @@ def test_qobj_reset(self): filename = self._get_resource_path('qobj/cpp_reset.json') with open(filename, 'r') as file: q_job = QuantumJob(json.load(file), - backend='local_qasm_simulator_cpp', + backend=self.backend, preformatted=True) result = self.backend.run(q_job) expected_data = { @@ -243,7 +244,7 @@ def test_qobj_save_load(self): filename = self._get_resource_path('qobj/cpp_save_load.json') with open(filename, 'r') as file: q_job = QuantumJob(json.load(file), - backend='local_qasm_simulator_cpp', + backend=self.backend, preformatted=True) result = self.backend.run(q_job) @@ -271,7 +272,7 @@ def test_qobj_single_qubit_gates(self): filename = self._get_resource_path('qobj/cpp_single_qubit_gates.json') with open(filename, 'r') as file: q_job = QuantumJob(json.load(file), - backend='local_qasm_simulator_cpp', + backend=self.backend, preformatted=True) result = self.backend.run(q_job) expected_data = { @@ -364,7 +365,7 @@ def test_qobj_two_qubit_gates(self): filename = self._get_resource_path('qobj/cpp_two_qubit_gates.json') with open(filename, 'r') as file: q_job = QuantumJob(json.load(file), - backend='local_qasm_simulator_cpp', + backend=self.backend, preformatted=True) result = self.backend.run(q_job) expected_data = { @@ -423,7 +424,7 @@ def test_conditionals(self): filename = self._get_resource_path('qobj/cpp_conditionals.json') with open(filename, 'r') as file: q_job = QuantumJob(json.load(file), - backend='local_qasm_simulator_cpp', + backend=self.backend, preformatted=True) result = self.backend.run(q_job) expected_data = { diff --git a/test/python/test_projectq_cpp_simulator.py b/test/python/test_projectq_cpp_simulator.py index 94ea0317534c..c264d6f9dac5 100644 --- a/test/python/test_projectq_cpp_simulator.py +++ b/test/python/test_projectq_cpp_simulator.py @@ -27,6 +27,9 @@ import qiskit.backends.local.qasmsimulator as qasm_simulator from qiskit import QuantumJob from qiskit import QuantumProgram +from qiskit import QuantumCircuit +from qiskit import QuantumRegister +from qiskit import ClassicalRegister from qiskit._compiler import compile_circuit from ._random_circuit_generator import RandomCircuitGenerator from .common import QiskitTestCase @@ -71,15 +74,16 @@ def setUpClass(cls): def test_gate_x(self): shots = 100 qp = QuantumProgram() - qr = qp.create_quantum_register("qr", 1) - cr = qp.create_classical_register("cr", 1) - qc = qp.create_circuit("circuitName", [qr], [cr]) + qr = QuantumRegister(1, "qr") + cr = ClassicalRegister(1, "cr") + qc = QuantumCircuit(qr, cr) qc.x(qr[0]) qc.measure(qr, cr) - result_pq = qp.execute(['circuitName'], + qp.add_circuit("circuit_name", qc) + result_pq = qp.execute('circuit_name', backend='local_projectq_simulator', seed=1, shots=shots) - self.assertEqual(result_pq.get_counts(result_pq.get_names()[0]), + self.assertEqual(result_pq.get_counts(), {'1': shots}) def test_entangle(self): @@ -87,12 +91,12 @@ def test_entangle(self): qp = QuantumProgram() qr = qp.create_quantum_register("qr", N) cr = qp.create_classical_register("cr", N) - qc = qp.create_circuit("circuitName", [qr], [cr]) + qc = qp.create_circuit("circuit_name", [qr], [cr]) qc.h(qr[0]) for i in range(1, N): qc.cx(qr[0], qr[i]) qc.measure(qr, cr) - result = qp.execute(['circuitName'], + result = qp.execute(['circuit_name'], backend='local_projectq_simulator', seed=1, shots=100) counts = result.get_counts(result.get_names()[0]) @@ -109,10 +113,10 @@ def test_random_circuits(self): shots = 1000 min_cnts = int(shots / 10) job_pq = QuantumJob(compiled_circuit, - backend='local_projectq_simulator', + backend=pq_simulator, seed=1, shots=shots) job_py = QuantumJob(compiled_circuit, - backend='local_qasm_simulator', + backend=local_simulator, seed=1, shots=shots) result_pq = pq_simulator.run(job_pq) result_py = local_simulator.run(job_py) diff --git a/test/python/test_qasm_python_simulator.py b/test/python/test_qasm_python_simulator.py index f61086931be5..e244680c64e8 100644 --- a/test/python/test_qasm_python_simulator.py +++ b/test/python/test_qasm_python_simulator.py @@ -66,7 +66,7 @@ def setUp(self): 'config': { 'max_credits': resources['max_credits'], 'shots': 1024, - 'backend': 'local_qasm_simulator', + 'backend_name': 'local_qasm_simulator', }, 'circuits': [ { @@ -77,7 +77,7 @@ def setUp(self): } ]} self.q_job = QuantumJob(self.qobj, - backend='local_qasm_simulator', + backend=QasmSimulator(), circuit_config=circuit_config, seed=self.seed, resources=resources, @@ -143,7 +143,7 @@ def test_if_statement(self): 'config': { 'max_credits': 3, 'shots': shots, - 'backend': 'local_qasm_simulator', + 'backend_name': 'local_qasm_simulator', }, 'circuits': [ { @@ -170,7 +170,7 @@ def test_if_statement(self): } ] } - q_job = QuantumJob(qobj, preformatted=True) + q_job = QuantumJob(qobj, backend=QasmSimulator(), preformatted=True) result = QasmSimulator().run(q_job) result_if_true = result.get_data('test_if_true') self.log.info('result_if_true circuit:') diff --git a/test/python/test_qasm_sympy_simulator.py b/test/python/test_qasm_sympy_simulator.py index 1f7818b37667..3e6e3c3f88b7 100644 --- a/test/python/test_qasm_sympy_simulator.py +++ b/test/python/test_qasm_sympy_simulator.py @@ -50,7 +50,7 @@ def setUp(self): 'config': { 'max_credits': resources['max_credits'], 'shots': 1024, - 'backend': 'local_sympy_qasm_simulator', + 'backend_name': 'local_sympy_qasm_simulator', }, 'circuits': [ { @@ -61,7 +61,7 @@ def setUp(self): } ]} self.q_job = QuantumJob(self.qobj, - backend='local_sympy_qasm_simulator', + backend=SympyQasmSimulator(), circuit_config=circuit_config, seed=self.seed, resources=resources, diff --git a/test/python/test_unitary_python_simulator.py b/test/python/test_unitary_python_simulator.py index 2dcbb2af5788..5901efe67ecf 100644 --- a/test/python/test_unitary_python_simulator.py +++ b/test/python/test_unitary_python_simulator.py @@ -60,7 +60,7 @@ def test_unitary_simulator(self): 'config': { 'max_credits': None, 'shots': 1, - 'backend': 'local_unitary_simulator' + 'backend_name': 'local_unitary_simulator' }, 'circuits': [ { @@ -84,7 +84,7 @@ def test_unitary_simulator(self): expected = np.loadtxt(self._get_resource_path('example_unitary_matrix.dat'), dtype='complex', delimiter=',') q_job = QuantumJob(qobj, - backend='local_unitary_simulator', + backend=UnitarySimulator(), preformatted=True) result = UnitarySimulator().run(q_job) @@ -142,7 +142,7 @@ def profile_unitary_simulator(self): self.qp = random_circuits.get_program() pr.enable() self.qp.execute(self.qp.get_circuit_names(), - backend='local_unitary_simulator') + backend=UnitarySimulator()) pr.disable() sout = io.StringIO() ps = pstats.Stats(pr, stream=sout).sort_stats('cumulative') diff --git a/test/python/test_unitary_sympy_simulator.py b/test/python/test_unitary_sympy_simulator.py index 4da5a31cab67..0b67220a52d3 100644 --- a/test/python/test_unitary_sympy_simulator.py +++ b/test/python/test_unitary_sympy_simulator.py @@ -52,7 +52,7 @@ def test_unitary_simulator(self): 'config': { 'max_credits': None, 'shots': 1, - 'backend': 'local_sympy_unitary_simulator' + 'backend_name': 'local_sympy_unitary_simulator' }, 'circuits': [ { @@ -70,7 +70,7 @@ def test_unitary_simulator(self): } q_job = QuantumJob(qobj, - backend='local_sympy_unitary_simulator', + backend=SympyUnitarySimulator(), preformatted=True) result = SympyUnitarySimulator().run(q_job)