Coverage for /home/runner/work/tket/tket/pytket/pytket/passes/resizeregpass.py: 100%
17 statements
« prev ^ index » next coverage.py v7.6.12, created at 2025-03-14 11:30 +0000
« prev ^ index » next coverage.py v7.6.12, created at 2025-03-14 11:30 +0000
1# Copyright Quantinuum
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
15from pytket.circuit import Bit, Circuit
16from pytket.unit_id import _TEMP_BIT_NAME
18from .._tket.passes import BasePass, CustomPass
20MAX_C_REG_WIDTH = 32
23def _is_scratch(bit: Bit) -> bool:
24 reg_name = bit.reg_name
25 return bool(reg_name == _TEMP_BIT_NAME) or reg_name.startswith(f"{_TEMP_BIT_NAME}_")
28def scratch_reg_resize_pass(max_size: int = MAX_C_REG_WIDTH) -> BasePass:
29 """Create a pass that breaks up the internal scratch bit registers into smaller
30 registers.
32 :param max_size: desired maximum size of scratch bit registers
33 :return: a pass to break up the scratch registers
34 """
36 def trans(circ: Circuit, max_size: int = max_size) -> Circuit:
37 # Find all scratch bits
38 scratch_bits = list(filter(_is_scratch, circ.bits))
39 # If the total number of scratch bits exceeds the max width, rename them
40 if len(scratch_bits) > max_size:
41 bits_map = {}
42 for i, bit in enumerate(scratch_bits):
43 bits_map[bit] = Bit(f"{_TEMP_BIT_NAME}_{i//max_size}", i % max_size)
44 circ.rename_units(bits_map) # type: ignore
45 return circ
47 return CustomPass(trans, label="resize scratch bits")