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

Improve efficiency of check #216

Merged
merged 2 commits into from
Sep 1, 2023
Merged
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
20 changes: 9 additions & 11 deletions pytket/extensions/quantinuum/backends/quantinuum.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,18 +132,15 @@ def _is_scratch(bit: Bit) -> bool:
return bool(reg_name == _TEMP_BIT_NAME) or reg_name.startswith(f"{_TEMP_BIT_NAME}_")


def _is_unused_scratch(bit: Bit, qasm: str) -> bool:
def _used_scratch_registers(qasm: str) -> Set[str]:
# See https://github.com/CQCL/tket/blob/e846e8a7bdcc4fa29967d211b7fbf452ec970dfb/
# pytket/pytket/qasm/qasm.py#L966
if not _is_scratch(bit):
return False
reg_name = re.escape(bit.reg_name)
def_matcher = re.compile(r"creg ({})\[\d+\]".format(reg_name))
arg_matcher = re.compile(r"({})\[\d+\]".format(reg_name))
return not any(
def_matcher.match(line) or arg_matcher.findall(line)
for line in qasm.split("\n")
)
def_matcher = re.compile(r"creg ({}\_*\d*)\[\d+\]".format(_TEMP_BIT_NAME))
regs = set()
for line in qasm.split("\n"):
if reg := def_matcher.match(line):
regs.add(reg.group(1))
return regs


def scratch_reg_resize_pass(max_size: int = MAX_C_REG_WIDTH) -> CustomPass:
Expand Down Expand Up @@ -817,10 +814,11 @@ def process_circuits(
results_selection = []
if language == Language.QASM:
quantinuum_circ = circuit_to_qasm_str(c0, header="hqslib1")
used_scratch_regs = _used_scratch_registers(quantinuum_circ)
for name, count in Counter(
bit.reg_name
for bit in c0.bits
if not _is_unused_scratch(bit, quantinuum_circ)
if not _is_scratch(bit) or bit.reg_name in used_scratch_regs
).items():
for i in range(count):
results_selection.append((name, i))
Expand Down
Loading