Beispiel #1
0
    def test_optimize_overlap_swap(self):
        """ Remove two swaps that overlap
            qr0:--X--------       qr0:--m--
                  |                     |
            qr1:--X--X-----       qr1:--|--
                     |       ==>        |
            qr2:-----X--m--       qr2:--|--
                        |               |
            cr0:--------.--       cr0:--.--
        """
        qr = QuantumRegister(3, 'qr')
        cr = ClassicalRegister(1, 'cr')
        circuit = QuantumCircuit(qr, cr)
        circuit.swap(qr[0], qr[1])
        circuit.swap(qr[1], qr[2])
        circuit.measure(qr[2], cr[0])

        expected = QuantumCircuit(qr, cr)
        expected.measure(qr[0], cr[0])

        pass_manager = PassManager()
        pass_manager.append(
            [OptimizeSwapBeforeMeasure(),
             DAGFixedPoint()],
            do_while=lambda property_set: not property_set['dag_fixed_point'])
        after = pass_manager.run(circuit)

        self.assertEqual(expected, after)
Beispiel #2
0
    def test_optimize_undone_swap(self):
        """ Remove redundant swap
            qr0:--X--X--m--       qr0:--m---
                  |  |  |               |
            qr1:--X--X--|--  ==>  qr1:--|--
                        |               |
            cr0:--------.--       cr0:--.--
        """
        qr = QuantumRegister(2, 'qr')
        cr = ClassicalRegister(1, 'cr')
        circuit = QuantumCircuit(qr, cr)
        circuit.swap(qr[0], qr[1])
        circuit.swap(qr[0], qr[1])
        circuit.measure(qr[0], cr[0])

        expected = QuantumCircuit(qr, cr)
        expected.measure(qr[0], cr[0])

        pass_manager = PassManager()
        pass_manager.append(
            [OptimizeSwapBeforeMeasure(),
             DAGFixedPoint()],
            do_while=lambda property_set: not property_set['dag_fixed_point'])
        after = transpile(circuit, pass_manager=pass_manager)

        self.assertEqual(expected, after)
    def run(self, qc):
        pass_ = Unroller(RxzCzLibrary, ['rx', 'rz', 'cz'])

        pm = PassManager(pass_)

        if self.optimize:

            def _opt_control(property_set):
                return not property_set['dag_fixed_point']

            pass_ = [
                Optimize1qRotations('rx'),
                Optimize1qRotations('rz'),
                XRZCommutation(back=True),
                ZRXCommutation(back=False),
                CZRZCommutation(back=True),
                CZCancellation(),
                DAGFixedPoint()
            ]

            # run until we reach a fixed point and no further changes can be made.
            pm.append(pass_, do_while=_opt_control)

        circ = pm.run(qc)

        return circ
    def test_empty_dag_true(self):
        """Test the dag fixed point of an empty dag."""
        circuit = QuantumCircuit()
        dag = circuit_to_dag(circuit)

        pass_ = DAGFixedPoint()
        pass_.run(dag)
        self.assertFalse(pass_.property_set["dag_fixed_point"])
        pass_.run(dag)
        self.assertTrue(pass_.property_set["dag_fixed_point"])
    def test_two_resets(self):
        """ Remove two initial resets
            qr0:--|0>-|0>--   ==>    qr0:----
        """
        qr = QuantumRegister(1, 'qr')
        circuit = QuantumCircuit(qr)
        circuit.reset(qr[0])
        circuit.reset(qr[0])

        expected = QuantumCircuit(qr)

        pass_manager = PassManager()
        pass_manager.append(
            [RemoveResetInZeroState(), DAGFixedPoint()],
            do_while=lambda property_set: not property_set['dag_fixed_point'])
        after = transpile(circuit, pass_manager=pass_manager)

        self.assertEqual(expected, after)
    def test_nonempty_dag_false(self):
        """Test the dag false fixed point of a non-empty dag."""
        qr = QuantumRegister(2)
        circuit = QuantumCircuit(qr)
        circuit.h(qr[0])
        circuit.cx(qr[0], qr[1])
        dag = circuit_to_dag(circuit)

        pass_ = DAGFixedPoint()
        pass_.run(dag)
        self.assertFalse(pass_.property_set["dag_fixed_point"])
        dag.remove_all_ops_named("h")
        pass_.run(dag)
        self.assertFalse(pass_.property_set["dag_fixed_point"])
Beispiel #7
0
    def test_optimize_rz_z(self):
        """ Remove two swaps that overlap
            qr0:--RZ-Z--m--       qr0:--m--
                        |               |
            cr0:--------.--       cr0:--.--
        """
        qr = QuantumRegister(1, 'qr')
        cr = ClassicalRegister(1, 'cr')
        circuit = QuantumCircuit(qr, cr)
        circuit.rz(0.1, qr[0])
        circuit.z(qr[0])
        circuit.measure(qr[0], cr[0])

        expected = QuantumCircuit(qr, cr)
        expected.measure(qr[0], cr[0])

        pass_manager = PassManager()
        pass_manager.append(
            [RemoveDiagonalGatesBeforeMeasure(), DAGFixedPoint()],
            do_while=lambda property_set: not property_set['dag_fixed_point'])
        after = transpile(circuit, pass_manager=pass_manager)

        self.assertEqual(expected, after)