def run(self, dag): """ Args: dag(DAGCircuit): DAG circuit. Returns: DAGCircuit: optimized DAG circuit. Raises: TranspilerError: If the template has not the right form or if the output circuit acts differently as the input circuit. """ circuit_dag = dag circuit_dag_dep = dag_to_dagdependency(circuit_dag) for template in self.template_list: if not isinstance(template, QuantumCircuit): raise TranspilerError('A template is a Quantumciruit().') if template.num_qubits > len(circuit_dag_dep.qubits): continue identity = np.identity(2**template.num_qubits, dtype=complex) try: comparison = np.allclose(Operator(template).data, identity) if not comparison: raise TranspilerError( 'A template is a Quantumciruit() that ' 'performs the identity.') except TypeError: pass template_dag_dep = circuit_to_dagdependency(template) template_m = TemplateMatching(circuit_dag_dep, template_dag_dep, self.heuristics_qubits_param, self.heuristics_backward_param) template_m.run_template_matching() matches = template_m.match_list if matches: maximal = MaximalMatches(matches) maximal.run_maximal_matches() max_matches = maximal.max_match_list substitution = TemplateSubstitution( max_matches, template_m.circuit_dag_dep, template_m.template_dag_dep) substitution.run_dag_opt() circuit_dag_dep = substitution.dag_dep_optimized else: continue circuit_dag = dagdependency_to_dag(circuit_dag_dep) return circuit_dag
def test_metadata(self): """Test circuit metadata is preservered through conversion.""" meta_dict = dict(experiment_id="1234", execution_number=4) qr = QuantumRegister(2) circuit_in = QuantumCircuit(qr, metadata=meta_dict) circuit_in.h(qr[0]) circuit_in.cx(qr[0], qr[1]) circuit_in.measure_all() dag = circuit_to_dag(circuit_in) self.assertEqual(dag.metadata, meta_dict) dag_dependency = dag_to_dagdependency(dag) self.assertEqual(dag_dependency.metadata, meta_dict) dag_out = dagdependency_to_dag(dag_dependency) self.assertEqual(dag_out.metadata, meta_dict)
def test_circuit_and_dag_dependency(self): """Check convert to dag dependency and back""" qr = QuantumRegister(3) cr = ClassicalRegister(3) circuit_in = QuantumCircuit(qr, cr) circuit_in.h(qr[0]) circuit_in.h(qr[1]) circuit_in.measure(qr[0], cr[0]) circuit_in.measure(qr[1], cr[1]) circuit_in.x(qr[0]).c_if(cr, 0x3) circuit_in.measure(qr[0], cr[0]) circuit_in.measure(qr[1], cr[1]) circuit_in.measure(qr[2], cr[2]) dag_in = circuit_to_dag(circuit_in) dag_dependency = dag_to_dagdependency(dag_in) dag_out = dagdependency_to_dag(dag_dependency) self.assertEqual(dag_out, dag_in)
def run(self, dag): """ Args: dag(DAGCircuit): DAG circuit. Returns: DAGCircuit: optimized DAG circuit. Raises: TranspilerError: If the template has not the right form or if the output circuit acts differently as the input circuit. """ circuit_dag = dag circuit_dag_dep = dag_to_dagdependency(circuit_dag) circuit = dagdependency_to_circuit(circuit_dag_dep) operator_ini = Operator(circuit) for template in self.template_list: if not isinstance(template, QuantumCircuit): raise TranspilerError('A template is a Quantumciruit().') identity = np.identity(2**template.num_qubits, dtype=complex) comparison = np.allclose(Operator(template).data, identity) if not comparison: raise TranspilerError( 'A template is a Quantumciruit() that performs the identity.' ) if template.num_qubits > len(circuit_dag_dep.qubits): raise TranspilerError( 'A template should have equal or less number of qubit' ' compared to the circuit.') template_dag_dep = circuit_to_dagdependency(template) template_m = TemplateMatching(circuit_dag_dep, template_dag_dep, self.heuristics_qubits_param, self.heuristics_backward_param) template_m.run_template_matching() matches = template_m.match_list if matches: maximal = MaximalMatches(matches) maximal.run_maximal_matches() max_matches = maximal.max_match_list substitution = TemplateSubstitution( max_matches, template_m.circuit_dag_dep, template_m.template_dag_dep) substitution.run_dag_opt() circuit_dag_dep = substitution.dag_dep_optimized circuit_intermediate = dagdependency_to_circuit( circuit_dag_dep) opertator_intermediate = Operator(circuit_intermediate) if operator_ini != opertator_intermediate: raise TranspilerError( 'A failure happened during the substitution.') else: continue circuit_dag = dagdependency_to_dag(circuit_dag_dep) return circuit_dag