# Copyright Quantinuum## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.frompytket.circuitimportBit,Circuitfrompytket.unit_idimport_TEMP_BIT_NAMEfrom.._tket.passesimportBasePass,CustomPassMAX_C_REG_WIDTH=32def_is_scratch(bit:Bit)->bool:reg_name=bit.reg_namereturnbool(reg_name==_TEMP_BIT_NAME)orreg_name.startswith(f"{_TEMP_BIT_NAME}_")
[docs]defscratch_reg_resize_pass(max_size:int=MAX_C_REG_WIDTH)->BasePass:"""Create a pass that breaks up the internal scratch bit registers into smaller registers. :param max_size: desired maximum size of scratch bit registers :return: a pass to break up the scratch registers """deftrans(circ:Circuit,max_size:int=max_size)->Circuit:# Find all scratch bitsscratch_bits=list(filter(_is_scratch,circ.bits))# If the total number of scratch bits exceeds the max width, rename themiflen(scratch_bits)>max_size:bits_map={}fori,bitinenumerate(scratch_bits):bits_map[bit]=Bit(f"{_TEMP_BIT_NAME}_{i//max_size}",i%max_size)circ.rename_units(bits_map)# type: ignorereturncircreturnCustomPass(trans,label="resize scratch bits")