def equivalent_transpile(self, backend, opt_level): """Simulate, transpile and simulate the present circuit. Verify that the counts are not significantly different before and after transpilation. """ assume(backend is None or backend.configuration().n_qubits >= len(self.qc.qubits)) shots = 4096 aer_qasm_simulator = self.backend aer_counts = execute(self.qc, backend=self.backend, shots=shots).result().get_counts() try: xpiled_qc = transpile(self.qc, backend=backend, optimization_level=opt_level) except Exception as e: failed_qasm = 'Exception caught during transpilation of circuit: \n{}'.format( self.qc.qasm()) raise RuntimeError(failed_qasm) from e xpiled_aer_counts = execute(xpiled_qc, backend=self.backend, shots=shots).result().get_counts() count_differences = dicts_almost_equal(aer_counts, xpiled_aer_counts, 0.05 * shots) assert count_differences == '', 'Counts not equivalent: {}\nFailing QASM: \n{}'.format( count_differences, self.qc.qasm())
def equivalent_transpile(self, conf): """Simulate, transpile and simulate the present circuit. Verify that the counts are not significantly different before and after transpilation. """ backend, opt_level, layout_method, routing_method, scheduling_method = conf assume(backend is None or backend.configuration().n_qubits >= len(self.qc.qubits)) print( f"Evaluating circuit at level {opt_level} on {backend} " f"using layout_method={layout_method} routing_method={routing_method} " f"and scheduling_method={scheduling_method}:\n{self.qc.qasm()}" ) shots = 4096 # Note that there's no transpilation here, which is why the gates are limited to only ones # that Aer supports natively. aer_counts = self.backend.run(self.qc, shots=shots).result().get_counts() try: xpiled_qc = transpile( self.qc, backend=backend, optimization_level=opt_level, layout_method=layout_method, routing_method=routing_method, scheduling_method=scheduling_method, ) except Exception as e: failed_qasm = "Exception caught during transpilation of circuit: \n{}".format( self.qc.qasm() ) raise RuntimeError(failed_qasm) from e xpiled_aer_counts = self.backend.run(xpiled_qc, shots=shots).result().get_counts() count_differences = dicts_almost_equal(aer_counts, xpiled_aer_counts, 0.05 * shots) assert ( count_differences == "" ), "Counts not equivalent: {}\nFailing QASM Input:\n{}\n\nFailing QASM Output:\n{}".format( count_differences, self.qc.qasm(), xpiled_qc.qasm() )
Examples of creating more complex pass managers can be seen in qiskit.transpiler.preset_passmanagers""" pass_manager = PassManager() pass_manager.append(TrivialLayout(coupling_map)) pass_manager.append(MyBasicAnalysisPass()) pass_manager.append(MyBasicTransformationPass(properties)) """ This allows us to simulate the noise a real device has, so that you don't have to wait for jobs to complete on the actual backends.""" noise_model = basic_device_noise_model(properties) simulator = Aer.get_backend('qasm_simulator') """ This is the circuit we are going to look at""" qc = QuantumCircuit(2, 2) qc.h(1) qc.measure(0, 0) circuits = [qc] result_normal = execute(circuits, simulator, coupling_map=coupling_map).result().get_counts() # NB we include the noise model in the second run result_noisy = execute(circuits, simulator, noise_model=noise_model, coupling_map=coupling_map).result().get_counts() """ Check to see how similar the counts from the two runs are, where delta the allowed difference between the counts. Returns an empty string if they are almost equal, otherwise returns an error message which can then be printed.""" equality_check = dicts_almost_equal(result_normal, result_noisy, delta=1e-8) if equality_check: print(equality_check)