Skip to content

Commit

Permalink
Fix #1576 max width in qasm (#1653)
Browse files Browse the repository at this point in the history
* fix maxwidth

* reduce testcase

* clean up

* Add  to  to changelog
  • Loading branch information
cqc-melf authored Nov 5, 2024
1 parent 9354613 commit 37d9a9c
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 3 deletions.
2 changes: 2 additions & 0 deletions pytket/docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Fixes:
* Fix `symbol_substitution` not preserving opgroups.
* Remove hardware inefficient circuit construction in `_tk1_to_rzsx`
* Support converting conditional `RangePredicate`s to QASM.
* Fix `maxwidth` parameter of `circuit_from_qasm_str`
* Add `scratch_reg_resize_pass` to `circuit_from_qasm_str`

1.34.0 (October 2024)
---------------------
Expand Down
13 changes: 11 additions & 2 deletions pytket/pytket/qasm/qasm.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,12 @@
create_logic_exp,
)
from pytket.qasm.grammar import grammar
from pytket.passes import AutoRebase, DecomposeBoxes, RemoveRedundancies
from pytket.passes import (
AutoRebase,
DecomposeBoxes,
RemoveRedundancies,
scratch_reg_resize_pass,
)
from pytket.wasm import WasmFileHandler


Expand Down Expand Up @@ -1028,7 +1033,11 @@ def circuit_from_qasm_str(
cast(CircuitTransformer, g_parser.options.transformer)._reset_context(
reset_wasm=False
)
return Circuit.from_dict(g_parser.parse(qasm_str)) # type: ignore[arg-type]

circ = Circuit.from_dict(g_parser.parse(qasm_str)) # type: ignore[arg-type]
cpass = scratch_reg_resize_pass(maxwidth)
cpass.apply(circ)
return circ


def circuit_from_qasm_io(
Expand Down
23 changes: 22 additions & 1 deletion pytket/tests/qasm_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,26 @@ def test_qasm_correct() -> None:
assert str(coms3) == correct_str3


def test_long_registers() -> None:
fname = str(curr_file_path / "qasm_test_files/longreg.qasm")
c = circuit_from_qasm(fname, maxwidth=64)
assert c.n_qubits == 0
assert c.n_bits == 79
assert len(c.c_registers) == 5
for creg in c.c_registers:
assert creg.size <= 64


def test_long_registers_2() -> None:
fname = str(curr_file_path / "qasm_test_files/longreg.qasm")
c = circuit_from_qasm(fname, maxwidth=100)
assert c.n_qubits == 0
assert c.n_bits == 79
assert len(c.c_registers) == 4
for creg in c.c_registers:
assert creg.size <= 100


def test_qasm_qubit() -> None:
with pytest.raises(RuntimeError) as errorinfo:
fname = str(curr_file_path / "qasm_test_files/test2.qasm")
Expand Down Expand Up @@ -602,7 +622,8 @@ def test_scratch_bits_filtering() -> None:
creg {_TEMP_BIT_NAME}_1[32];
{_TEMP_BIT_NAME}[0] = (a[0] ^ b[0]);
if({_TEMP_BIT_NAME}[0]==1) x q[0];
"""
""",
maxwidth=64,
)
assert c.get_c_register(_TEMP_BIT_NAME)
assert c.get_c_register(f"{_TEMP_BIT_NAME}_1")
Expand Down
83 changes: 83 additions & 0 deletions pytket/tests/qasm_test_files/longreg.qasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
OPENQASM 2.0;
include "hqslib1.inc";

creg a[1];
creg b[1];
creg c[1];

if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;
if(c!=0) a = b;

0 comments on commit 37d9a9c

Please sign in to comment.