def test_custom_multiple_circuits(self): """Test transpiling with custom pass manager and multiple circuits. This tests created a deadlock, so it needs to be monitored for timeout. See: https://github.com/Qiskit/qiskit-terra/issues/3925 """ qc = QuantumCircuit(2) qc.h(0) qc.cx(0, 1) pm_conf = PassManagerConfig(initial_layout=None, basis_gates=['u1', 'u2', 'u3', 'cx'], coupling_map=CouplingMap([[0, 1]]), backend_properties=None, seed_transpiler=1) passmanager = level_0_pass_manager(pm_conf) transpiled = passmanager.run([qc, qc]) expected = QuantumCircuit(QuantumRegister(2, 'q')) expected.u2(0, 3.141592653589793, 0) expected.cx(0, 1) self.assertEqual(len(transpiled), 2) self.assertEqual(transpiled[0], expected) self.assertEqual(transpiled[1], expected)
def transpile_circuit(circuit, transpile_config): """Select a PassManager and run a single circuit through it. Args: circuit (QuantumCircuit): circuit to transpile transpile_config (TranspileConfig): configuration dictating how to transpile Returns: QuantumCircuit: transpiled circuit Raises: TranspilerError: if transpile_config is not valid or transpilation incurs error """ # either the pass manager is already selected... if transpile_config.pass_manager is not None: pass_manager = transpile_config.pass_manager # or we choose an appropriate one based on desired optimization level (default: level 1) else: # Workaround for ion trap support: If basis gates includes # Mølmer-Sørensen (rxx) and the circuit includes gates outside the basis, # first unroll to u3, cx, then run MSBasisDecomposer to target basis. basic_insts = ['measure', 'reset', 'barrier', 'snapshot'] device_insts = set(transpile_config.basis_gates).union(basic_insts) ms_basis_swap = None if 'rxx' in transpile_config.basis_gates and \ not device_insts >= circuit.count_ops().keys(): ms_basis_swap = transpile_config.basis_gates transpile_config.basis_gates = list( set(['u3', 'cx']).union(transpile_config.basis_gates)) level = transpile_config.optimization_level if level is None: level = 1 if level == 0: pass_manager = level_0_pass_manager(transpile_config) elif level == 1: pass_manager = level_1_pass_manager(transpile_config) elif level == 2: pass_manager = level_2_pass_manager(transpile_config) elif level == 3: pass_manager = level_3_pass_manager(transpile_config) else: raise TranspilerError("optimization_level can range from 0 to 3.") if ms_basis_swap is not None: pass_manager.append(MSBasisDecomposer(ms_basis_swap)) # Set a callback on the pass manager there is one if getattr(transpile_config, 'callback', None): pass_manager.callback = transpile_config.callback out_circuit = pass_manager.run(circuit) out_circuit.name = transpile_config.output_name return out_circuit
def _transpile_circuit( circuit_config_tuple: Tuple[QuantumCircuit, Dict]) -> QuantumCircuit: """Select a PassManager and run a single circuit through it. Args: circuit_config_tuple (tuple): circuit (QuantumCircuit): circuit to transpile transpile_config (dict): configuration dictating how to transpile. The dictionary has the following format: {'optimization_level': int, 'output_name': string, 'callback': callable, 'pass_manager_config': PassManagerConfig} Returns: The transpiled circuit Raises: TranspilerError: if transpile_config is not valid or transpilation incurs error """ circuit, transpile_config = circuit_config_tuple pass_manager_config = transpile_config['pass_manager_config'] if transpile_config['faulty_qubits_map']: pass_manager_config.initial_layout = _remap_layout_faulty_backend( pass_manager_config.initial_layout, transpile_config['faulty_qubits_map']) # we choose an appropriate one based on desired optimization level level = transpile_config['optimization_level'] if level == 0: pass_manager = level_0_pass_manager(pass_manager_config) elif level == 1: pass_manager = level_1_pass_manager(pass_manager_config) elif level == 2: pass_manager = level_2_pass_manager(pass_manager_config) elif level == 3: pass_manager = level_3_pass_manager(pass_manager_config) else: raise TranspilerError("optimization_level can range from 0 to 3.") if pass_manager_config.scheduling_method is not None: if pass_manager_config.basis_gates: if 'delay' not in pass_manager_config.basis_gates: pass_manager_config.basis_gates.append('delay') else: pass_manager_config.basis_gates = ['delay'] result = pass_manager.run(circuit, callback=transpile_config['callback'], output_name=transpile_config['output_name']) if transpile_config['faulty_qubits_map']: return _remap_circuit_faulty_backend( result, transpile_config['backend_num_qubits'], pass_manager_config.backend_properties, transpile_config['faulty_qubits_map']) return result
def _transpile_circuit( circuit_config_tuple: Tuple[QuantumCircuit, Dict]) -> QuantumCircuit: """Select a PassManager and run a single circuit through it. Args: circuit_config_tuple (tuple): circuit (QuantumCircuit): circuit to transpile transpile_config (dict): configuration dictating how to transpile. The dictionary has the following format: {'optimization_level': int, 'pass_manager': PassManager, 'output_name': string, 'callback': callable, 'pass_manager_config': PassManagerConfig} Returns: The transpiled circuit Raises: TranspilerError: if transpile_config is not valid or transpilation incurs error """ circuit, transpile_config = circuit_config_tuple pass_manager_config = transpile_config['pass_manager_config'] ms_basis_swap = None if (pass_manager_config.translation_method == 'unroller' and pass_manager_config.basis_gates is not None): # Workaround for ion trap support: If basis gates includes # Mølmer-Sørensen (rxx) and the circuit includes gates outside the basis, # first unroll to u3, cx, then run MSBasisDecomposer to target basis. basic_insts = ['measure', 'reset', 'barrier', 'snapshot'] device_insts = set(pass_manager_config.basis_gates).union(basic_insts) if 'rxx' in pass_manager_config.basis_gates and \ not device_insts >= circuit.count_ops().keys(): ms_basis_swap = pass_manager_config.basis_gates pass_manager_config.basis_gates = list( set(['u3', 'cx']).union(pass_manager_config.basis_gates)) # we choose an appropriate one based on desired optimization level level = transpile_config['optimization_level'] if level == 0: pass_manager = level_0_pass_manager(pass_manager_config) elif level == 1: pass_manager = level_1_pass_manager(pass_manager_config) elif level == 2: pass_manager = level_2_pass_manager(pass_manager_config) elif level == 3: pass_manager = level_3_pass_manager(pass_manager_config) else: raise TranspilerError("optimization_level can range from 0 to 3.") if ms_basis_swap is not None: pass_manager.append(MSBasisDecomposer(ms_basis_swap)) return pass_manager.run(circuit, callback=transpile_config['callback'], output_name=transpile_config['output_name'])
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 transpile_circuit(circuit, transpile_config): """Select a PassManager and run a single circuit through it. Args: circuit (QuantumCircuit): circuit to transpile transpile_config (TranspileConfig): configuration dictating how to transpile Returns: QuantumCircuit: transpiled circuit Raises: TranspilerError: if transpile_config is not valid or transpilation incurs error """ # either the pass manager is already selected... if transpile_config.pass_manager: pass_manager = transpile_config.pass_manager # or we choose an appropriate one based on desired optimization level (default: level 1) else: level = transpile_config.optimization_level if level is None: level = 1 if level == 0: pass_manager = level_0_pass_manager(transpile_config) elif level == 1: pass_manager = level_1_pass_manager(transpile_config) elif level == 2: pass_manager = level_2_pass_manager(transpile_config) elif level == 3: pass_manager = level_3_pass_manager(transpile_config) else: raise TranspilerError("optimization_level can range from 0 to 3.") # Set a callback on the pass manager there is one if getattr(transpile_config, 'callback', None): pass_manager.callback = transpile_config.callback out_circuit = pass_manager.run(circuit) out_circuit.name = transpile_config.output_name return out_circuit
def transpile_circuit(circuit, transpile_config): """Select a PassManager and run a single circuit through it. Args: circuit (QuantumCircuit): circuit to transpile transpile_config (TranspileConfig): configuration dictating how to transpile Returns: QuantumCircuit: transpiled circuit Raises: TranspilerError: if transpile_config is not valid or transpilation incurs error """ # if the pass manager is not already selected, choose an appropriate one. if transpile_config.pass_manager: pass_manager = transpile_config.pass_manager elif transpile_config.optimization_level is not None: level = transpile_config.optimization_level if level == 0: pass_manager = level_0_pass_manager(transpile_config) elif level == 1: pass_manager = level_1_pass_manager(transpile_config) elif level == 2: pass_manager = level_2_pass_manager(transpile_config) elif level == 3: pass_manager = level_3_pass_manager(transpile_config) else: raise TranspilerError("optimization_level can range from 0 to 3.") # legacy behavior elif transpile_config.coupling_map: pass_manager = level_1_pass_manager(transpile_config) else: pass_manager = default_pass_manager_simulator(transpile_config) # Set a callback on the pass manager if it's set if getattr(transpile_config, 'callback', None): pass_manager.callback = transpile_config.callback return pass_manager.run(circuit)