Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix issue with to long scratch pass #69

Merged
merged 2 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion pytket/qir/conversion/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
import pyqir

from pytket import wasm
from pytket.circuit import Circuit
from pytket._tket.circuit import _TEMP_BIT_NAME # type: ignore
from pytket.circuit import Bit, Circuit # type: ignore
from pytket.passes import CustomPass # type: ignore

from .conversion import QirGenerator
from .module import tketqirModule
Expand All @@ -43,6 +45,7 @@ def pytket_to_qir(
qir_format: QIRFormat = QIRFormat.BINARY,
wfh: Optional[wasm.WasmFileHandler] = None,
int_type: int = 64,
cut_pytket_register: bool = False,
) -> Union[str, bytes, None]:
"""converts given pytket circuit to qir

Expand All @@ -57,6 +60,9 @@ def pytket_to_qir(
:type pyqir_0_6_compatibility: bool
:param int_type: size of each integer, allowed value 32 and 64
:type int_type: int
:param cut_pytket_register: breaks up the internal scratch bit registers
into smaller registers, default value false
:type cut_pytket_register: bool
"""

if len(circ.q_registers) > 1 or (
Expand All @@ -71,6 +77,10 @@ def pytket_to_qir(
if int_type != 32 and int_type != 64:
raise ValueError("the integer size must be 32 or 64")

if cut_pytket_register:
cpass = _scratch_reg_resize_pass(int_type)
cpass.apply(circ)

for creg in circ.c_registers:
if creg.size > 64:
raise ValueError("classical registers must not have more than 64 bits")
Expand Down Expand Up @@ -115,3 +125,29 @@ def pytket_to_qir(
return populated_module.module.ir()
else:
assert not "unsupported return type" # type: ignore


def _scratch_reg_resize_pass(max_size: int) -> CustomPass:
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is coppied, I have set up a task to move this over to pytket to have only one place where this function can be maintained in one step: CQCL/tket#975

"""Given a max scratch register width, return a compiler pass that
breaks up the internal scratch bit registers into smaller registers
"""

def trans(circ: Circuit, max_size: int = max_size) -> Circuit:
# Find all scratch bits
scratch_bits = [
bit
for bit in circ.bits
if (
bit.reg_name == _TEMP_BIT_NAME
or bit.reg_name.startswith(f"{_TEMP_BIT_NAME}_")
)
]
# If the total number of scratch bits exceeds the max width, rename them
if len(scratch_bits) > max_size:
bits_map = {}
for i, bit in enumerate(scratch_bits):
bits_map[bit] = Bit(f"{_TEMP_BIT_NAME}_{i//max_size}", i % max_size)
circ.rename_units(bits_map)
return circ

return CustomPass(trans, label="resize scratch bits")
34 changes: 34 additions & 0 deletions tests/conditional_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import pytest
from utilities import check_qir_result # type: ignore

from pytket.circuit import ( # type: ignore[attr-defined]
Expand Down Expand Up @@ -226,6 +227,39 @@ def test_pytket_qir_conditional_10() -> None:
check_qir_result(result, "test_pytket_qir_conditional_10")


def test_pytket_qir_conditional_11() -> None:
# test conditional with to big scratch register

circ = Circuit(7, name="testcirc")

syn = circ.add_c_register("syn", 4)

for _ in range(11):
circ.X(0, condition=reg_eq(syn, 1))
circ.X(0, condition=reg_eq(syn, 2))
circ.X(0, condition=reg_eq(syn, 2))
circ.X(0, condition=reg_eq(syn, 3))
circ.X(0, condition=reg_eq(syn, 4))
circ.X(0, condition=reg_eq(syn, 4))

with pytest.raises(Exception):
pytket_to_qir(
circ,
name="test_pytket_qir_conditional_11",
qir_format=QIRFormat.STRING,
cut_pytket_register=False,
)

result = pytket_to_qir(
circ,
name="test_pytket_qir_conditional_11",
qir_format=QIRFormat.STRING,
cut_pytket_register=True,
)

check_qir_result(result, "test_pytket_qir_conditional_11")


if __name__ == "__main__":
test_pytket_qir_conditional()
test_pytket_qir_conditional_ii()
Expand Down
Loading