def test_basic_reordering(self, QE_TOKEN, QE_URL, hub=None, group=None, project=None): """a simple reordering within a 2-qubit register""" sim_backend_name, real_backend_name = self._get_backends( QE_TOKEN, QE_URL, hub, group, project) sim = get_backend(sim_backend_name) real = get_backend(real_backend_name) if not sim or not real: raise unittest.SkipTest('no remote device available') q = qiskit.QuantumRegister(2) c = qiskit.ClassicalRegister(2) circ = qiskit.QuantumCircuit(q, c) circ.h(q[0]) circ.measure(q[0], c[1]) circ.measure(q[1], c[0]) shots = 2000 qobj_real = transpiler.compile(circ, real, shots=shots) qobj_sim = transpiler.compile(circ, sim, shots=shots) result_real = real.run(qobj_real).result(timeout=600) result_sim = sim.run(qobj_sim).result(timeout=600) counts_real = result_real.get_counts() counts_sim = result_sim.get_counts() self.log.info(counts_real) self.log.info(counts_sim) threshold = 0.1 * shots self.assertDictAlmostEqual(counts_real, counts_sim, threshold)
def test_basic_reordering(self, qe_token, qe_url): """a simple reordering within a 2-qubit register""" sim_backend_name, real_backend_name = self._get_backends( qe_token, qe_url) sim = get_backend(sim_backend_name) real = get_backend(real_backend_name) if not sim or not real: raise unittest.SkipTest('no remote device available') qr = qiskit.QuantumRegister(2) cr = qiskit.ClassicalRegister(2) circuit = qiskit.QuantumCircuit(qr, cr) circuit.h(qr[0]) circuit.measure(qr[0], cr[1]) circuit.measure(qr[1], cr[0]) shots = 2000 qobj_real = transpiler.compile(circuit, real, shots=shots) qobj_sim = transpiler.compile(circuit, sim, shots=shots) result_real = real.run(qobj_real).result(timeout=600) result_sim = sim.run(qobj_sim).result(timeout=600) counts_real = result_real.get_counts() counts_sim = result_sim.get_counts() self.log.info(counts_real) self.log.info(counts_sim) threshold = 0.1 * shots self.assertDictAlmostEqual(counts_real, counts_sim, threshold)
def test_random_circuits(self): qk_simulator = get_backend('local_qasm_simulator') for circuit in self.rqg.get_circuits(format_='QuantumCircuit'): self.log.info(circuit.qasm()) compiled_circuit = compile_circuit(circuit) shots = 100 job_pq = QuantumJob(compiled_circuit, backend=pq_simulator, seed=1, shots=shots) job_qk = QuantumJob(compiled_circuit, backend=qk_simulator, seed=1, shots=shots) result_pq = pq_simulator.run(job_pq).result() result_qk = qk_simulator.run(job_qk).result() counts_pq = result_pq.get_counts(result_pq.get_names()[0]) counts_qk = result_qk.get_counts(result_qk.get_names()[0]) self.log.info('local_qasm_simulator_projectq: %s', str(counts_pq)) self.log.info('local_qasm_simulator: %s', str(counts_qk)) states = counts_qk.keys() | counts_pq.keys() # contingency table ctable = numpy.array([[counts_pq.get(key, 0) for key in states], [counts_qk.get(key, 0) for key in states]]) result = chi2_contingency(ctable) self.log.info('chi2_contingency: %s', str(result)) with self.subTest(circuit=circuit): self.assertGreater(result[1], 0.01)
def test_output_style(self): qk_simulator = get_backend('local_qasm_simulator') qr = QuantumRegister(2) cr = ClassicalRegister(2) qc = QuantumCircuit(qr, cr, name='test_output_order') qc.h(qr[0]) qc.measure(qr[0], cr[0]) qc.measure(qr[1], cr[1]) shots = 100 counts_pq, counts_qk, result = self.run_on_simulators(qc, pq_simulator, qk_simulator, shots=shots, seed=1) self.assertGreater(result[1], 0.01) cr1 = ClassicalRegister(1) cr2 = ClassicalRegister(1) qc = QuantumCircuit(qr, cr1, cr2, name='test_output_separation') qc.h(qr[0]) qc.measure(qr[0], cr1[0]) qc.measure(qr[1], cr2[0]) counts_pq, counts_qk, result = self.run_on_simulators(qc, pq_simulator, qk_simulator, shots=shots, seed=1) self.log.info('chi2_contingency: %s', str(result)) self.assertGreater(result[1], 0.01)
def test_run_job_processor_local_parallel(self): def job_done_callback(results): try: self.log.info(pprint.pformat(results)) for result in results: self.assertTrue(result.get_status() == 'COMPLETED') except Exception as e: self.job_processor_exception = e finally: self.job_processor_finished = True njobs = 20 job_list = [] # TODO: make this run on `local_qasm_simulator` after `do_compile` # is fixed in _quantumjob. backend = get_backend('local_qasm_simulator_py') for _ in range(njobs): compiled_circuit = compile_circuit(self.qc) quantum_job = QuantumJob(compiled_circuit, backend=backend) job_list.append(quantum_job) self.job_processor_finished = False self.job_processor_exception = None jp = jobprocessor.JobProcessor(job_list, max_workers=None, callback=job_done_callback) jp.submit() while not self.job_processor_finished: # Wait until the job_done_callback is invoked and completed. pass if self.job_processor_exception: raise self.job_processor_exception
def test_compile_two_remote(self, QE_TOKEN, QE_URL, hub=None, group=None, project=None): """Test Compiler remote on two circuits. If all correct some should exists. """ register(QE_TOKEN, QE_URL, hub, group, project) backend = least_busy(available_backends()) backend = get_backend(backend) qubit_reg = qiskit.QuantumRegister(2, name='q') clbit_reg = qiskit.ClassicalRegister(2, name='c') qc = qiskit.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 = qiskit.QuantumCircuit(qubit_reg, clbit_reg, name="extra") qc_extra.measure(qubit_reg, clbit_reg) qobj = transpiler.compile([qc, qc_extra], backend) # FIXME should test against the qobj when defined self.assertEqual(len(qobj), 3)
def test_run_local_backend_unitary(self): backend = get_backend('local_unitary_simulator') compiled_circuit = compile_circuit(self.qc) quantum_job = QuantumJob(compiled_circuit, do_compile=False, backend=backend) jobprocessor.run_backend(quantum_job)
def test_compile_two_run_remote(self, QE_TOKEN, QE_URL, hub=None, group=None, project=None): """Test Compiler and run two circuits. If all correct some should exists. """ register(QE_TOKEN, QE_URL, hub, group, project) backend = available_backends({'local': False, 'simulator': True})[0] backend = get_backend(backend) qubit_reg = qiskit.QuantumRegister(2, name='q') clbit_reg = qiskit.ClassicalRegister(2, name='c') qc = qiskit.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 = qiskit.QuantumCircuit(qubit_reg, clbit_reg, name="extra") qc_extra.measure(qubit_reg, clbit_reg) qobj = transpiler.compile([qc, qc_extra], backend) job = backend.run(qobj) result = job.result() self.assertIsInstance(result, Result)
def lowest_pending_jobs(list_of_backends): """Returns the backend with lowest pending jobs.""" backends = [get_backend(name) for name in list_of_backends] backends = filter(lambda x: x.status.get('available', False), backends) by_pending_jobs = sorted(backends, key=lambda x: x.status['pending_jobs']) return by_pending_jobs[0].name
def test_compile_two(self): """Test Compiler. If all correct some should exists. """ backend = get_backend('local_qasm_simulator') qubit_reg = qiskit.QuantumRegister(2) clbit_reg = qiskit.ClassicalRegister(2) qubit_reg2 = qiskit.QuantumRegister(2) clbit_reg2 = qiskit.ClassicalRegister(2) qc = qiskit.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 = qiskit.QuantumCircuit(qubit_reg, qubit_reg2, clbit_reg, clbit_reg2, name="extra") qc_extra.measure(qubit_reg, clbit_reg) qobj = transpiler.compile([qc, qc_extra], backend) # FIXME should validate the Qobj when defined self.assertIsInstance(qobj, Qobj)
def test_random_circuits(self): qk_simulator = get_backend('local_qasm_simulator') for circuit in self.rqg.get_circuits(format_='QuantumCircuit'): self.log.info(circuit.qasm()) shots = 1000 min_cnts = int(shots / 10) result_pq = execute(circuit, pq_simulator.name) result_qk = execute(circuit, qk_simulator.name) counts_pq = result_pq.get_counts(result_pq.get_names()[0]) counts_qk = result_qk.get_counts(result_qk.get_names()[0]) # filter states with few counts counts_pq = { key: cnt for key, cnt in counts_pq.items() if cnt > min_cnts } counts_qk = { key: cnt for key, cnt in counts_qk.items() if cnt > min_cnts } self.log.info('local_qasm_simulator_projectq: %s', str(counts_pq)) self.log.info('local_qasm_simulator: %s', str(counts_qk)) threshold = 0.05 * shots self.assertDictAlmostEqual(counts_pq, counts_qk, threshold) states = counts_qk.keys() # contingency table ctable = numpy.array([[counts_pq[key] for key in states], [counts_qk[key] for key in states]]) result = chi2_contingency(ctable) self.log.info('chi2_contingency: %s', str(result)) with self.subTest(circuit=circuit): self.assertGreater(result[1], 0.01)
def test_mix_local_remote_jobs(self, QE_TOKEN, QE_URL): """test mixing local and remote jobs Internally local jobs execute in seperate processes since they are CPU bound and remote jobs execute in seperate threads since they are I/O bound. The module gets results from potentially both kinds in one list. Test that this works. """ provider = IBMQProvider(QE_TOKEN, QE_URL) remote_backend = provider.available_backends({'simulator': True})[0] local_backend = get_backend('local_qasm_simulator') njobs = 6 job_list = [] backend_type = [local_backend, remote_backend] i = 0 for circuit in self.rqg.get_circuits(format_='QuantumCircuit')[:njobs]: compiled_circuit = compile_circuit(circuit) backend = backend_type[i % len(backend_type)] self.log.info(backend) quantum_job = QuantumJob(compiled_circuit, backend=backend) job_list.append(quantum_job) i += 1 jp = jobprocessor.JobProcessor(job_list, max_workers=None, callback=None) jp.submit()
def test_compile_job(self): """Test compilation as part of job""" backend = get_backend('local_qasm_simulator') quantum_job = QuantumJob(self.qasm_circ, do_compile=True, backend=backend) jp = jobprocessor.JobProcessor([quantum_job], callback=None) jp.submit()
def test_alias(self): """test short alias names work""" compact_name = "local_qasm_simulator" backend = get_backend(compact_name) if not _skip_class: self.assertIsInstance(backend, QasmSimulatorCpp) else: self.assertIsInstance(backend, QasmSimulatorPy)
def test_init_job_processor(self): njobs = 5 job_list = [] backend = get_backend('local_qasm_simulator') for _ in range(njobs): quantum_job = QuantumJob(self.qc, backend, do_compile=False) job_list.append(quantum_job) _ = jobprocessor.JobProcessor(job_list, callback=None)
def test_run_local_backend_compile(self): # TODO: make this run on `local_qasm_simulator` after `do_compile` # is fixed in _quantumjob. backend = get_backend('local_qasm_simulator_py') quantum_job = QuantumJob(self.qasm_circ, do_compile=True, backend=backend) jobprocessor.run_backend(quantum_job)
def test_local_unitary_simulator(self): backend = wrapper.get_backend('local_unitary_simulator_py') qobj = wrapper.compile(self.circuits, backend=backend) cc = qobj['circuits'][0]['compiled_circuit'] ccq = qobj['circuits'][0]['compiled_circuit_qasm'] self.assertIn(self.qr_name, map(lambda x: x[0], cc['header']['qubit_labels'])) self.assertIn(self.qr_name, ccq) self.assertIn(self.cr_name, map(lambda x: x[0], cc['header']['clbit_labels'])) self.assertIn(self.cr_name, ccq)
def test_run_local_backend_qasm(self): # TODO: make this run on `local_qasm_simulator` after `do_compile` # is fixed in _quantumjob. backend = get_backend('local_qasm_simulator_py') dag_circuit = compile_circuit(self.qc) quantum_job = QuantumJob(dag_circuit, do_compile=False, backend=backend) jobprocessor.run_backend(quantum_job)
def lowest_pending_jobs(): """Returns the backend with lowest pending jobs.""" list_of_backends = available_backends( {'local': False, 'simulator': False}) device_status = [get_backend(backend).status for backend in list_of_backends] best = min([x for x in device_status if x['available'] is True], key=lambda x: x['pending_jobs']) return best['name']
def test_local_unitary_simulator(self): backend = wrapper.get_backend('local_unitary_simulator_py') qobj = wrapper.compile(self.circuits, backend=backend) exp = qobj.experiments[0] c_qasm = exp.header.compiled_circuit_qasm self.assertIn(self.qr_name, map(lambda x: x[0], exp.header.qubit_labels)) self.assertIn(self.qr_name, c_qasm) self.assertIn(self.cr_name, map(lambda x: x[0], exp.header.clbit_labels)) self.assertIn(self.cr_name, c_qasm)
def test_multi_register_reordering(self, QE_TOKEN, QE_URL, hub=None, group=None, project=None): """a more complicated reordering across 3 registers of different sizes""" sim_backend_name, real_backend_name = self._get_backends( QE_TOKEN, QE_URL, hub, group, project) if not sim_backend_name or not real_backend_name: raise unittest.SkipTest('no remote device available') sim = get_backend(sim_backend_name) real = get_backend(real_backend_name) q0 = qiskit.QuantumRegister(2) q1 = qiskit.QuantumRegister(2) q2 = qiskit.QuantumRegister(1) c0 = qiskit.ClassicalRegister(2) c1 = qiskit.ClassicalRegister(2) c2 = qiskit.ClassicalRegister(1) circ = qiskit.QuantumCircuit(q0, q1, q2, c0, c1, c2) circ.h(q0[0]) circ.cx(q0[0], q2[0]) circ.x(q1[1]) circ.h(q2[0]) circ.ccx(q2[0], q1[1], q1[0]) circ.barrier() circ.measure(q0[0], c2[0]) circ.measure(q0[1], c0[1]) circ.measure(q1[0], c0[0]) circ.measure(q1[1], c1[0]) circ.measure(q2[0], c1[1]) shots = 4000 qobj_real = transpiler.compile(circ, real, shots=shots) qobj_sim = transpiler.compile(circ, sim, shots=shots) result_real = real.run(qobj_real).result(timeout=600) result_sim = sim.run(qobj_sim).result(timeout=600) counts_real = result_real.get_counts() counts_sim = result_sim.get_counts() threshold = 0.2 * shots self.assertDictAlmostEqual(counts_real, counts_sim, threshold)
def test_random_local(self): """test randomly generated circuits on local_qasm_simulator""" njobs = 5 job_list = [] backend = get_backend('local_qasm_simulator') for circuit in self.rqg.get_circuits(format_='QuantumCircuit')[:njobs]: compiled_circuit = compile_circuit(circuit) quantum_job = QuantumJob(compiled_circuit, backend=backend) job_list.append(quantum_job) jp = jobprocessor.JobProcessor(job_list, max_workers=1, callback=None) jp.submit()
def test_local_qasm_simulator_cpp(self): backend = wrapper.get_backend('local_qasm_simulator_cpp') qobj = wrapper.compile(self.circuits, backend=backend) cc = qobj.experiments[0] ccq = qobj.experiments[0].header.compiled_circuit_qasm self.assertIn(self.qr_name, map(lambda x: x[0], cc.header.qubit_labels)) self.assertIn(self.qr_name, ccq) self.assertIn(self.cr_name, map(lambda x: x[0], cc.header.clbit_labels)) self.assertIn(self.cr_name, ccq)
def test_run_job_processor_local(self): njobs = 5 job_list = [] backend = get_backend('local_qasm_simulator') for _ in range(njobs): compiled_circuit = compile_circuit(self.qc) quantum_job = QuantumJob(compiled_circuit, backend=backend, do_compile=False) job_list.append(quantum_job) jp = jobprocessor.JobProcessor(job_list, callback=None) jp.submit()
def test_multi_register_reordering(self, qe_token, qe_url): """a more complicated reordering across 3 registers of different sizes""" sim_backend_name, real_backend_name = self._get_backends( qe_token, qe_url) if not sim_backend_name or not real_backend_name: raise unittest.SkipTest('no remote device available') sim = get_backend(sim_backend_name) real = get_backend(real_backend_name) qr0 = qiskit.QuantumRegister(2) qr1 = qiskit.QuantumRegister(2) qr2 = qiskit.QuantumRegister(1) cr0 = qiskit.ClassicalRegister(2) cr1 = qiskit.ClassicalRegister(2) cr2 = qiskit.ClassicalRegister(1) circuit = qiskit.QuantumCircuit(qr0, qr1, qr2, cr0, cr1, cr2) circuit.h(qr0[0]) circuit.cx(qr0[0], qr2[0]) circuit.x(qr1[1]) circuit.h(qr2[0]) circuit.ccx(qr2[0], qr1[1], qr1[0]) circuit.barrier() circuit.measure(qr0[0], cr2[0]) circuit.measure(qr0[1], cr0[1]) circuit.measure(qr1[0], cr0[0]) circuit.measure(qr1[1], cr1[0]) circuit.measure(qr2[0], cr1[1]) shots = 4000 qobj_real = transpiler.compile(circuit, real, shots=shots) qobj_sim = transpiler.compile(circuit, sim, shots=shots) result_real = real.run(qobj_real).result(timeout=600) result_sim = sim.run(qobj_sim).result(timeout=600) counts_real = result_real.get_counts() counts_sim = result_sim.get_counts() threshold = 0.2 * shots self.assertDictAlmostEqual(counts_real, counts_sim, threshold)
def test_example_multiple_compile(self): """Test a toy example compiling multiple circuits. Pass if the results are correct. """ backend = get_backend('local_qasm_simulator') coupling_map = [[0, 1], [0, 2], [1, 2], [3, 2], [3, 4], [4, 2]] qr = QuantumRegister(5) cr = ClassicalRegister(5) bell = QuantumCircuit(qr, cr) ghz = QuantumCircuit(qr, cr) # Create a GHZ state ghz.h(qr[0]) for i in range(4): ghz.cx(qr[i], qr[i + 1]) # Insert a barrier before measurement ghz.barrier() # Measure all of the qubits in the standard basis for i in range(5): ghz.measure(qr[i], cr[i]) # Create a Bell state bell.h(qr[0]) bell.cx(qr[0], qr[1]) bell.barrier() bell.measure(qr[0], cr[0]) bell.measure(qr[1], cr[1]) shots = 2048 bell_qobj = compile(bell, backend='local_qasm_simulator', shots=shots, seed=10) ghz_qobj = compile(ghz, backend='local_qasm_simulator', shots=shots, coupling_map=coupling_map, seed=10) bell_result = backend.run(bell_qobj).result() ghz_result = backend.run(ghz_qobj).result() threshold = 0.04 * shots counts_bell = bell_result.get_counts() target_bell = {'00000': shots / 2, '00011': shots / 2} self.assertDictAlmostEqual(counts_bell, target_bell, threshold) counts_ghz = ghz_result.get_counts() target_ghz = {'00000': shots / 2, '11111': shots / 2} self.assertDictAlmostEqual(counts_ghz, target_ghz, threshold)
def test_execute(self): """Test Execute. If all correct some should exists. """ backend = get_backend('local_qasm_simulator') qubit_reg = qiskit.QuantumRegister(2) clbit_reg = qiskit.ClassicalRegister(2) qc = qiskit.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) results = job.result() self.assertIsInstance(results, Result)
def test_compile_run(self): """Test Compiler and run. If all correct some should exists. """ backend = get_backend('local_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") qc.h(qubit_reg[0]) qc.cx(qubit_reg[0], qubit_reg[1]) qc.measure(qubit_reg, clbit_reg) qobj = transpiler.compile(qc, backend) result = backend.run(qobj).result() self.assertIsInstance(result, Result)
def test_compile(self): """Test Compiler. If all correct some should exists. """ backend = get_backend('local_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") qc.h(qubit_reg[0]) qc.cx(qubit_reg[0], qubit_reg[1]) qc.measure(qubit_reg, clbit_reg) qobj = transpiler.compile(qc, backend) # FIXME should test against the qobj when defined self.assertEqual(len(qobj), 3)
def test_execute_remote(self, qe_token, qe_url): """Test Execute remote. If all correct some should exists. """ register(qe_token, qe_url) backend = available_backends({'local': False, 'simulator': True})[0] backend = get_backend(backend) qubit_reg = qiskit.QuantumRegister(2) clbit_reg = qiskit.ClassicalRegister(2) qc = qiskit.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=TestCompiler.seed) results = job.result() self.assertIsInstance(results, Result)
def test_deprecated(self): """test deprecated backends are resolved correctly""" old_name = "local_qiskit_simulator" if not _skip_class: new_backend = get_backend(old_name) self.assertIsInstance(new_backend, QasmSimulatorCpp)