def test_example_multiple_compile(self): """Test a toy example compiling multiple circuits. Pass if the results are correct. """ backend = qiskit.Aer.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_pass_manager_none(self): """Test passing the default (None) pass manager to the transpiler. It should perform the default qiskit flow: unroll, swap_mapper, direction_mapper, cx cancellation, optimize_1q_gates and should be equivalent to using wrapper.compile """ q = QuantumRegister(2) circ = QuantumCircuit(q) circ.h(q[0]) circ.h(q[0]) circ.cx(q[0], q[1]) circ.cx(q[0], q[1]) circ.cx(q[0], q[1]) circ.cx(q[0], q[1]) coupling_map = [[1, 0]] basis_gates = 'u1,u2,u3,cx,id' dag_circuit = DAGCircuit.fromQuantumCircuit(circ) dag_circuit = transpile(dag_circuit, coupling_map=coupling_map, basis_gates=basis_gates, pass_manager=None) transpiler_json = DagUnroller(dag_circuit, JsonBackend(dag_circuit.basis)).execute() qobj = wrapper.compile(circ, backend='local_qasm_simulator', coupling_map=coupling_map, basis_gates=basis_gates) compiler_json = qobj.experiments[0].as_dict() # Remove extra Qobj header parameters. compiler_json.pop('config') compiler_json['header'].pop('name') compiler_json['header'].pop('compiled_circuit_qasm') self.assertDictEqual(transpiler_json, compiler_json)
def test_compile_coupling_map(self): """Test compile_coupling_map. If all correct should return data with the same stats. The circuit may be different. """ backend = qiskit.Aer.get_backend('qasm_simulator') qr = QuantumRegister(3, 'qr') cr = ClassicalRegister(3, 'cr') qc = QuantumCircuit(qr, cr) qc.h(qr[0]) qc.cx(qr[0], qr[1]) qc.cx(qr[0], qr[2]) qc.measure(qr[0], cr[0]) qc.measure(qr[1], cr[1]) qc.measure(qr[2], cr[2]) shots = 1024 coupling_map = [[0, 1], [1, 2]] initial_layout = {("qr", 0): ("q", 0), ("qr", 1): ("q", 1), ("qr", 2): ("q", 2)} qobj = compile(qc, backend=backend, shots=shots, coupling_map=coupling_map, initial_layout=initial_layout, seed=88) job = backend.run(qobj) result = job.result() qasm_to_check = qc.qasm() self.assertEqual(len(qasm_to_check), 173) counts = result.get_counts(qc) target = {'000': shots / 2, '111': shots / 2} threshold = 0.04 * shots self.assertDictAlmostEqual(counts, target, threshold)
def test_qobj_sympy_statevector_simulator(self): qobj = wrapper.compile(self.circuits, backend='sympy_statevector_simulator') 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_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_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_qobj_sympy_unitary_simulator(self): SyQ = SympyProvider() backend = SyQ.get_backend('unitary_simulator') qobj = wrapper.compile(self.circuits, backend) cc = qobj.experiments[0].as_dict() 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_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_parallel_compile(self): """Trigger parallel routines in compile. """ backend = FakeBackEnd() qr = QuantumRegister(16) cr = ClassicalRegister(2) qc = QuantumCircuit(qr, cr) qc.h(qr[0]) for k in range(1, 15): qc.cx(qr[0], qr[k]) qc.measure(qr[5], cr[0]) qlist = [qc for k in range(10)] qobj = compile(qlist, backend=backend) self.assertEqual(len(qobj.experiments), 10)
def test_already_matching(self): """Map qubit i -> i if circuit is already compatible with topology. """ backend = FakeBackend() qr = QuantumRegister(16, 'qr') cr = ClassicalRegister(4, 'cr') qc = QuantumCircuit(qr, cr) qc.h(qr) qc.cx(qr[1], qr[0]) qc.cx(qr[6], qr[11]) qc.cx(qr[8], qr[7]) qc.measure(qr[1], cr[0]) qc.measure(qr[0], cr[1]) qc.measure(qr[6], cr[2]) qc.measure(qr[11], cr[3]) qobj = compile(qc, backend=backend) for qubit_layout in qobj.experiments[0].config.layout: self.assertEqual(qubit_layout[0][1], qubit_layout[1][1])
# Setting up the backend print("(Local Backends)") for backend_name in available_backends({'local': True}): backend = get_backend(backend_name) print(backend.status) my_backend_name = 'local_qasm_simulator' my_backend = get_backend(my_backend_name) print("(Local QASM Simulator configuration) ") pprint.pprint(my_backend.configuration) print("(Local QASM Simulator calibration) ") pprint.pprint(my_backend.calibration) print("(Local QASM Simulator parameters) ") pprint.pprint(my_backend.parameters) # Compiling the job qobj = compile([qc1, qc2], my_backend) # I think we need to make a qobj into a class # Runing the job sim_result = my_backend.run(QuantumJob(qobj, preformatted=True)) # ideally # 1. we need to make the run take as the input a qobj # 2. we need to make the run return a job object # # job = my_backend.run(qobj) # sim_result=job.retrieve # the job is a new object that runs when it does and i dont wait for it to # finish and can get results later # other job methods # job.cancel -- use to abort the job # job.status -- the status of the job