def test_retrieve_job_uses_appropriate_backend(self, qe_token, qe_url): """Test that retrieved jobs come from their appropriate backend.""" IBMQ.enable_account(qe_token, qe_url) simulator_backend = IBMQ.get_backend('ibmq_qasm_simulator') backends = IBMQ.backends(simulator=False) real_backend = least_busy(backends) qobj_sim = compile(self._qc, simulator_backend) job_sim = simulator_backend.run(qobj_sim) qobj_real = compile(self._qc, real_backend) job_real = real_backend.run(qobj_real) # test a retrieved job's backend is the same as the queried backend self.assertEqual(simulator_backend.retrieve_job(job_sim.job_id()).backend().name(), simulator_backend.name()) self.assertEqual(real_backend.retrieve_job(job_real.job_id()).backend().name(), real_backend.name()) # test retrieve requests for jobs that exist on other backends throw errors with self.assertWarns(Warning) as context_manager: self.assertRaises(IBMQBackendError, simulator_backend.retrieve_job, job_real.job_id()) self.assertIn('belongs to', str(context_manager.warning)) with self.assertWarns(Warning) as context_manager: self.assertRaises(IBMQBackendError, real_backend.retrieve_job, job_sim.job_id()) self.assertIn('belongs to', str(context_manager.warning))
def test_run_simulator(self, qe_token, qe_url): """Test running in a simulator.""" IBMQ.enable_account(qe_token, qe_url) backend = IBMQ.get_backend('ibmq_qasm_simulator') qr = QuantumRegister(2, 'q') cr = ClassicalRegister(2, 'c') qc = QuantumCircuit(qr, cr, name='hadamard') qc.h(qr) qc.measure(qr, cr) qobj = compile([self._qc, qc], backend) shots = qobj.config.shots job = backend.run(qobj) result = job.result() counts_qx1 = result.get_counts(0) counts_qx2 = result.get_counts(1) counts_ex1 = {'00': shots/2, '11': shots/2} counts_ex2 = {'00': shots/4, '11': shots/4, '10': shots/4, '01': shots/4} states1 = counts_qx1.keys() | counts_ex1.keys() states2 = counts_qx2.keys() | counts_ex2.keys() # contingency table ctable1 = numpy.array([[counts_qx1.get(key, 0) for key in states1], [counts_ex1.get(key, 0) for key in states1]]) ctable2 = numpy.array([[counts_qx2.get(key, 0) for key in states2], [counts_ex2.get(key, 0) for key in states2]]) self.log.info('states1: %s', str(states1)) self.log.info('states2: %s', str(states2)) self.log.info('ctable1: %s', str(ctable1)) self.log.info('ctable2: %s', str(ctable2)) contingency1 = chi2_contingency(ctable1) contingency2 = chi2_contingency(ctable2) self.log.info('chi2_contingency1: %s', str(contingency1)) self.log.info('chi2_contingency2: %s', str(contingency2)) self.assertGreater(contingency1[1], 0.01) self.assertGreater(contingency2[1], 0.01)
def test_get_jobs_filter_counts(self, qe_token, qe_url): """Test retrieving jobs from a backend filtered by counts.""" # TODO: consider generalizing backend name # TODO: this tests depends on the previous executions of the user IBMQ.enable_account(qe_token, qe_url) backend = IBMQ.get_backend('ibmq_qasm_simulator') my_filter = {'backend.name': 'ibmq_qasm_simulator', 'shots': 1024, 'qasms.result.data.counts.00': {'lt': 500}} self.log.info('searching for at most 5 jobs with 1024 shots, a count ' 'for "00" of < 500, on the ibmq_qasm_simulator backend') with warnings.catch_warnings(): # Disable warnings from pre-qobj jobs. warnings.filterwarnings('ignore', category=DeprecationWarning, module='qiskit.providers.ibmq.ibmqbackend') job_list = backend.jobs(limit=5, skip=0, db_filter=my_filter) for i, job in enumerate(job_list): self.log.info('match #%d', i) result = job.result() self.assertTrue(any(cresult.data.counts.to_dict()['0x0'] < 500 for cresult in result.results))
def test_run_job_object_storage(self, qe_token, qe_url): """Test running a job against a simulator using object storage.""" IBMQ.enable_account(qe_token, qe_url) # Create a Qobj. backend_name = 'ibmq_qasm_simulator' backend = IBMQ.get_backend(backend_name) circuit = transpile(self.qc1, backend, seed_transpiler=self.seed) qobj = assemble(circuit, backend, shots=1) print('complied') # Run the job through the IBMQClient directly using object storage. api = backend._api job = api.job_submit_object_storage(backend_name, qobj.as_dict()) job_id = job['id'] self.assertEqual(job['kind'], 'q-object-external-storage') # Wait for completion. api.job_final_status_websocket(job_id) # Fetch results and qobj via object storage. result = api.job_result_object_storage(job_id) qobj_downloaded = api.job_download_qobj_object_storage(job_id) self.assertEqual(qobj_downloaded, qobj.as_dict()) self.assertEqual(result['status'], 'COMPLETED')
def test_execute_several_circuits_simulator_online(self, qe_token, qe_url): """Test execute_several_circuits_simulator_online.""" IBMQ.enable_account(qe_token, qe_url) backend = IBMQ.get_backend('ibmq_qasm_simulator') qr = QuantumRegister(2) cr = ClassicalRegister(2) qcr1 = QuantumCircuit(qr, cr, name='qc1') qcr2 = QuantumCircuit(qr, cr, name='qc2') qcr1.h(qr) qcr2.h(qr[0]) qcr2.cx(qr[0], qr[1]) qcr1.measure(qr[0], cr[0]) qcr1.measure(qr[1], cr[1]) qcr2.measure(qr[0], cr[0]) qcr2.measure(qr[1], cr[1]) shots = 1024 qobj = compile([qcr1, qcr2], backend=backend, seed=73846087, shots=shots) job = backend.run(qobj) result = job.result() counts1 = result.get_counts(qcr1) counts2 = result.get_counts(qcr2) target1 = { '00': shots / 4, '01': shots / 4, '10': shots / 4, '11': shots / 4 } target2 = {'00': shots / 2, '11': shots / 2} threshold = 0.04 * shots self.assertDictAlmostEqual(counts1, target1, threshold) self.assertDictAlmostEqual(counts2, target2, threshold)
def test_online_qasm_simulator_two_registers(self, qe_token, qe_url): """Test online_qasm_simulator_two_registers.""" IBMQ.enable_account(qe_token, qe_url) backend = IBMQ.get_backend('ibmq_qasm_simulator') qr1 = QuantumRegister(2) cr1 = ClassicalRegister(2) qr2 = QuantumRegister(2) cr2 = ClassicalRegister(2) qcr1 = QuantumCircuit(qr1, qr2, cr1, cr2, name="circuit1") qcr2 = QuantumCircuit(qr1, qr2, cr1, cr2, name="circuit2") qcr1.x(qr1[0]) qcr2.x(qr2[1]) qcr1.measure(qr1[0], cr1[0]) qcr1.measure(qr1[1], cr1[1]) qcr1.measure(qr2[0], cr2[0]) qcr1.measure(qr2[1], cr2[1]) qcr2.measure(qr1[0], cr1[0]) qcr2.measure(qr1[1], cr1[1]) qcr2.measure(qr2[0], cr2[0]) qcr2.measure(qr2[1], cr2[1]) shots = 1024 qobj = compile([qcr1, qcr2], backend, seed=8458, shots=shots, seed_mapper=88434) job = backend.run(qobj) result = job.result() result1 = result.get_counts(qcr1) result2 = result.get_counts(qcr2) self.assertEqual(result1, {'00 01': 1024}) self.assertEqual(result2, {'10 00': 1024})
def test_get_backend_name(self, qe_token, qe_url): """Test getting a backend name.""" IBMQ.enable_account(qe_token, qe_url) backend = IBMQ.get_backend('ibmq_qasm_simulator') qobj = compile(self._qc, backend) job = backend.run(qobj) self.assertTrue(job.backend().name() == backend.name())
def test_aliases(self, qe_token, qe_url): """Test that display names of devices map the regular names.""" IBMQ.enable_account(qe_token, qe_url) aliased_names = IBMQ._aliased_backend_names() for display_name, backend_name in aliased_names.items(): with self.subTest(display_name=display_name, backend_name=backend_name): try: backend_by_name = IBMQ.get_backend(backend_name) except QiskitBackendNotFoundError: # The real name of the backend might not exist pass else: backend_by_display_name = IBMQ.get_backend(display_name) self.assertEqual(backend_by_name, backend_by_display_name) self.assertEqual(backend_by_display_name.name(), backend_name)
def test_ibmq_result_fields(self, qe_token, qe_url): """Test components of a result from a remote simulator.""" IBMQ.enable_account(qe_token, qe_url) remote_backend = IBMQ.get_backend(local=False, simulator=True) remote_result = execute(self._qc1, remote_backend).result() self.assertEqual(remote_result.backend_name, remote_backend.name()) self.assertIsInstance(remote_result.job_id, str) self.assertEqual(remote_result.status, 'COMPLETED') self.assertEqual(remote_result.results[0].status, 'DONE')
def test_job_id(self, qe_token, qe_url): """Test getting a job id.""" IBMQ.enable_account(qe_token, qe_url) backend = IBMQ.get_backend('ibmq_qasm_simulator') qobj = compile(self._qc, backend) job = backend.run(qobj) self.log.info('job_id: %s', job.job_id()) self.assertTrue(job.job_id() is not None)
def test_double_submit_fails(self, qe_token, qe_url): """Test submitting a job twice.""" IBMQ.enable_account(qe_token, qe_url) backend = IBMQ.get_backend('ibmq_qasm_simulator') qobj = compile(self._qc, backend) # backend.run() will automatically call job.submit() job = backend.run(qobj) with self.assertRaises(JobError): job.submit()
def test_api_run_job_fail_backend(self, qe_token, qe_url): """Test running a job against an invalid backend.""" IBMQ.enable_account(qe_token, qe_url) backend_name = 'ibmq_qasm_simulator' backend = IBMQ.get_backend(backend_name) qobj = compile(self.qc1, backend=backend, seed=self.seed, shots=1) api = backend._api self.assertRaises(BadBackendError, api.run_job, qobj.as_dict(), 'INVALID_BACKEND_NAME')
def test_retrieve_job(self, qe_token, qe_url): """Test retrieving a single job.""" IBMQ.enable_account(qe_token, qe_url) backend = IBMQ.get_backend('ibmq_qasm_simulator') qobj = compile(self._qc, backend) job = backend.run(qobj) rjob = backend.retrieve_job(job.job_id()) self.assertEqual(job.job_id(), rjob.job_id()) self.assertEqual(job.result().get_counts(), rjob.result().get_counts()) self.assertEqual(job.qobj().as_dict(), qobj.as_dict())
def test_run_async_simulator(self, qe_token, qe_url): """Test running in a simulator asynchronously.""" IBMQJob._executor = futures.ThreadPoolExecutor(max_workers=2) IBMQ.enable_account(qe_token, qe_url) backend = IBMQ.get_backend('ibmq_qasm_simulator') self.log.info('submitting to backend %s', backend.name()) num_qubits = 16 qr = QuantumRegister(num_qubits, 'qr') cr = ClassicalRegister(num_qubits, 'cr') qc = QuantumCircuit(qr, cr) for i in range(num_qubits - 1): qc.cx(qr[i], qr[i + 1]) qc.measure(qr, cr) qobj = compile([qc] * 10, backend) num_jobs = 5 job_array = [backend.run(qobj) for _ in range(num_jobs)] found_async_jobs = False timeout = 30 start_time = time.time() while not found_async_jobs: check = sum( [job.status() is JobStatus.RUNNING for job in job_array]) if check >= 2: self.log.info('found %d simultaneous jobs', check) break if all([job.status() is JobStatus.DONE for job in job_array]): # done too soon? don't generate error self.log.warning('all jobs completed before simultaneous jobs ' 'could be detected') break for job in job_array: self.log.info('%s %s %s %s', job.status(), job.status() is JobStatus.RUNNING, check, job.job_id()) self.log.info('- %s', str(time.time() - start_time)) if time.time() - start_time > timeout: raise TimeoutError('failed to see multiple running jobs after ' '{0} s'.format(timeout)) time.sleep(0.2) result_array = [job.result() for job in job_array] self.log.info('got back all job results') # Ensure all jobs have finished. self.assertTrue( all([job.status() is JobStatus.DONE for job in job_array])) self.assertTrue(all([result.success for result in result_array])) # Ensure job ids are unique. job_ids = [job.job_id() for job in job_array] self.assertEqual(sorted(job_ids), sorted(list(set(job_ids))))
def test_cancel(self, qe_token, qe_url): """Test job cancelation.""" IBMQ.enable_account(qe_token, qe_url) backend_name = ('ibmq_20_tokyo' if self.using_ibmq_credentials else 'ibmqx4') backend = IBMQ.get_backend(backend_name) qobj = compile(self._qc, backend) job = backend.run(qobj) self.wait_for_initialization(job, timeout=5) can_cancel = job.cancel() self.assertTrue(can_cancel) self.assertTrue(job.status() is JobStatus.CANCELLED)
def setUp(self): super().setUp() self._testing_device = os.getenv('IBMQ_QOBJ_DEVICE', None) self._qe_token = os.getenv('IBMQ_TOKEN', None) self._qe_url = os.getenv('IBMQ_QOBJ_URL') if not self._testing_device or not self._qe_token or not self._qe_url: self.skipTest('No credentials or testing device available for ' 'testing Qobj capabilities.') IBMQ.enable_account(self._qe_token, self._qe_url) self._backend = IBMQ.get_backend(self._testing_device) self._qc = _bell_circuit()
def test_websockets_simulator(self, qe_token, qe_url): """Test checking status of a job via websockets for a simulator.""" IBMQ.enable_account(qe_token, qe_url) backend = IBMQ.get_backend(simulator=True) qc = transpile(self._qc1, backend=backend) qobj = assemble(qc, backend=backend) job = backend.run(qobj) # Manually disable the non-websocket pooling. job._wait_for_final_status = None result = job.result() self.assertEqual(result.status, 'COMPLETED')
def test_api_run_job(self, qe_token, qe_url): """Test running a job against a simulator.""" IBMQ.enable_account(qe_token, qe_url) backend_name = 'ibmq_qasm_simulator' backend = IBMQ.get_backend(backend_name) qobj = compile(self.qc1, backend=backend, seed=self.seed, shots=1) api = backend._api job = api.run_job(qobj.as_dict(), backend_name) check_status = None if 'status' in job: check_status = job['status'] self.assertIsNotNone(check_status)
def test_retrieve_job(self, qe_token, qe_url): """Test retrieving a single job.""" IBMQ.enable_account(qe_token, qe_url) backend = IBMQ.get_backend('ibmq_qasm_simulator') qobj = compile(self._qc, backend) job = backend.run(qobj) rjob = backend.retrieve_job(job.job_id()) self.assertEqual(job.job_id(), rjob.job_id()) self.assertEqual(job.result().get_counts(), rjob.result().get_counts()) if getattr(backend.configuration(), 'allow_q_object'): self.assertEqual(job.qobj().as_dict(), qobj.as_dict()) else: self.assertEqual(job.qobj(), None)
def test_compile_run_remote(self, qe_token, qe_url): """Test Compiler and run remote.""" IBMQ.enable_account(qe_token, qe_url) backend = IBMQ.get_backend(local=False, simulator=True) qubit_reg = QuantumRegister(2, name='q') clbit_reg = ClassicalRegister(2, name='c') qc = QuantumCircuit(qubit_reg, clbit_reg, name="bell") qc.h(qubit_reg[0]) qc.cx(qubit_reg[0], qubit_reg[1]) qc.measure(qubit_reg, clbit_reg) qobj = compile(qc, backend, seed=self.seed) job = backend.run(qobj) result = job.result(timeout=20) self.assertIsInstance(result, Result)
def test_execute_remote(self, qe_token, qe_url): """Test Execute remote.""" IBMQ.enable_account(qe_token, qe_url) backend = IBMQ.get_backend(local=False, simulator=True) qubit_reg = QuantumRegister(2) clbit_reg = ClassicalRegister(2) qc = QuantumCircuit(qubit_reg, clbit_reg) qc.h(qubit_reg[0]) qc.cx(qubit_reg[0], qubit_reg[1]) qc.measure(qubit_reg, clbit_reg) job = execute(qc, backend, seed=self.seed) results = job.result() self.assertIsInstance(results, Result)
def setUp(self): super().setUp() self._testing_device = os.getenv('IBMQ_QOBJ_DEVICE', None) self._qe_token = os.getenv('IBMQ_TOKEN', None) self._qe_url = os.getenv('IBMQ_QOBJ_URL') if not self._testing_device or not self._qe_token or not self._qe_url: self.skipTest("No credentials or testing device available for " "testing Qobj capabilities.") IBMQ.enable_account(self._qe_token, self._qe_url) self._local_backend = BasicAer.get_backend('qasm_simulator') self._remote_backend = IBMQ.get_backend(self._testing_device) self.log.info('Remote backend: %s', self._remote_backend.name()) self.log.info('Local backend: %s', self._local_backend.name())
def test_run_job(self, qe_token, qe_url): """Test running a job against a simulator.""" IBMQ.enable_account(qe_token, qe_url) # Create a Qobj. backend_name = 'ibmq_qasm_simulator' backend = IBMQ.get_backend(backend_name) circuit = transpile(self.qc1, backend, seed_transpiler=self.seed) qobj = assemble(circuit, backend, shots=1) # Run the job through the IBMQClient directly. api = backend._api job = api.submit_job(qobj.to_dict(), backend_name) self.assertIn('status', job) self.assertIsNotNone(job['status'])
def test_run_job(self, qe_token, qe_url): """Test running a job against a simulator.""" _ = self._get_client(qe_token, qe_url) IBMQ.enable_account(qe_token, qe_url) # Create a Qobj. backend_name = 'ibmq_qasm_simulator' backend = IBMQ.get_backend(backend_name) qobj = compile(self.qc1, backend=backend, seed=self.seed, shots=1) # Run the job through the IBMQClient directly. api = backend._api job = api.run_job(qobj.as_dict(), backend_name) self.assertIn('status', job) self.assertIsNotNone(job['status'])
def test_get_job_includes(self, qe_token, qe_url): """Check the field includes parameter for get_job.""" IBMQ.enable_account(qe_token, qe_url) backend_name = 'ibmq_qasm_simulator' backend = IBMQ.get_backend(backend_name) circuit = transpile(self.qc1, backend, seed_transpiler=self.seed) qobj = assemble(circuit, backend, shots=1) api = backend._api job = api.submit_job(qobj.as_dict(), backend_name) job_id = job['id'] # Get the job, excluding a parameter. self.assertIn('shots', job) job_excluded = api.get_job(job_id, exclude_fields=['shots']) self.assertNotIn('shots', job_excluded)
def test_conditional_operation(self, qe_token, qe_url): """Test conditional operation.""" IBMQ.enable_account(qe_token, qe_url) backend = IBMQ.get_backend('ibmq_qasm_simulator') qr = QuantumRegister(4) cr = ClassicalRegister(4) circuit = QuantumCircuit(qr, cr) circuit.x(qr[0]) circuit.x(qr[2]) circuit.measure(qr[0], cr[0]) circuit.x(qr[0]).c_if(cr, 1) qobj = assemble(transpile(circuit, backend=backend)) result = backend.run(qobj).result() self.assertEqual(result.get_counts(circuit), {'0001': 1024})
def test_execute_one_circuit_simulator_online(self, qe_token, qe_url): """Test execute_one_circuit_simulator_online.""" IBMQ.enable_account(qe_token, qe_url) backend = IBMQ.get_backend('ibmq_qasm_simulator') qr = QuantumRegister(1) cr = ClassicalRegister(1) qc = QuantumCircuit(qr, cr, name='qc') qc.h(qr[0]) qc.measure(qr[0], cr[0]) qobj = compile(qc, backend=backend, seed=73846087) shots = qobj.config.shots job = backend.run(qobj) result = job.result() counts = result.get_counts(qc) target = {'0': shots / 2, '1': shots / 2} threshold = 0.04 * shots self.assertDictAlmostEqual(counts, target, threshold)
def test_get_job_includes(self, qe_token, qe_url): """Check the field includes parameter for get_job.""" _ = self._get_api(qe_token, qe_url) IBMQ.enable_account(qe_token, qe_url) backend_name = 'ibmq_qasm_simulator' backend = IBMQ.get_backend(backend_name) qobj = compile([self.qc1, self.qc2], backend=backend, seed=self.seed, shots=1) api = backend._api job = api.run_job(qobj.as_dict(), backend_name) job_id = job['id'] # Get the job, excluding a parameter. self.assertIn('deleted', job) job_excluded = api.get_job(job_id, exclude_fields=['deleted']) self.assertNotIn('deleted', job_excluded)
def test_compile_two_run_remote(self, qe_token, qe_url): """Test Compiler and run two circuits.""" IBMQ.enable_account(qe_token, qe_url) backend = IBMQ.get_backend(local=False, simulator=True) qubit_reg = QuantumRegister(2, name='q') clbit_reg = ClassicalRegister(2, name='c') qc = QuantumCircuit(qubit_reg, clbit_reg, name="bell") qc.h(qubit_reg[0]) qc.cx(qubit_reg[0], qubit_reg[1]) qc.measure(qubit_reg, clbit_reg) qc_extra = QuantumCircuit(qubit_reg, clbit_reg, name="extra") qc_extra.measure(qubit_reg, clbit_reg) qobj = assemble(transpile([qc, qc_extra], backend=backend, seed_transpiler=self.seed), backend=backend) job = backend.run(qobj) result = job.result() self.assertIsInstance(result, Result)
def test_error_message_qasm(self, qe_token, qe_url): """Test retrieving job error messages including QASM status(es).""" IBMQ.enable_account(qe_token, qe_url) backend = IBMQ.get_backend('ibmq_qasm_simulator') qr = QuantumRegister(5) # 5 is sufficient for this test cr = ClassicalRegister(2) qc = QuantumCircuit(qr, cr) qc.cx(qr[0], qr[1]) qc_new = transpile(qc, backend) qobj = assemble(qc_new, shots=1000) qobj.experiments[0].instructions[0].name = 'test_name' job_sim = backend.run(qobj) with self.assertRaises(JobError): job_sim.result() message = job_sim.error_message() self.assertIn('Job resulted in the following QASM status(es): ', message)