Exemplo n.º 1
0
    def _compile_layered(self, target_unitary: Unitary,
                         unitary_primitive_counts: Dict[UnitaryPrimitive,
                                                        int], threshold: float,
                         max_step_count: int) -> CompilerResult:
        '''
        Internal implementation of layered STOQ compilation.
        See documentation for Compiler.compile_layered() for full details.
        '''
        compiled_sequence = UnitarySequence(self.dimension)
        cost_by_step = []

        while (not compiled_sequence.product().close_to(
                target_unitary, threshold)
               and len(cost_by_step) < max_step_count):
            self.beta = min(self.beta + self.annealing_rate, self.max_beta)
            product_before_change = compiled_sequence.product()
            self._make_random_change_layered(compiled_sequence,
                                             unitary_primitive_counts)
            current_cost = target_unitary.distance_from(product_before_change)
            proposed_cost = target_unitary.distance_from(
                compiled_sequence.product())
            accept = self._accept_proposed_change(target_unitary, current_cost,
                                                  proposed_cost)
            if accept:
                cost_by_step.append(proposed_cost)
            else:
                compiled_sequence.undo()
                cost_by_step.append(current_cost)

        return compiled_sequence, cost_by_step
Exemplo n.º 2
0
    def test_undo(self) -> None:
        dimension = 2

        identity = Unitary.identity(dimension)

        sequence = UnitarySequence(dimension)
        assert sequence.get_length() == 0

        with pytest.raises(Exception):
            sequence.undo()

        sequence.append_first(
            UnitarySequenceEntry(UnitaryDefinitions.sigmax(), [0]))
        assert sequence.get_length() == 1
        assert sequence.product().close_to(UnitaryDefinitions.sigmax())

        sequence.undo()
        assert sequence.get_length() == 0
        assert sequence.product().close_to(identity)

        with pytest.raises(Exception):
            sequence.undo()

        sequence.append_first(
            UnitarySequenceEntry(UnitaryDefinitions.sigmay(), [0]))
        sequence.append_first(
            UnitarySequenceEntry(UnitaryDefinitions.sigmay(), [0]))
        assert sequence.get_length() == 2
        assert sequence.product().close_to(identity)

        sequence.remove_last()
        assert sequence.get_length() == 1
        assert sequence.product().close_to(UnitaryDefinitions.sigmay())

        sequence.undo()
        assert sequence.get_length() == 2
        assert sequence.product().close_to(identity)

        with pytest.raises(Exception):
            sequence.undo()