def test_retrieve_job_uses_appropriate_backend(self, backend): """Test that retrieved jobs come from their appropriate backend.""" backend_1 = backend # Get a second backend. backend_2 = None provider = backend.provider() for my_backend in provider.backends(): if my_backend.status().operational and my_backend.name() != backend_1.name(): backend_2 = my_backend break if not backend_2: raise SkipTest('Skipping test that requires multiple backends') job_1 = backend_1.run(transpile(ReferenceCircuits.bell(), backend_1), validate_qobj=True) job_2 = backend_2.run(transpile(ReferenceCircuits.bell(), backend_2), validate_qobj=True) # test a retrieved job's backend is the same as the queried backend self.assertEqual(backend_1.retrieve_job(job_1.job_id()).backend().name(), backend_1.name()) self.assertEqual(backend_2.retrieve_job(job_2.job_id()).backend().name(), backend_2.name()) # test retrieve requests for jobs that exist on other backends throw errors with self.assertWarns(Warning) as context_manager: self.assertRaises(IBMQBackendError, backend_1.retrieve_job, job_2.job_id()) self.assertIn('belongs to', str(context_manager.warning)) with self.assertWarns(Warning) as context_manager: self.assertRaises(IBMQBackendError, backend_2.retrieve_job, job_1.job_id()) self.assertIn('belongs to', str(context_manager.warning)) # Cleanup for job in [job_1, job_2]: cancel_job(job)
def test_retrieve_job_uses_appropriate_backend(self): """Test that retrieved jobs come from their appropriate backend.""" backend_1 = self.real_device_backend # Get a second backend. backend_2 = None provider = self.real_device_backend.provider for my_backend in provider.backends(): if my_backend.status( ).operational and my_backend.name != backend_1.name: backend_2 = my_backend break if not backend_2: raise SkipTest("Skipping test that requires multiple backends") job_1 = backend_1.run(transpile(ReferenceCircuits.bell(), backend_1)) job_2 = backend_2.run(transpile(ReferenceCircuits.bell(), backend_2)) # test a retrieved job's backend is the same as the queried backend self.assertEqual( provider.backend.job(job_1.job_id()).backend().name, backend_1.name) self.assertEqual( provider.backend.job(job_2.job_id()).backend().name, backend_2.name) # Cleanup for job in [job_1, job_2]: cancel_job(job)
def setUp(self): """Initial test setup.""" super().setUp() self._qc = ReferenceCircuits.bell() self._jm = IBMQJobManager() self._fake_api_backend = None self._fake_api_provider = None
def test_retrieve_active_jobs(self): """Test retrieving jobs that are currently unfinished.""" backend = most_busy_backend(self.provider, instance=self.dependencies.instance) active_job_statuses = { api_status_to_job_status(status) for status in ApiJobStatus if status not in API_JOB_FINAL_STATES } job = backend.run(transpile(ReferenceCircuits.bell(), backend)) active_jobs = backend.active_jobs() if not job.in_final_state(): # Job is still active. self.assertIn(job.job_id(), [active_job.job_id() for active_job in active_jobs]) for active_job in active_jobs: self.assertTrue( active_job._status in active_job_statuses, "status for job {} is '{}' but it should be '{}'.".format( active_job.job_id(), active_job._status, active_job_statuses), ) # Cancel job so it doesn't consume more resources. cancel_job(job)
def test_simulator_with_noise_model(self, backend): """Test using simulator with a noise model.""" noise_model = NoiseModel.from_backend(backend) result = self.sim_backend.run( transpile(ReferenceCircuits.bell(), backend=self.sim_backend), noise_model=noise_model).result() self.assertTrue(result)
def test_run_circuit(self): """Test run_circuits""" job = self.provider.run_circuits(ReferenceCircuits.bell(), backend_name=self.backend.name(), shots=100) counts = job.result().get_counts() self.assertEqual(100, sum(counts.values()))
def setUpClass(cls, provider): """Initial class level setup.""" # pylint: disable=arguments-differ super().setUpClass() cls.provider = provider cls.sim_backend = provider.get_backend('ibmq_qasm_simulator') cls.bell = transpile(ReferenceCircuits.bell(), backend=cls.sim_backend)
def test_run_simulator(self): """Test running in a simulator.""" qr = QuantumRegister(2, 'q') cr = ClassicalRegister(2, 'c') qc = QuantumCircuit(qr, cr, name='hadamard') qc.h(qr) qc.measure(qr, cr) qobj = assemble(transpile([ReferenceCircuits.bell(), qc], backend=self.sim_backend), backend=self.sim_backend) shots = qobj.config.shots job = self.sim_backend.run(qobj, validate_qobj=True) 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_retrieve_jobs_queued(self): """Test retrieving jobs that are queued.""" backend = most_busy_backend(self.provider) job = backend.run(transpile(ReferenceCircuits.bell(), backend)) # Wait for the job to queue, run, or reach a final state. leave_states = list(JOB_FINAL_STATES) + [JobStatus.QUEUED, JobStatus.RUNNING] while job.status() not in leave_states: time.sleep(0.5) before_status = job._status job_list_queued = backend.jobs(status=JobStatus.QUEUED, limit=5, start_datetime=self.last_month) if before_status is JobStatus.QUEUED and job.status() is JobStatus.QUEUED: self.assertIn(job.job_id(), [queued_job.job_id() for queued_job in job_list_queued], "job {} is queued but not retrieved when filtering for queued jobs." .format(job.job_id())) for queued_job in job_list_queued: self.assertTrue(queued_job._status == JobStatus.QUEUED, "status for job {} is '{}' but it should be {}" .format(queued_job.job_id(), queued_job._status, JobStatus.QUEUED)) # Cancel job so it doesn't consume more resources. cancel_job(job)
def test_run_qobj(self): """Test running a Qobj.""" qobj = assemble(transpile(ReferenceCircuits.bell(), self.backend), self.backend) with self.assertWarns(DeprecationWarning): job = self.backend.run(qobj) cancel_job(job)
def run_with_api(self, api): """Creates a new ``IBMJob`` running with the provided API object.""" backend = IBMBackend(FakeBogota().configuration(), mock.Mock(), api_client=api) circuit = transpile(ReferenceCircuits.bell()) self._current_api = api self._current_qjob = backend.run(circuit) self._current_qjob.refresh = mock.Mock() return self._current_qjob
def test_job_widget(self): """Test jobs tab.""" backend = self.dependencies.provider.get_backend("ibmq_qasm_simulator") job = backend.run(transpile(ReferenceCircuits.bell(), backend)) create_job_widget(mock.MagicMock(), job, backend=backend.name, status=job.status().value)
def test_paused_backend_warning(self): """Test that a warning is given when running jobs on a paused backend.""" backend = self.dependencies.provider.get_backend("ibmq_qasm_simulator") paused_status = backend.status() paused_status.status_msg = "internal" backend.status = mock.MagicMock(return_value=paused_status) with self.assertWarns(Warning): backend.run(ReferenceCircuits.bell())
def test_execute_two_remote(self): """Test executing two circuits on a remote backend.""" qc = ReferenceCircuits.bell() qc_extra = QuantumCircuit(2, 2) qc_extra.measure_all() job = execute([qc, qc_extra], self.sim_backend, seed_transpiler=self.seed) results = job.result() self.assertIsInstance(results, Result)
def setUpClass(cls, dependencies: IntegrationTestDependencies) -> None: """Initial class level setup.""" # pylint: disable=arguments-differ super().setUpClass() cls.dependencies = dependencies cls.sim_backend = cls.dependencies.provider.get_backend("ibmq_qasm_simulator") cls._qc = transpile(ReferenceCircuits.bell(), backend=cls.sim_backend) cls.last_week = datetime.now() - timedelta(days=7)
def setUpClass(cls, provider): """Initial class level setup.""" # pylint: disable=arguments-differ super().setUpClass() cls.provider = provider cls.sim_backend = provider.get_backend('ibmq_qasm_simulator') cls.bell = transpile(ReferenceCircuits.bell(), cls.sim_backend) cls.sim_job = cls.sim_backend.run(cls.bell, validate_qobj=True) cls.last_month = datetime.now() - timedelta(days=30)
def test_websockets_device(self, backend): """Test checking status of a job via websockets for a device.""" job = backend.run(transpile(ReferenceCircuits.bell(), backend), shots=1) # Manually disable the non-websocket polling. job._api_client._job_final_status_polling = self._job_final_status_polling job.wait_for_final_state(wait=300, callback=self.simple_job_callback) result = job.result() self.assertTrue(result.success)
def setUp(self): """Initial test setup.""" super().setUp() self._qc = ReferenceCircuits.bell() self.fake_backend = self.sim_backend self.fake_provider = self.dependencies.provider self._set_fake_client(BaseFakeAccountClient()) self.fake_backend._provider = self.fake_provider self.fake_provider.backend._provider = self.fake_provider self.fake_backend._configuration.max_experiments = 5
def setUpClass(cls, backend: IBMBackend, dependencies: IntegrationTestDependencies) -> None: """Initial class level setup.""" # pylint: disable=arguments-differ super().setUpClass() cls.dependencies = dependencies cls.sim_backend = dependencies.provider.get_backend( "ibmq_qasm_simulator", instance=dependencies.instance) cls.bell = transpile(ReferenceCircuits.bell(), cls.sim_backend) cls.real_device_backend = backend
def test_sim_backend_options(self): """Test simulator backend options.""" provider: IBMProvider = self.backend.provider backend = provider.get_backend("ibmq_qasm_simulator") backend.options.shots = 2048 backend.set_options(memory=True) job = backend.run(ReferenceCircuits.bell(), shots=1024, foo="foo") backend_options = provider.backend.job(job.job_id()).backend_options() self.assertEqual(backend_options["shots"], 1024) self.assertTrue(backend_options["memory"]) self.assertEqual(backend_options["foo"], "foo")
def test_sim_backend_options(self): """Test simulator backend options.""" provider = self.backend.provider() backend = provider.get_backend('ibmq_qasm_simulator') backend.options.shots = 2048 backend.set_options(memory=True) job = backend.run(ReferenceCircuits.bell(), shots=1024, foo='foo') qobj = backend.retrieve_job(job.job_id()).qobj() self.assertEqual(qobj.config.shots, 1024) self.assertTrue(qobj.config.memory) self.assertEqual(qobj.config.foo, 'foo')
def test_websockets_timeout(self): """Test timeout checking status of a job via websockets.""" backend = most_busy_backend(self.provider) job = backend.run(transpile(ReferenceCircuits.bell(), backend), validate_qobj=True, shots=backend.configuration().max_shots) try: with self.assertRaises(JobTimeoutError): job.result(timeout=0.1) finally: cancel_job(job)
def bell_in_qobj(backend: IBMQBackend, shots: int = 1024) -> QasmQobj: """Return a bell circuit in Qobj format. Args: backend: Backend to use for transpiling the circuit. shots: Number of shots. Returns: A bell circuit in Qobj format. """ return assemble(transpile(ReferenceCircuits.bell(), backend=backend), backend=backend, shots=shots)
def test_wait_for_final_state_timeout(self): """Test waiting for job to reach final state times out.""" backend = most_busy_backend(self.provider) job = backend.run(transpile(ReferenceCircuits.bell(), backend=backend), validate_qobj=True) try: self.assertRaises(IBMQJobTimeoutError, job.wait_for_final_state, timeout=0.1) finally: # Ensure all threads ended. for thread in job._executor._threads: thread.join(0.1) cancel_job(job)
def test_job_backend_properties_and_status(self): """Test the backend properties and status of a job.""" for desc, provider in self.providers.items(): backend = provider.backends( simulator=False, operational=True, filters=lambda b: b.configuration().n_qubits >= 5)[0] with self.subTest(desc=desc, backend=backend): job = self._submit_job_with_retry(ReferenceCircuits.bell(), backend) self.assertIsNotNone(job.properties()) self.assertTrue(job.status()) # Cancel job so it doesn't consume more resources. cancel_job(job, verify=True)
def test_retrieve_failed_job_simulator_partial(self): """Test retrieving partial results from a simulator backend.""" qc_new = transpile(ReferenceCircuits.bell(), self.sim_backend) qobj = assemble([qc_new] * 2, backend=self.sim_backend) qobj.experiments[1].instructions[1].name = 'bad_instruction' job = self.sim_backend.run(qobj, validate_qobj=True) result = job.result(partial=True) self.assertIsInstance(result, Result) self.assertTrue(result.results[0].success) self.assertFalse(result.results[1].success)
def submit_and_cancel(backend: IBMBackend) -> IBMJob: """Submit and cancel a job. Args: backend: Backend to submit the job to. Returns: Cancelled job. """ circuit = transpile(ReferenceCircuits.bell(), backend=backend) job = backend.run(circuit) cancel_job(job, True) return job
def test_private_job(self, provider): """Test a private job.""" backend = provider.get_backend('ibmq_qasm_simulator') qc = ReferenceCircuits.bell() job = execute(qc, backend=backend) self.assertIsNotNone(job.qobj()) self.assertIsNotNone(job.result()) # Wait a bit for databases to update. time.sleep(2) with self.assertRaises(IBMQBackendApiError) as err_cm: backend.retrieve_job(job.job_id()) self.assertIn('3250', str(err_cm.exception))
def submit_job_one_bad_instr(backend: IBMQBackend) -> IBMQJob: """Submit a job that contains one good and one bad instruction. Args: backend: Backend to submit the job to. Returns: Submitted job. """ qc_new = transpile(ReferenceCircuits.bell(), backend) qobj = assemble([qc_new] * 2, backend=backend) qobj.experiments[1].instructions[1].name = 'bad_instruction' job = backend.run(qobj, validate_qobj=True) return job
def test_coder_qc(self): """Test runtime encoder and decoder for circuits.""" bell = ReferenceCircuits.bell() unbound = EfficientSU2(num_qubits=4, reps=1, entanglement='linear') subtests = (bell, unbound, [bell, unbound]) for circ in subtests: with self.subTest(circ=circ): encoded = json.dumps(circ, cls=RuntimeEncoder) self.assertIsInstance(encoded, str) decoded = json.loads(encoded, cls=RuntimeDecoder) if not isinstance(circ, list): decoded = [decoded] self.assertTrue( all(isinstance(item, QuantumCircuit) for item in decoded))