class ExecutePassManager(QiskitTestCase): pm_conf = PassManagerConfig(initial_layout=None, basis_gates=['u1', 'u2', 'u3', 'cx', 'id'], coupling_map=CouplingMap([(0, 1), (1, 2), (2, 3), (3, 4)]), backend_properties=None, seed_transpiler=1) backend = Aer.get_backend('qasm_simulator') shots = 2048 def assertEqualCounts(self, result, expected): result_count = result.get_counts() expected_count = expected.get_counts() for key in set(result_count.keys()).union(expected_count.keys()): with self.subTest(key=key): diff = abs( result_count.get(key, 0) - expected_count.get(key, 0)) self.assertLess(diff / self.shots * 100, 2.5) def execute(self, circuit): return execute(circuit, self.backend, basis_gates=['u1', 'u2', 'u3', 'id', 'cx'], seed_simulator=0, seed_transpiler=0, shots=self.shots)
def pm_config(seed, backend): return PassManagerConfig(initial_layout=None, basis_gates=['u1', 'u2', 'u3', 'cx', 'id'], coupling_map=CouplingMap( backend().configuration().coupling_map), backend_properties=backend().properties(), seed_transpiler=seed)
def test_with_pass_manager(self): """ Test Bernstein Vazirani using PassManager """ quantum_instance = QuantumInstance( BasicAer.get_backend('qasm_simulator'), pass_manager=level_0_pass_manager( PassManagerConfig(basis_gates=['cx', 'u1', 'u2', 'u3']))) alg = BernsteinVazirani(oracle=TruthTableOracle(bitmaps="01100110"), quantum_instance=quantum_instance) result = alg.run() self.assertEqual(result['result'], '011')
def test_custom_pass_manager(self, backend): """Test quantum kernel with a custom pass manager.""" quantum_instance = QuantumInstance( backend, shots=100, seed_simulator=algorithm_globals.random_seed, seed_transpiler=algorithm_globals.random_seed, pass_manager=level_1_pass_manager( PassManagerConfig(basis_gates=["u3", "cx"])), bound_pass_manager=level_1_pass_manager( PassManagerConfig(basis_gates=["u3", "cx"])), ) kernel = QuantumKernel(feature_map=self.feature_map, quantum_instance=quantum_instance) svc = SVC(kernel=kernel.evaluate) svc.fit(self.sample_train, self.label_train) score = svc.score(self.sample_test, self.label_test) self.assertEqual(score, 0.5)
def test_2step_transpile(self): """Test the two-step transpiler pass.""" # count how often the pass for parameterized circuits is called pre_counter = LogPass("pre_passmanager") pre_pass = PassManager(pre_counter) config = PassManagerConfig(basis_gates=["u3", "cx"]) pre_pass += level_1_pass_manager(config) # ... and the pass for bound circuits bound_counter = LogPass("bound_pass_manager") bound_pass = PassManager(bound_counter) quantum_instance = QuantumInstance( backend=BasicAer.get_backend("statevector_simulator"), basis_gates=["u3", "cx"], pass_manager=pre_pass, bound_pass_manager=bound_pass, ) optimizer = SPSA(maxiter=5, learning_rate=0.01, perturbation=0.01) vqe = VQE(optimizer=optimizer, quantum_instance=quantum_instance) _ = vqe.compute_minimum_eigenvalue(Z) with self.assertLogs(logger, level="INFO") as cm: _ = vqe.compute_minimum_eigenvalue(Z) expected = [ "pre_passmanager", "bound_pass_manager", "bound_pass_manager", "bound_pass_manager", "bound_pass_manager", "bound_pass_manager", "bound_pass_manager", "bound_pass_manager", "bound_pass_manager", "bound_pass_manager", "bound_pass_manager", "bound_pass_manager", "pre_passmanager", "bound_pass_manager", ] self.assertEqual([record.message for record in cm.records], expected)