Exemple #1
0
    def run(self, circuit: Circuit, data: dict[str, Any]) -> None:
        """Perform the pass's operation, see BasePass for more info."""

        # Check data for windows markers
        if 'window_markers' not in data:
            _logger.warning('Did not find any window markers.')
            return

        window_markers = data['window_markers']
        _logger.debug('Found window_markers: %s.' % str(window_markers))

        if not is_iterable(window_markers):
            _logger.debug('Invalid type for window_markers.')
            return

        if not all(is_integer(marker) for marker in window_markers):
            _logger.debug('Invalid type for window_markers.')
            return

        # Resynthesis each window
        index_shift = 0
        for marker in window_markers:
            marker -= index_shift

            # Slice window
            begin_cycle = int(marker - self.window_size // 2)
            end_cycle = int(marker + np.ceil(self.window_size / 2))

            if begin_cycle < 0:
                begin_cycle = 0

            if end_cycle > circuit.get_num_cycles():
                end_cycle = circuit.get_num_cycles() - 1

            window = Circuit(circuit.get_size(), circuit.get_radixes())
            window.extend(circuit[begin_cycle:end_cycle])

            _logger.info(
                'Resynthesizing window from cycle '
                f'{begin_cycle} to {end_cycle}.', )

            # Synthesize
            utry = window.get_unitary()
            new_window = self.synthesispass.synthesize(utry, data)

            # Replace
            if self.replace_filter(new_window, window):
                _logger.debug('Replacing window with synthesized circuit.')

                actual_window_size = end_cycle - begin_cycle
                for _ in range(actual_window_size):
                    circuit.pop_cycle(begin_cycle)

                circuit.insert_circuit(
                    begin_cycle,
                    new_window,
                    list(range(circuit.get_size())),
                )

                index_shift += actual_window_size - new_window.get_num_cycles()
Exemple #2
0
 def test_type_invalid_2(self, not_an_int: Any) -> None:
     circuit = Circuit(4, [2, 2, 3, 3])
     try:
         circuit.pop_cycle(not_an_int)
     except TypeError:
         return
     except BaseException:
         assert False, 'Unexpected Exception.'
Exemple #3
0
 def test_type_valid_2(self, an_int: int) -> None:
     circuit = Circuit(4, [2, 2, 3, 3])
     try:
         circuit.pop_cycle(an_int)
     except TypeError:
         assert False, 'Unexpected TypeError.'
     except BaseException:
         return
Exemple #4
0
 def test_index_invalid_2(self, cycle_index: int) -> None:
     circuit = Circuit(4, [2, 2, 3, 3])
     circuit.append_gate(HGate(), [0])
     circuit.append_gate(HGate(), [0])
     circuit.append_gate(HGate(), [0])
     try:
         circuit.pop_cycle(cycle_index)
     except IndexError:
         return
     except BaseException:
         assert False, 'Unexpected Exception.'
Exemple #5
0
 def test_multi_qudit(self, cycle_index: int) -> None:
     circuit = Circuit(4, [2, 2, 3, 3])
     circuit.append_gate(
         ConstantUnitaryGate(np.identity(36), [2, 2, 3, 3]),
         [0, 1, 2, 3],
     )
     circuit.append_gate(
         ConstantUnitaryGate(np.identity(36), [2, 2, 3, 3]),
         [0, 1, 2, 3],
     )
     circuit.append_gate(
         ConstantUnitaryGate(np.identity(36), [2, 2, 3, 3]),
         [0, 1, 2, 3],
     )
     assert circuit.get_num_cycles() == 3
     assert circuit.get_num_operations() == 3
     circuit.pop_cycle(cycle_index)
     assert circuit.get_num_cycles() == 2
     assert circuit.get_num_operations() == 2
Exemple #6
0
 def test_index_valid_1(self, r6_qudit_circuit: Circuit) -> None:
     num_cycles = r6_qudit_circuit.get_num_cycles()
     for i in range(num_cycles):
         r6_qudit_circuit.pop_cycle(-1)
         assert r6_qudit_circuit.get_num_cycles() == num_cycles - i - 1