Esempio n. 1
0
    def test_calibrations(self):
        """Test that calibrations are properly copied over."""
        circuit_in = QuantumCircuit(1)
        circuit_in.add_calibration('h', [0], None)
        self.assertEqual(len(circuit_in.calibrations), 1)

        dag_dependency = circuit_to_dagdependency(circuit_in)
        self.assertEqual(len(dag_dependency.calibrations), 1)

        circuit_out = dagdependency_to_circuit(dag_dependency)
        self.assertEqual(len(circuit_out.calibrations), 1)
 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_dependency = circuit_to_dagdependency(circuit_in)
     self.assertEqual(dag_dependency.metadata, meta_dict)
     circuit_out = dagdependency_to_circuit(dag_dependency)
     self.assertEqual(circuit_out.metadata, meta_dict)
Esempio n. 3
0
 def test_circuit_and_dag_canonical(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_dependency = circuit_to_dagdependency(circuit_in)
     circuit_out = dagdependency_to_circuit(dag_dependency)
     self.assertEqual(circuit_out, circuit_in)
Esempio n. 4
0
    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, DAGDependency)):
                raise TranspilerError(
                    "A template is a Quantumciruit or a DAGDependency.")

            if len(template.qubits) > len(circuit_dag_dep.qubits):
                continue

            identity = np.identity(2**len(template.qubits), dtype=complex)
            try:
                if isinstance(template, DAGDependency):
                    data = Operator(dagdependency_to_circuit(template)).data
                else:
                    data = Operator(template).data

                comparison = np.allclose(data, identity)

                if not comparison:
                    raise TranspilerError(
                        "A template is a Quantumciruit() that "
                        "performs the identity.")
            except TypeError:
                pass

            if isinstance(template, QuantumCircuit):
                template_dag_dep = circuit_to_dagdependency(template)
            else:
                template_dag_dep = 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,
                    self.user_cost_dict,
                )
                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 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